shareAiConsult.html 33 KB

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