shareAiConsult.html 30 KB

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