shareAiConsult.html 40 KB

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