shareAiConsult.html 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">
  6. <meta name="format-detection" content="telephone=no">
  7. <title>与U宝对话</title>
  8. <meta property="og:title" content="和U宝的对话">
  9. <meta property="og:description" content="点击查看对话内容">
  10. <meta property="og:site_name" content="U宝AI智能助手">
  11. <meta name="application-name" content="U宝AI智能助手">
  12. <link rel="icon" type="image/png" href="https://alien-volume.oss-cn-beijing.aliyuncs.com/icon/ubao.png">
  13. <link rel="apple-touch-icon" href="https://alien-volume.oss-cn-beijing.aliyuncs.com/icon/ubao.png">
  14. <style>
  15. :root {
  16. --bg: #f4f6fb;
  17. --text: #151515;
  18. --muted: #aaaaaa;
  19. --orange: #f47d1f;
  20. --safe-bottom: env(safe-area-inset-bottom, 0px);
  21. }
  22. * {
  23. margin: 0;
  24. padding: 0;
  25. box-sizing: border-box;
  26. }
  27. html {
  28. -webkit-tap-highlight-color: transparent;
  29. }
  30. body {
  31. font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif;
  32. background: var(--bg);
  33. color: var(--text);
  34. min-height: 100vh;
  35. padding-bottom: calc(88px + var(--safe-bottom));
  36. }
  37. .hero {
  38. padding: 16px 15px 0;
  39. }
  40. .hero__title {
  41. font-size: 20px;
  42. font-weight: 700;
  43. line-height: 1.4;
  44. word-break: break-word;
  45. }
  46. .hero__meta {
  47. margin-top: 8px;
  48. font-size: 12px;
  49. color: var(--muted);
  50. line-height: 1.4;
  51. }
  52. .hero__divider {
  53. margin-top: 14px;
  54. height: 1px;
  55. background: rgba(170, 170, 170, 0.25);
  56. }
  57. .conversation {
  58. padding: 14px 15px 0;
  59. }
  60. .row {
  61. display: flex;
  62. margin-bottom: 12px;
  63. }
  64. .row--user {
  65. justify-content: flex-end;
  66. }
  67. .row--ai {
  68. justify-content: flex-start;
  69. }
  70. .user-bubble {
  71. max-width: 78%;
  72. padding: 10px 12px;
  73. background: var(--orange);
  74. border-radius: 6px;
  75. color: #fff;
  76. font-size: 15px;
  77. font-weight: 500;
  78. line-height: 1.5;
  79. white-space: pre-wrap;
  80. word-break: break-word;
  81. }
  82. .ai-card {
  83. width: 100%;
  84. padding: 14px 13px;
  85. background: #fff;
  86. border-radius: 8px;
  87. box-shadow: 0 1px 6px rgba(0, 0, 0, 0.04);
  88. }
  89. .ai-card__text {
  90. font-size: 14px;
  91. line-height: 1.65;
  92. white-space: pre-wrap;
  93. word-break: break-word;
  94. text-align: left;
  95. }
  96. .fallback-card {
  97. margin: 14px 15px 0;
  98. padding: 14px 13px;
  99. background: #fff;
  100. border-radius: 8px;
  101. }
  102. .empty {
  103. padding: 48px 20px;
  104. text-align: center;
  105. color: var(--muted);
  106. font-size: 14px;
  107. }
  108. .footer {
  109. position: fixed;
  110. left: 0;
  111. right: 0;
  112. bottom: 0;
  113. z-index: 20;
  114. padding: 10px 15px calc(10px + var(--safe-bottom));
  115. background: linear-gradient(180deg, rgba(244, 246, 251, 0) 0%, #f4f6fb 36%);
  116. pointer-events: none;
  117. }
  118. .footer__dock {
  119. position: relative;
  120. width: 100%;
  121. min-height: 44px;
  122. pointer-events: auto;
  123. }
  124. .continue-btn {
  125. width: 100%;
  126. height: 44px;
  127. border: none;
  128. border-radius: 999px;
  129. background: linear-gradient(90deg, #ffb347 0%, #f47d1f 55%, #ff7849 100%);
  130. box-shadow: 0 4px 12px rgba(244, 125, 31, 0.28);
  131. color: #fff;
  132. font-size: 16px;
  133. font-weight: 600;
  134. touch-action: manipulation;
  135. }
  136. #launch-btn {
  137. display: none;
  138. position: absolute;
  139. left: 0;
  140. right: 0;
  141. top: 0;
  142. z-index: 2;
  143. width: 100%;
  144. height: 44px;
  145. min-height: 44px;
  146. border-radius: 999px;
  147. overflow: hidden;
  148. }
  149. body.is-wechat.wx-jssdk-ready #launch-btn {
  150. display: block;
  151. }
  152. body.is-wechat.wx-jssdk-ready #btnContinue {
  153. pointer-events: none;
  154. }
  155. #openAppToast {
  156. display: none;
  157. position: fixed;
  158. left: 16px;
  159. right: 16px;
  160. bottom: calc(72px + var(--safe-bottom));
  161. z-index: 10001;
  162. padding: 10px 14px;
  163. font-size: 13px;
  164. line-height: 1.45;
  165. color: #fff;
  166. text-align: center;
  167. background: rgba(0, 0, 0, 0.78);
  168. border-radius: 8px;
  169. pointer-events: none;
  170. word-break: break-all;
  171. }
  172. </style>
  173. </head>
  174. <body>
  175. <main id="main">
  176. <div class="empty">对话内容加载中…</div>
  177. </main>
  178. <div id="openAppToast" role="status" aria-live="polite"></div>
  179. <footer class="footer">
  180. <div class="footer__dock">
  181. <button type="button" class="continue-btn" id="btnContinue">和U宝继续聊 →</button>
  182. <wx-open-launch-app id="launch-btn" appid="wxf5f1efe3a9f5012e" extinfo="">
  183. <script type="text/wxtag-template">
  184. <style>
  185. .wx-open-app-btn {
  186. display: block;
  187. width: 100%;
  188. height: 44px;
  189. margin: 0;
  190. padding: 0;
  191. border: none;
  192. border-radius: 999px;
  193. background: transparent;
  194. color: transparent;
  195. font-size: 16px;
  196. line-height: 44px;
  197. text-align: center;
  198. cursor: pointer;
  199. -webkit-tap-highlight-color: transparent;
  200. }
  201. </style>
  202. <button class="wx-open-app-btn" aria-label="和U宝继续聊">打开 App</button>
  203. </script>
  204. </wx-open-launch-app>
  205. </div>
  206. </footer>
  207. <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
  208. <script>
  209. (function () {
  210. "use strict";
  211. var API_BASE = "https://test.ailien.shop/alienStore";
  212. var WECHAT_MP_APP_ID = "wx412792c77f47babd";
  213. var WECHAT_OPEN_APP_ID = "wxf5f1efe3a9f5012e";
  214. var WECHAT_GET_WX_CONFIG_PATH = "/wx/getWxConfig";
  215. var H5_PAGE_BASE_FALLBACK = "https://test.ailien.shop/h5/HBuilderProjects/";
  216. var WX_GET_CONFIG_SIGN_URL = H5_PAGE_BASE_FALLBACK + "shareAiConsult.html";
  217. var WECHAT_JS_SAFE_HOSTS = ["uat.ailien.shop", "test.ailien.shop"];
  218. var APP_ANDROID_PACKAGE = "com.alien.Udianzaizhe";
  219. var APP_IOS_URL_SCHEME = "shopro://";
  220. var APP_UNI_AI_PATH = "pages/aiSearchResult/index";
  221. var sharePayloadCache = null;
  222. var weChatJssdkConfigured = false;
  223. var wxConfigSignRetriedBaseUrl = false;
  224. var wxInitLastError = "";
  225. var wxJssdkInitPromise = null;
  226. function escHtml(s) {
  227. return String(s || "")
  228. .replace(/&/g, "&amp;")
  229. .replace(/</g, "&lt;")
  230. .replace(/>/g, "&gt;")
  231. .replace(/"/g, "&quot;");
  232. }
  233. function q(name) {
  234. try {
  235. var v = new URLSearchParams(location.search || "").get(name);
  236. return v == null ? "" : String(v);
  237. } catch (e) {
  238. return "";
  239. }
  240. }
  241. function getQueryParam(name) {
  242. var m = new RegExp("[?&]" + name + "=([^&]*)").exec(window.location.search);
  243. if (!m) return "";
  244. try {
  245. return decodeURIComponent(m[1].replace(/\+/g, " "));
  246. } catch (e) {
  247. return "";
  248. }
  249. }
  250. function parsePayload() {
  251. var raw = getQueryParam("payload");
  252. if (!raw) {
  253. try {
  254. var hash = String(location.hash || "").replace(/^#/, "");
  255. if (hash) {
  256. var hp = new URLSearchParams(hash).get("payload");
  257. if (hp != null && String(hp) !== "") raw = String(hp);
  258. }
  259. } catch (eHash) {}
  260. }
  261. if (!raw) return null;
  262. try {
  263. return JSON.parse(raw);
  264. } catch (e) {
  265. return null;
  266. }
  267. }
  268. function getFirstUserQuestion(messages) {
  269. var list = Array.isArray(messages) ? messages : [];
  270. for (var i = 0; i < list.length; i++) {
  271. var m = list[i];
  272. if (!m || m.role !== "user") continue;
  273. if (m.isImage) return "[图片]";
  274. var t = String(m.content || "").trim();
  275. if (t) return t;
  276. }
  277. return "";
  278. }
  279. function formatDiscussionTitle(question) {
  280. var qText = String(question || "").trim();
  281. if (!qText) return "与AI助手对话";
  282. if (qText.slice(-3) === "的讨论") return qText;
  283. return qText + "的讨论";
  284. }
  285. function formatDate(ts) {
  286. var d = ts ? new Date(Number(ts)) : new Date();
  287. if (isNaN(d.getTime())) d = new Date();
  288. return d.getFullYear() + "年" + (d.getMonth() + 1) + "月" + d.getDate() + "日";
  289. }
  290. function renderMessages(messages) {
  291. var html = "";
  292. var list = Array.isArray(messages) ? messages : [];
  293. for (var i = 0; i < list.length; i++) {
  294. var msg = list[i];
  295. if (!msg) continue;
  296. if (msg.role === "user") {
  297. var userText = msg.isImage ? "[图片]" : String(msg.content || "").trim();
  298. if (!userText) continue;
  299. html +=
  300. '<div class="row row--user"><div class="user-bubble">' +
  301. escHtml(userText) +
  302. "</div></div>";
  303. continue;
  304. }
  305. if (msg.role === "ai") {
  306. var aiText = String(msg.content || "").trim();
  307. if (!aiText) continue;
  308. html +=
  309. '<div class="row row--ai"><div class="ai-card"><div class="ai-card__text">' +
  310. escHtml(aiText) +
  311. "</div></div></div>";
  312. }
  313. }
  314. return html;
  315. }
  316. function applyWeixinShareMeta() {
  317. var siteName = "U宝AI智能助手";
  318. var iconUrl =
  319. "https://alien-volume.oss-cn-beijing.aliyuncs.com/icon/ubao.png";
  320. var setMeta = function (key, content) {
  321. var el =
  322. document.querySelector('meta[property="' + key + '"]') ||
  323. document.querySelector('meta[name="' + key + '"]');
  324. if (!el) {
  325. el = document.createElement("meta");
  326. if (key.indexOf("og:") === 0) {
  327. el.setAttribute("property", key);
  328. } else {
  329. el.setAttribute("name", key);
  330. }
  331. document.head.appendChild(el);
  332. }
  333. el.setAttribute("content", content);
  334. };
  335. setMeta("og:site_name", siteName);
  336. setMeta("application-name", siteName);
  337. setMeta("og:title", "和U宝的对话");
  338. setMeta("og:description", "点击查看对话内容");
  339. var links = document.querySelectorAll('link[rel="icon"], link[rel="apple-touch-icon"]');
  340. for (var i = 0; i < links.length; i++) {
  341. links[i].setAttribute("href", iconUrl);
  342. }
  343. }
  344. function render(data) {
  345. applyWeixinShareMeta();
  346. sharePayloadCache = data || null;
  347. var main = document.getElementById("main");
  348. if (!data) {
  349. main.innerHTML = '<div class="empty">对话内容不存在或链接已失效</div>';
  350. refreshWxLaunchTagAttrs();
  351. return;
  352. }
  353. var messages = data.messages || [];
  354. var firstQuestion =
  355. String(data.firstQuestion || "").trim() || getFirstUserQuestion(messages);
  356. var pageTitle = formatDiscussionTitle(firstQuestion);
  357. var dateText = formatDate(data.shareTime);
  358. document.title = pageTitle;
  359. var bodyHtml =
  360. '<section class="hero">' +
  361. '<h1 class="hero__title">' +
  362. escHtml(pageTitle) +
  363. "</h1>" +
  364. '<p class="hero__meta">' +
  365. escHtml(dateText) +
  366. " · 内容由AI生成,不能完全保障真实</p>" +
  367. '<div class="hero__divider"></div>' +
  368. "</section>";
  369. var convHtml = renderMessages(messages);
  370. if (convHtml) {
  371. bodyHtml += '<section class="conversation">' + convHtml + "</section>";
  372. } else if (data.content) {
  373. bodyHtml +=
  374. '<section class="fallback-card"><div class="ai-card__text">' +
  375. escHtml(data.content) +
  376. "</div></section>";
  377. } else {
  378. bodyHtml += '<div class="empty">暂无对话内容</div>';
  379. }
  380. main.innerHTML = bodyHtml;
  381. refreshWxLaunchTagAttrs();
  382. }
  383. function buildAppDeepLink() {
  384. var path = APP_UNI_AI_PATH;
  385. var root = APP_IOS_URL_SCHEME.replace(/\/$/, "");
  386. return root + "/" + path + "?pageType=home";
  387. }
  388. /** wx-open-launch-app extinfo:仅跳转 AI 首页,不传分享上下文 */
  389. function buildWeChatLaunchExtinfo() {
  390. return APP_UNI_AI_PATH + "?pageType=home";
  391. }
  392. function isWeChatInAppBrowser() {
  393. return /MicroMessenger/i.test(navigator.userAgent || "");
  394. }
  395. function isIOSWeChatBrowser() {
  396. var ua = navigator.userAgent || "";
  397. return /MicroMessenger/i.test(ua) && /iPhone|iPad|iPod/i.test(ua);
  398. }
  399. function isWxDebugOn() {
  400. return q("wxDebug") === "1";
  401. }
  402. function isWxForceDebug() {
  403. return q("wxForce") === "1";
  404. }
  405. function isWxPcAutoDebugHost() {
  406. var h = (location.hostname || "").toLowerCase();
  407. if (h === "localhost" || h === "127.0.0.1") return true;
  408. for (var i = 0; i < WECHAT_JS_SAFE_HOSTS.length; i++) {
  409. if (WECHAT_JS_SAFE_HOSTS[i] === h) return true;
  410. }
  411. return false;
  412. }
  413. function isWxPcBrowser() {
  414. return !isWeChatInAppBrowser();
  415. }
  416. function shouldInitWeChatJssdkOnLoad() {
  417. if (isWeChatInAppBrowser()) return true;
  418. return isWxForceDebug() || isWxPcAutoDebugHost();
  419. }
  420. function shouldFetchWxConfig(fromUserClick) {
  421. if (isWeChatInAppBrowser()) return true;
  422. if (fromUserClick) return true;
  423. return isWxForceDebug() || isWxPcAutoDebugHost();
  424. }
  425. function getWxHtmlUrl() {
  426. var forced = String(q("wxSignUrl") || "").trim();
  427. if (forced) return forced.split("#")[0];
  428. return String(location.href || "").split("#")[0];
  429. }
  430. function getWxSignPageUrlForApi() {
  431. var htmlUrl = getWxHtmlUrl();
  432. if (htmlUrl && /^https?:\/\//i.test(htmlUrl)) return htmlUrl;
  433. return WX_GET_CONFIG_SIGN_URL;
  434. }
  435. function getWxHtmlUrlBase() {
  436. var htmlUrl = getWxSignPageUrlForApi();
  437. try {
  438. var u = new URL(htmlUrl);
  439. return u.origin + u.pathname;
  440. } catch (eU) {
  441. return WX_GET_CONFIG_SIGN_URL;
  442. }
  443. }
  444. /** 签名 url 不含 query/hash,避免 payload 过长导致 wx.config / 开放标签 launch:fail */
  445. function getWxConfigSignUrl() {
  446. if (String(q("wxSignFullUrl") || "") === "1") return getWxSignPageUrlForApi();
  447. return getWxHtmlUrlBase();
  448. }
  449. function getWxGetConfigApiUrl() {
  450. return API_BASE.replace(/\/$/, "") + WECHAT_GET_WX_CONFIG_PATH;
  451. }
  452. function buildWxGetConfigRequestBody(htmlUrl) {
  453. return {
  454. url: String(htmlUrl || "").split("#")[0].trim(),
  455. };
  456. }
  457. function resolveWxConfigAppIdFromSignData(d) {
  458. if (!d || typeof d !== "object") return "";
  459. var mp =
  460. d.mpAppId ||
  461. d.mpAppid ||
  462. d.officialAppId ||
  463. d.gzhAppId ||
  464. d.serviceAppId;
  465. if (mp != null && String(mp).trim() !== "") return String(mp).trim();
  466. var fromQuery = String(q("wxMpAppId") || WECHAT_MP_APP_ID || "").trim();
  467. if (fromQuery) return fromQuery;
  468. var raw = d.appId || d.appid || d.wxAppId;
  469. return raw != null && String(raw).trim() !== "" ? String(raw).trim() : "";
  470. }
  471. function normalizeWxJssdkSignPayload(res, signUrlUsed) {
  472. if (!res || typeof res !== "object") return null;
  473. var d = res.data != null && typeof res.data === "object" ? res.data : res;
  474. if (!d || typeof d !== "object") return null;
  475. var appId = resolveWxConfigAppIdFromSignData(d);
  476. var timestamp = d.timestamp != null ? d.timestamp : d.timeStamp;
  477. var nonceStr =
  478. d.nonceStr != null && String(d.nonceStr) !== ""
  479. ? d.nonceStr
  480. : d.noncestr != null && String(d.noncestr) !== ""
  481. ? d.noncestr
  482. : d.nonce;
  483. var signature = d.signature || d.sign;
  484. if (!appId || timestamp == null || nonceStr == null || nonceStr === "" || !signature) {
  485. return null;
  486. }
  487. return {
  488. appId: String(appId),
  489. timestamp: Number(timestamp),
  490. nonceStr: String(nonceStr),
  491. signature: String(signature),
  492. signUrl: String(signUrlUsed || "")
  493. .split("#")[0]
  494. .trim(),
  495. };
  496. }
  497. function getWxErrMsg(err) {
  498. if (!err) return "";
  499. if (err.errMsg) return String(err.errMsg);
  500. if (typeof err === "string") return err;
  501. try {
  502. return JSON.stringify(err);
  503. } catch (e) {
  504. return String(err);
  505. }
  506. }
  507. function formatWxConfigErrorTip(err, signPageUrl) {
  508. var errMsg = getWxErrMsg(err);
  509. var tip = "微信 JSSDK 配置失败";
  510. if (/invalid signature/i.test(errMsg)) {
  511. return (
  512. tip +
  513. ":签名无效。请核对:①后端用与前端相同的 url 签名;②url=" +
  514. (signPageUrl || getWxHtmlUrl()) +
  515. ";③nonceStr/timestamp 与接口返回一致;④appId=" +
  516. WECHAT_MP_APP_ID
  517. );
  518. }
  519. if (/require\s*subscribe/i.test(errMsg)) {
  520. return tip + ":" + errMsg + "(服务号需用户已关注)";
  521. }
  522. return errMsg ? tip + ":" + errMsg : tip;
  523. }
  524. function setWxInitError(msg) {
  525. wxInitLastError = String(msg || "").trim();
  526. if (wxInitLastError) console.warn("[wx]", wxInitLastError);
  527. }
  528. function requestWeChatJssdkSignAndConfig(htmlUrlOptional) {
  529. var htmlUrl = String(htmlUrlOptional != null ? htmlUrlOptional : getWxConfigSignUrl())
  530. .split("#")[0]
  531. .trim();
  532. if (!htmlUrl || !/^https?:\/\//i.test(htmlUrl)) {
  533. htmlUrl = WX_GET_CONFIG_SIGN_URL;
  534. }
  535. if (isWxDebugOn()) {
  536. try {
  537. window.alert("htmlUrl(签名用,应与地址栏一致):\n" + htmlUrl);
  538. } catch (eDbg) {}
  539. }
  540. var requestUrl = getWxGetConfigApiUrl();
  541. var requestBody = buildWxGetConfigRequestBody(htmlUrl);
  542. return fetch(requestUrl, {
  543. method: "POST",
  544. mode: "cors",
  545. credentials: "omit",
  546. headers: {
  547. Accept: "application/json",
  548. "Content-Type": "application/json;charset=UTF-8",
  549. },
  550. body: JSON.stringify(requestBody),
  551. })
  552. .then(function (r) {
  553. if (r.ok) return r.json();
  554. return r
  555. .text()
  556. .catch(function () {
  557. return "";
  558. })
  559. .then(function (text) {
  560. var hint = "";
  561. try {
  562. var j = JSON.parse(text);
  563. hint = j.msg || j.message || "";
  564. } catch (eP) {
  565. if (text) hint = text.slice(0, 120);
  566. }
  567. throw new Error("getWxConfig HTTP " + r.status + (hint ? ":" + hint : ""));
  568. });
  569. })
  570. .then(function (res) {
  571. if (res && res.code != null) {
  572. var c = Number(res.code);
  573. if (c !== 200 && c !== 0 && res.success !== true) {
  574. throw new Error(res.msg || res.message || "getWxConfig code " + c);
  575. }
  576. }
  577. var sign = normalizeWxJssdkSignPayload(res, htmlUrl);
  578. if (!sign) {
  579. throw new Error("getWxConfig 缺少 appId/timestamp/nonceStr/signature(见控制台)");
  580. }
  581. if (typeof wx === "undefined") {
  582. if (isWxPcBrowser()) {
  583. console.log("[wx] PC getWxConfig 成功(无 jweixin)", sign);
  584. return true;
  585. }
  586. setWxInitError("jweixin.js 未加载");
  587. return false;
  588. }
  589. return applyWxConfigFromSign(sign, htmlUrl);
  590. });
  591. }
  592. function applyWxConfigFromSign(sign, htmlUrl) {
  593. var wxConfigParams = {
  594. debug: isWxDebugOn(),
  595. appId: String(sign.appId),
  596. timestamp: sign.timestamp,
  597. nonceStr: String(sign.nonceStr),
  598. signature: String(sign.signature),
  599. jsApiList: [],
  600. openTagList: ["wx-open-launch-app"],
  601. };
  602. return new Promise(function (resolve) {
  603. wx.config(wxConfigParams);
  604. wx.ready(function () {
  605. weChatJssdkConfigured = true;
  606. document.body.classList.add("wx-jssdk-ready");
  607. refreshWxLaunchTagAttrs();
  608. console.log("[wx.config] ready, htmlUrl=", htmlUrl);
  609. resolve(true);
  610. });
  611. wx.error(function (err) {
  612. weChatJssdkConfigured = false;
  613. document.body.classList.remove("wx-jssdk-ready");
  614. wxJssdkInitPromise = null;
  615. var errMsg = getWxErrMsg(err);
  616. setWxInitError(formatWxConfigErrorTip(err, htmlUrl));
  617. if (isWxDebugOn()) window.alert(wxInitLastError);
  618. console.warn("[wx.config]", errMsg, "htmlUrl=", htmlUrl);
  619. var baseUrl = getWxHtmlUrlBase();
  620. var fullUrl = getWxSignPageUrlForApi();
  621. if (
  622. !/invalid signature/i.test(errMsg) ||
  623. wxConfigSignRetriedBaseUrl ||
  624. baseUrl === fullUrl ||
  625. htmlUrl === baseUrl
  626. ) {
  627. resolve(false);
  628. return;
  629. }
  630. wxConfigSignRetriedBaseUrl = true;
  631. requestWeChatJssdkSignAndConfig(baseUrl).then(resolve);
  632. });
  633. });
  634. }
  635. function refreshWxLaunchTagAttrs() {
  636. var tag = document.getElementById("launch-btn");
  637. if (!tag) return;
  638. try {
  639. tag.setAttribute("appid", WECHAT_OPEN_APP_ID);
  640. tag.setAttribute("extinfo", buildWeChatLaunchExtinfo());
  641. } catch (eA) {}
  642. }
  643. function bindWeChatLaunchTagEvents() {
  644. var tag = document.getElementById("launch-btn");
  645. if (!tag || tag._wxLaunchBound) return;
  646. tag._wxLaunchBound = true;
  647. refreshWxLaunchTagAttrs();
  648. tag.addEventListener("launch", function () {
  649. console.log("[wx-open-launch-app] launch ok");
  650. });
  651. tag.addEventListener("error", function (e) {
  652. var detail = e && e.detail;
  653. var errMsg =
  654. detail && detail.errMsg
  655. ? String(detail.errMsg)
  656. : detail && detail.errmsg
  657. ? String(detail.errmsg)
  658. : "";
  659. console.warn("[wx-open-launch-app]", detail);
  660. showAppOpenFailTip(
  661. errMsg
  662. ? "未能打开 App:" + errMsg
  663. : "未能打开 App,请确认已安装最新版「U店在哪」"
  664. );
  665. });
  666. }
  667. function initWeChatOpenLaunchApp(fromUserClick) {
  668. if (!shouldFetchWxConfig(!!fromUserClick)) {
  669. return Promise.resolve(false);
  670. }
  671. if (wxJssdkInitPromise && !fromUserClick) return wxJssdkInitPromise;
  672. wxConfigSignRetriedBaseUrl = false;
  673. wxInitLastError = "";
  674. var htmlUrl = getWxConfigSignUrl();
  675. bindWeChatLaunchTagEvents();
  676. wxJssdkInitPromise = requestWeChatJssdkSignAndConfig(htmlUrl)
  677. .then(function (ok) {
  678. if (ok === true) return true;
  679. if (!wxInitLastError) {
  680. setWxInitError("wx.config 失败,可加 ?wxDebug=1 查看 htmlUrl");
  681. }
  682. return false;
  683. })
  684. .catch(function (e) {
  685. var msg = e && e.message ? e.message : "getWxConfig 请求失败";
  686. setWxInitError(msg);
  687. console.warn("[wx] requestWeChatJssdkSignAndConfig failed", msg, "htmlUrl=", htmlUrl);
  688. if (isWeChatInAppBrowser() && isWxDebugOn()) window.alert(msg);
  689. return false;
  690. })
  691. .finally(function () {
  692. if (!weChatJssdkConfigured) wxJssdkInitPromise = null;
  693. });
  694. return wxJssdkInitPromise;
  695. }
  696. function scheduleWeChatJssdkBootstrap() {
  697. if (!shouldInitWeChatJssdkOnLoad()) return;
  698. var attempts = 0;
  699. function tick() {
  700. attempts += 1;
  701. if (weChatJssdkConfigured) return;
  702. initWeChatOpenLaunchApp();
  703. if (!weChatJssdkConfigured && attempts < 8 && typeof wx === "undefined") {
  704. setTimeout(tick, 400);
  705. }
  706. }
  707. if (isIOSWeChatBrowser()) {
  708. setTimeout(tick, 350);
  709. } else {
  710. tick();
  711. }
  712. }
  713. function showFabToast(msg, ms) {
  714. var tip = String(msg || "").trim();
  715. if (!tip) return;
  716. var el = document.getElementById("openAppToast");
  717. if (el) {
  718. el.textContent = tip;
  719. el.style.display = "block";
  720. if (showFabToast._t) clearTimeout(showFabToast._t);
  721. showFabToast._t = setTimeout(function () {
  722. el.style.display = "none";
  723. }, ms || 2800);
  724. }
  725. console.log("[openApp]", tip);
  726. }
  727. function showDownloadTip() {
  728. var msg = "请到应用商店下载「U店在哪」";
  729. if (typeof uni !== "undefined" && typeof uni.showToast === "function") {
  730. uni.showToast({ title: msg, icon: "none", duration: 2500 });
  731. } else {
  732. window.alert(msg);
  733. }
  734. }
  735. function showAppOpenFailTip(msg) {
  736. var tip = msg || "未能打开 App,请确认已安装最新版「U店在哪」。";
  737. if (typeof uni !== "undefined" && typeof uni.showToast === "function") {
  738. uni.showToast({ title: msg, icon: "none", duration: 2800 });
  739. } else if (isWeChatInAppBrowser()) {
  740. window.alert(tip);
  741. } else {
  742. showFabToast(tip);
  743. }
  744. }
  745. function launchAppDeepLink(deepLink) {
  746. try {
  747. var a = document.createElement("a");
  748. a.href = deepLink;
  749. a.setAttribute("target", "_self");
  750. document.body.appendChild(a);
  751. a.click();
  752. document.body.removeChild(a);
  753. } catch (e1) {}
  754. try {
  755. window.location.href = deepLink;
  756. } catch (e2) {}
  757. }
  758. function tryOpenHBuilderAppViaScheme() {
  759. var deepLink = buildAppDeepLink();
  760. if (typeof plus !== "undefined" && plus.runtime) {
  761. var installed = null;
  762. try {
  763. if (typeof plus.runtime.isApplicationExist === "function") {
  764. installed = plus.runtime.isApplicationExist({
  765. pname: APP_ANDROID_PACKAGE,
  766. action: APP_IOS_URL_SCHEME,
  767. });
  768. }
  769. } catch (e) {
  770. console.warn(e);
  771. }
  772. try {
  773. plus.runtime.openURL(deepLink);
  774. } catch (e2) {
  775. console.warn(e2);
  776. if (installed === false) {
  777. showDownloadTip();
  778. }
  779. }
  780. return;
  781. }
  782. var done = false;
  783. function finish() {
  784. if (done) return;
  785. done = true;
  786. document.removeEventListener("visibilitychange", onVis);
  787. window.removeEventListener("pagehide", onHide);
  788. }
  789. function onVis() {
  790. if (document.visibilityState === "hidden") finish();
  791. }
  792. function onHide() {
  793. finish();
  794. }
  795. document.addEventListener("visibilitychange", onVis);
  796. window.addEventListener("pagehide", onHide);
  797. try {
  798. launchAppDeepLink(deepLink);
  799. } catch (e3) {
  800. finish();
  801. showDownloadTip();
  802. return;
  803. }
  804. window.setTimeout(function () {
  805. finish();
  806. }, 3200);
  807. }
  808. function tryOpenApp() {
  809. if (isWeChatInAppBrowser()) {
  810. if (weChatJssdkConfigured) return;
  811. initWeChatOpenLaunchApp(true).then(function (ok) {
  812. if (!ok) {
  813. showAppOpenFailTip(wxInitLastError || "微信唤起 App 初始化失败");
  814. }
  815. });
  816. return;
  817. }
  818. tryOpenHBuilderAppViaScheme();
  819. }
  820. if (isWeChatInAppBrowser()) {
  821. document.body.classList.add("is-wechat");
  822. }
  823. bindWeChatLaunchTagEvents();
  824. scheduleWeChatJssdkBootstrap();
  825. document.getElementById("btnContinue").addEventListener("click", tryOpenApp);
  826. render(parsePayload());
  827. })();
  828. </script>
  829. </body>
  830. </html>