shareAiConsult.html 29 KB

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