shareAiConsult.html 34 KB

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