shareAiConsult.html 38 KB

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