shareAiConsult.html 31 KB

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