shareAiConsult.html 30 KB

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