shareAiConsult.html 27 KB

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