shareAiConsult.html 28 KB

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