shareAiConsult.html 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  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" || msg.role === "assistant") {
  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 countDisplayableMessages(messages) {
  486. var list = Array.isArray(messages) ? messages : [];
  487. var count = 0;
  488. for (var i = 0; i < list.length; i++) {
  489. var msg = list[i];
  490. if (!msg) continue;
  491. if (msg.role === "user") {
  492. if (msg.isImage && msg.imageUrl) {
  493. count += 1;
  494. continue;
  495. }
  496. if (String(msg.content || "").trim()) count += 1;
  497. continue;
  498. }
  499. if (msg.role === "ai" || msg.role === "assistant") {
  500. if (String(msg.content || "").trim()) count += 1;
  501. }
  502. }
  503. return count;
  504. }
  505. function parseMessagesFromContent(text) {
  506. var raw = String(text || "").trim();
  507. if (!raw) return [];
  508. var list = [];
  509. var blocks = raw.split(/\n\n+/);
  510. for (var i = 0; i < blocks.length; i++) {
  511. var block = String(blocks[i] || "").trim();
  512. if (!block) continue;
  513. var userMatch = block.match(/^我[::]\s*([\s\S]*)$/);
  514. if (userMatch) {
  515. var userText = String(userMatch[1] || "").trim();
  516. if (userText) list.push({ role: "user", content: userText });
  517. continue;
  518. }
  519. var aiMatch = block.match(/^UBAO[::]\s*([\s\S]*)$/i);
  520. if (aiMatch) {
  521. var aiText = String(aiMatch[1] || "").trim();
  522. if (aiText) list.push({ role: "ai", content: aiText });
  523. }
  524. }
  525. if (list.length) return list;
  526. var current = null;
  527. var lines = raw.split(/\n/);
  528. for (var j = 0; j < lines.length; j++) {
  529. var line = String(lines[j] || "");
  530. var userLine = line.match(/^我[::]\s*(.*)$/);
  531. var aiLine = line.match(/^UBAO[::]\s*(.*)$/i);
  532. if (userLine) {
  533. if (current) list.push(current);
  534. current = { role: "user", content: String(userLine[1] || "").trim() };
  535. continue;
  536. }
  537. if (aiLine) {
  538. if (current) list.push(current);
  539. current = { role: "ai", content: String(aiLine[1] || "").trim() };
  540. continue;
  541. }
  542. if (current && line.trim()) {
  543. current.content = (current.content ? current.content + "\n" : "") + line.trim();
  544. }
  545. }
  546. if (current && String(current.content || "").trim()) list.push(current);
  547. return list;
  548. }
  549. function resolveShareMessagesForDisplay(data) {
  550. var fromArray = Array.isArray(data && data.messages) ? data.messages : [];
  551. var fromContent = parseMessagesFromContent((data && data.content) || "");
  552. var arrayCount = countDisplayableMessages(fromArray);
  553. var contentCount = countDisplayableMessages(fromContent);
  554. // 分享链接超长时 messages 会被截断,但 content 仍保留完整对话文本
  555. if (contentCount > arrayCount) return fromContent;
  556. if (arrayCount) return fromArray;
  557. return fromContent;
  558. }
  559. function render(data) {
  560. applyWeixinShareMeta();
  561. sharePayloadCache = data || null;
  562. var main = document.getElementById("main");
  563. if (!data) {
  564. main.innerHTML = '<div class="empty">对话内容不存在或链接已失效</div>';
  565. refreshWxLaunchTagAttrs();
  566. return;
  567. }
  568. var messages = resolveShareMessagesForDisplay(data);
  569. var firstQuestion =
  570. String(data.firstQuestion || "").trim() ||
  571. getFirstUserQuestion(messages) ||
  572. (function () {
  573. var text = String(data.content || "").trim();
  574. var m = text.match(/^我[::]([\s\S]+?)(?:\n\n|$)/);
  575. return m ? String(m[1] || "").trim() : "";
  576. })();
  577. var pageTitle = formatDiscussionTitle(firstQuestion);
  578. var dateText = formatDate(data.shareTime);
  579. document.title = pageTitle;
  580. var bodyHtml =
  581. '<section class="hero">' +
  582. '<h1 class="hero__title">' +
  583. escHtml(pageTitle) +
  584. "</h1>" +
  585. '<p class="hero__meta">' +
  586. escHtml(dateText) +
  587. " · 内容由AI生成,不能完全保障真实</p>" +
  588. '<div class="hero__divider"></div>' +
  589. "</section>";
  590. var convHtml = renderMessages(messages);
  591. if (convHtml) {
  592. bodyHtml +=
  593. '<section class="conversation">' +
  594. convHtml +
  595. '</section><div class="scroll-spacer" aria-hidden="true"></div>';
  596. } else {
  597. bodyHtml += '<div class="empty">暂无对话内容</div>';
  598. }
  599. main.innerHTML = bodyHtml;
  600. refreshWxLaunchTagAttrs();
  601. }
  602. function buildAiConsultOpenParams() {
  603. var params = new URLSearchParams();
  604. params.set("pageType", "home");
  605. params.set("fromShareAiConsult", "1");
  606. return params;
  607. }
  608. function buildAppDeepLink() {
  609. var path = APP_UNI_AI_PATH;
  610. var qs = buildAiConsultOpenParams().toString();
  611. var root = APP_IOS_URL_SCHEME.replace(/\/$/, "");
  612. return root + "/" + path + (qs ? "?" + qs : "");
  613. }
  614. function buildWeChatLaunchExtinfo(useBarePathOnly) {
  615. var path = APP_UNI_AI_PATH.replace(/^\//, "");
  616. if (useBarePathOnly) return path;
  617. var qs = buildAiConsultOpenParams().toString();
  618. var uniPage = qs ? path + "?" + qs : path;
  619. if (uniPage.length <= 1024) return uniPage;
  620. return path + "?pageType=home&fromShareAiConsult=1";
  621. }
  622. function isWeChatInAppBrowser() {
  623. return /MicroMessenger/i.test(navigator.userAgent || "");
  624. }
  625. function getWxShareEntryFrom() {
  626. var from = q("from");
  627. if (from) return String(from).trim().toLowerCase();
  628. try {
  629. var h = location.hash || "";
  630. var qi = h.indexOf("?");
  631. if (qi >= 0) {
  632. var hf = new URLSearchParams(h.slice(qi + 1)).get("from");
  633. if (hf) return String(hf).trim().toLowerCase();
  634. }
  635. } catch (eH) {}
  636. return "";
  637. }
  638. function logWxEntryDiagnostics() {
  639. if (!isWeChatInAppBrowser()) return;
  640. console.log("[wx-entry]", {
  641. entryFrom: getWxShareEntryFrom() || "(无)",
  642. wxJssdkReady: weChatJssdkConfigured,
  643. signUrl: getWxConfigSignUrl(),
  644. });
  645. }
  646. function isIOSWeChatBrowser() {
  647. var ua = navigator.userAgent || "";
  648. return /MicroMessenger/i.test(ua) && /iPhone|iPad|iPod/i.test(ua);
  649. }
  650. function isWxDebugOn() {
  651. return q("wxDebug") === "1";
  652. }
  653. function isWxForceDebug() {
  654. return q("wxForce") === "1";
  655. }
  656. function isWxPcAutoDebugHost() {
  657. var h = (location.hostname || "").toLowerCase();
  658. if (h === "localhost" || h === "127.0.0.1") return true;
  659. for (var i = 0; i < WECHAT_JS_SAFE_HOSTS.length; i++) {
  660. if (WECHAT_JS_SAFE_HOSTS[i] === h) return true;
  661. }
  662. return false;
  663. }
  664. function isWxPcBrowser() {
  665. return !isWeChatInAppBrowser();
  666. }
  667. function shouldInitWeChatJssdkOnLoad() {
  668. if (isWeChatInAppBrowser()) return true;
  669. return isWxForceDebug() || isWxPcAutoDebugHost();
  670. }
  671. function shouldFetchWxConfig(fromUserClick) {
  672. if (isWeChatInAppBrowser()) return true;
  673. if (fromUserClick) return true;
  674. return isWxForceDebug() || isWxPcAutoDebugHost();
  675. }
  676. function getWxHtmlUrl() {
  677. var forced = String(q("wxSignUrl") || "").trim();
  678. if (forced) return forced.split("#")[0];
  679. return String(location.href || "").split("#")[0];
  680. }
  681. function getWxSignPageUrlForApi() {
  682. var htmlUrl = getWxHtmlUrl();
  683. if (htmlUrl && /^https?:\/\//i.test(htmlUrl)) return htmlUrl;
  684. return WX_GET_CONFIG_SIGN_URL;
  685. }
  686. function getWxHtmlUrlBase() {
  687. var htmlUrl = getWxSignPageUrlForApi();
  688. try {
  689. var u = new URL(htmlUrl);
  690. return u.origin + u.pathname;
  691. } catch (eU) {
  692. return WX_GET_CONFIG_SIGN_URL;
  693. }
  694. }
  695. /** 默认 pathname 验签:AI 分享链常带超长 payload,与后端无参签名一致,避免开放标签 fail_check */
  696. function getWxConfigSignUrl() {
  697. if (String(q("wxSignFullUrl") || "") === "1") return getWxSignPageUrlForApi();
  698. if (String(q("wxSignBaseOnly") || "") === "1") return getWxHtmlUrlBase();
  699. if (isWeChatInAppBrowser()) return getWxHtmlUrlBase();
  700. return getWxSignPageUrlForApi();
  701. }
  702. function getWxGetConfigApiUrl() {
  703. return API_BASE.replace(/\/$/, "") + WECHAT_GET_WX_CONFIG_PATH;
  704. }
  705. function buildWxGetConfigRequestBody(htmlUrl) {
  706. return {
  707. url: String(htmlUrl || "").split("#")[0].trim(),
  708. };
  709. }
  710. function resolveWxConfigAppIdFromSignData(d) {
  711. if (!d || typeof d !== "object") return "";
  712. var mp =
  713. d.mpAppId ||
  714. d.mpAppid ||
  715. d.officialAppId ||
  716. d.gzhAppId ||
  717. d.serviceAppId;
  718. if (mp != null && String(mp).trim() !== "") return String(mp).trim();
  719. var fromQuery = String(q("wxMpAppId") || WECHAT_MP_APP_ID || "").trim();
  720. if (fromQuery) return fromQuery;
  721. var raw = d.appId || d.appid || d.wxAppId;
  722. return raw != null && String(raw).trim() !== "" ? String(raw).trim() : "";
  723. }
  724. function normalizeWxJssdkSignPayload(res, signUrlUsed) {
  725. if (!res || typeof res !== "object") return null;
  726. var d = res.data != null && typeof res.data === "object" ? res.data : res;
  727. if (!d || typeof d !== "object") return null;
  728. var appId = resolveWxConfigAppIdFromSignData(d);
  729. var timestamp = d.timestamp != null ? d.timestamp : d.timeStamp;
  730. var nonceStr =
  731. d.nonceStr != null && String(d.nonceStr) !== ""
  732. ? d.nonceStr
  733. : d.noncestr != null && String(d.noncestr) !== ""
  734. ? d.noncestr
  735. : d.nonce;
  736. var signature = d.signature || d.sign;
  737. if (!appId || timestamp == null || nonceStr == null || nonceStr === "" || !signature) {
  738. return null;
  739. }
  740. return {
  741. appId: String(appId),
  742. timestamp: Number(timestamp),
  743. nonceStr: String(nonceStr),
  744. signature: String(signature),
  745. signUrl: String(signUrlUsed || "")
  746. .split("#")[0]
  747. .trim(),
  748. };
  749. }
  750. function getWxErrMsg(err) {
  751. if (!err) return "";
  752. if (err.errMsg) return String(err.errMsg);
  753. if (typeof err === "string") return err;
  754. try {
  755. return JSON.stringify(err);
  756. } catch (e) {
  757. return String(err);
  758. }
  759. }
  760. function formatWxConfigErrorTip(err, signPageUrl) {
  761. var errMsg = getWxErrMsg(err);
  762. var tip = "微信 JSSDK 配置失败";
  763. if (/invalid signature/i.test(errMsg)) {
  764. return (
  765. tip +
  766. ":签名无效。请核对:①后端用与前端相同的 url 签名;②url=" +
  767. (signPageUrl || getWxHtmlUrl()) +
  768. ";③nonceStr/timestamp 与接口返回一致;④appId=" +
  769. WECHAT_MP_APP_ID
  770. );
  771. }
  772. if (/require\s*subscribe/i.test(errMsg)) {
  773. return tip + ":" + errMsg + "(服务号需用户已关注)";
  774. }
  775. return errMsg ? tip + ":" + errMsg : tip;
  776. }
  777. function setWxInitError(msg) {
  778. wxInitLastError = String(msg || "").trim();
  779. if (wxInitLastError) console.warn("[wx]", wxInitLastError);
  780. }
  781. function requestWeChatJssdkSignAndConfig(htmlUrlOptional) {
  782. var htmlUrl = String(htmlUrlOptional != null ? htmlUrlOptional : getWxConfigSignUrl())
  783. .split("#")[0]
  784. .trim();
  785. if (!htmlUrl || !/^https?:\/\//i.test(htmlUrl)) {
  786. htmlUrl = WX_GET_CONFIG_SIGN_URL;
  787. }
  788. if (isWxDebugOn()) {
  789. try {
  790. window.alert("htmlUrl(签名用,应与地址栏一致):\n" + htmlUrl);
  791. } catch (eDbg) {}
  792. }
  793. var requestUrl = getWxGetConfigApiUrl();
  794. var requestBody = buildWxGetConfigRequestBody(htmlUrl);
  795. return fetch(requestUrl, {
  796. method: "POST",
  797. mode: "cors",
  798. credentials: "omit",
  799. headers: {
  800. Accept: "application/json",
  801. "Content-Type": "application/json;charset=UTF-8",
  802. },
  803. body: JSON.stringify(requestBody),
  804. })
  805. .then(function (r) {
  806. if (r.ok) return r.json();
  807. return r
  808. .text()
  809. .catch(function () {
  810. return "";
  811. })
  812. .then(function (text) {
  813. var hint = "";
  814. try {
  815. var j = JSON.parse(text);
  816. hint = j.msg || j.message || "";
  817. } catch (eP) {
  818. if (text) hint = text.slice(0, 120);
  819. }
  820. throw new Error("getWxConfig HTTP " + r.status + (hint ? ":" + hint : ""));
  821. });
  822. })
  823. .then(function (res) {
  824. if (res && res.code != null) {
  825. var c = Number(res.code);
  826. if (c !== 200 && c !== 0 && res.success !== true) {
  827. throw new Error(res.msg || res.message || "getWxConfig code " + c);
  828. }
  829. }
  830. var sign = normalizeWxJssdkSignPayload(res, htmlUrl);
  831. if (!sign) {
  832. throw new Error("getWxConfig 缺少 appId/timestamp/nonceStr/signature(见控制台)");
  833. }
  834. if (typeof wx === "undefined") {
  835. if (isWxPcBrowser()) {
  836. console.log("[wx] PC getWxConfig 成功(无 jweixin)", sign);
  837. return true;
  838. }
  839. setWxInitError("jweixin.js 未加载");
  840. return false;
  841. }
  842. return applyWxConfigFromSign(sign, htmlUrl);
  843. });
  844. }
  845. function applyWxConfigFromSign(sign, htmlUrl) {
  846. var wxConfigParams = {
  847. debug: isWxDebugOn(),
  848. appId: String(sign.appId),
  849. timestamp: sign.timestamp,
  850. nonceStr: String(sign.nonceStr),
  851. signature: String(sign.signature),
  852. jsApiList: [],
  853. openTagList: ["wx-open-launch-app"],
  854. };
  855. return new Promise(function (resolve) {
  856. wx.config(wxConfigParams);
  857. wx.ready(function () {
  858. weChatJssdkConfigured = true;
  859. document.body.classList.add("wx-jssdk-ready");
  860. refreshWxLaunchTagAttrs();
  861. console.log("[wx.config] ready, htmlUrl=", htmlUrl);
  862. resolve(true);
  863. });
  864. wx.error(function (err) {
  865. weChatJssdkConfigured = false;
  866. document.body.classList.remove("wx-jssdk-ready");
  867. wxJssdkInitPromise = null;
  868. var errMsg = getWxErrMsg(err);
  869. setWxInitError(formatWxConfigErrorTip(err, htmlUrl));
  870. if (isWxDebugOn()) window.alert(wxInitLastError);
  871. console.warn("[wx.config]", errMsg, "htmlUrl=", htmlUrl);
  872. var baseUrl = getWxHtmlUrlBase();
  873. var fullUrl = getWxSignPageUrlForApi();
  874. if (
  875. !/invalid signature/i.test(errMsg) ||
  876. wxConfigSignRetriedBaseUrl ||
  877. baseUrl === fullUrl ||
  878. htmlUrl === baseUrl
  879. ) {
  880. resolve(false);
  881. return;
  882. }
  883. wxConfigSignRetriedBaseUrl = true;
  884. requestWeChatJssdkSignAndConfig(baseUrl).then(resolve);
  885. });
  886. });
  887. }
  888. function refreshWxLaunchTagAttrs(useBarePathOnly) {
  889. var tag = document.getElementById("launch-btn");
  890. if (!tag) return;
  891. try {
  892. tag.setAttribute("appid", WECHAT_OPEN_APP_ID);
  893. tag.setAttribute("extinfo", buildWeChatLaunchExtinfo(!!useBarePathOnly));
  894. } catch (eA) {}
  895. }
  896. function bindWeChatLaunchTagEvents() {
  897. var tag = document.getElementById("launch-btn");
  898. if (!tag || tag._wxLaunchBound) return;
  899. tag._wxLaunchBound = true;
  900. refreshWxLaunchTagAttrs();
  901. tag.addEventListener("launch", function () {
  902. console.log("[wx-open-launch-app] launch ok");
  903. });
  904. tag.addEventListener("error", function (e) {
  905. var detail = e && e.detail;
  906. var errMsg =
  907. detail && detail.errMsg
  908. ? String(detail.errMsg)
  909. : detail && detail.errmsg
  910. ? String(detail.errmsg)
  911. : "";
  912. console.warn(
  913. "[wx-open-launch-app]",
  914. detail,
  915. "extinfo=",
  916. tag.getAttribute("extinfo")
  917. );
  918. if (/launch:fail_check/i.test(errMsg)) {
  919. showAppOpenFailTip(
  920. "请用微信分享卡片进入,或确认已安装最新版「U店在哪」"
  921. );
  922. return;
  923. }
  924. if (
  925. /launch:fail/i.test(errMsg) &&
  926. !tag._wxLaunchBareRetried &&
  927. tag.getAttribute("extinfo") !== buildWeChatLaunchExtinfo(true)
  928. ) {
  929. tag._wxLaunchBareRetried = true;
  930. refreshWxLaunchTagAttrs(true);
  931. showFabToast("请再点一次「和U宝继续聊」");
  932. return;
  933. }
  934. if (/launch:fail/i.test(errMsg) && !getWxShareEntryFrom()) {
  935. showAppOpenFailTip(
  936. "当前环境无法直接打开 App,请使用 App「分享到微信」后点分享卡片进入"
  937. );
  938. return;
  939. }
  940. showAppOpenFailTip(
  941. errMsg
  942. ? "未能打开 App:" + errMsg
  943. : "未能打开 App,请确认已安装最新版「U店在哪」"
  944. );
  945. });
  946. }
  947. function initWeChatOpenLaunchApp(fromUserClick) {
  948. if (!shouldFetchWxConfig(!!fromUserClick)) {
  949. return Promise.resolve(false);
  950. }
  951. if (wxJssdkInitPromise && !fromUserClick) return wxJssdkInitPromise;
  952. wxConfigSignRetriedBaseUrl = false;
  953. wxInitLastError = "";
  954. var htmlUrl = getWxConfigSignUrl();
  955. bindWeChatLaunchTagEvents();
  956. wxJssdkInitPromise = requestWeChatJssdkSignAndConfig(htmlUrl)
  957. .then(function (ok) {
  958. if (ok === true) return true;
  959. if (!wxInitLastError) {
  960. setWxInitError("wx.config 失败,可加 ?wxDebug=1 查看 htmlUrl");
  961. }
  962. return false;
  963. })
  964. .catch(function (e) {
  965. var msg = e && e.message ? e.message : "getWxConfig 请求失败";
  966. setWxInitError(msg);
  967. console.warn("[wx] requestWeChatJssdkSignAndConfig failed", msg, "htmlUrl=", htmlUrl);
  968. if (isWeChatInAppBrowser() && isWxDebugOn()) window.alert(msg);
  969. return false;
  970. })
  971. .finally(function () {
  972. if (!weChatJssdkConfigured) wxJssdkInitPromise = null;
  973. });
  974. return wxJssdkInitPromise;
  975. }
  976. function scheduleWeChatJssdkBootstrap() {
  977. if (!shouldInitWeChatJssdkOnLoad()) return;
  978. var attempts = 0;
  979. function tick() {
  980. attempts += 1;
  981. if (weChatJssdkConfigured) return;
  982. initWeChatOpenLaunchApp();
  983. if (!weChatJssdkConfigured && attempts < 8 && typeof wx === "undefined") {
  984. setTimeout(tick, 400);
  985. }
  986. }
  987. if (isIOSWeChatBrowser()) {
  988. setTimeout(tick, 350);
  989. } else {
  990. tick();
  991. }
  992. }
  993. function showFabToast(msg, ms) {
  994. var tip = String(msg || "").trim();
  995. if (!tip) return;
  996. var el = document.getElementById("openAppToast");
  997. if (el) {
  998. el.textContent = tip;
  999. el.style.display = "block";
  1000. if (showFabToast._t) clearTimeout(showFabToast._t);
  1001. showFabToast._t = setTimeout(function () {
  1002. el.style.display = "none";
  1003. }, ms || 2800);
  1004. }
  1005. console.log("[openApp]", tip);
  1006. }
  1007. function showDownloadTip() {
  1008. var msg = "请到应用商店下载「U店在哪」";
  1009. if (typeof uni !== "undefined" && typeof uni.showToast === "function") {
  1010. uni.showToast({ title: msg, icon: "none", duration: 2500 });
  1011. } else {
  1012. window.alert(msg);
  1013. }
  1014. }
  1015. function showAppOpenFailTip(msg) {
  1016. var tip = msg || "未能打开 App,请确认已安装最新版「U店在哪」。";
  1017. if (typeof uni !== "undefined" && typeof uni.showToast === "function") {
  1018. uni.showToast({ title: msg, icon: "none", duration: 2800 });
  1019. } else if (isWeChatInAppBrowser()) {
  1020. window.alert(tip);
  1021. } else {
  1022. showFabToast(tip);
  1023. }
  1024. }
  1025. function launchAppDeepLink(deepLink) {
  1026. try {
  1027. var a = document.createElement("a");
  1028. a.href = deepLink;
  1029. a.setAttribute("target", "_self");
  1030. document.body.appendChild(a);
  1031. a.click();
  1032. document.body.removeChild(a);
  1033. } catch (e1) {}
  1034. try {
  1035. window.location.href = deepLink;
  1036. } catch (e2) {}
  1037. }
  1038. function tryOpenHBuilderAppViaScheme() {
  1039. var deepLink = buildAppDeepLink();
  1040. if (typeof plus !== "undefined" && plus.runtime) {
  1041. var installed = null;
  1042. try {
  1043. if (typeof plus.runtime.isApplicationExist === "function") {
  1044. installed = plus.runtime.isApplicationExist({
  1045. pname: APP_ANDROID_PACKAGE,
  1046. action: APP_IOS_URL_SCHEME,
  1047. });
  1048. }
  1049. } catch (e) {
  1050. console.warn(e);
  1051. }
  1052. try {
  1053. plus.runtime.openURL(deepLink);
  1054. } catch (e2) {
  1055. console.warn(e2);
  1056. if (installed === false) {
  1057. showDownloadTip();
  1058. }
  1059. }
  1060. return;
  1061. }
  1062. var done = false;
  1063. function finish() {
  1064. if (done) return;
  1065. done = true;
  1066. document.removeEventListener("visibilitychange", onVis);
  1067. window.removeEventListener("pagehide", onHide);
  1068. }
  1069. function onVis() {
  1070. if (document.visibilityState === "hidden") finish();
  1071. }
  1072. function onHide() {
  1073. finish();
  1074. }
  1075. document.addEventListener("visibilitychange", onVis);
  1076. window.addEventListener("pagehide", onHide);
  1077. try {
  1078. launchAppDeepLink(deepLink);
  1079. } catch (e3) {
  1080. finish();
  1081. showDownloadTip();
  1082. return;
  1083. }
  1084. window.setTimeout(function () {
  1085. finish();
  1086. }, 3200);
  1087. }
  1088. function tryOpenApp() {
  1089. if (isWeChatInAppBrowser()) return;
  1090. tryOpenHBuilderAppViaScheme();
  1091. }
  1092. function boot() {
  1093. var launchTag = document.getElementById("launch-btn");
  1094. if (launchTag) launchTag.setAttribute("appid", WECHAT_OPEN_APP_ID);
  1095. bindWeChatLaunchTagEvents();
  1096. if (isWeChatInAppBrowser()) {
  1097. document.body.classList.add("is-wechat");
  1098. logWxEntryDiagnostics();
  1099. }
  1100. if (shouldInitWeChatJssdkOnLoad()) {
  1101. scheduleWeChatJssdkBootstrap();
  1102. }
  1103. document.addEventListener("WeixinOpenTagsError", function (e) {
  1104. console.warn("[WeixinOpenTagsError]", e && e.detail);
  1105. });
  1106. var btnContinue = document.getElementById("btnContinue");
  1107. if (btnContinue) {
  1108. if (isWeChatInAppBrowser()) {
  1109. btnContinue.addEventListener("click", function () {
  1110. if (!weChatJssdkConfigured) {
  1111. showFabToast(
  1112. wxInitLastError || "微信 SDK 初始化中,请稍候再点底部按钮"
  1113. );
  1114. initWeChatOpenLaunchApp(true);
  1115. }
  1116. });
  1117. } else {
  1118. btnContinue.addEventListener("click", tryOpenApp);
  1119. }
  1120. }
  1121. render(parsePayload());
  1122. }
  1123. if (document.readyState === "complete") {
  1124. boot();
  1125. } else {
  1126. window.addEventListener("load", boot);
  1127. }
  1128. })();
  1129. </script>
  1130. </body>
  1131. </html>