shareAiConsult.html 37 KB

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