shareAiConsult.html 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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. position: absolute;
  175. left: 0;
  176. top: 0;
  177. width: 100%;
  178. height: 100%;
  179. min-height: 48px;
  180. border-radius: 999px;
  181. overflow: hidden;
  182. }
  183. body.is-wechat.wx-jssdk-ready #launch-btn {
  184. display: block;
  185. }
  186. body.is-wechat.wx-jssdk-ready #btnContinue {
  187. visibility: hidden;
  188. pointer-events: none;
  189. }
  190. #openAppToast {
  191. display: none;
  192. position: fixed;
  193. left: 16px;
  194. right: 16px;
  195. bottom: calc(72px + var(--safe-bottom));
  196. z-index: 10001;
  197. padding: 10px 14px;
  198. font-size: 13px;
  199. line-height: 1.45;
  200. color: #fff;
  201. text-align: center;
  202. background: rgba(0, 0, 0, 0.78);
  203. border-radius: 8px;
  204. pointer-events: none;
  205. word-break: break-all;
  206. }
  207. </style>
  208. </head>
  209. <body>
  210. <main id="main">
  211. <div class="empty">对话内容加载中…</div>
  212. </main>
  213. <div id="openAppToast" role="status" aria-live="polite"></div>
  214. <footer class="footer">
  215. <div class="footer__dock">
  216. <div class="footer__btn-slot">
  217. <button type="button" class="continue-btn" id="btnContinue" aria-label="和U宝继续聊">
  218. <img class="continue-btn__label" src="images/uCharts.png" alt="和U宝继续聊" decoding="async">
  219. </button>
  220. <wx-open-launch-app id="launch-btn" appid="wxf5f1efe3a9f5012e" extinfo="">
  221. <script type="text/wxtag-template">
  222. <style>
  223. .wx-open-app-btn {
  224. display: flex;
  225. align-items: center;
  226. justify-content: center;
  227. width: 100%;
  228. height: 48px;
  229. margin: 0;
  230. padding: 0;
  231. border: none;
  232. border-radius: 999px;
  233. background: linear-gradient(90deg, #ffb84c 0%, #ff6b3e 100%);
  234. box-shadow: 0 4px 16px rgba(255, 107, 62, 0.32);
  235. cursor: pointer;
  236. -webkit-tap-highlight-color: transparent;
  237. box-sizing: border-box;
  238. }
  239. .wx-open-app-btn__label {
  240. display: block;
  241. height: 20px;
  242. width: auto;
  243. max-width: 90%;
  244. pointer-events: none;
  245. }
  246. </style>
  247. <button class="wx-open-app-btn" aria-label="和U宝继续聊">
  248. <img class="wx-open-app-btn__label" src="https://test.ailien.shop/h5/HBuilderProjects/images/uCharts.png" alt="和U宝继续聊" decoding="async">
  249. </button>
  250. </script>
  251. </wx-open-launch-app>
  252. </div>
  253. </div>
  254. </footer>
  255. <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
  256. <script>
  257. (function () {
  258. "use strict";
  259. var API_BASE = "https://test.ailien.shop/alienStore";
  260. var WECHAT_MP_APP_ID = "wx412792c77f47babd";
  261. var WECHAT_OPEN_APP_ID = "wxf5f1efe3a9f5012e";
  262. var WECHAT_GET_WX_CONFIG_PATH = "/wx/getWxConfig";
  263. var H5_PAGE_BASE_FALLBACK = "https://test.ailien.shop/h5/HBuilderProjects/";
  264. var WX_GET_CONFIG_SIGN_URL = H5_PAGE_BASE_FALLBACK + "shareAiConsult.html";
  265. var WECHAT_JS_SAFE_HOSTS = ["uat.ailien.shop", "test.ailien.shop"];
  266. var APP_ANDROID_PACKAGE = "com.alien.Udianzaizhe";
  267. var APP_IOS_URL_SCHEME = "shopro://";
  268. var APP_UNI_AI_PATH = "pages/aiSearchResult/index";
  269. var sharePayloadCache = null;
  270. var weChatJssdkConfigured = false;
  271. var wxConfigSignRetriedBaseUrl = false;
  272. var wxInitLastError = "";
  273. var wxJssdkInitPromise = null;
  274. function escHtml(s) {
  275. return String(s || "")
  276. .replace(/&/g, "&amp;")
  277. .replace(/</g, "&lt;")
  278. .replace(/>/g, "&gt;")
  279. .replace(/"/g, "&quot;");
  280. }
  281. function q(name) {
  282. try {
  283. var v = new URLSearchParams(location.search || "").get(name);
  284. return v == null ? "" : String(v);
  285. } catch (e) {
  286. return "";
  287. }
  288. }
  289. function getQueryParam(name) {
  290. var m = new RegExp("[?&]" + name + "=([^&]*)").exec(window.location.search);
  291. if (!m) return "";
  292. try {
  293. return decodeURIComponent(m[1].replace(/\+/g, " "));
  294. } catch (e) {
  295. return "";
  296. }
  297. }
  298. function parsePayload() {
  299. var raw = getQueryParam("payload");
  300. if (!raw) return null;
  301. try {
  302. return JSON.parse(raw);
  303. } catch (e) {
  304. return null;
  305. }
  306. }
  307. function getFirstUserQuestion(messages) {
  308. var list = Array.isArray(messages) ? messages : [];
  309. for (var i = 0; i < list.length; i++) {
  310. var m = list[i];
  311. if (!m || m.role !== "user") continue;
  312. if (m.isImage) return "[图片]";
  313. var t = String(m.content || "").trim();
  314. if (t) return t;
  315. }
  316. return "";
  317. }
  318. function formatDiscussionTitle(question) {
  319. var qText = String(question || "").trim();
  320. if (!qText) return "与AI助手对话";
  321. if (qText.slice(-3) === "的讨论") return qText;
  322. return qText + "的讨论";
  323. }
  324. function formatDate(ts) {
  325. var d = ts ? new Date(Number(ts)) : new Date();
  326. if (isNaN(d.getTime())) d = new Date();
  327. return d.getFullYear() + "年" + (d.getMonth() + 1) + "月" + d.getDate() + "日";
  328. }
  329. function renderMessages(messages) {
  330. var html = "";
  331. var list = Array.isArray(messages) ? messages : [];
  332. for (var i = 0; i < list.length; i++) {
  333. var msg = list[i];
  334. if (!msg) continue;
  335. if (msg.role === "user") {
  336. var userText = msg.isImage ? "[图片]" : String(msg.content || "").trim();
  337. if (!userText) continue;
  338. html +=
  339. '<div class="row row--user"><div class="user-bubble">' +
  340. escHtml(userText) +
  341. "</div></div>";
  342. continue;
  343. }
  344. if (msg.role === "ai") {
  345. var aiText = String(msg.content || "").trim();
  346. if (!aiText) continue;
  347. html +=
  348. '<div class="row row--ai"><div class="ai-card"><div class="ai-card__text">' +
  349. escHtml(aiText) +
  350. "</div></div></div>";
  351. }
  352. }
  353. return html;
  354. }
  355. function applyWeixinShareMeta() {
  356. var siteName = "U店在哪";
  357. var iconUrl =
  358. "https://alien-volume.oss-cn-beijing.aliyuncs.com/icon/ubao.png";
  359. var setMeta = function (key, content) {
  360. var el =
  361. document.querySelector('meta[property="' + key + '"]') ||
  362. document.querySelector('meta[name="' + key + '"]');
  363. if (!el) {
  364. el = document.createElement("meta");
  365. if (key.indexOf("og:") === 0) {
  366. el.setAttribute("property", key);
  367. } else {
  368. el.setAttribute("name", key);
  369. }
  370. document.head.appendChild(el);
  371. }
  372. el.setAttribute("content", content);
  373. };
  374. setMeta("og:site_name", siteName);
  375. setMeta("application-name", siteName);
  376. setMeta("og:title", "和U宝的对话");
  377. setMeta("og:description", "点击查看对话内容");
  378. var links = document.querySelectorAll('link[rel="icon"], link[rel="apple-touch-icon"]');
  379. for (var i = 0; i < links.length; i++) {
  380. links[i].setAttribute("href", iconUrl);
  381. }
  382. }
  383. function render(data) {
  384. applyWeixinShareMeta();
  385. sharePayloadCache = data || null;
  386. var main = document.getElementById("main");
  387. if (!data) {
  388. main.innerHTML = '<div class="empty">对话内容不存在或链接已失效</div>';
  389. refreshWxLaunchTagAttrs();
  390. return;
  391. }
  392. var messages = data.messages || [];
  393. var firstQuestion =
  394. String(data.firstQuestion || "").trim() || getFirstUserQuestion(messages);
  395. var pageTitle = formatDiscussionTitle(firstQuestion);
  396. var dateText = formatDate(data.shareTime);
  397. document.title = pageTitle;
  398. var bodyHtml =
  399. '<section class="hero">' +
  400. '<h1 class="hero__title">' +
  401. escHtml(pageTitle) +
  402. "</h1>" +
  403. '<p class="hero__meta">' +
  404. escHtml(dateText) +
  405. " · 内容由AI生成,不能完全保障真实</p>" +
  406. '<div class="hero__divider"></div>' +
  407. "</section>";
  408. var convHtml = renderMessages(messages);
  409. if (convHtml) {
  410. bodyHtml += '<section class="conversation">' + convHtml + "</section>";
  411. } else if (data.content) {
  412. bodyHtml +=
  413. '<section class="fallback-card"><div class="ai-card__text">' +
  414. escHtml(data.content) +
  415. "</div></section>";
  416. } else {
  417. bodyHtml += '<div class="empty">暂无对话内容</div>';
  418. }
  419. main.innerHTML = bodyHtml;
  420. refreshWxLaunchTagAttrs();
  421. }
  422. function buildAiConsultOpenParams() {
  423. var params = new URLSearchParams();
  424. params.set("pageType", "home");
  425. params.set("fromShareAiConsult", "1");
  426. return params;
  427. }
  428. function buildAppDeepLink() {
  429. var path = APP_UNI_AI_PATH;
  430. var qs = buildAiConsultOpenParams().toString();
  431. var root = APP_IOS_URL_SCHEME.replace(/\/$/, "");
  432. return root + "/" + path + (qs ? "?" + qs : "");
  433. }
  434. function buildWeChatLaunchExtinfo() {
  435. var qs = buildAiConsultOpenParams().toString();
  436. var uniPage = qs ? APP_UNI_AI_PATH + "?" + qs : APP_UNI_AI_PATH;
  437. return uniPage.length <= 1024 ? uniPage : APP_UNI_AI_PATH + "?pageType=home&fromShareAiConsult=1";
  438. }
  439. function isWeChatInAppBrowser() {
  440. return /MicroMessenger/i.test(navigator.userAgent || "");
  441. }
  442. function isIOSWeChatBrowser() {
  443. var ua = navigator.userAgent || "";
  444. return /MicroMessenger/i.test(ua) && /iPhone|iPad|iPod/i.test(ua);
  445. }
  446. function isWxDebugOn() {
  447. return q("wxDebug") === "1";
  448. }
  449. function isWxForceDebug() {
  450. return q("wxForce") === "1";
  451. }
  452. function isWxPcAutoDebugHost() {
  453. var h = (location.hostname || "").toLowerCase();
  454. if (h === "localhost" || h === "127.0.0.1") return true;
  455. for (var i = 0; i < WECHAT_JS_SAFE_HOSTS.length; i++) {
  456. if (WECHAT_JS_SAFE_HOSTS[i] === h) return true;
  457. }
  458. return false;
  459. }
  460. function isWxPcBrowser() {
  461. return !isWeChatInAppBrowser();
  462. }
  463. function shouldInitWeChatJssdkOnLoad() {
  464. if (isWeChatInAppBrowser()) return true;
  465. return isWxForceDebug() || isWxPcAutoDebugHost();
  466. }
  467. function shouldFetchWxConfig(fromUserClick) {
  468. if (isWeChatInAppBrowser()) return true;
  469. if (fromUserClick) return true;
  470. return isWxForceDebug() || isWxPcAutoDebugHost();
  471. }
  472. function getWxHtmlUrl() {
  473. var forced = String(q("wxSignUrl") || "").trim();
  474. if (forced) return forced.split("#")[0];
  475. return String(location.href || "").split("#")[0];
  476. }
  477. function getWxSignPageUrlForApi() {
  478. var htmlUrl = getWxHtmlUrl();
  479. if (htmlUrl && /^https?:\/\//i.test(htmlUrl)) return htmlUrl;
  480. return WX_GET_CONFIG_SIGN_URL;
  481. }
  482. function getWxHtmlUrlBase() {
  483. var htmlUrl = getWxSignPageUrlForApi();
  484. try {
  485. var u = new URL(htmlUrl);
  486. return u.origin + u.pathname;
  487. } catch (eU) {
  488. return WX_GET_CONFIG_SIGN_URL;
  489. }
  490. }
  491. function getWxConfigSignUrl() {
  492. if (String(q("wxSignBaseOnly") || "") === "1") return getWxHtmlUrlBase();
  493. return getWxSignPageUrlForApi();
  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 refreshWxLaunchTagAttrs() {
  682. var tag = document.getElementById("launch-btn");
  683. if (!tag) return;
  684. try {
  685. tag.setAttribute("appid", WECHAT_OPEN_APP_ID);
  686. tag.setAttribute("extinfo", buildWeChatLaunchExtinfo());
  687. } catch (eA) {}
  688. }
  689. function bindWeChatLaunchTagEvents() {
  690. var tag = document.getElementById("launch-btn");
  691. if (!tag || tag._wxLaunchBound) return;
  692. tag._wxLaunchBound = true;
  693. refreshWxLaunchTagAttrs();
  694. tag.addEventListener("launch", function () {
  695. console.log("[wx-open-launch-app] launch ok");
  696. });
  697. tag.addEventListener("error", function (e) {
  698. var detail = e && e.detail;
  699. var errMsg =
  700. detail && detail.errMsg
  701. ? String(detail.errMsg)
  702. : detail && detail.errmsg
  703. ? String(detail.errmsg)
  704. : "";
  705. console.warn("[wx-open-launch-app]", detail);
  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: msg, icon: "none", duration: 2800 });
  785. } else if (isWeChatInAppBrowser()) {
  786. window.alert(tip);
  787. } else {
  788. showFabToast(tip);
  789. }
  790. }
  791. function launchAppDeepLink(deepLink) {
  792. try {
  793. var a = document.createElement("a");
  794. a.href = deepLink;
  795. a.setAttribute("target", "_self");
  796. document.body.appendChild(a);
  797. a.click();
  798. document.body.removeChild(a);
  799. } catch (e1) {}
  800. try {
  801. window.location.href = deepLink;
  802. } catch (e2) {}
  803. }
  804. function tryOpenHBuilderAppViaScheme() {
  805. var deepLink = buildAppDeepLink();
  806. if (typeof plus !== "undefined" && plus.runtime) {
  807. var installed = null;
  808. try {
  809. if (typeof plus.runtime.isApplicationExist === "function") {
  810. installed = plus.runtime.isApplicationExist({
  811. pname: APP_ANDROID_PACKAGE,
  812. action: APP_IOS_URL_SCHEME,
  813. });
  814. }
  815. } catch (e) {
  816. console.warn(e);
  817. }
  818. try {
  819. plus.runtime.openURL(deepLink);
  820. } catch (e2) {
  821. console.warn(e2);
  822. if (installed === false) {
  823. showDownloadTip();
  824. }
  825. }
  826. return;
  827. }
  828. var done = false;
  829. function finish() {
  830. if (done) return;
  831. done = true;
  832. document.removeEventListener("visibilitychange", onVis);
  833. window.removeEventListener("pagehide", onHide);
  834. }
  835. function onVis() {
  836. if (document.visibilityState === "hidden") finish();
  837. }
  838. function onHide() {
  839. finish();
  840. }
  841. document.addEventListener("visibilitychange", onVis);
  842. window.addEventListener("pagehide", onHide);
  843. try {
  844. launchAppDeepLink(deepLink);
  845. } catch (e3) {
  846. finish();
  847. showDownloadTip();
  848. return;
  849. }
  850. window.setTimeout(function () {
  851. finish();
  852. }, 3200);
  853. }
  854. function tryOpenApp() {
  855. if (isWeChatInAppBrowser()) {
  856. if (!weChatJssdkConfigured) {
  857. initWeChatOpenLaunchApp(true).then(function (ok) {
  858. if (!ok) {
  859. showAppOpenFailTip(wxInitLastError || "微信唤起 App 初始化失败");
  860. }
  861. });
  862. }
  863. return;
  864. }
  865. tryOpenHBuilderAppViaScheme();
  866. }
  867. if (isWeChatInAppBrowser()) {
  868. document.body.classList.add("is-wechat");
  869. }
  870. bindWeChatLaunchTagEvents();
  871. scheduleWeChatJssdkBootstrap();
  872. document.getElementById("btnContinue").addEventListener("click", tryOpenApp);
  873. render(parsePayload());
  874. })();
  875. </script>
  876. </body>
  877. </html>