shareAiConsult.html 32 KB

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