shareAiConsult.html 32 KB

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