shareAiConsult.html 33 KB

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