shareAiConsult.html 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058
  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. /** 仅发图时的默认提示,分享页不展示 */
  521. var IMAGE_ONLY_DEFAULT_PROMPT = "请帮我看看这些图片";
  522. function isImageOnlyPlaceholderText(text) {
  523. var t = String(text || "").trim();
  524. if (!t) return true;
  525. if (t === IMAGE_ONLY_DEFAULT_PROMPT) return true;
  526. if (t === "[图片]" || t === "【图片】" || t === "[图]" || t === "【图】") return true;
  527. return false;
  528. }
  529. var sharePayloadCache = null;
  530. var conversationApiDebugInfo = null;
  531. var weChatJssdkConfigured = false;
  532. var wxConfigSignRetriedBaseUrl = false;
  533. var wxInitLastError = "";
  534. var wxJssdkInitPromise = null;
  535. function escHtml(s) {
  536. return String(s || "")
  537. .replace(/&/g, "&amp;")
  538. .replace(/</g, "&lt;")
  539. .replace(/>/g, "&gt;")
  540. .replace(/"/g, "&quot;");
  541. }
  542. var IMAGE_EXT = "(?:jpe?g|png|gif|webp|bmp|avif)";
  543. var IMAGE_URL_PATTERN =
  544. "https?:\\/\\/[^\\s))\\]]+\\." + IMAGE_EXT + "(?:\\?[^\\s))\\]]*)?";
  545. function isInsideImageUrlSyntax(full, offset) {
  546. if (offset >= 2 && full.slice(offset - 2, offset) === "](") return true;
  547. if (offset >= 1 && full[offset - 1] === "(") return true;
  548. if (offset >= 6 && full.slice(offset - 6, offset) === "{{IMG|") return true;
  549. return false;
  550. }
  551. /** AI 回复常见:店名(https://...jpg) — 转为可渲染的图片占位 */
  552. function embedInlineImageUrls(text) {
  553. if (text == null) return "";
  554. var t = String(text);
  555. t = t.replace(
  556. new RegExp("([^\\n((]+?)\\((" + IMAGE_URL_PATTERN + ")\\)", "gi"),
  557. function (_, label, url) {
  558. var name = String(label).trim();
  559. if (!name) return "\n\n{{IMG|" + url + "|}}\n\n";
  560. var alt = name.length > 24 ? "图片" : name.replace(/[\[\]]/g, "");
  561. return name + "\n\n{{IMG|" + url + "|" + alt + "}}\n\n";
  562. }
  563. );
  564. t = t.replace(
  565. new RegExp("\\((" + IMAGE_URL_PATTERN + ")\\)", "gi"),
  566. function (_, url) {
  567. return "\n\n{{IMG|" + url + "|}}\n\n";
  568. }
  569. );
  570. t = t.replace(new RegExp(IMAGE_URL_PATTERN, "gi"), function (url, offset, full) {
  571. if (isInsideImageUrlSyntax(full, offset)) return url;
  572. return "\n\n{{IMG|" + url + "|}}\n\n";
  573. });
  574. return t.replace(/\n{3,}/g, "\n\n");
  575. }
  576. function formatTextBlockHtml(text) {
  577. return escHtml(text).replace(/\n/g, "<br>");
  578. }
  579. function buildAiImageTag(url, alt) {
  580. var safeUrl = escHtml(url);
  581. var safeAlt = escHtml(alt || "图片");
  582. return (
  583. '<img class="ai-card__image" src="' +
  584. safeUrl +
  585. '" alt="' +
  586. safeAlt +
  587. '" loading="lazy" decoding="async" />'
  588. );
  589. }
  590. function renderAiContentHtml(text) {
  591. var t = embedInlineImageUrls(text);
  592. var html = "";
  593. var tokenRe = /\{\{IMG\|([^|]+)\|([^}]*)\}\}/g;
  594. var lastIndex = 0;
  595. var match;
  596. while ((match = tokenRe.exec(t)) !== null) {
  597. if (match.index > lastIndex) {
  598. html += formatTextBlockHtml(t.slice(lastIndex, match.index));
  599. }
  600. html += buildAiImageTag(match[1], match[2]);
  601. lastIndex = tokenRe.lastIndex;
  602. }
  603. if (lastIndex < t.length) {
  604. html += formatTextBlockHtml(t.slice(lastIndex));
  605. }
  606. return html;
  607. }
  608. function q(name) {
  609. try {
  610. var v = new URLSearchParams(location.search || "").get(name);
  611. return v == null ? "" : String(v);
  612. } catch (e) {
  613. return "";
  614. }
  615. }
  616. function getQueryParam(name) {
  617. var m = new RegExp("[?&]" + name + "=([^&]*)").exec(window.location.search);
  618. if (!m) return "";
  619. try {
  620. return decodeURIComponent(m[1].replace(/\+/g, " "));
  621. } catch (e) {
  622. return "";
  623. }
  624. }
  625. function parsePayload() {
  626. var raw = getQueryParam("payload");
  627. if (!raw) return null;
  628. try {
  629. return JSON.parse(raw);
  630. } catch (e) {
  631. return null;
  632. }
  633. }
  634. function getConversationQueryParams() {
  635. var sessionId = String(q("session_id") || q("sessionId") || "").trim();
  636. var userId = String(q("user_id") || q("userId") || "").trim();
  637. var type = String(q("type") || "").trim();
  638. if (!sessionId || !userId) return null;
  639. return { sessionId: sessionId, userId: userId, type: type };
  640. }
  641. function parseMessageIdsFromQuery() {
  642. var raw = String(q("message_ids") || q("messageIds") || "").trim();
  643. if (!raw) return null;
  644. return raw
  645. .split(",")
  646. .map(function (s) {
  647. return String(s || "").trim();
  648. })
  649. .filter(Boolean);
  650. }
  651. function getMessageImageUrls(msg) {
  652. if (!msg) return [];
  653. if (Array.isArray(msg.imageUrls) && msg.imageUrls.length) {
  654. return msg.imageUrls
  655. .map(function (u) {
  656. return String(u || "").trim();
  657. })
  658. .filter(Boolean);
  659. }
  660. if (msg.imageUrl) return [String(msg.imageUrl)];
  661. return [];
  662. }
  663. function buildUserBubbleImagesHtml(imageUrls) {
  664. if (!Array.isArray(imageUrls) || !imageUrls.length) return "";
  665. var imgs = "";
  666. for (var j = 0; j < imageUrls.length; j++) {
  667. imgs +=
  668. '<img class="user-bubble__image" src="' +
  669. escHtml(imageUrls[j]) +
  670. '" alt="图片" loading="lazy" decoding="async" />';
  671. }
  672. return '<div class="user-bubble__images">' + imgs + "</div>";
  673. }
  674. function normalizeHistoryRowImageUrls(item) {
  675. var raw = item.image_urls || item.imageUrls || item.images;
  676. if (Array.isArray(raw)) {
  677. return raw
  678. .map(function (u) {
  679. return String(u || "").trim();
  680. })
  681. .filter(Boolean);
  682. }
  683. if (typeof raw === "string" && raw.trim()) {
  684. return raw
  685. .split(",")
  686. .map(function (s) {
  687. return s.trim();
  688. })
  689. .filter(Boolean);
  690. }
  691. return [];
  692. }
  693. function filterConversationMessagesByIds(messages, idList) {
  694. if (!Array.isArray(idList) || !idList.length) return messages;
  695. var idSet = {};
  696. for (var i = 0; i < idList.length; i++) {
  697. idSet[String(idList[i])] = true;
  698. }
  699. return (Array.isArray(messages) ? messages : []).filter(function (msg) {
  700. if (!msg || msg.id == null || msg.id === "") return false;
  701. return !!idSet[String(msg.id)];
  702. });
  703. }
  704. function getConversationApiBase(type) {
  705. return String(type || "").trim() === "1"
  706. ? AI_UBAO_CONVERSATION_API
  707. : AI_LEGAL_CONVERSATION_API;
  708. }
  709. function buildConversationApiUrl(sessionId, userId, type) {
  710. var params = new URLSearchParams();
  711. params.set("session_id", sessionId);
  712. params.set("user_id", userId);
  713. return getConversationApiBase(type) + "?" + params.toString();
  714. }
  715. function normalizeConversationRole(raw) {
  716. var role = String(raw || "").trim().toLowerCase();
  717. if (
  718. role === "user" ||
  719. role === "human" ||
  720. role === "customer" ||
  721. role === "question" ||
  722. role === "1" ||
  723. role === "我"
  724. ) {
  725. return "user";
  726. }
  727. if (
  728. role === "ai" ||
  729. role === "assistant" ||
  730. role === "bot" ||
  731. role === "ubao" ||
  732. role === "answer" ||
  733. role === "2" ||
  734. role === "system"
  735. ) {
  736. return "ai";
  737. }
  738. return "";
  739. }
  740. function normalizeConversationApiPayload(res) {
  741. if (!res || typeof res !== "object") return null;
  742. if (res.code != null) {
  743. var code = Number(res.code);
  744. if (code !== 200 && code !== 0 && res.success !== true) {
  745. throw new Error(res.msg || res.message || "对话接口返回 code " + code);
  746. }
  747. }
  748. var root = res.data != null && typeof res.data === "object" ? res.data : res;
  749. var rawList =
  750. (Array.isArray(root) && root) ||
  751. (Array.isArray(root.messages) && root.messages) ||
  752. (Array.isArray(root.messageList) && root.messageList) ||
  753. (Array.isArray(root.conversation) && root.conversation) ||
  754. (Array.isArray(root.conversations) && root.conversations) ||
  755. (Array.isArray(root.records) && root.records) ||
  756. (Array.isArray(root.list) && root.list) ||
  757. null;
  758. if (!rawList) return null;
  759. var messages = [];
  760. for (var i = 0; i < rawList.length; i++) {
  761. var item = rawList[i];
  762. if (!item || typeof item !== "object") continue;
  763. var msgId = item.id != null ? item.id : item.message_id;
  764. var role = normalizeConversationRole(
  765. item.role || item.sender || item.messageRole || item.type || item.messageType || item.senderType
  766. );
  767. var content = String(
  768. item.content != null
  769. ? item.content
  770. : item.message != null
  771. ? item.message
  772. : item.text != null
  773. ? item.text
  774. : ""
  775. ).trim();
  776. if (!role && item.question) {
  777. role = "user";
  778. content = String(item.question).trim();
  779. } else if (!role && item.answer) {
  780. role = "ai";
  781. content = String(item.answer).trim();
  782. } else if (!role && item.userMessage) {
  783. role = "user";
  784. content = String(item.userMessage).trim();
  785. } else if (!role && (item.aiMessage || item.assistantMessage)) {
  786. role = "ai";
  787. content = String(item.aiMessage || item.assistantMessage).trim();
  788. }
  789. var imageUrls = normalizeHistoryRowImageUrls(item);
  790. if (imageUrls.length) {
  791. messages.push({
  792. id: msgId,
  793. role: role || "user",
  794. isImage: true,
  795. imageUrls: imageUrls,
  796. imageUrl: imageUrls[0],
  797. content: content,
  798. });
  799. continue;
  800. }
  801. if (!content) continue;
  802. var imageUrl = item.imageUrl || item.image || item.imgUrl || item.url;
  803. if (imageUrl && /^(https?:)?\/\//i.test(String(imageUrl))) {
  804. messages.push({
  805. id: msgId,
  806. role: role || "user",
  807. isImage: true,
  808. imageUrls: [String(imageUrl)],
  809. imageUrl: String(imageUrl),
  810. });
  811. continue;
  812. }
  813. messages.push({
  814. id: msgId,
  815. role: role || (messages.length % 2 === 0 ? "user" : "ai"),
  816. content: content,
  817. });
  818. }
  819. if (!messages.length) return null;
  820. return {
  821. messages: messages,
  822. shareTime:
  823. root.shareTime ||
  824. root.createdAt ||
  825. root.createTime ||
  826. root.updateTime ||
  827. root.timestamp ||
  828. res.timestamp,
  829. firstQuestion: root.firstQuestion || root.title || root.topic || "",
  830. sessionId: root.sessionId || root.session_id || "",
  831. userId: root.userId || root.user_id || "",
  832. };
  833. }
  834. function formatJsonForDebug(value) {
  835. if (value == null) return "(无)";
  836. try {
  837. return JSON.stringify(value, null, 2);
  838. } catch (e) {
  839. return String(value);
  840. }
  841. }
  842. function setConversationApiDebugInfo(info) {
  843. conversationApiDebugInfo = info;
  844. renderApiDebugModal();
  845. }
  846. function renderApiDebugModal() {
  847. var body = document.getElementById("apiDebugBody");
  848. if (!body) return;
  849. if (!conversationApiDebugInfo) {
  850. body.innerHTML =
  851. '<div class="api-debug-section"><div class="api-debug-section__label">说明</div>' +
  852. '<pre class="api-debug-section__pre">尚未请求聊天记录接口(缺少 session_id / user_id 参数时使用 payload 渲染)</pre></div>';
  853. return;
  854. }
  855. var info = conversationApiDebugInfo;
  856. var html =
  857. '<div class="api-debug-section"><div class="api-debug-section__label">接口地址</div>' +
  858. '<pre class="api-debug-section__pre">' +
  859. escHtml(info.apiBase || info.requestUrl || "(无)") +
  860. "</pre></div>" +
  861. '<div class="api-debug-section"><div class="api-debug-section__label">请求方式</div>' +
  862. '<pre class="api-debug-section__pre">' +
  863. escHtml(info.method || "GET") +
  864. "</pre></div>" +
  865. '<div class="api-debug-section"><div class="api-debug-section__label">完整请求 URL</div>' +
  866. '<pre class="api-debug-section__pre">' +
  867. escHtml(info.requestUrl || "(无)") +
  868. "</pre></div>" +
  869. '<div class="api-debug-section"><div class="api-debug-section__label">请求参数</div>' +
  870. '<pre class="api-debug-section__pre">' +
  871. escHtml(formatJsonForDebug(info.params)) +
  872. "</pre></div>";
  873. if (info.error) {
  874. html +=
  875. '<div class="api-debug-section"><div class="api-debug-section__label">请求错误</div>' +
  876. '<pre class="api-debug-section__pre">' +
  877. escHtml(info.error) +
  878. "</pre></div>";
  879. }
  880. html +=
  881. '<div class="api-debug-section"><div class="api-debug-section__label">接口返回数据</div>' +
  882. '<pre class="api-debug-section__pre">' +
  883. escHtml(formatJsonForDebug(info.response)) +
  884. "</pre></div>";
  885. body.innerHTML = html;
  886. }
  887. function showApiDebugModal() {
  888. renderApiDebugModal();
  889. var mask = document.getElementById("apiDebugMask");
  890. if (mask) {
  891. mask.classList.add("is-open");
  892. mask.setAttribute("aria-hidden", "false");
  893. }
  894. }
  895. function hideApiDebugModal() {
  896. var mask = document.getElementById("apiDebugMask");
  897. if (mask) {
  898. mask.classList.remove("is-open");
  899. mask.setAttribute("aria-hidden", "true");
  900. }
  901. }
  902. function bindApiDebugModalEvents() {
  903. var btn = document.getElementById("apiDebugBtn");
  904. var closeBtn = document.getElementById("apiDebugClose");
  905. var mask = document.getElementById("apiDebugMask");
  906. if (btn) btn.addEventListener("click", showApiDebugModal);
  907. if (closeBtn) closeBtn.addEventListener("click", hideApiDebugModal);
  908. if (mask) {
  909. mask.addEventListener("click", function (e) {
  910. if (e.target === mask) hideApiDebugModal();
  911. });
  912. }
  913. }
  914. function showImagePreview(url) {
  915. var urlStr = String(url || "").trim();
  916. if (!urlStr) return;
  917. var mask = document.getElementById("imagePreview");
  918. var img = document.getElementById("imagePreviewImg");
  919. if (!mask || !img) return;
  920. img.src = urlStr;
  921. mask.classList.add("is-open");
  922. mask.setAttribute("aria-hidden", "false");
  923. document.body.style.overflow = "hidden";
  924. }
  925. function hideImagePreview() {
  926. var mask = document.getElementById("imagePreview");
  927. var img = document.getElementById("imagePreviewImg");
  928. if (!mask) return;
  929. mask.classList.remove("is-open");
  930. mask.setAttribute("aria-hidden", "true");
  931. if (img) img.src = "";
  932. document.body.style.overflow = "";
  933. }
  934. function bindImagePreviewEvents() {
  935. var main = document.getElementById("main");
  936. var mask = document.getElementById("imagePreview");
  937. var closeBtn = document.getElementById("imagePreviewClose");
  938. if (main) {
  939. main.addEventListener("click", function (e) {
  940. var target = e.target;
  941. if (!target || target.tagName !== "IMG") return;
  942. if (
  943. target.classList.contains("user-bubble__image") ||
  944. target.classList.contains("ai-card__image")
  945. ) {
  946. showImagePreview(target.getAttribute("src") || target.src);
  947. }
  948. });
  949. }
  950. if (closeBtn) closeBtn.addEventListener("click", hideImagePreview);
  951. if (mask) {
  952. mask.addEventListener("click", function (e) {
  953. if (e.target === mask) hideImagePreview();
  954. });
  955. }
  956. }
  957. function fetchConversationHistory(sessionId, userId, type) {
  958. var requestUrl = buildConversationApiUrl(sessionId, userId, type);
  959. var apiBase = getConversationApiBase(type);
  960. var params = {
  961. session_id: sessionId,
  962. user_id: userId,
  963. type: type || "",
  964. };
  965. setConversationApiDebugInfo({
  966. apiBase: apiBase,
  967. requestUrl: requestUrl,
  968. method: "GET",
  969. params: params,
  970. response: null,
  971. error: null,
  972. });
  973. console.log("[conversation-api] GET", requestUrl);
  974. return fetch(requestUrl, {
  975. method: "GET",
  976. mode: "cors",
  977. credentials: "omit",
  978. headers: { Accept: "application/json" },
  979. })
  980. .then(function (r) {
  981. if (r.ok) return r.json();
  982. return r
  983. .text()
  984. .catch(function () {
  985. return "";
  986. })
  987. .then(function (text) {
  988. var hint = "";
  989. var parsed = null;
  990. try {
  991. parsed = JSON.parse(text);
  992. hint = parsed.msg || parsed.message || "";
  993. } catch (eP) {
  994. if (text) hint = text.slice(0, 120);
  995. }
  996. setConversationApiDebugInfo({
  997. apiBase: apiBase,
  998. requestUrl: requestUrl,
  999. method: "GET",
  1000. params: params,
  1001. response: parsed || text || null,
  1002. error: "HTTP " + r.status + (hint ? ":" + hint : ""),
  1003. });
  1004. throw new Error("对话接口 HTTP " + r.status + (hint ? ":" + hint : ""));
  1005. });
  1006. })
  1007. .then(function (res) {
  1008. setConversationApiDebugInfo({
  1009. apiBase: apiBase,
  1010. requestUrl: requestUrl,
  1011. method: "GET",
  1012. params: params,
  1013. response: res,
  1014. error: null,
  1015. });
  1016. var data = normalizeConversationApiPayload(res);
  1017. if (!data) {
  1018. throw new Error("对话接口返回数据为空或格式无法解析");
  1019. }
  1020. return data;
  1021. })
  1022. .catch(function (e) {
  1023. if (!conversationApiDebugInfo || !conversationApiDebugInfo.error) {
  1024. setConversationApiDebugInfo({
  1025. apiBase: apiBase,
  1026. requestUrl: requestUrl,
  1027. method: "GET",
  1028. params: params,
  1029. response: conversationApiDebugInfo && conversationApiDebugInfo.response,
  1030. error: e && e.message ? e.message : String(e),
  1031. });
  1032. }
  1033. throw e;
  1034. });
  1035. }
  1036. function loadPageData() {
  1037. var main = document.getElementById("main");
  1038. var params = getConversationQueryParams();
  1039. if (!params) {
  1040. setConversationApiDebugInfo(null);
  1041. render(parsePayload());
  1042. return;
  1043. }
  1044. var filterIds = parseMessageIdsFromQuery();
  1045. main.innerHTML = '<div class="empty">对话内容加载中…</div>';
  1046. fetchConversationHistory(params.sessionId, params.userId, params.type)
  1047. .then(function (data) {
  1048. if (filterIds && filterIds.length) {
  1049. var filtered = filterConversationMessagesByIds(data.messages, filterIds);
  1050. if (!filtered.length) {
  1051. throw new Error("未找到所选对话内容");
  1052. }
  1053. data = Object.assign({}, data, { messages: filtered });
  1054. }
  1055. render(data);
  1056. })
  1057. .catch(function (e) {
  1058. var tip = e && e.message ? e.message : "对话加载失败,请稍后重试";
  1059. console.warn("[conversation-api]", tip, e);
  1060. main.innerHTML = '<div class="empty">' + escHtml(tip) + "</div>";
  1061. refreshWxLaunchTagAttrs();
  1062. });
  1063. }
  1064. function getFirstUserQuestion(messages) {
  1065. var list = Array.isArray(messages) ? messages : [];
  1066. for (var i = 0; i < list.length; i++) {
  1067. var m = list[i];
  1068. if (!m || m.role !== "user") continue;
  1069. if (m.isImage) {
  1070. var imgText = String(m.content || "").trim();
  1071. if (imgText && !isImageOnlyPlaceholderText(imgText)) return imgText;
  1072. continue;
  1073. }
  1074. var t = String(m.content || "").trim();
  1075. if (t) return t;
  1076. }
  1077. return "";
  1078. }
  1079. function formatDiscussionTitle(question) {
  1080. var qText = String(question || "").trim();
  1081. if (!qText) return "与AI助手对话";
  1082. if (qText.slice(-3) === "的讨论") return qText;
  1083. return qText + "的讨论";
  1084. }
  1085. function normalizeShareTimestamp(ts) {
  1086. if (ts == null || ts === "") return null;
  1087. var n = Number(ts);
  1088. if (!isFinite(n) || n <= 0) return null;
  1089. if (n < 1e12) n = n * 1000;
  1090. var d = new Date(n);
  1091. if (isNaN(d.getTime()) || d.getFullYear() < 2020) return null;
  1092. return n;
  1093. }
  1094. function resolveDisplayShareTime(data) {
  1095. var fromUrl = q("shareTime") || q("share_time");
  1096. var urlTs = normalizeShareTimestamp(fromUrl);
  1097. if (urlTs != null) return urlTs;
  1098. var dataTs = normalizeShareTimestamp(data && data.shareTime);
  1099. if (dataTs != null) return dataTs;
  1100. return Date.now();
  1101. }
  1102. function formatDate(ts) {
  1103. var normalized = normalizeShareTimestamp(ts);
  1104. var d = normalized != null ? new Date(normalized) : new Date();
  1105. return d.getFullYear() + "年" + (d.getMonth() + 1) + "月" + d.getDate() + "日";
  1106. }
  1107. function renderMessages(messages) {
  1108. var html = "";
  1109. var list = Array.isArray(messages) ? messages : [];
  1110. for (var i = 0; i < list.length; i++) {
  1111. var msg = list[i];
  1112. if (!msg) continue;
  1113. if (msg.role === "user") {
  1114. var imageUrls = getMessageImageUrls(msg);
  1115. if (msg.isImage && imageUrls.length) {
  1116. html +=
  1117. '<div class="row row--user"><div class="user-bubble user-bubble--images">' +
  1118. buildUserBubbleImagesHtml(imageUrls) +
  1119. "</div></div>";
  1120. var imageUserText = String(msg.content || "").trim();
  1121. if (imageUserText && !isImageOnlyPlaceholderText(imageUserText)) {
  1122. html +=
  1123. '<div class="row row--user"><div class="user-bubble">' +
  1124. escHtml(imageUserText) +
  1125. "</div></div>";
  1126. }
  1127. continue;
  1128. }
  1129. var userText = String(msg.content || "").trim();
  1130. if (!userText || isImageOnlyPlaceholderText(userText)) continue;
  1131. html +=
  1132. '<div class="row row--user"><div class="user-bubble">' +
  1133. escHtml(userText) +
  1134. "</div></div>";
  1135. continue;
  1136. }
  1137. if (msg.role === "ai" || msg.role === "assistant") {
  1138. var aiText = String(msg.content || "").trim();
  1139. if (!aiText) continue;
  1140. html +=
  1141. '<div class="row row--ai"><div class="ai-card"><div class="ai-card__text">' +
  1142. renderAiContentHtml(aiText) +
  1143. "</div></div></div>";
  1144. }
  1145. }
  1146. return html;
  1147. }
  1148. function applyWeixinShareMeta() {
  1149. var siteName = "U店在哪";
  1150. var iconUrl =
  1151. "https://alien-volume.oss-cn-beijing.aliyuncs.com/icon/ubao.png";
  1152. var setMeta = function (key, content) {
  1153. var el =
  1154. document.querySelector('meta[property="' + key + '"]') ||
  1155. document.querySelector('meta[name="' + key + '"]');
  1156. if (!el) {
  1157. el = document.createElement("meta");
  1158. if (key.indexOf("og:") === 0) {
  1159. el.setAttribute("property", key);
  1160. } else {
  1161. el.setAttribute("name", key);
  1162. }
  1163. document.head.appendChild(el);
  1164. }
  1165. el.setAttribute("content", content);
  1166. };
  1167. setMeta("og:site_name", siteName);
  1168. setMeta("application-name", siteName);
  1169. setMeta("og:title", "和U宝的对话");
  1170. setMeta("og:description", "点击查看对话内容");
  1171. var links = document.querySelectorAll('link[rel="icon"], link[rel="apple-touch-icon"]');
  1172. for (var i = 0; i < links.length; i++) {
  1173. links[i].setAttribute("href", iconUrl);
  1174. }
  1175. }
  1176. function countDisplayableMessages(messages) {
  1177. var list = Array.isArray(messages) ? messages : [];
  1178. var count = 0;
  1179. for (var i = 0; i < list.length; i++) {
  1180. var msg = list[i];
  1181. if (!msg) continue;
  1182. if (msg.role === "user") {
  1183. if (msg.isImage && getMessageImageUrls(msg).length) {
  1184. count += 1;
  1185. continue;
  1186. }
  1187. var userContent = String(msg.content || "").trim();
  1188. if (userContent && !isImageOnlyPlaceholderText(userContent)) count += 1;
  1189. continue;
  1190. }
  1191. if (msg.role === "ai" || msg.role === "assistant") {
  1192. if (String(msg.content || "").trim()) count += 1;
  1193. }
  1194. }
  1195. return count;
  1196. }
  1197. function parseMessagesFromContent(text) {
  1198. var raw = String(text || "").trim();
  1199. if (!raw) return [];
  1200. var list = [];
  1201. var blocks = raw.split(/\n\n+/);
  1202. for (var i = 0; i < blocks.length; i++) {
  1203. var block = String(blocks[i] || "").trim();
  1204. if (!block) continue;
  1205. var userMatch = block.match(/^我[::]\s*([\s\S]*)$/);
  1206. if (userMatch) {
  1207. var userText = String(userMatch[1] || "").trim();
  1208. if (userText) list.push({ role: "user", content: userText });
  1209. continue;
  1210. }
  1211. var aiMatch = block.match(/^UBAO[::]\s*([\s\S]*)$/i);
  1212. if (aiMatch) {
  1213. var aiText = String(aiMatch[1] || "").trim();
  1214. if (aiText) list.push({ role: "ai", content: aiText });
  1215. }
  1216. }
  1217. if (list.length) return list;
  1218. var current = null;
  1219. var lines = raw.split(/\n/);
  1220. for (var j = 0; j < lines.length; j++) {
  1221. var line = String(lines[j] || "");
  1222. var userLine = line.match(/^我[::]\s*(.*)$/);
  1223. var aiLine = line.match(/^UBAO[::]\s*(.*)$/i);
  1224. if (userLine) {
  1225. if (current) list.push(current);
  1226. current = { role: "user", content: String(userLine[1] || "").trim() };
  1227. continue;
  1228. }
  1229. if (aiLine) {
  1230. if (current) list.push(current);
  1231. current = { role: "ai", content: String(aiLine[1] || "").trim() };
  1232. continue;
  1233. }
  1234. if (current && line.trim()) {
  1235. current.content = (current.content ? current.content + "\n" : "") + line.trim();
  1236. }
  1237. }
  1238. if (current && String(current.content || "").trim()) list.push(current);
  1239. return list;
  1240. }
  1241. function resolveShareMessagesForDisplay(data) {
  1242. var fromArray = Array.isArray(data && data.messages) ? data.messages : [];
  1243. var fromContent = parseMessagesFromContent((data && data.content) || "");
  1244. var arrayCount = countDisplayableMessages(fromArray);
  1245. var contentCount = countDisplayableMessages(fromContent);
  1246. // 分享链接超长时 messages 会被截断,但 content 仍保留完整对话文本
  1247. if (contentCount > arrayCount) return fromContent;
  1248. if (arrayCount) return fromArray;
  1249. return fromContent;
  1250. }
  1251. function render(data) {
  1252. applyWeixinShareMeta();
  1253. sharePayloadCache = data || null;
  1254. var main = document.getElementById("main");
  1255. if (!data) {
  1256. main.innerHTML = '<div class="empty">对话内容不存在或链接已失效</div>';
  1257. refreshWxLaunchTagAttrs();
  1258. return;
  1259. }
  1260. var messages = resolveShareMessagesForDisplay(data);
  1261. var firstQuestion =
  1262. String(data.firstQuestion || "").trim() ||
  1263. getFirstUserQuestion(messages) ||
  1264. (function () {
  1265. var text = String(data.content || "").trim();
  1266. var m = text.match(/^我[::]([\s\S]+?)(?:\n\n|$)/);
  1267. return m ? String(m[1] || "").trim() : "";
  1268. })();
  1269. var pageTitle = formatDiscussionTitle(firstQuestion);
  1270. var dateText = formatDate(resolveDisplayShareTime(data));
  1271. document.title = pageTitle;
  1272. var bodyHtml =
  1273. '<section class="hero">' +
  1274. '<h1 class="hero__title">' +
  1275. escHtml(pageTitle) +
  1276. "</h1>" +
  1277. '<p class="hero__meta">' +
  1278. escHtml(dateText) +
  1279. " · 内容由AI生成,不能完全保障真实</p>" +
  1280. '<div class="hero__divider"></div>' +
  1281. "</section>";
  1282. var convHtml = renderMessages(messages);
  1283. if (convHtml) {
  1284. bodyHtml +=
  1285. '<section class="conversation">' +
  1286. convHtml +
  1287. '</section><div class="scroll-spacer" aria-hidden="true"></div>';
  1288. } else {
  1289. bodyHtml += '<div class="empty">暂无对话内容</div>';
  1290. }
  1291. main.innerHTML = bodyHtml;
  1292. refreshWxLaunchTagAttrs();
  1293. }
  1294. function buildAiConsultOpenParams() {
  1295. var params = new URLSearchParams();
  1296. params.set("pageType", "home");
  1297. params.set("fromShareAiConsult", "1");
  1298. return params;
  1299. }
  1300. function buildAppDeepLink() {
  1301. var path = APP_UNI_AI_PATH;
  1302. var qs = buildAiConsultOpenParams().toString();
  1303. var root = APP_IOS_URL_SCHEME.replace(/\/$/, "");
  1304. return root + "/" + path + (qs ? "?" + qs : "");
  1305. }
  1306. function buildWeChatLaunchExtinfo(useBarePathOnly) {
  1307. var path = APP_UNI_AI_PATH.replace(/^\//, "");
  1308. if (useBarePathOnly) return path;
  1309. var qs = buildAiConsultOpenParams().toString();
  1310. var uniPage = qs ? path + "?" + qs : path;
  1311. if (uniPage.length <= 1024) return uniPage;
  1312. return path + "?pageType=home&fromShareAiConsult=1";
  1313. }
  1314. function isWeChatInAppBrowser() {
  1315. return /MicroMessenger/i.test(navigator.userAgent || "");
  1316. }
  1317. function getWxShareEntryFrom() {
  1318. var from = q("from");
  1319. if (from) return String(from).trim().toLowerCase();
  1320. try {
  1321. var h = location.hash || "";
  1322. var qi = h.indexOf("?");
  1323. if (qi >= 0) {
  1324. var hf = new URLSearchParams(h.slice(qi + 1)).get("from");
  1325. if (hf) return String(hf).trim().toLowerCase();
  1326. }
  1327. } catch (eH) {}
  1328. return "";
  1329. }
  1330. function logWxEntryDiagnostics() {
  1331. if (!isWeChatInAppBrowser()) return;
  1332. console.log("[wx-entry]", {
  1333. entryFrom: getWxShareEntryFrom() || "(无)",
  1334. wxJssdkReady: weChatJssdkConfigured,
  1335. signUrl: getWxConfigSignUrl(),
  1336. });
  1337. }
  1338. function isIOSWeChatBrowser() {
  1339. var ua = navigator.userAgent || "";
  1340. return /MicroMessenger/i.test(ua) && /iPhone|iPad|iPod/i.test(ua);
  1341. }
  1342. function isWxDebugOn() {
  1343. return q("wxDebug") === "1";
  1344. }
  1345. function isWxForceDebug() {
  1346. return q("wxForce") === "1";
  1347. }
  1348. function isWxConfigOnClickDebug() {
  1349. return q("wxConfigOnClick") === "1";
  1350. }
  1351. function isWxPcAutoDebugHost() {
  1352. var h = (location.hostname || "").toLowerCase();
  1353. if (h === "localhost" || h === "127.0.0.1") return true;
  1354. for (var i = 0; i < WECHAT_JS_SAFE_HOSTS.length; i++) {
  1355. if (WECHAT_JS_SAFE_HOSTS[i] === h) return true;
  1356. }
  1357. return false;
  1358. }
  1359. function isWxPcBrowser() {
  1360. return !isWeChatInAppBrowser();
  1361. }
  1362. function shouldInitWeChatJssdkOnLoad() {
  1363. if (isWeChatInAppBrowser()) return true;
  1364. return isWxForceDebug() || isWxPcAutoDebugHost();
  1365. }
  1366. function shouldFetchWxConfig(fromUserClick) {
  1367. if (isWeChatInAppBrowser()) return true;
  1368. if (fromUserClick) return true;
  1369. return isWxForceDebug() || isWxPcAutoDebugHost();
  1370. }
  1371. function getWxHtmlUrl() {
  1372. var forced = String(q("wxSignUrl") || "").trim();
  1373. if (forced) return forced.split("#")[0];
  1374. return String(location.href || "").split("#")[0];
  1375. }
  1376. function getWxSignPageUrlForApi() {
  1377. var htmlUrl = getWxHtmlUrl();
  1378. if (htmlUrl && /^https?:\/\//i.test(htmlUrl)) return htmlUrl;
  1379. return WX_GET_CONFIG_SIGN_URL;
  1380. }
  1381. function getWxHtmlUrlBase() {
  1382. var htmlUrl = getWxSignPageUrlForApi();
  1383. try {
  1384. var u = new URL(htmlUrl);
  1385. return u.origin + u.pathname;
  1386. } catch (eU) {
  1387. return WX_GET_CONFIG_SIGN_URL;
  1388. }
  1389. }
  1390. /** 与 secondShareGoods 一致:默认用当前页完整 URL 验签(须与微信地址栏一致,含 query) */
  1391. function getWxConfigSignUrl() {
  1392. if (String(q("wxSignBaseOnly") || "") === "1") return getWxHtmlUrlBase();
  1393. return getWxSignPageUrlForApi();
  1394. }
  1395. function getWxGetConfigApiUrl() {
  1396. return API_BASE.replace(/\/$/, "") + WECHAT_GET_WX_CONFIG_PATH;
  1397. }
  1398. function buildWxGetConfigRequestBody(htmlUrl) {
  1399. return {
  1400. url: String(htmlUrl || "").split("#")[0].trim(),
  1401. };
  1402. }
  1403. function resolveWxConfigAppIdFromSignData(d) {
  1404. if (!d || typeof d !== "object") return "";
  1405. var mp =
  1406. d.mpAppId ||
  1407. d.mpAppid ||
  1408. d.officialAppId ||
  1409. d.gzhAppId ||
  1410. d.serviceAppId;
  1411. if (mp != null && String(mp).trim() !== "") return String(mp).trim();
  1412. var fromQuery = String(q("wxMpAppId") || WECHAT_MP_APP_ID || "").trim();
  1413. if (fromQuery) return fromQuery;
  1414. var raw = d.appId || d.appid || d.wxAppId;
  1415. return raw != null && String(raw).trim() !== "" ? String(raw).trim() : "";
  1416. }
  1417. function normalizeWxJssdkSignPayload(res, signUrlUsed) {
  1418. if (!res || typeof res !== "object") return null;
  1419. var d = res.data != null && typeof res.data === "object" ? res.data : res;
  1420. if (!d || typeof d !== "object") return null;
  1421. var appId = resolveWxConfigAppIdFromSignData(d);
  1422. var timestamp = d.timestamp != null ? d.timestamp : d.timeStamp;
  1423. var nonceStr =
  1424. d.nonceStr != null && String(d.nonceStr) !== ""
  1425. ? d.nonceStr
  1426. : d.noncestr != null && String(d.noncestr) !== ""
  1427. ? d.noncestr
  1428. : d.nonce;
  1429. var signature = d.signature || d.sign;
  1430. if (!appId || timestamp == null || nonceStr == null || nonceStr === "" || !signature) {
  1431. return null;
  1432. }
  1433. return {
  1434. appId: String(appId),
  1435. timestamp: Number(timestamp),
  1436. nonceStr: String(nonceStr),
  1437. signature: String(signature),
  1438. signUrl: String(signUrlUsed || "")
  1439. .split("#")[0]
  1440. .trim(),
  1441. };
  1442. }
  1443. function getWxErrMsg(err) {
  1444. if (!err) return "";
  1445. if (err.errMsg) return String(err.errMsg);
  1446. if (typeof err === "string") return err;
  1447. try {
  1448. return JSON.stringify(err);
  1449. } catch (e) {
  1450. return String(err);
  1451. }
  1452. }
  1453. function formatWxConfigErrorTip(err, signPageUrl) {
  1454. var errMsg = getWxErrMsg(err);
  1455. var tip = "微信 JSSDK 配置失败";
  1456. if (/invalid signature/i.test(errMsg)) {
  1457. return (
  1458. tip +
  1459. ":签名无效。请核对:①后端用与前端相同的 url 签名;②url=" +
  1460. (signPageUrl || getWxHtmlUrl()) +
  1461. ";③nonceStr/timestamp 与接口返回一致;④appId=" +
  1462. WECHAT_MP_APP_ID
  1463. );
  1464. }
  1465. if (/require\s*subscribe/i.test(errMsg)) {
  1466. return tip + ":" + errMsg + "(服务号需用户已关注)";
  1467. }
  1468. return errMsg ? tip + ":" + errMsg : tip;
  1469. }
  1470. function setWxInitError(msg) {
  1471. wxInitLastError = String(msg || "").trim();
  1472. if (wxInitLastError) console.warn("[wx]", wxInitLastError);
  1473. }
  1474. function requestWeChatJssdkSignAndConfig(htmlUrlOptional) {
  1475. var htmlUrl = String(htmlUrlOptional != null ? htmlUrlOptional : getWxConfigSignUrl())
  1476. .split("#")[0]
  1477. .trim();
  1478. if (!htmlUrl || !/^https?:\/\//i.test(htmlUrl)) {
  1479. htmlUrl = WX_GET_CONFIG_SIGN_URL;
  1480. }
  1481. if (isWxDebugOn()) {
  1482. try {
  1483. window.alert("htmlUrl(签名用,应与地址栏一致):\n" + htmlUrl);
  1484. } catch (eDbg) {}
  1485. }
  1486. var requestUrl = getWxGetConfigApiUrl();
  1487. var requestBody = buildWxGetConfigRequestBody(htmlUrl);
  1488. console.log("[wx] htmlUrl=", htmlUrl);
  1489. console.log("[wx] POST getWxConfig →", requestUrl, requestBody);
  1490. return fetch(requestUrl, {
  1491. method: "POST",
  1492. mode: "cors",
  1493. credentials: "omit",
  1494. headers: {
  1495. Accept: "application/json",
  1496. "Content-Type": "application/json;charset=UTF-8",
  1497. },
  1498. body: JSON.stringify(requestBody),
  1499. })
  1500. .then(function (r) {
  1501. if (r.ok) return r.json();
  1502. return r
  1503. .text()
  1504. .catch(function () {
  1505. return "";
  1506. })
  1507. .then(function (text) {
  1508. var hint = "";
  1509. try {
  1510. var j = JSON.parse(text);
  1511. hint = j.msg || j.message || "";
  1512. } catch (eP) {
  1513. if (text) hint = text.slice(0, 120);
  1514. }
  1515. throw new Error("getWxConfig HTTP " + r.status + (hint ? ":" + hint : ""));
  1516. });
  1517. })
  1518. .then(function (res) {
  1519. if (res && res.code != null) {
  1520. var c = Number(res.code);
  1521. if (c !== 200 && c !== 0 && res.success !== true) {
  1522. throw new Error(res.msg || res.message || "getWxConfig code " + c);
  1523. }
  1524. }
  1525. var sign = normalizeWxJssdkSignPayload(res, htmlUrl);
  1526. if (!sign) {
  1527. console.warn("[wx] getWxConfig 响应字段不全", res, "htmlUrl=", htmlUrl);
  1528. throw new Error("getWxConfig 缺少 appId/timestamp/nonceStr/signature(见控制台)");
  1529. }
  1530. if (sign.appId !== WECHAT_MP_APP_ID) {
  1531. console.warn(
  1532. "[wx] 后端 appId=" + sign.appId + ",期望服务号 " + WECHAT_MP_APP_ID
  1533. );
  1534. }
  1535. if (typeof wx === "undefined") {
  1536. if (isWxPcBrowser()) {
  1537. console.log("[wx] PC getWxConfig 成功(无 jweixin)", sign);
  1538. return true;
  1539. }
  1540. setWxInitError("jweixin.js 未加载");
  1541. return false;
  1542. }
  1543. return applyWxConfigFromSign(sign, htmlUrl);
  1544. });
  1545. }
  1546. function applyWxConfigFromSign(sign, htmlUrl) {
  1547. var wxConfigParams = {
  1548. debug: isWxDebugOn(),
  1549. appId: String(sign.appId),
  1550. timestamp: sign.timestamp,
  1551. nonceStr: String(sign.nonceStr),
  1552. signature: String(sign.signature),
  1553. jsApiList: [],
  1554. openTagList: ["wx-open-launch-app"],
  1555. };
  1556. return new Promise(function (resolve) {
  1557. wx.config(wxConfigParams);
  1558. wx.ready(function () {
  1559. weChatJssdkConfigured = true;
  1560. document.body.classList.add("wx-jssdk-ready");
  1561. refreshWxLaunchTagAttrs();
  1562. console.log("[wx.config] ready, htmlUrl=", htmlUrl);
  1563. resolve(true);
  1564. });
  1565. wx.error(function (err) {
  1566. weChatJssdkConfigured = false;
  1567. document.body.classList.remove("wx-jssdk-ready");
  1568. wxJssdkInitPromise = null;
  1569. var errMsg = getWxErrMsg(err);
  1570. setWxInitError(formatWxConfigErrorTip(err, htmlUrl));
  1571. if (isWxDebugOn()) window.alert(wxInitLastError);
  1572. console.warn("[wx.config]", errMsg, "htmlUrl=", htmlUrl);
  1573. var baseUrl = getWxHtmlUrlBase();
  1574. var fullUrl = getWxSignPageUrlForApi();
  1575. if (
  1576. !/invalid signature/i.test(errMsg) ||
  1577. wxConfigSignRetriedBaseUrl ||
  1578. baseUrl === fullUrl ||
  1579. htmlUrl === baseUrl
  1580. ) {
  1581. resolve(false);
  1582. return;
  1583. }
  1584. wxConfigSignRetriedBaseUrl = true;
  1585. requestWeChatJssdkSignAndConfig(baseUrl).then(resolve);
  1586. });
  1587. });
  1588. }
  1589. function refreshWxLaunchTagAttrs(useBarePathOnly) {
  1590. var tag = document.getElementById("launch-btn");
  1591. if (!tag) return;
  1592. try {
  1593. tag.setAttribute("appid", WECHAT_OPEN_APP_ID);
  1594. tag.setAttribute("extinfo", buildWeChatLaunchExtinfo(!!useBarePathOnly));
  1595. console.log("[wx-open-launch-app] extinfo=", tag.getAttribute("extinfo"));
  1596. } catch (eA) {}
  1597. }
  1598. function bindWeChatLaunchTagEvents() {
  1599. var tag = document.getElementById("launch-btn");
  1600. if (!tag || tag._wxLaunchBound) return;
  1601. tag._wxLaunchBound = true;
  1602. refreshWxLaunchTagAttrs();
  1603. tag.addEventListener("launch", function () {
  1604. console.log("[wx-open-launch-app] launch ok");
  1605. });
  1606. tag.addEventListener("error", function (e) {
  1607. var detail = e && e.detail;
  1608. var errMsg =
  1609. detail && detail.errMsg
  1610. ? String(detail.errMsg)
  1611. : detail && detail.errmsg
  1612. ? String(detail.errmsg)
  1613. : "";
  1614. console.warn(
  1615. "[wx-open-launch-app]",
  1616. detail,
  1617. "extinfo=",
  1618. tag.getAttribute("extinfo")
  1619. );
  1620. if (/launch:fail_check/i.test(errMsg)) {
  1621. showAppOpenFailTip(
  1622. "请用微信分享卡片进入,或确认已安装最新版「U店在哪」"
  1623. );
  1624. return;
  1625. }
  1626. // if (
  1627. // /launch:fail/i.test(errMsg) &&
  1628. // !tag._wxLaunchBareRetried &&
  1629. // tag.getAttribute("extinfo") !== buildWeChatLaunchExtinfo(true)
  1630. // ) {
  1631. // tag._wxLaunchBareRetried = true;
  1632. // refreshWxLaunchTagAttrs(true);
  1633. // showFabToast("请再点一次「和U宝继续聊」");
  1634. // return;
  1635. // }
  1636. if (/launch:fail/i.test(errMsg) && !getWxShareEntryFrom()) {
  1637. showAppOpenFailTip("请前往应用商店下载U店在哪app");
  1638. return;
  1639. }
  1640. showAppOpenFailTip(
  1641. errMsg
  1642. ? "未能打开 App:" + errMsg
  1643. : "未能打开 App,请确认已安装最新版「U店在哪」"
  1644. );
  1645. });
  1646. }
  1647. function initWeChatOpenLaunchApp(fromUserClick) {
  1648. if (!shouldFetchWxConfig(!!fromUserClick)) {
  1649. return Promise.resolve(false);
  1650. }
  1651. if (wxJssdkInitPromise && !fromUserClick) return wxJssdkInitPromise;
  1652. wxConfigSignRetriedBaseUrl = false;
  1653. wxInitLastError = "";
  1654. var htmlUrl = getWxConfigSignUrl();
  1655. bindWeChatLaunchTagEvents();
  1656. wxJssdkInitPromise = requestWeChatJssdkSignAndConfig(htmlUrl)
  1657. .then(function (ok) {
  1658. if (ok === true) return true;
  1659. if (!wxInitLastError) {
  1660. setWxInitError("wx.config 失败,可加 ?wxDebug=1 查看 htmlUrl");
  1661. }
  1662. return false;
  1663. })
  1664. .catch(function (e) {
  1665. var msg = e && e.message ? e.message : "getWxConfig 请求失败";
  1666. setWxInitError(msg);
  1667. console.warn("[wx] requestWeChatJssdkSignAndConfig failed", msg, "htmlUrl=", htmlUrl);
  1668. if (isWeChatInAppBrowser() && isWxDebugOn()) window.alert(msg);
  1669. return false;
  1670. })
  1671. .finally(function () {
  1672. if (!weChatJssdkConfigured) wxJssdkInitPromise = null;
  1673. });
  1674. return wxJssdkInitPromise;
  1675. }
  1676. function scheduleWeChatJssdkBootstrap() {
  1677. if (!shouldInitWeChatJssdkOnLoad()) return;
  1678. var attempts = 0;
  1679. function tick() {
  1680. attempts += 1;
  1681. if (weChatJssdkConfigured) return;
  1682. initWeChatOpenLaunchApp();
  1683. if (!weChatJssdkConfigured && attempts < 8 && typeof wx === "undefined") {
  1684. setTimeout(tick, 400);
  1685. }
  1686. }
  1687. if (isIOSWeChatBrowser()) {
  1688. setTimeout(tick, 350);
  1689. } else {
  1690. tick();
  1691. }
  1692. }
  1693. function showFabToast(msg, ms) {
  1694. var tip = String(msg || "").trim();
  1695. if (!tip) return;
  1696. var el = document.getElementById("openAppToast");
  1697. if (el) {
  1698. el.textContent = tip;
  1699. el.style.display = "block";
  1700. if (showFabToast._t) clearTimeout(showFabToast._t);
  1701. showFabToast._t = setTimeout(function () {
  1702. el.style.display = "none";
  1703. }, ms || 2800);
  1704. }
  1705. console.log("[openApp]", tip);
  1706. }
  1707. function showDownloadTip() {
  1708. var ua = navigator.userAgent || "";
  1709. var isIOS = /iP(hone|od|ad)/i.test(ua) || (navigator.platform === "MacIntel" && (navigator.maxTouchPoints || 0) > 1);
  1710. window.location.href = isIOS ? APP_IOS_DOWNLOAD_URL : APP_DOWNLOAD_URL;
  1711. }
  1712. function showAppOpenFailTip(msg) {
  1713. var tip = msg || "未能打开 App,请确认已安装最新版「U店在哪」。";
  1714. if (typeof uni !== "undefined" && typeof uni.showToast === "function") {
  1715. uni.showToast({ title: msg, icon: "none", duration: 2800 });
  1716. } else if (isWeChatInAppBrowser()) {
  1717. window.alert(tip);
  1718. } else {
  1719. showFabToast(tip);
  1720. }
  1721. }
  1722. function launchAppDeepLink(deepLink) {
  1723. try {
  1724. var a = document.createElement("a");
  1725. a.href = deepLink;
  1726. a.setAttribute("target", "_self");
  1727. document.body.appendChild(a);
  1728. a.click();
  1729. document.body.removeChild(a);
  1730. } catch (e1) {}
  1731. try {
  1732. window.location.href = deepLink;
  1733. } catch (e2) {}
  1734. }
  1735. function tryOpenHBuilderAppViaScheme() {
  1736. var deepLink = buildAppDeepLink();
  1737. if (typeof plus !== "undefined" && plus.runtime) {
  1738. var installed = null;
  1739. try {
  1740. if (typeof plus.runtime.isApplicationExist === "function") {
  1741. installed = plus.runtime.isApplicationExist({
  1742. pname: APP_ANDROID_PACKAGE,
  1743. action: APP_IOS_URL_SCHEME,
  1744. });
  1745. }
  1746. } catch (e) {
  1747. console.warn(e);
  1748. }
  1749. if (installed === false) {
  1750. showDownloadTip();
  1751. return;
  1752. }
  1753. try {
  1754. plus.runtime.openURL(deepLink);
  1755. } catch (e2) {
  1756. console.warn(e2);
  1757. showDownloadTip();
  1758. }
  1759. return;
  1760. }
  1761. var done = false;
  1762. function finish() {
  1763. if (done) return;
  1764. done = true;
  1765. document.removeEventListener("visibilitychange", onVis);
  1766. window.removeEventListener("pagehide", onHide);
  1767. }
  1768. function onVis() {
  1769. if (document.visibilityState === "hidden") finish();
  1770. }
  1771. function onHide() {
  1772. finish();
  1773. }
  1774. document.addEventListener("visibilitychange", onVis);
  1775. window.addEventListener("pagehide", onHide);
  1776. try {
  1777. launchAppDeepLink(deepLink);
  1778. } catch (e3) {
  1779. finish();
  1780. showDownloadTip();
  1781. return;
  1782. }
  1783. window.setTimeout(function () {
  1784. finish();
  1785. }, 3200);
  1786. }
  1787. function tryFetchWxConfigOnPcClick() {
  1788. if (!isWxPcBrowser()) return Promise.resolve(false);
  1789. showFabToast("正在请求 getWxConfig…");
  1790. wxJssdkInitPromise = null;
  1791. return initWeChatOpenLaunchApp(true).then(function (ok) {
  1792. if (ok) {
  1793. showFabToast(
  1794. weChatJssdkConfigured
  1795. ? "getWxConfig 成功,wx.config 已就绪"
  1796. : "getWxConfig 成功"
  1797. );
  1798. } else {
  1799. showFabToast(wxInitLastError || "getWxConfig 失败");
  1800. }
  1801. return ok;
  1802. });
  1803. }
  1804. function tryOpenHBuilderApp() {
  1805. if (isWeChatInAppBrowser()) return;
  1806. if (!isWxPcBrowser()) {
  1807. tryOpenHBuilderAppViaScheme();
  1808. return;
  1809. }
  1810. tryFetchWxConfigOnPcClick().then(function () {
  1811. if (isWxConfigOnClickDebug() && !isWxForceDebug() && !isWxPcAutoDebugHost()) {
  1812. return;
  1813. }
  1814. tryOpenHBuilderAppViaScheme();
  1815. });
  1816. }
  1817. function boot() {
  1818. var launchTag = document.getElementById("launch-btn");
  1819. if (launchTag) launchTag.setAttribute("appid", WECHAT_OPEN_APP_ID);
  1820. bindWeChatLaunchTagEvents();
  1821. if (isWeChatInAppBrowser()) {
  1822. document.body.classList.add("is-wechat");
  1823. logWxEntryDiagnostics();
  1824. }
  1825. if (shouldInitWeChatJssdkOnLoad()) {
  1826. if (isWxPcBrowser() && (isWxForceDebug() || isWxPcAutoDebugHost())) {
  1827. document.body.classList.add("wx-pc-debug");
  1828. }
  1829. scheduleWeChatJssdkBootstrap();
  1830. }
  1831. document.addEventListener("WeixinOpenTagsError", function (e) {
  1832. console.warn("[WeixinOpenTagsError]", e && e.detail);
  1833. });
  1834. if (isWeChatInAppBrowser()) {
  1835. var openBtnWx = document.getElementById("openApp");
  1836. if (openBtnWx) {
  1837. openBtnWx.addEventListener("click", function () {
  1838. if (!weChatJssdkConfigured) {
  1839. showFabToast(
  1840. wxInitLastError || "微信 SDK 初始化中,请稍候再点底部按钮"
  1841. );
  1842. }
  1843. });
  1844. }
  1845. } else {
  1846. var openAppBtn = document.getElementById("openApp");
  1847. if (openAppBtn) {
  1848. openAppBtn.addEventListener("click", tryOpenHBuilderApp);
  1849. }
  1850. }
  1851. bindApiDebugModalEvents();
  1852. bindImagePreviewEvents();
  1853. loadPageData();
  1854. }
  1855. if (document.readyState === "complete") {
  1856. boot();
  1857. } else {
  1858. window.addEventListener("load", boot);
  1859. }
  1860. })();
  1861. </script>
  1862. </body>
  1863. </html>