shareAiConsult.html 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108
  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::after {
  128. content: "本回答由AI生成,内容仅供参考,请仔细甄别";
  129. display: block;
  130. margin-top: 8px;
  131. font-size: 12px;
  132. line-height: 1.5;
  133. color: #999999;
  134. text-align: left;
  135. font-style: italic;
  136. }
  137. .ai-card__image {
  138. display: block;
  139. max-width: 100%;
  140. height: auto;
  141. margin: 8px 0;
  142. border-radius: 6px;
  143. cursor: pointer;
  144. }
  145. .user-bubble__image {
  146. display: block;
  147. max-width: 180px;
  148. border-radius: 4px;
  149. }
  150. .user-bubble--images {
  151. padding: 8px 10px;
  152. }
  153. .user-bubble__images {
  154. display: flex;
  155. flex-wrap: nowrap;
  156. gap: 8px;
  157. overflow-x: auto;
  158. overflow-y: hidden;
  159. -webkit-overflow-scrolling: touch;
  160. scrollbar-width: none;
  161. -ms-overflow-style: none;
  162. }
  163. .user-bubble__images::-webkit-scrollbar {
  164. display: none;
  165. }
  166. .user-bubble__images .user-bubble__image {
  167. flex: 0 0 auto;
  168. width: 72px;
  169. height: 72px;
  170. max-width: none;
  171. object-fit: cover;
  172. border-radius: 8px;
  173. cursor: pointer;
  174. }
  175. .image-preview {
  176. display: none;
  177. position: fixed;
  178. inset: 0;
  179. z-index: 10004;
  180. align-items: center;
  181. justify-content: center;
  182. padding: 16px;
  183. background: rgba(0, 0, 0, 0.92);
  184. }
  185. .image-preview.is-open {
  186. display: flex;
  187. }
  188. .image-preview__img {
  189. max-width: 100%;
  190. max-height: 100%;
  191. object-fit: contain;
  192. -webkit-user-drag: none;
  193. user-select: none;
  194. pointer-events: none;
  195. }
  196. .image-preview__close {
  197. position: absolute;
  198. top: calc(12px + env(safe-area-inset-top, 0px));
  199. right: 12px;
  200. z-index: 1;
  201. width: 36px;
  202. height: 36px;
  203. border: none;
  204. border-radius: 50%;
  205. background: rgba(255, 255, 255, 0.18);
  206. color: #fff;
  207. font-size: 24px;
  208. line-height: 1;
  209. cursor: pointer;
  210. }
  211. .fallback-card {
  212. margin: 14px 15px 0;
  213. padding: 14px 13px;
  214. background: #fff;
  215. border-radius: 8px;
  216. box-shadow: 0 1px 6px rgba(0, 0, 0, 0.04);
  217. }
  218. .fallback-card .ai-card__text {
  219. font-size: 14px;
  220. line-height: 1.65;
  221. white-space: pre-wrap;
  222. word-break: break-word;
  223. text-align: left;
  224. }
  225. .empty {
  226. padding: 48px 20px;
  227. text-align: center;
  228. color: var(--muted);
  229. font-size: 14px;
  230. }
  231. .fab-wrap {
  232. position: fixed;
  233. left: 0;
  234. right: 0;
  235. bottom: 0;
  236. z-index: 200;
  237. padding: 12px 24px calc(12px + var(--safe-bottom));
  238. background: linear-gradient(to top, rgba(255, 255, 255, 0.98) 70%, transparent);
  239. pointer-events: none;
  240. }
  241. .fab-dock__slot {
  242. width: 100%;
  243. max-width: 280px;
  244. height: 48px;
  245. margin: 0 auto;
  246. pointer-events: auto;
  247. }
  248. #openApp.fab {
  249. padding: 0 10px;
  250. -webkit-font-smoothing: antialiased;
  251. text-rendering: optimizeLegibility;
  252. }
  253. .fab__brand-clip {
  254. display: block;
  255. flex-shrink: 0;
  256. line-height: 0;
  257. }
  258. .fab__brand-img {
  259. display: block;
  260. height: 20px;
  261. width: auto;
  262. max-width: 90%;
  263. pointer-events: none;
  264. -webkit-user-drag: none;
  265. }
  266. .fab__label {
  267. font-size: 15px;
  268. line-height: 1;
  269. letter-spacing: 0.04em;
  270. white-space: nowrap;
  271. }
  272. #launch-btn {
  273. display: none;
  274. width: 100%;
  275. height: 48px;
  276. min-height: 48px;
  277. border-radius: 999px;
  278. overflow: hidden;
  279. opacity: 1;
  280. }
  281. body.is-wechat.wx-jssdk-ready #launch-btn {
  282. display: block;
  283. }
  284. body.is-wechat.wx-jssdk-ready #openApp {
  285. display: none !important;
  286. }
  287. .fab-wrap .fab {
  288. pointer-events: auto;
  289. }
  290. #openApp {
  291. touch-action: manipulation;
  292. }
  293. .fab {
  294. display: flex;
  295. align-items: center;
  296. justify-content: center;
  297. gap: 4px;
  298. width: 100%;
  299. max-width: 280px;
  300. margin: 0 auto;
  301. height: 48px;
  302. border: none;
  303. border-radius: 999px;
  304. background: linear-gradient(90deg, #ffb84c 0%, #ff6b3e 100%);
  305. color: #fff;
  306. font-size: 16px;
  307. font-weight: 600;
  308. box-shadow: 0 4px 16px rgba(255, 107, 62, 0.32);
  309. cursor: pointer;
  310. }
  311. .home-indicator {
  312. height: 5px;
  313. background: #000;
  314. border-radius: 3px;
  315. width: 134px;
  316. margin: 8px auto 4px;
  317. opacity: 0.2;
  318. }
  319. #openAppToast {
  320. display: none;
  321. position: fixed;
  322. left: 16px;
  323. right: 16px;
  324. bottom: calc(72px + var(--safe-bottom));
  325. z-index: 10001;
  326. padding: 10px 14px;
  327. font-size: 13px;
  328. line-height: 1.45;
  329. color: #fff;
  330. text-align: center;
  331. background: rgba(0, 0, 0, 0.78);
  332. border-radius: 8px;
  333. pointer-events: none;
  334. word-break: break-all;
  335. }
  336. .api-debug-btn {
  337. position: fixed;
  338. top: calc(12px + env(safe-area-inset-top, 0px));
  339. right: 12px;
  340. z-index: 10002;
  341. padding: 6px 10px;
  342. border: none;
  343. border-radius: 6px;
  344. background: rgba(0, 0, 0, 0.55);
  345. color: #fff;
  346. font-size: 12px;
  347. line-height: 1;
  348. cursor: pointer;
  349. }
  350. .api-debug-mask {
  351. display: none;
  352. position: fixed;
  353. inset: 0;
  354. z-index: 10003;
  355. background: rgba(0, 0, 0, 0.45);
  356. }
  357. .api-debug-mask.is-open {
  358. display: block;
  359. }
  360. .api-debug-modal {
  361. position: absolute;
  362. left: 12px;
  363. right: 12px;
  364. top: 50%;
  365. max-height: 80vh;
  366. transform: translateY(-50%);
  367. display: flex;
  368. flex-direction: column;
  369. background: #fff;
  370. border-radius: 12px;
  371. overflow: hidden;
  372. box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18);
  373. }
  374. .api-debug-modal__header {
  375. display: flex;
  376. align-items: center;
  377. justify-content: space-between;
  378. padding: 14px 16px;
  379. border-bottom: 1px solid #eee;
  380. flex-shrink: 0;
  381. }
  382. .api-debug-modal__title {
  383. font-size: 16px;
  384. font-weight: 600;
  385. }
  386. .api-debug-modal__close {
  387. padding: 4px 8px;
  388. border: none;
  389. background: transparent;
  390. color: #666;
  391. font-size: 14px;
  392. cursor: pointer;
  393. }
  394. .api-debug-modal__body {
  395. flex: 1;
  396. min-height: 0;
  397. overflow-y: auto;
  398. -webkit-overflow-scrolling: touch;
  399. padding: 12px 16px 16px;
  400. }
  401. .api-debug-section {
  402. margin-bottom: 14px;
  403. }
  404. .api-debug-section:last-child {
  405. margin-bottom: 0;
  406. }
  407. .api-debug-section__label {
  408. margin-bottom: 6px;
  409. font-size: 13px;
  410. font-weight: 600;
  411. color: #333;
  412. }
  413. .api-debug-section__pre {
  414. margin: 0;
  415. padding: 10px 12px;
  416. background: #f5f6f8;
  417. border-radius: 8px;
  418. font-size: 12px;
  419. line-height: 1.5;
  420. white-space: pre-wrap;
  421. word-break: break-all;
  422. color: #222;
  423. font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  424. }
  425. </style>
  426. </head>
  427. <body>
  428. <main id="main">
  429. <div class="empty">对话内容加载中…</div>
  430. </main>
  431. <div id="openAppToast" role="status" aria-live="polite"></div>
  432. <!-- <button type="button" class="api-debug-btn" id="apiDebugBtn" aria-label="查看接口信息">接口</button> -->
  433. <div class="api-debug-mask" id="apiDebugMask" aria-hidden="true">
  434. <div class="api-debug-modal" role="dialog" aria-labelledby="apiDebugTitle">
  435. <div class="api-debug-modal__header">
  436. <span class="api-debug-modal__title" id="apiDebugTitle">聊天记录接口</span>
  437. <button type="button" class="api-debug-modal__close" id="apiDebugClose">关闭</button>
  438. </div>
  439. <div class="api-debug-modal__body" id="apiDebugBody"></div>
  440. </div>
  441. </div>
  442. <div class="image-preview" id="imagePreview" aria-hidden="true">
  443. <button type="button" class="image-preview__close" id="imagePreviewClose" aria-label="关闭预览">×</button>
  444. <img class="image-preview__img" id="imagePreviewImg" src="" alt="图片预览" />
  445. </div>
  446. <div class="fab-wrap">
  447. <div class="fab-dock__slot">
  448. <!-- 与 secondShareGoods 相同顺序:openApp 在下,launch-btn 在上,避免挡住点击 -->
  449. <button type="button" class="fab" id="openApp" aria-label="和U宝继续聊">
  450. <span class="fab__brand-clip" aria-hidden="true">
  451. <img class="fab__brand-img" src="images/uCharts.png" alt="和U宝继续聊" decoding="async">
  452. </span>
  453. </button>
  454. <wx-open-launch-app id="launch-btn" appid="wxf5f1efe3a9f5012e" extinfo="">
  455. <script type="text/wxtag-template">
  456. <style>
  457. .wx-open-app-btn {
  458. display: flex;
  459. align-items: center;
  460. justify-content: center;
  461. width: 100%;
  462. height: 48px;
  463. margin: 0;
  464. padding: 0 10px;
  465. border: none;
  466. border-radius: 999px;
  467. background: linear-gradient(90deg, #ffb84c 0%, #ff6b3e 100%);
  468. box-shadow: 0 4px 16px rgba(255, 107, 62, 0.32);
  469. cursor: pointer;
  470. overflow: hidden;
  471. -webkit-tap-highlight-color: transparent;
  472. box-sizing: border-box;
  473. font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Helvetica Neue", sans-serif;
  474. -webkit-font-smoothing: antialiased;
  475. }
  476. .wx-open-app-btn .fab__brand-clip {
  477. display: block;
  478. flex-shrink: 0;
  479. line-height: 0;
  480. }
  481. .wx-open-app-btn .fab__brand-img {
  482. display: block;
  483. height: 20px;
  484. width: auto;
  485. max-width: 90%;
  486. pointer-events: none;
  487. -webkit-user-drag: none;
  488. }
  489. </style>
  490. <button class="wx-open-app-btn" aria-label="和U宝继续聊">
  491. <span class="fab__brand-clip" aria-hidden="true">
  492. <img class="fab__brand-img" src="https://prod.ailien.shop/h5/HBuilderProjects/images/uCharts.png" alt="和U宝继续聊" decoding="async">
  493. </span>
  494. </button>
  495. </script>
  496. </wx-open-launch-app>
  497. </div>
  498. <div class="home-indicator" aria-hidden="true"></div>
  499. </div>
  500. <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
  501. <script>
  502. (function () {
  503. "use strict";
  504. var API_BASE = "https://prod.ailien.shop/alienStore";
  505. var WECHAT_MP_APP_ID = "wx412792c77f47babd";
  506. var WECHAT_OPEN_APP_ID = "wxf5f1efe3a9f5012e";
  507. var WECHAT_GET_WX_CONFIG_PATH = "/wx/getWxConfig";
  508. var H5_PAGE_BASE_FALLBACK = "https://prod.ailien.shop/h5/HBuilderProjects/";
  509. var WX_GET_CONFIG_SIGN_URL = H5_PAGE_BASE_FALLBACK + "shareAiConsult.html";
  510. var WECHAT_JS_SAFE_HOSTS = ["uat.ailien.shop", "prod.ailien.shop"];
  511. var APP_ANDROID_PACKAGE = "com.alien.Udianzaizhe";
  512. var APP_DOWNLOAD_URL = "https://a.app.qq.com/o/simple.jsp?pkgname=com.alien.Udianzaina";
  513. var APP_IOS_DOWNLOAD_URL = "https://apps.apple.com/cn/app/u店在哪/id6752243565";
  514. var APP_IOS_URL_SCHEME = "shopro://";
  515. var APP_UNI_AI_PATH = "pages/aiSearchResult/index";
  516. var AI_LEGAL_CONVERSATION_API =
  517. "https://prod.ailien.shop/ai/life-manager/api/v1/legal-consultation/history/conversation";
  518. var AI_UBAO_CONVERSATION_API =
  519. "https://prod.ailien.shop/ai/life-manager/api/v1/ubao-conversation/history/conversation";
  520. var sharePayloadCache = null;
  521. var conversationApiDebugInfo = null;
  522. var weChatJssdkConfigured = false;
  523. var wxConfigSignRetriedBaseUrl = false;
  524. var wxInitLastError = "";
  525. var wxJssdkInitPromise = null;
  526. function escHtml(s) {
  527. return String(s || "")
  528. .replace(/&/g, "&amp;")
  529. .replace(/</g, "&lt;")
  530. .replace(/>/g, "&gt;")
  531. .replace(/"/g, "&quot;");
  532. }
  533. var IMAGE_EXT = "(?:jpe?g|png|gif|webp|bmp|avif)";
  534. var IMAGE_URL_PATTERN =
  535. "https?:\\/\\/[^\\s))\\]]+\\." + IMAGE_EXT + "(?:\\?[^\\s))\\]]*)?";
  536. function isInsideImageUrlSyntax(full, offset) {
  537. if (offset >= 2 && full.slice(offset - 2, offset) === "](") return true;
  538. if (offset >= 1 && full[offset - 1] === "(") return true;
  539. if (offset >= 6 && full.slice(offset - 6, offset) === "{{IMG|") return true;
  540. return false;
  541. }
  542. /** AI 回复常见:店名(https://...jpg) — 转为可渲染的图片占位 */
  543. function embedInlineImageUrls(text) {
  544. if (text == null) return "";
  545. var t = String(text);
  546. t = t.replace(
  547. new RegExp("([^\\n((]+?)\\((" + IMAGE_URL_PATTERN + ")\\)", "gi"),
  548. function (_, label, url) {
  549. var name = String(label).trim();
  550. if (!name) return "\n\n{{IMG|" + url + "|}}\n\n";
  551. var alt = name.length > 24 ? "图片" : name.replace(/[\[\]]/g, "");
  552. return name + "\n\n{{IMG|" + url + "|" + alt + "}}\n\n";
  553. }
  554. );
  555. t = t.replace(
  556. new RegExp("\\((" + IMAGE_URL_PATTERN + ")\\)", "gi"),
  557. function (_, url) {
  558. return "\n\n{{IMG|" + url + "|}}\n\n";
  559. }
  560. );
  561. t = t.replace(new RegExp(IMAGE_URL_PATTERN, "gi"), function (url, offset, full) {
  562. if (isInsideImageUrlSyntax(full, offset)) return url;
  563. return "\n\n{{IMG|" + url + "|}}\n\n";
  564. });
  565. return t.replace(/\n{3,}/g, "\n\n");
  566. }
  567. function formatTextBlockHtml(text) {
  568. return escHtml(text).replace(/\n/g, "<br>");
  569. }
  570. function buildAiImageTag(url, alt) {
  571. var safeUrl = escHtml(url);
  572. var safeAlt = escHtml(alt || "图片");
  573. return (
  574. '<img class="ai-card__image" src="' +
  575. safeUrl +
  576. '" alt="' +
  577. safeAlt +
  578. '" loading="lazy" decoding="async" />'
  579. );
  580. }
  581. function renderAiContentHtml(text) {
  582. var t = embedInlineImageUrls(text);
  583. var html = "";
  584. var tokenRe = /\{\{IMG\|([^|]+)\|([^}]*)\}\}/g;
  585. var lastIndex = 0;
  586. var match;
  587. while ((match = tokenRe.exec(t)) !== null) {
  588. if (match.index > lastIndex) {
  589. html += formatTextBlockHtml(t.slice(lastIndex, match.index));
  590. }
  591. html += buildAiImageTag(match[1], match[2]);
  592. lastIndex = tokenRe.lastIndex;
  593. }
  594. if (lastIndex < t.length) {
  595. html += formatTextBlockHtml(t.slice(lastIndex));
  596. }
  597. return html;
  598. }
  599. function q(name) {
  600. try {
  601. var v = new URLSearchParams(location.search || "").get(name);
  602. return v == null ? "" : String(v);
  603. } catch (e) {
  604. return "";
  605. }
  606. }
  607. function getQueryParam(name) {
  608. var m = new RegExp("[?&]" + name + "=([^&]*)").exec(window.location.search);
  609. if (!m) return "";
  610. try {
  611. return decodeURIComponent(m[1].replace(/\+/g, " "));
  612. } catch (e) {
  613. return "";
  614. }
  615. }
  616. function parsePayload() {
  617. var raw = getQueryParam("payload");
  618. if (!raw) return null;
  619. try {
  620. return JSON.parse(raw);
  621. } catch (e) {
  622. return null;
  623. }
  624. }
  625. function getConversationQueryParams() {
  626. var sessionId = String(q("session_id") || q("sessionId") || "").trim();
  627. var userId = String(q("user_id") || q("userId") || "").trim();
  628. var type = String(q("type") || "").trim();
  629. if (!sessionId || !userId) return null;
  630. return { sessionId: sessionId, userId: userId, type: type };
  631. }
  632. /** type=1 U宝;type=0 AI客服;其它(如 2)律师咨询 / 法律U宝 */
  633. function getSharePageType() {
  634. return String(q("type") || "").trim() || "1";
  635. }
  636. function getShareBrandByType(type) {
  637. var t = String(type || "").trim() || "1";
  638. if (t === "0") {
  639. return {
  640. ogTitle: "和AI客服的对话",
  641. ogDescription: "点击查看对话内容",
  642. appPath: "pages/aiKfChat/index",
  643. pageType: "",
  644. fabLabel: "和AI客服继续聊",
  645. };
  646. }
  647. if (t !== "1") {
  648. return {
  649. ogTitle: "和法律U宝的对话",
  650. ogDescription: "点击查看对话内容",
  651. appPath: "pages/aiSearchResult/index",
  652. pageType: "laywer",
  653. fabLabel: "和法律U宝继续聊",
  654. };
  655. }
  656. return {
  657. ogTitle: "和U宝的对话",
  658. ogDescription: "点击查看对话内容",
  659. appPath: "pages/aiSearchResult/index",
  660. pageType: "home",
  661. fabLabel: "和U宝继续聊",
  662. };
  663. }
  664. function updateFabLabels(label) {
  665. var text = String(label || "和U宝继续聊").trim() || "和U宝继续聊";
  666. var openApp = document.getElementById("openApp");
  667. if (openApp) {
  668. openApp.setAttribute("aria-label", text);
  669. var img = openApp.querySelector(".fab__brand-img");
  670. if (img) img.setAttribute("alt", text);
  671. }
  672. }
  673. function parseMessageIdsFromQuery() {
  674. var raw = String(q("message_ids") || q("messageIds") || "").trim();
  675. if (!raw) return null;
  676. return raw
  677. .split(",")
  678. .map(function (s) {
  679. return String(s || "").trim();
  680. })
  681. .filter(Boolean);
  682. }
  683. function getMessageImageUrls(msg) {
  684. if (!msg) return [];
  685. if (Array.isArray(msg.imageUrls) && msg.imageUrls.length) {
  686. return msg.imageUrls
  687. .map(function (u) {
  688. return String(u || "").trim();
  689. })
  690. .filter(Boolean);
  691. }
  692. if (msg.imageUrl) return [String(msg.imageUrl)];
  693. return [];
  694. }
  695. function buildUserBubbleImagesHtml(imageUrls) {
  696. if (!Array.isArray(imageUrls) || !imageUrls.length) return "";
  697. var imgs = "";
  698. for (var j = 0; j < imageUrls.length; j++) {
  699. imgs +=
  700. '<img class="user-bubble__image" src="' +
  701. escHtml(imageUrls[j]) +
  702. '" alt="图片" loading="lazy" decoding="async" />';
  703. }
  704. return '<div class="user-bubble__images">' + imgs + "</div>";
  705. }
  706. function normalizeHistoryRowImageUrls(item) {
  707. var raw = item.image_urls || item.imageUrls || item.images;
  708. if (Array.isArray(raw)) {
  709. return raw
  710. .map(function (u) {
  711. return String(u || "").trim();
  712. })
  713. .filter(Boolean);
  714. }
  715. if (typeof raw === "string" && raw.trim()) {
  716. return raw
  717. .split(",")
  718. .map(function (s) {
  719. return s.trim();
  720. })
  721. .filter(Boolean);
  722. }
  723. return [];
  724. }
  725. function filterConversationMessagesByIds(messages, idList) {
  726. if (!Array.isArray(idList) || !idList.length) return messages;
  727. var idSet = {};
  728. for (var i = 0; i < idList.length; i++) {
  729. idSet[String(idList[i])] = true;
  730. }
  731. return (Array.isArray(messages) ? messages : []).filter(function (msg) {
  732. if (!msg || msg.id == null || msg.id === "") return false;
  733. return !!idSet[String(msg.id)];
  734. });
  735. }
  736. function getConversationApiBase(type) {
  737. return String(type || "").trim() === "1"
  738. ? AI_UBAO_CONVERSATION_API
  739. : AI_LEGAL_CONVERSATION_API;
  740. }
  741. function buildConversationApiUrl(sessionId, userId, type) {
  742. var params = new URLSearchParams();
  743. params.set("session_id", sessionId);
  744. params.set("user_id", userId);
  745. return getConversationApiBase(type) + "?" + params.toString();
  746. }
  747. function normalizeConversationRole(raw) {
  748. var role = String(raw || "").trim().toLowerCase();
  749. if (
  750. role === "user" ||
  751. role === "human" ||
  752. role === "customer" ||
  753. role === "question" ||
  754. role === "1" ||
  755. role === "我"
  756. ) {
  757. return "user";
  758. }
  759. if (
  760. role === "ai" ||
  761. role === "assistant" ||
  762. role === "bot" ||
  763. role === "ubao" ||
  764. role === "answer" ||
  765. role === "2" ||
  766. role === "system"
  767. ) {
  768. return "ai";
  769. }
  770. return "";
  771. }
  772. function normalizeConversationApiPayload(res) {
  773. if (!res || typeof res !== "object") return null;
  774. if (res.code != null) {
  775. var code = Number(res.code);
  776. if (code !== 200 && code !== 0 && res.success !== true) {
  777. throw new Error(res.msg || res.message || "对话接口返回 code " + code);
  778. }
  779. }
  780. var root = res.data != null && typeof res.data === "object" ? res.data : res;
  781. var rawList =
  782. (Array.isArray(root) && root) ||
  783. (Array.isArray(root.messages) && root.messages) ||
  784. (Array.isArray(root.messageList) && root.messageList) ||
  785. (Array.isArray(root.conversation) && root.conversation) ||
  786. (Array.isArray(root.conversations) && root.conversations) ||
  787. (Array.isArray(root.records) && root.records) ||
  788. (Array.isArray(root.list) && root.list) ||
  789. null;
  790. if (!rawList) return null;
  791. var messages = [];
  792. for (var i = 0; i < rawList.length; i++) {
  793. var item = rawList[i];
  794. if (!item || typeof item !== "object") continue;
  795. var msgId = item.id != null ? item.id : item.message_id;
  796. var role = normalizeConversationRole(
  797. item.role || item.sender || item.messageRole || item.type || item.messageType || item.senderType
  798. );
  799. var content = String(
  800. item.content != null
  801. ? item.content
  802. : item.message != null
  803. ? item.message
  804. : item.text != null
  805. ? item.text
  806. : ""
  807. ).trim();
  808. if (!role && item.question) {
  809. role = "user";
  810. content = String(item.question).trim();
  811. } else if (!role && item.answer) {
  812. role = "ai";
  813. content = String(item.answer).trim();
  814. } else if (!role && item.userMessage) {
  815. role = "user";
  816. content = String(item.userMessage).trim();
  817. } else if (!role && (item.aiMessage || item.assistantMessage)) {
  818. role = "ai";
  819. content = String(item.aiMessage || item.assistantMessage).trim();
  820. }
  821. var imageUrls = normalizeHistoryRowImageUrls(item);
  822. if (imageUrls.length) {
  823. messages.push({
  824. id: msgId,
  825. role: role || "user",
  826. isImage: true,
  827. imageUrls: imageUrls,
  828. imageUrl: imageUrls[0],
  829. content: content,
  830. });
  831. continue;
  832. }
  833. if (!content) continue;
  834. var imageUrl = item.imageUrl || item.image || item.imgUrl || item.url;
  835. if (imageUrl && /^(https?:)?\/\//i.test(String(imageUrl))) {
  836. messages.push({
  837. id: msgId,
  838. role: role || "user",
  839. isImage: true,
  840. imageUrls: [String(imageUrl)],
  841. imageUrl: String(imageUrl),
  842. });
  843. continue;
  844. }
  845. messages.push({
  846. id: msgId,
  847. role: role || (messages.length % 2 === 0 ? "user" : "ai"),
  848. content: content,
  849. });
  850. }
  851. if (!messages.length) return null;
  852. return {
  853. messages: messages,
  854. shareTime:
  855. root.shareTime ||
  856. root.createdAt ||
  857. root.createTime ||
  858. root.updateTime ||
  859. root.timestamp ||
  860. res.timestamp,
  861. firstQuestion: root.firstQuestion || root.title || root.topic || "",
  862. sessionId: root.sessionId || root.session_id || "",
  863. userId: root.userId || root.user_id || "",
  864. };
  865. }
  866. function formatJsonForDebug(value) {
  867. if (value == null) return "(无)";
  868. try {
  869. return JSON.stringify(value, null, 2);
  870. } catch (e) {
  871. return String(value);
  872. }
  873. }
  874. function setConversationApiDebugInfo(info) {
  875. conversationApiDebugInfo = info;
  876. renderApiDebugModal();
  877. }
  878. function renderApiDebugModal() {
  879. var body = document.getElementById("apiDebugBody");
  880. if (!body) return;
  881. if (!conversationApiDebugInfo) {
  882. body.innerHTML =
  883. '<div class="api-debug-section"><div class="api-debug-section__label">说明</div>' +
  884. '<pre class="api-debug-section__pre">尚未请求聊天记录接口(缺少 session_id / user_id 参数时使用 payload 渲染)</pre></div>';
  885. return;
  886. }
  887. var info = conversationApiDebugInfo;
  888. var html =
  889. '<div class="api-debug-section"><div class="api-debug-section__label">接口地址</div>' +
  890. '<pre class="api-debug-section__pre">' +
  891. escHtml(info.apiBase || info.requestUrl || "(无)") +
  892. "</pre></div>" +
  893. '<div class="api-debug-section"><div class="api-debug-section__label">请求方式</div>' +
  894. '<pre class="api-debug-section__pre">' +
  895. escHtml(info.method || "GET") +
  896. "</pre></div>" +
  897. '<div class="api-debug-section"><div class="api-debug-section__label">完整请求 URL</div>' +
  898. '<pre class="api-debug-section__pre">' +
  899. escHtml(info.requestUrl || "(无)") +
  900. "</pre></div>" +
  901. '<div class="api-debug-section"><div class="api-debug-section__label">请求参数</div>' +
  902. '<pre class="api-debug-section__pre">' +
  903. escHtml(formatJsonForDebug(info.params)) +
  904. "</pre></div>";
  905. if (info.error) {
  906. html +=
  907. '<div class="api-debug-section"><div class="api-debug-section__label">请求错误</div>' +
  908. '<pre class="api-debug-section__pre">' +
  909. escHtml(info.error) +
  910. "</pre></div>";
  911. }
  912. html +=
  913. '<div class="api-debug-section"><div class="api-debug-section__label">接口返回数据</div>' +
  914. '<pre class="api-debug-section__pre">' +
  915. escHtml(formatJsonForDebug(info.response)) +
  916. "</pre></div>";
  917. body.innerHTML = html;
  918. }
  919. function showApiDebugModal() {
  920. renderApiDebugModal();
  921. var mask = document.getElementById("apiDebugMask");
  922. if (mask) {
  923. mask.classList.add("is-open");
  924. mask.setAttribute("aria-hidden", "false");
  925. }
  926. }
  927. function hideApiDebugModal() {
  928. var mask = document.getElementById("apiDebugMask");
  929. if (mask) {
  930. mask.classList.remove("is-open");
  931. mask.setAttribute("aria-hidden", "true");
  932. }
  933. }
  934. function bindApiDebugModalEvents() {
  935. var btn = document.getElementById("apiDebugBtn");
  936. var closeBtn = document.getElementById("apiDebugClose");
  937. var mask = document.getElementById("apiDebugMask");
  938. if (btn) btn.addEventListener("click", showApiDebugModal);
  939. if (closeBtn) closeBtn.addEventListener("click", hideApiDebugModal);
  940. if (mask) {
  941. mask.addEventListener("click", function (e) {
  942. if (e.target === mask) hideApiDebugModal();
  943. });
  944. }
  945. }
  946. function showImagePreview(url) {
  947. var urlStr = String(url || "").trim();
  948. if (!urlStr) return;
  949. var mask = document.getElementById("imagePreview");
  950. var img = document.getElementById("imagePreviewImg");
  951. if (!mask || !img) return;
  952. img.src = urlStr;
  953. mask.classList.add("is-open");
  954. mask.setAttribute("aria-hidden", "false");
  955. document.body.style.overflow = "hidden";
  956. }
  957. function hideImagePreview() {
  958. var mask = document.getElementById("imagePreview");
  959. var img = document.getElementById("imagePreviewImg");
  960. if (!mask) return;
  961. mask.classList.remove("is-open");
  962. mask.setAttribute("aria-hidden", "true");
  963. if (img) img.src = "";
  964. document.body.style.overflow = "";
  965. }
  966. function bindImagePreviewEvents() {
  967. var main = document.getElementById("main");
  968. var mask = document.getElementById("imagePreview");
  969. var closeBtn = document.getElementById("imagePreviewClose");
  970. if (main) {
  971. main.addEventListener("click", function (e) {
  972. var target = e.target;
  973. if (!target || target.tagName !== "IMG") return;
  974. if (
  975. target.classList.contains("user-bubble__image") ||
  976. target.classList.contains("ai-card__image")
  977. ) {
  978. showImagePreview(target.getAttribute("src") || target.src);
  979. }
  980. });
  981. }
  982. if (closeBtn) closeBtn.addEventListener("click", hideImagePreview);
  983. if (mask) {
  984. mask.addEventListener("click", function (e) {
  985. if (e.target === mask) hideImagePreview();
  986. });
  987. }
  988. }
  989. function fetchConversationHistory(sessionId, userId, type) {
  990. var requestUrl = buildConversationApiUrl(sessionId, userId, type);
  991. var apiBase = getConversationApiBase(type);
  992. var params = {
  993. session_id: sessionId,
  994. user_id: userId,
  995. type: type || "",
  996. };
  997. setConversationApiDebugInfo({
  998. apiBase: apiBase,
  999. requestUrl: requestUrl,
  1000. method: "GET",
  1001. params: params,
  1002. response: null,
  1003. error: null,
  1004. });
  1005. console.log("[conversation-api] GET", requestUrl);
  1006. return fetch(requestUrl, {
  1007. method: "GET",
  1008. mode: "cors",
  1009. credentials: "omit",
  1010. headers: { Accept: "application/json" },
  1011. })
  1012. .then(function (r) {
  1013. if (r.ok) return r.json();
  1014. return r
  1015. .text()
  1016. .catch(function () {
  1017. return "";
  1018. })
  1019. .then(function (text) {
  1020. var hint = "";
  1021. var parsed = null;
  1022. try {
  1023. parsed = JSON.parse(text);
  1024. hint = parsed.msg || parsed.message || "";
  1025. } catch (eP) {
  1026. if (text) hint = text.slice(0, 120);
  1027. }
  1028. setConversationApiDebugInfo({
  1029. apiBase: apiBase,
  1030. requestUrl: requestUrl,
  1031. method: "GET",
  1032. params: params,
  1033. response: parsed || text || null,
  1034. error: "HTTP " + r.status + (hint ? ":" + hint : ""),
  1035. });
  1036. throw new Error("对话接口 HTTP " + r.status + (hint ? ":" + hint : ""));
  1037. });
  1038. })
  1039. .then(function (res) {
  1040. setConversationApiDebugInfo({
  1041. apiBase: apiBase,
  1042. requestUrl: requestUrl,
  1043. method: "GET",
  1044. params: params,
  1045. response: res,
  1046. error: null,
  1047. });
  1048. var data = normalizeConversationApiPayload(res);
  1049. if (!data) {
  1050. throw new Error("对话接口返回数据为空或格式无法解析");
  1051. }
  1052. return data;
  1053. })
  1054. .catch(function (e) {
  1055. if (!conversationApiDebugInfo || !conversationApiDebugInfo.error) {
  1056. setConversationApiDebugInfo({
  1057. apiBase: apiBase,
  1058. requestUrl: requestUrl,
  1059. method: "GET",
  1060. params: params,
  1061. response: conversationApiDebugInfo && conversationApiDebugInfo.response,
  1062. error: e && e.message ? e.message : String(e),
  1063. });
  1064. }
  1065. throw e;
  1066. });
  1067. }
  1068. function loadPageData() {
  1069. var main = document.getElementById("main");
  1070. var params = getConversationQueryParams();
  1071. if (!params) {
  1072. setConversationApiDebugInfo(null);
  1073. render(parsePayload());
  1074. return;
  1075. }
  1076. var filterIds = parseMessageIdsFromQuery();
  1077. updateFabLabels(getShareBrandByType(getSharePageType()).fabLabel);
  1078. main.innerHTML = '<div class="empty">对话内容加载中…</div>';
  1079. fetchConversationHistory(params.sessionId, params.userId, params.type)
  1080. .then(function (data) {
  1081. if (filterIds && filterIds.length) {
  1082. var filtered = filterConversationMessagesByIds(data.messages, filterIds);
  1083. if (!filtered.length) {
  1084. throw new Error("未找到所选对话内容");
  1085. }
  1086. data = Object.assign({}, data, { messages: filtered });
  1087. }
  1088. render(data);
  1089. })
  1090. .catch(function (e) {
  1091. var tip = e && e.message ? e.message : "对话加载失败,请稍后重试";
  1092. console.warn("[conversation-api]", tip, e);
  1093. main.innerHTML = '<div class="empty">' + escHtml(tip) + "</div>";
  1094. updateFabLabels(getShareBrandByType(getSharePageType()).fabLabel);
  1095. refreshWxLaunchTagAttrs();
  1096. });
  1097. }
  1098. function getFirstUserQuestion(messages) {
  1099. var list = Array.isArray(messages) ? messages : [];
  1100. for (var i = 0; i < list.length; i++) {
  1101. var m = list[i];
  1102. if (!m || m.role !== "user") continue;
  1103. if (m.isImage) return "[图片]";
  1104. var t = String(m.content || "").trim();
  1105. if (t) return t;
  1106. }
  1107. return "";
  1108. }
  1109. function formatDiscussionTitle(question) {
  1110. var qText = String(question || "").trim();
  1111. if (!qText) return "与AI助手对话";
  1112. if (qText.slice(-3) === "的讨论") return qText;
  1113. return qText + "的讨论";
  1114. }
  1115. function normalizeShareTimestamp(ts) {
  1116. if (ts == null || ts === "") return null;
  1117. var n = Number(ts);
  1118. if (!isFinite(n) || n <= 0) return null;
  1119. if (n < 1e12) n = n * 1000;
  1120. var d = new Date(n);
  1121. if (isNaN(d.getTime()) || d.getFullYear() < 2020) return null;
  1122. return n;
  1123. }
  1124. function resolveDisplayShareTime(data) {
  1125. var fromUrl = q("shareTime") || q("share_time");
  1126. var urlTs = normalizeShareTimestamp(fromUrl);
  1127. if (urlTs != null) return urlTs;
  1128. var dataTs = normalizeShareTimestamp(data && data.shareTime);
  1129. if (dataTs != null) return dataTs;
  1130. return Date.now();
  1131. }
  1132. function formatDate(ts) {
  1133. var normalized = normalizeShareTimestamp(ts);
  1134. var d = normalized != null ? new Date(normalized) : new Date();
  1135. return d.getFullYear() + "年" + (d.getMonth() + 1) + "月" + d.getDate() + "日";
  1136. }
  1137. function renderMessages(messages) {
  1138. var html = "";
  1139. var list = Array.isArray(messages) ? messages : [];
  1140. for (var i = 0; i < list.length; i++) {
  1141. var msg = list[i];
  1142. if (!msg) continue;
  1143. if (msg.role === "user") {
  1144. var imageUrls = getMessageImageUrls(msg);
  1145. if (msg.isImage && imageUrls.length) {
  1146. html +=
  1147. '<div class="row row--user"><div class="user-bubble user-bubble--images">' +
  1148. buildUserBubbleImagesHtml(imageUrls) +
  1149. "</div></div>";
  1150. var imageUserText = String(msg.content || "").trim();
  1151. if (imageUserText) {
  1152. html +=
  1153. '<div class="row row--user"><div class="user-bubble">' +
  1154. escHtml(imageUserText) +
  1155. "</div></div>";
  1156. }
  1157. continue;
  1158. }
  1159. var userText = String(msg.content || "").trim();
  1160. if (!userText) continue;
  1161. html +=
  1162. '<div class="row row--user"><div class="user-bubble">' +
  1163. escHtml(userText) +
  1164. "</div></div>";
  1165. continue;
  1166. }
  1167. if (msg.role === "ai" || msg.role === "assistant") {
  1168. var aiText = String(msg.content || "").trim();
  1169. if (!aiText) continue;
  1170. html +=
  1171. '<div class="row row--ai"><div class="ai-card"><div class="ai-card__text">' +
  1172. renderAiContentHtml(aiText) +
  1173. "</div></div></div>";
  1174. }
  1175. }
  1176. return html;
  1177. }
  1178. function applyWeixinShareMeta() {
  1179. var brand = getShareBrandByType(getSharePageType());
  1180. var siteName = "U店在哪";
  1181. var iconUrl =
  1182. "https://alien-volume.oss-cn-beijing.aliyuncs.com/icon/ubao.png";
  1183. var setMeta = function (key, content) {
  1184. var el =
  1185. document.querySelector('meta[property="' + key + '"]') ||
  1186. document.querySelector('meta[name="' + key + '"]');
  1187. if (!el) {
  1188. el = document.createElement("meta");
  1189. if (key.indexOf("og:") === 0) {
  1190. el.setAttribute("property", key);
  1191. } else {
  1192. el.setAttribute("name", key);
  1193. }
  1194. document.head.appendChild(el);
  1195. }
  1196. el.setAttribute("content", content);
  1197. };
  1198. setMeta("og:site_name", siteName);
  1199. setMeta("application-name", siteName);
  1200. setMeta("og:title", brand.ogTitle);
  1201. setMeta("og:description", brand.ogDescription);
  1202. var links = document.querySelectorAll('link[rel="icon"], link[rel="apple-touch-icon"]');
  1203. for (var i = 0; i < links.length; i++) {
  1204. links[i].setAttribute("href", iconUrl);
  1205. }
  1206. }
  1207. function countDisplayableMessages(messages) {
  1208. var list = Array.isArray(messages) ? messages : [];
  1209. var count = 0;
  1210. for (var i = 0; i < list.length; i++) {
  1211. var msg = list[i];
  1212. if (!msg) continue;
  1213. if (msg.role === "user") {
  1214. if (msg.isImage && getMessageImageUrls(msg).length) {
  1215. count += 1;
  1216. continue;
  1217. }
  1218. if (String(msg.content || "").trim()) count += 1;
  1219. continue;
  1220. }
  1221. if (msg.role === "ai" || msg.role === "assistant") {
  1222. if (String(msg.content || "").trim()) count += 1;
  1223. }
  1224. }
  1225. return count;
  1226. }
  1227. function parseMessagesFromContent(text) {
  1228. var raw = String(text || "").trim();
  1229. if (!raw) return [];
  1230. var list = [];
  1231. var blocks = raw.split(/\n\n+/);
  1232. for (var i = 0; i < blocks.length; i++) {
  1233. var block = String(blocks[i] || "").trim();
  1234. if (!block) continue;
  1235. var userMatch = block.match(/^我[::]\s*([\s\S]*)$/);
  1236. if (userMatch) {
  1237. var userText = String(userMatch[1] || "").trim();
  1238. if (userText) list.push({ role: "user", content: userText });
  1239. continue;
  1240. }
  1241. var aiMatch = block.match(/^UBAO[::]\s*([\s\S]*)$/i);
  1242. if (aiMatch) {
  1243. var aiText = String(aiMatch[1] || "").trim();
  1244. if (aiText) list.push({ role: "ai", content: aiText });
  1245. }
  1246. }
  1247. if (list.length) return list;
  1248. var current = null;
  1249. var lines = raw.split(/\n/);
  1250. for (var j = 0; j < lines.length; j++) {
  1251. var line = String(lines[j] || "");
  1252. var userLine = line.match(/^我[::]\s*(.*)$/);
  1253. var aiLine = line.match(/^UBAO[::]\s*(.*)$/i);
  1254. if (userLine) {
  1255. if (current) list.push(current);
  1256. current = { role: "user", content: String(userLine[1] || "").trim() };
  1257. continue;
  1258. }
  1259. if (aiLine) {
  1260. if (current) list.push(current);
  1261. current = { role: "ai", content: String(aiLine[1] || "").trim() };
  1262. continue;
  1263. }
  1264. if (current && line.trim()) {
  1265. current.content = (current.content ? current.content + "\n" : "") + line.trim();
  1266. }
  1267. }
  1268. if (current && String(current.content || "").trim()) list.push(current);
  1269. return list;
  1270. }
  1271. function resolveShareMessagesForDisplay(data) {
  1272. var fromArray = Array.isArray(data && data.messages) ? data.messages : [];
  1273. var fromContent = parseMessagesFromContent((data && data.content) || "");
  1274. var arrayCount = countDisplayableMessages(fromArray);
  1275. var contentCount = countDisplayableMessages(fromContent);
  1276. // 分享链接超长时 messages 会被截断,但 content 仍保留完整对话文本
  1277. if (contentCount > arrayCount) return fromContent;
  1278. if (arrayCount) return fromArray;
  1279. return fromContent;
  1280. }
  1281. function render(data) {
  1282. var brand = getShareBrandByType(getSharePageType());
  1283. applyWeixinShareMeta();
  1284. updateFabLabels(brand.fabLabel);
  1285. sharePayloadCache = data || null;
  1286. var main = document.getElementById("main");
  1287. if (!data) {
  1288. main.innerHTML = '<div class="empty">对话内容不存在或链接已失效</div>';
  1289. refreshWxLaunchTagAttrs();
  1290. return;
  1291. }
  1292. var messages = resolveShareMessagesForDisplay(data);
  1293. var firstQuestion =
  1294. String(data.firstQuestion || "").trim() ||
  1295. getFirstUserQuestion(messages) ||
  1296. (function () {
  1297. var text = String(data.content || "").trim();
  1298. var m = text.match(/^我[::]([\s\S]+?)(?:\n\n|$)/);
  1299. return m ? String(m[1] || "").trim() : "";
  1300. })();
  1301. var pageTitle = formatDiscussionTitle(firstQuestion);
  1302. var dateText = formatDate(resolveDisplayShareTime(data));
  1303. document.title = pageTitle;
  1304. var bodyHtml =
  1305. '<section class="hero">' +
  1306. '<h1 class="hero__title">' +
  1307. escHtml(pageTitle) +
  1308. "</h1>" +
  1309. '<p class="hero__meta">' +
  1310. escHtml(dateText) +
  1311. " · 内容由AI生成,不能完全保障真实</p>" +
  1312. '<div class="hero__divider"></div>' +
  1313. "</section>";
  1314. var convHtml = renderMessages(messages);
  1315. if (convHtml) {
  1316. bodyHtml +=
  1317. '<section class="conversation">' +
  1318. convHtml +
  1319. '</section><div class="scroll-spacer" aria-hidden="true"></div>';
  1320. } else {
  1321. bodyHtml += '<div class="empty">暂无对话内容</div>';
  1322. }
  1323. main.innerHTML = bodyHtml;
  1324. refreshWxLaunchTagAttrs();
  1325. }
  1326. function buildAiConsultOpenParams() {
  1327. var brand = getShareBrandByType(getSharePageType());
  1328. var params = new URLSearchParams();
  1329. if (brand.pageType) params.set("pageType", brand.pageType);
  1330. params.set("fromShareAiConsult", "1");
  1331. var conv = getConversationQueryParams();
  1332. if (conv) {
  1333. if (conv.sessionId) params.set("session_id", conv.sessionId);
  1334. if (conv.userId) params.set("user_id", conv.userId);
  1335. if (conv.type) params.set("type", conv.type);
  1336. }
  1337. return params;
  1338. }
  1339. function buildWeChatLaunchExtinfoFallback(path, brand) {
  1340. var b = brand || getShareBrandByType(getSharePageType());
  1341. if (b.pageType) {
  1342. return path + "?pageType=" + encodeURIComponent(b.pageType) + "&fromShareAiConsult=1";
  1343. }
  1344. return path + "?fromShareAiConsult=1";
  1345. }
  1346. function buildAppDeepLink() {
  1347. var brand = getShareBrandByType(getSharePageType());
  1348. var path = brand.appPath || APP_UNI_AI_PATH;
  1349. var qs = buildAiConsultOpenParams().toString();
  1350. var root = APP_IOS_URL_SCHEME.replace(/\/$/, "");
  1351. return root + "/" + path + (qs ? "?" + qs : "");
  1352. }
  1353. function buildWeChatLaunchExtinfo(useBarePathOnly) {
  1354. var brand = getShareBrandByType(getSharePageType());
  1355. var path = (brand.appPath || APP_UNI_AI_PATH).replace(/^\//, "");
  1356. if (useBarePathOnly) return path;
  1357. var qs = buildAiConsultOpenParams().toString();
  1358. var uniPage = qs ? path + "?" + qs : path;
  1359. if (uniPage.length <= 1024) return uniPage;
  1360. return buildWeChatLaunchExtinfoFallback(path, brand);
  1361. }
  1362. function isWeChatInAppBrowser() {
  1363. return /MicroMessenger/i.test(navigator.userAgent || "");
  1364. }
  1365. function getWxShareEntryFrom() {
  1366. var from = q("from");
  1367. if (from) return String(from).trim().toLowerCase();
  1368. try {
  1369. var h = location.hash || "";
  1370. var qi = h.indexOf("?");
  1371. if (qi >= 0) {
  1372. var hf = new URLSearchParams(h.slice(qi + 1)).get("from");
  1373. if (hf) return String(hf).trim().toLowerCase();
  1374. }
  1375. } catch (eH) {}
  1376. return "";
  1377. }
  1378. function logWxEntryDiagnostics() {
  1379. if (!isWeChatInAppBrowser()) return;
  1380. console.log("[wx-entry]", {
  1381. entryFrom: getWxShareEntryFrom() || "(无)",
  1382. wxJssdkReady: weChatJssdkConfigured,
  1383. signUrl: getWxConfigSignUrl(),
  1384. });
  1385. }
  1386. function isIOSWeChatBrowser() {
  1387. var ua = navigator.userAgent || "";
  1388. return /MicroMessenger/i.test(ua) && /iPhone|iPad|iPod/i.test(ua);
  1389. }
  1390. function isWxDebugOn() {
  1391. return q("wxDebug") === "1";
  1392. }
  1393. function isWxForceDebug() {
  1394. return q("wxForce") === "1";
  1395. }
  1396. function isWxConfigOnClickDebug() {
  1397. return q("wxConfigOnClick") === "1";
  1398. }
  1399. function isWxPcAutoDebugHost() {
  1400. var h = (location.hostname || "").toLowerCase();
  1401. if (h === "localhost" || h === "127.0.0.1") return true;
  1402. for (var i = 0; i < WECHAT_JS_SAFE_HOSTS.length; i++) {
  1403. if (WECHAT_JS_SAFE_HOSTS[i] === h) return true;
  1404. }
  1405. return false;
  1406. }
  1407. function isWxPcBrowser() {
  1408. return !isWeChatInAppBrowser();
  1409. }
  1410. function shouldInitWeChatJssdkOnLoad() {
  1411. if (isWeChatInAppBrowser()) return true;
  1412. return isWxForceDebug() || isWxPcAutoDebugHost();
  1413. }
  1414. function shouldFetchWxConfig(fromUserClick) {
  1415. if (isWeChatInAppBrowser()) return true;
  1416. if (fromUserClick) return true;
  1417. return isWxForceDebug() || isWxPcAutoDebugHost();
  1418. }
  1419. function getWxHtmlUrl() {
  1420. var forced = String(q("wxSignUrl") || "").trim();
  1421. if (forced) return forced.split("#")[0];
  1422. return String(location.href || "").split("#")[0];
  1423. }
  1424. function getWxSignPageUrlForApi() {
  1425. var htmlUrl = getWxHtmlUrl();
  1426. if (htmlUrl && /^https?:\/\//i.test(htmlUrl)) return htmlUrl;
  1427. return WX_GET_CONFIG_SIGN_URL;
  1428. }
  1429. function getWxHtmlUrlBase() {
  1430. var htmlUrl = getWxSignPageUrlForApi();
  1431. try {
  1432. var u = new URL(htmlUrl);
  1433. return u.origin + u.pathname;
  1434. } catch (eU) {
  1435. return WX_GET_CONFIG_SIGN_URL;
  1436. }
  1437. }
  1438. /** 与 secondShareGoods 一致:默认用当前页完整 URL 验签(须与微信地址栏一致,含 query) */
  1439. function getWxConfigSignUrl() {
  1440. if (String(q("wxSignBaseOnly") || "") === "1") return getWxHtmlUrlBase();
  1441. return getWxSignPageUrlForApi();
  1442. }
  1443. function getWxGetConfigApiUrl() {
  1444. return API_BASE.replace(/\/$/, "") + WECHAT_GET_WX_CONFIG_PATH;
  1445. }
  1446. function buildWxGetConfigRequestBody(htmlUrl) {
  1447. return {
  1448. url: String(htmlUrl || "").split("#")[0].trim(),
  1449. };
  1450. }
  1451. function resolveWxConfigAppIdFromSignData(d) {
  1452. if (!d || typeof d !== "object") return "";
  1453. var mp =
  1454. d.mpAppId ||
  1455. d.mpAppid ||
  1456. d.officialAppId ||
  1457. d.gzhAppId ||
  1458. d.serviceAppId;
  1459. if (mp != null && String(mp).trim() !== "") return String(mp).trim();
  1460. var fromQuery = String(q("wxMpAppId") || WECHAT_MP_APP_ID || "").trim();
  1461. if (fromQuery) return fromQuery;
  1462. var raw = d.appId || d.appid || d.wxAppId;
  1463. return raw != null && String(raw).trim() !== "" ? String(raw).trim() : "";
  1464. }
  1465. function normalizeWxJssdkSignPayload(res, signUrlUsed) {
  1466. if (!res || typeof res !== "object") return null;
  1467. var d = res.data != null && typeof res.data === "object" ? res.data : res;
  1468. if (!d || typeof d !== "object") return null;
  1469. var appId = resolveWxConfigAppIdFromSignData(d);
  1470. var timestamp = d.timestamp != null ? d.timestamp : d.timeStamp;
  1471. var nonceStr =
  1472. d.nonceStr != null && String(d.nonceStr) !== ""
  1473. ? d.nonceStr
  1474. : d.noncestr != null && String(d.noncestr) !== ""
  1475. ? d.noncestr
  1476. : d.nonce;
  1477. var signature = d.signature || d.sign;
  1478. if (!appId || timestamp == null || nonceStr == null || nonceStr === "" || !signature) {
  1479. return null;
  1480. }
  1481. return {
  1482. appId: String(appId),
  1483. timestamp: Number(timestamp),
  1484. nonceStr: String(nonceStr),
  1485. signature: String(signature),
  1486. signUrl: String(signUrlUsed || "")
  1487. .split("#")[0]
  1488. .trim(),
  1489. };
  1490. }
  1491. function getWxErrMsg(err) {
  1492. if (!err) return "";
  1493. if (err.errMsg) return String(err.errMsg);
  1494. if (typeof err === "string") return err;
  1495. try {
  1496. return JSON.stringify(err);
  1497. } catch (e) {
  1498. return String(err);
  1499. }
  1500. }
  1501. function formatWxConfigErrorTip(err, signPageUrl) {
  1502. var errMsg = getWxErrMsg(err);
  1503. var tip = "微信 JSSDK 配置失败";
  1504. if (/invalid signature/i.test(errMsg)) {
  1505. return (
  1506. tip +
  1507. ":签名无效。请核对:①后端用与前端相同的 url 签名;②url=" +
  1508. (signPageUrl || getWxHtmlUrl()) +
  1509. ";③nonceStr/timestamp 与接口返回一致;④appId=" +
  1510. WECHAT_MP_APP_ID
  1511. );
  1512. }
  1513. if (/require\s*subscribe/i.test(errMsg)) {
  1514. return tip + ":" + errMsg + "(服务号需用户已关注)";
  1515. }
  1516. return errMsg ? tip + ":" + errMsg : tip;
  1517. }
  1518. function setWxInitError(msg) {
  1519. wxInitLastError = String(msg || "").trim();
  1520. if (wxInitLastError) console.warn("[wx]", wxInitLastError);
  1521. }
  1522. function requestWeChatJssdkSignAndConfig(htmlUrlOptional) {
  1523. var htmlUrl = String(htmlUrlOptional != null ? htmlUrlOptional : getWxConfigSignUrl())
  1524. .split("#")[0]
  1525. .trim();
  1526. if (!htmlUrl || !/^https?:\/\//i.test(htmlUrl)) {
  1527. htmlUrl = WX_GET_CONFIG_SIGN_URL;
  1528. }
  1529. if (isWxDebugOn()) {
  1530. try {
  1531. window.alert("htmlUrl(签名用,应与地址栏一致):\n" + htmlUrl);
  1532. } catch (eDbg) {}
  1533. }
  1534. var requestUrl = getWxGetConfigApiUrl();
  1535. var requestBody = buildWxGetConfigRequestBody(htmlUrl);
  1536. console.log("[wx] htmlUrl=", htmlUrl);
  1537. console.log("[wx] POST getWxConfig →", requestUrl, requestBody);
  1538. return fetch(requestUrl, {
  1539. method: "POST",
  1540. mode: "cors",
  1541. credentials: "omit",
  1542. headers: {
  1543. Accept: "application/json",
  1544. "Content-Type": "application/json;charset=UTF-8",
  1545. },
  1546. body: JSON.stringify(requestBody),
  1547. })
  1548. .then(function (r) {
  1549. if (r.ok) return r.json();
  1550. return r
  1551. .text()
  1552. .catch(function () {
  1553. return "";
  1554. })
  1555. .then(function (text) {
  1556. var hint = "";
  1557. try {
  1558. var j = JSON.parse(text);
  1559. hint = j.msg || j.message || "";
  1560. } catch (eP) {
  1561. if (text) hint = text.slice(0, 120);
  1562. }
  1563. throw new Error("getWxConfig HTTP " + r.status + (hint ? ":" + hint : ""));
  1564. });
  1565. })
  1566. .then(function (res) {
  1567. if (res && res.code != null) {
  1568. var c = Number(res.code);
  1569. if (c !== 200 && c !== 0 && res.success !== true) {
  1570. throw new Error(res.msg || res.message || "getWxConfig code " + c);
  1571. }
  1572. }
  1573. var sign = normalizeWxJssdkSignPayload(res, htmlUrl);
  1574. if (!sign) {
  1575. console.warn("[wx] getWxConfig 响应字段不全", res, "htmlUrl=", htmlUrl);
  1576. throw new Error("getWxConfig 缺少 appId/timestamp/nonceStr/signature(见控制台)");
  1577. }
  1578. if (sign.appId !== WECHAT_MP_APP_ID) {
  1579. console.warn(
  1580. "[wx] 后端 appId=" + sign.appId + ",期望服务号 " + WECHAT_MP_APP_ID
  1581. );
  1582. }
  1583. if (typeof wx === "undefined") {
  1584. if (isWxPcBrowser()) {
  1585. console.log("[wx] PC getWxConfig 成功(无 jweixin)", sign);
  1586. return true;
  1587. }
  1588. setWxInitError("jweixin.js 未加载");
  1589. return false;
  1590. }
  1591. return applyWxConfigFromSign(sign, htmlUrl);
  1592. });
  1593. }
  1594. function applyWxConfigFromSign(sign, htmlUrl) {
  1595. var wxConfigParams = {
  1596. debug: isWxDebugOn(),
  1597. appId: String(sign.appId),
  1598. timestamp: sign.timestamp,
  1599. nonceStr: String(sign.nonceStr),
  1600. signature: String(sign.signature),
  1601. jsApiList: [],
  1602. openTagList: ["wx-open-launch-app"],
  1603. };
  1604. return new Promise(function (resolve) {
  1605. wx.config(wxConfigParams);
  1606. wx.ready(function () {
  1607. weChatJssdkConfigured = true;
  1608. document.body.classList.add("wx-jssdk-ready");
  1609. refreshWxLaunchTagAttrs();
  1610. console.log("[wx.config] ready, htmlUrl=", htmlUrl);
  1611. resolve(true);
  1612. });
  1613. wx.error(function (err) {
  1614. weChatJssdkConfigured = false;
  1615. document.body.classList.remove("wx-jssdk-ready");
  1616. wxJssdkInitPromise = null;
  1617. var errMsg = getWxErrMsg(err);
  1618. setWxInitError(formatWxConfigErrorTip(err, htmlUrl));
  1619. if (isWxDebugOn()) window.alert(wxInitLastError);
  1620. console.warn("[wx.config]", errMsg, "htmlUrl=", htmlUrl);
  1621. var baseUrl = getWxHtmlUrlBase();
  1622. var fullUrl = getWxSignPageUrlForApi();
  1623. if (
  1624. !/invalid signature/i.test(errMsg) ||
  1625. wxConfigSignRetriedBaseUrl ||
  1626. baseUrl === fullUrl ||
  1627. htmlUrl === baseUrl
  1628. ) {
  1629. resolve(false);
  1630. return;
  1631. }
  1632. wxConfigSignRetriedBaseUrl = true;
  1633. requestWeChatJssdkSignAndConfig(baseUrl).then(resolve);
  1634. });
  1635. });
  1636. }
  1637. function refreshWxLaunchTagAttrs(useBarePathOnly) {
  1638. var tag = document.getElementById("launch-btn");
  1639. if (!tag) return;
  1640. try {
  1641. tag.setAttribute("appid", WECHAT_OPEN_APP_ID);
  1642. tag.setAttribute("extinfo", buildWeChatLaunchExtinfo(!!useBarePathOnly));
  1643. console.log("[wx-open-launch-app] extinfo=", tag.getAttribute("extinfo"));
  1644. } catch (eA) {}
  1645. }
  1646. function bindWeChatLaunchTagEvents() {
  1647. var tag = document.getElementById("launch-btn");
  1648. if (!tag || tag._wxLaunchBound) return;
  1649. tag._wxLaunchBound = true;
  1650. refreshWxLaunchTagAttrs();
  1651. tag.addEventListener("launch", function () {
  1652. console.log("[wx-open-launch-app] launch ok");
  1653. });
  1654. tag.addEventListener("error", function (e) {
  1655. var detail = e && e.detail;
  1656. var errMsg =
  1657. detail && detail.errMsg
  1658. ? String(detail.errMsg)
  1659. : detail && detail.errmsg
  1660. ? String(detail.errmsg)
  1661. : "";
  1662. console.warn(
  1663. "[wx-open-launch-app]",
  1664. detail,
  1665. "extinfo=",
  1666. tag.getAttribute("extinfo")
  1667. );
  1668. if (/launch:fail_check/i.test(errMsg)) {
  1669. showAppOpenFailTip(
  1670. "请用微信分享卡片进入,或确认已安装最新版「U店在哪」"
  1671. );
  1672. return;
  1673. }
  1674. // if (
  1675. // /launch:fail/i.test(errMsg) &&
  1676. // !tag._wxLaunchBareRetried &&
  1677. // tag.getAttribute("extinfo") !== buildWeChatLaunchExtinfo(true)
  1678. // ) {
  1679. // tag._wxLaunchBareRetried = true;
  1680. // refreshWxLaunchTagAttrs(true);
  1681. // showFabToast("请再点一次「和U宝继续聊」");
  1682. // return;
  1683. // }
  1684. if (/launch:fail/i.test(errMsg) && !getWxShareEntryFrom()) {
  1685. showAppOpenFailTip("请前往应用商店下载U店在哪app");
  1686. return;
  1687. }
  1688. showAppOpenFailTip(
  1689. errMsg
  1690. ? "未能打开 App:" + errMsg
  1691. : "未能打开 App,请确认已安装最新版「U店在哪」"
  1692. );
  1693. });
  1694. }
  1695. function initWeChatOpenLaunchApp(fromUserClick) {
  1696. if (!shouldFetchWxConfig(!!fromUserClick)) {
  1697. return Promise.resolve(false);
  1698. }
  1699. if (wxJssdkInitPromise && !fromUserClick) return wxJssdkInitPromise;
  1700. wxConfigSignRetriedBaseUrl = false;
  1701. wxInitLastError = "";
  1702. var htmlUrl = getWxConfigSignUrl();
  1703. bindWeChatLaunchTagEvents();
  1704. wxJssdkInitPromise = requestWeChatJssdkSignAndConfig(htmlUrl)
  1705. .then(function (ok) {
  1706. if (ok === true) return true;
  1707. if (!wxInitLastError) {
  1708. setWxInitError("wx.config 失败,可加 ?wxDebug=1 查看 htmlUrl");
  1709. }
  1710. return false;
  1711. })
  1712. .catch(function (e) {
  1713. var msg = e && e.message ? e.message : "getWxConfig 请求失败";
  1714. setWxInitError(msg);
  1715. console.warn("[wx] requestWeChatJssdkSignAndConfig failed", msg, "htmlUrl=", htmlUrl);
  1716. if (isWeChatInAppBrowser() && isWxDebugOn()) window.alert(msg);
  1717. return false;
  1718. })
  1719. .finally(function () {
  1720. if (!weChatJssdkConfigured) wxJssdkInitPromise = null;
  1721. });
  1722. return wxJssdkInitPromise;
  1723. }
  1724. function scheduleWeChatJssdkBootstrap() {
  1725. if (!shouldInitWeChatJssdkOnLoad()) return;
  1726. var attempts = 0;
  1727. function tick() {
  1728. attempts += 1;
  1729. if (weChatJssdkConfigured) return;
  1730. initWeChatOpenLaunchApp();
  1731. if (!weChatJssdkConfigured && attempts < 8 && typeof wx === "undefined") {
  1732. setTimeout(tick, 400);
  1733. }
  1734. }
  1735. if (isIOSWeChatBrowser()) {
  1736. setTimeout(tick, 350);
  1737. } else {
  1738. tick();
  1739. }
  1740. }
  1741. function showFabToast(msg, ms) {
  1742. var tip = String(msg || "").trim();
  1743. if (!tip) return;
  1744. var el = document.getElementById("openAppToast");
  1745. if (el) {
  1746. el.textContent = tip;
  1747. el.style.display = "block";
  1748. if (showFabToast._t) clearTimeout(showFabToast._t);
  1749. showFabToast._t = setTimeout(function () {
  1750. el.style.display = "none";
  1751. }, ms || 2800);
  1752. }
  1753. console.log("[openApp]", tip);
  1754. }
  1755. function showDownloadTip() {
  1756. var ua = navigator.userAgent || "";
  1757. var isIOS = /iP(hone|od|ad)/i.test(ua) || (navigator.platform === "MacIntel" && (navigator.maxTouchPoints || 0) > 1);
  1758. window.location.href = isIOS ? APP_IOS_DOWNLOAD_URL : APP_DOWNLOAD_URL;
  1759. }
  1760. function showAppOpenFailTip(msg) {
  1761. var tip = msg || "未能打开 App,请确认已安装最新版「U店在哪」。";
  1762. if (typeof uni !== "undefined" && typeof uni.showToast === "function") {
  1763. uni.showToast({ title: msg, icon: "none", duration: 2800 });
  1764. } else if (isWeChatInAppBrowser()) {
  1765. window.alert(tip);
  1766. } else {
  1767. showFabToast(tip);
  1768. }
  1769. }
  1770. function launchAppDeepLink(deepLink) {
  1771. try {
  1772. var a = document.createElement("a");
  1773. a.href = deepLink;
  1774. a.setAttribute("target", "_self");
  1775. document.body.appendChild(a);
  1776. a.click();
  1777. document.body.removeChild(a);
  1778. } catch (e1) {}
  1779. try {
  1780. window.location.href = deepLink;
  1781. } catch (e2) {}
  1782. }
  1783. function tryOpenHBuilderAppViaScheme() {
  1784. var deepLink = buildAppDeepLink();
  1785. if (typeof plus !== "undefined" && plus.runtime) {
  1786. var installed = null;
  1787. try {
  1788. if (typeof plus.runtime.isApplicationExist === "function") {
  1789. installed = plus.runtime.isApplicationExist({
  1790. pname: APP_ANDROID_PACKAGE,
  1791. action: APP_IOS_URL_SCHEME,
  1792. });
  1793. }
  1794. } catch (e) {
  1795. console.warn(e);
  1796. }
  1797. if (installed === false) {
  1798. showDownloadTip();
  1799. return;
  1800. }
  1801. try {
  1802. plus.runtime.openURL(deepLink);
  1803. } catch (e2) {
  1804. console.warn(e2);
  1805. showDownloadTip();
  1806. }
  1807. return;
  1808. }
  1809. var done = false;
  1810. function finish() {
  1811. if (done) return;
  1812. done = true;
  1813. document.removeEventListener("visibilitychange", onVis);
  1814. window.removeEventListener("pagehide", onHide);
  1815. }
  1816. function onVis() {
  1817. if (document.visibilityState === "hidden") finish();
  1818. }
  1819. function onHide() {
  1820. finish();
  1821. }
  1822. document.addEventListener("visibilitychange", onVis);
  1823. window.addEventListener("pagehide", onHide);
  1824. try {
  1825. launchAppDeepLink(deepLink);
  1826. } catch (e3) {
  1827. finish();
  1828. showDownloadTip();
  1829. return;
  1830. }
  1831. window.setTimeout(function () {
  1832. finish();
  1833. }, 3200);
  1834. }
  1835. function tryFetchWxConfigOnPcClick() {
  1836. if (!isWxPcBrowser()) return Promise.resolve(false);
  1837. showFabToast("正在请求 getWxConfig…");
  1838. wxJssdkInitPromise = null;
  1839. return initWeChatOpenLaunchApp(true).then(function (ok) {
  1840. if (ok) {
  1841. showFabToast(
  1842. weChatJssdkConfigured
  1843. ? "getWxConfig 成功,wx.config 已就绪"
  1844. : "getWxConfig 成功"
  1845. );
  1846. } else {
  1847. showFabToast(wxInitLastError || "getWxConfig 失败");
  1848. }
  1849. return ok;
  1850. });
  1851. }
  1852. function tryOpenHBuilderApp() {
  1853. if (isWeChatInAppBrowser()) return;
  1854. if (!isWxPcBrowser()) {
  1855. tryOpenHBuilderAppViaScheme();
  1856. return;
  1857. }
  1858. tryFetchWxConfigOnPcClick().then(function () {
  1859. if (isWxConfigOnClickDebug() && !isWxForceDebug() && !isWxPcAutoDebugHost()) {
  1860. return;
  1861. }
  1862. tryOpenHBuilderAppViaScheme();
  1863. });
  1864. }
  1865. function boot() {
  1866. var launchTag = document.getElementById("launch-btn");
  1867. if (launchTag) launchTag.setAttribute("appid", WECHAT_OPEN_APP_ID);
  1868. bindWeChatLaunchTagEvents();
  1869. if (isWeChatInAppBrowser()) {
  1870. document.body.classList.add("is-wechat");
  1871. logWxEntryDiagnostics();
  1872. }
  1873. if (shouldInitWeChatJssdkOnLoad()) {
  1874. if (isWxPcBrowser() && (isWxForceDebug() || isWxPcAutoDebugHost())) {
  1875. document.body.classList.add("wx-pc-debug");
  1876. }
  1877. scheduleWeChatJssdkBootstrap();
  1878. }
  1879. document.addEventListener("WeixinOpenTagsError", function (e) {
  1880. console.warn("[WeixinOpenTagsError]", e && e.detail);
  1881. });
  1882. if (isWeChatInAppBrowser()) {
  1883. var openBtnWx = document.getElementById("openApp");
  1884. if (openBtnWx) {
  1885. openBtnWx.addEventListener("click", function () {
  1886. if (!weChatJssdkConfigured) {
  1887. showFabToast(
  1888. wxInitLastError || "微信 SDK 初始化中,请稍候再点底部按钮"
  1889. );
  1890. }
  1891. });
  1892. }
  1893. } else {
  1894. var openAppBtn = document.getElementById("openApp");
  1895. if (openAppBtn) {
  1896. openAppBtn.addEventListener("click", tryOpenHBuilderApp);
  1897. }
  1898. }
  1899. bindApiDebugModalEvents();
  1900. bindImagePreviewEvents();
  1901. loadPageData();
  1902. }
  1903. if (document.readyState === "complete") {
  1904. boot();
  1905. } else {
  1906. window.addEventListener("load", boot);
  1907. }
  1908. })();
  1909. </script>
  1910. </body>
  1911. </html>