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