shareAiConsult.html 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  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宝AI智能助手">
  11. <meta name="application-name" content="U宝AI智能助手">
  12. <link rel="icon" type="image/png" href="https://alien-volume.oss-cn-beijing.aliyuncs.com/icon/ubao.png">
  13. <link rel="apple-touch-icon" href="https://alien-volume.oss-cn-beijing.aliyuncs.com/icon/ubao.png">
  14. <style>
  15. :root {
  16. --bg: #f4f6fb;
  17. --text: #151515;
  18. --muted: #aaaaaa;
  19. --orange: #f47d1f;
  20. --safe-bottom: env(safe-area-inset-bottom, 0px);
  21. }
  22. * {
  23. margin: 0;
  24. padding: 0;
  25. box-sizing: border-box;
  26. }
  27. html {
  28. -webkit-tap-highlight-color: transparent;
  29. }
  30. body {
  31. font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif;
  32. background: var(--bg);
  33. color: var(--text);
  34. min-height: 100vh;
  35. padding-bottom: calc(88px + var(--safe-bottom));
  36. }
  37. .hero {
  38. padding: 16px 15px 0;
  39. }
  40. .hero__title {
  41. font-size: 20px;
  42. font-weight: 700;
  43. line-height: 1.4;
  44. word-break: break-word;
  45. }
  46. .hero__meta {
  47. margin-top: 8px;
  48. font-size: 12px;
  49. color: var(--muted);
  50. line-height: 1.4;
  51. }
  52. .hero__divider {
  53. margin-top: 14px;
  54. height: 1px;
  55. background: rgba(170, 170, 170, 0.25);
  56. }
  57. .conversation {
  58. padding: 14px 15px 0;
  59. }
  60. .row {
  61. display: flex;
  62. margin-bottom: 12px;
  63. }
  64. .row--user {
  65. justify-content: flex-end;
  66. }
  67. .row--ai {
  68. justify-content: flex-start;
  69. }
  70. .user-bubble {
  71. max-width: 78%;
  72. padding: 10px 12px;
  73. background: var(--orange);
  74. border-radius: 6px;
  75. color: #fff;
  76. font-size: 15px;
  77. font-weight: 500;
  78. line-height: 1.5;
  79. white-space: pre-wrap;
  80. word-break: break-word;
  81. }
  82. .ai-card {
  83. width: 100%;
  84. padding: 14px 13px;
  85. background: #fff;
  86. border-radius: 8px;
  87. box-shadow: 0 1px 6px rgba(0, 0, 0, 0.04);
  88. }
  89. .ai-card__text {
  90. font-size: 14px;
  91. line-height: 1.65;
  92. white-space: pre-wrap;
  93. word-break: break-word;
  94. text-align: left;
  95. }
  96. .fallback-card {
  97. margin: 14px 15px 0;
  98. padding: 14px 13px;
  99. background: #fff;
  100. border-radius: 8px;
  101. }
  102. .empty {
  103. padding: 48px 20px;
  104. text-align: center;
  105. color: var(--muted);
  106. font-size: 14px;
  107. }
  108. .footer {
  109. position: fixed;
  110. left: 0;
  111. right: 0;
  112. bottom: 0;
  113. z-index: 20;
  114. padding: 10px 15px calc(10px + var(--safe-bottom));
  115. background: linear-gradient(180deg, rgba(244, 246, 251, 0) 0%, #f4f6fb 36%);
  116. pointer-events: none;
  117. }
  118. .footer__dock {
  119. position: relative;
  120. width: 100%;
  121. pointer-events: auto;
  122. }
  123. .continue-btn {
  124. width: 100%;
  125. height: 44px;
  126. border: none;
  127. border-radius: 999px;
  128. background: linear-gradient(90deg, #ffb347 0%, #f47d1f 55%, #ff7849 100%);
  129. box-shadow: 0 4px 12px rgba(244, 125, 31, 0.28);
  130. color: #fff;
  131. font-size: 16px;
  132. font-weight: 600;
  133. touch-action: manipulation;
  134. }
  135. #launch-btn {
  136. display: none;
  137. position: absolute;
  138. left: 0;
  139. right: 0;
  140. top: 0;
  141. width: 100%;
  142. height: 44px;
  143. min-height: 44px;
  144. border-radius: 999px;
  145. overflow: hidden;
  146. }
  147. body.is-wechat.wx-jssdk-ready #launch-btn {
  148. display: block;
  149. }
  150. body.is-wechat.wx-jssdk-ready #btnContinue {
  151. visibility: hidden;
  152. pointer-events: none;
  153. }
  154. #openAppToast {
  155. display: none;
  156. position: fixed;
  157. left: 16px;
  158. right: 16px;
  159. bottom: calc(72px + var(--safe-bottom));
  160. z-index: 10001;
  161. padding: 10px 14px;
  162. font-size: 13px;
  163. line-height: 1.45;
  164. color: #fff;
  165. text-align: center;
  166. background: rgba(0, 0, 0, 0.78);
  167. border-radius: 8px;
  168. pointer-events: none;
  169. word-break: break-all;
  170. }
  171. </style>
  172. </head>
  173. <body>
  174. <main id="main">
  175. <div class="empty">对话内容加载中…</div>
  176. </main>
  177. <div id="openAppToast" role="status" aria-live="polite"></div>
  178. <footer class="footer">
  179. <div class="footer__dock">
  180. <button type="button" class="continue-btn" id="btnContinue">和U宝继续聊 →</button>
  181. <wx-open-launch-app id="launch-btn" appid="wxf5f1efe3a9f5012e" extinfo="">
  182. <script type="text/wxtag-template">
  183. <style>
  184. .wx-open-app-btn {
  185. display: block;
  186. width: 100%;
  187. height: 44px;
  188. margin: 0;
  189. padding: 0;
  190. border: none;
  191. border-radius: 999px;
  192. background: linear-gradient(90deg, #ffb347 0%, #f47d1f 55%, #ff7849 100%);
  193. box-shadow: 0 4px 12px rgba(244, 125, 31, 0.28);
  194. color: #fff;
  195. font-size: 16px;
  196. font-weight: 600;
  197. line-height: 44px;
  198. text-align: center;
  199. cursor: pointer;
  200. -webkit-tap-highlight-color: transparent;
  201. }
  202. </style>
  203. <button class="wx-open-app-btn">和U宝继续聊 →</button>
  204. </script>
  205. </wx-open-launch-app>
  206. </div>
  207. </footer>
  208. <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
  209. <script>
  210. (function () {
  211. "use strict";
  212. var API_BASE = "https://test.ailien.shop/alienStore";
  213. var WECHAT_MP_APP_ID = "wx412792c77f47babd";
  214. var WECHAT_OPEN_APP_ID = "wxf5f1efe3a9f5012e";
  215. var WECHAT_GET_WX_CONFIG_PATH = "/wx/getWxConfig";
  216. var H5_PAGE_BASE_FALLBACK = "https://test.ailien.shop/h5/HBuilderProjects/";
  217. var WX_GET_CONFIG_SIGN_URL = H5_PAGE_BASE_FALLBACK + "shareAiConsult.html";
  218. var WECHAT_JS_SAFE_HOSTS = ["uat.ailien.shop", "test.ailien.shop"];
  219. var APP_ANDROID_PACKAGE = "com.alien.Udianzaizhe";
  220. var APP_IOS_URL_SCHEME = "shopro://";
  221. var APP_UNI_AI_PATH = "pages/aiSearchResult/index";
  222. var sharePayloadCache = null;
  223. var weChatJssdkConfigured = false;
  224. var wxConfigSignRetriedBaseUrl = false;
  225. var wxInitLastError = "";
  226. var wxJssdkInitPromise = null;
  227. function escHtml(s) {
  228. return String(s || "")
  229. .replace(/&/g, "&amp;")
  230. .replace(/</g, "&lt;")
  231. .replace(/>/g, "&gt;")
  232. .replace(/"/g, "&quot;");
  233. }
  234. function q(name) {
  235. try {
  236. var v = new URLSearchParams(location.search || "").get(name);
  237. return v == null ? "" : String(v);
  238. } catch (e) {
  239. return "";
  240. }
  241. }
  242. function getQueryParam(name) {
  243. var m = new RegExp("[?&]" + name + "=([^&]*)").exec(window.location.search);
  244. if (!m) return "";
  245. try {
  246. return decodeURIComponent(m[1].replace(/\+/g, " "));
  247. } catch (e) {
  248. return "";
  249. }
  250. }
  251. function parsePayload() {
  252. var raw = getQueryParam("payload");
  253. if (!raw) {
  254. try {
  255. var hash = String(location.hash || "").replace(/^#/, "");
  256. if (hash) {
  257. var hp = new URLSearchParams(hash).get("payload");
  258. if (hp != null && String(hp) !== "") raw = String(hp);
  259. }
  260. } catch (eHash) {}
  261. }
  262. if (!raw) return null;
  263. try {
  264. return JSON.parse(raw);
  265. } catch (e) {
  266. return null;
  267. }
  268. }
  269. function getFirstUserQuestion(messages) {
  270. var list = Array.isArray(messages) ? messages : [];
  271. for (var i = 0; i < list.length; i++) {
  272. var m = list[i];
  273. if (!m || m.role !== "user") continue;
  274. if (m.isImage) return "[图片]";
  275. var t = String(m.content || "").trim();
  276. if (t) return t;
  277. }
  278. return "";
  279. }
  280. function formatDiscussionTitle(question) {
  281. var qText = String(question || "").trim();
  282. if (!qText) return "与AI助手对话";
  283. if (qText.slice(-3) === "的讨论") return qText;
  284. return qText + "的讨论";
  285. }
  286. function formatDate(ts) {
  287. var d = ts ? new Date(Number(ts)) : new Date();
  288. if (isNaN(d.getTime())) d = new Date();
  289. return d.getFullYear() + "年" + (d.getMonth() + 1) + "月" + d.getDate() + "日";
  290. }
  291. function renderMessages(messages) {
  292. var html = "";
  293. var list = Array.isArray(messages) ? messages : [];
  294. for (var i = 0; i < list.length; i++) {
  295. var msg = list[i];
  296. if (!msg) continue;
  297. if (msg.role === "user") {
  298. var userText = msg.isImage ? "[图片]" : String(msg.content || "").trim();
  299. if (!userText) continue;
  300. html +=
  301. '<div class="row row--user"><div class="user-bubble">' +
  302. escHtml(userText) +
  303. "</div></div>";
  304. continue;
  305. }
  306. if (msg.role === "ai") {
  307. var aiText = String(msg.content || "").trim();
  308. if (!aiText) continue;
  309. html +=
  310. '<div class="row row--ai"><div class="ai-card"><div class="ai-card__text">' +
  311. escHtml(aiText) +
  312. "</div></div></div>";
  313. }
  314. }
  315. return html;
  316. }
  317. function applyWeixinShareMeta() {
  318. var siteName = "U宝AI智能助手";
  319. var iconUrl =
  320. "https://alien-volume.oss-cn-beijing.aliyuncs.com/icon/ubao.png";
  321. var setMeta = function (key, content) {
  322. var el =
  323. document.querySelector('meta[property="' + key + '"]') ||
  324. document.querySelector('meta[name="' + key + '"]');
  325. if (!el) {
  326. el = document.createElement("meta");
  327. if (key.indexOf("og:") === 0) {
  328. el.setAttribute("property", key);
  329. } else {
  330. el.setAttribute("name", key);
  331. }
  332. document.head.appendChild(el);
  333. }
  334. el.setAttribute("content", content);
  335. };
  336. setMeta("og:site_name", siteName);
  337. setMeta("application-name", siteName);
  338. setMeta("og:title", "和U宝的对话");
  339. setMeta("og:description", "点击查看对话内容");
  340. var links = document.querySelectorAll('link[rel="icon"], link[rel="apple-touch-icon"]');
  341. for (var i = 0; i < links.length; i++) {
  342. links[i].setAttribute("href", iconUrl);
  343. }
  344. }
  345. function render(data) {
  346. applyWeixinShareMeta();
  347. sharePayloadCache = data || null;
  348. var main = document.getElementById("main");
  349. if (!data) {
  350. main.innerHTML = '<div class="empty">对话内容不存在或链接已失效</div>';
  351. refreshWxLaunchTagAttrs();
  352. return;
  353. }
  354. var messages = data.messages || [];
  355. var firstQuestion =
  356. String(data.firstQuestion || "").trim() || getFirstUserQuestion(messages);
  357. var pageTitle = formatDiscussionTitle(firstQuestion);
  358. var dateText = formatDate(data.shareTime);
  359. document.title = pageTitle;
  360. var bodyHtml =
  361. '<section class="hero">' +
  362. '<h1 class="hero__title">' +
  363. escHtml(pageTitle) +
  364. "</h1>" +
  365. '<p class="hero__meta">' +
  366. escHtml(dateText) +
  367. " · 内容由AI生成,不能完全保障真实</p>" +
  368. '<div class="hero__divider"></div>' +
  369. "</section>";
  370. var convHtml = renderMessages(messages);
  371. if (convHtml) {
  372. bodyHtml += '<section class="conversation">' + convHtml + "</section>";
  373. } else if (data.content) {
  374. bodyHtml +=
  375. '<section class="fallback-card"><div class="ai-card__text">' +
  376. escHtml(data.content) +
  377. "</div></section>";
  378. } else {
  379. bodyHtml += '<div class="empty">暂无对话内容</div>';
  380. }
  381. main.innerHTML = bodyHtml;
  382. refreshWxLaunchTagAttrs();
  383. }
  384. function buildAppDeepLink() {
  385. var path = APP_UNI_AI_PATH;
  386. var root = APP_IOS_URL_SCHEME.replace(/\/$/, "");
  387. return root + "/" + path + "?pageType=home";
  388. }
  389. /** wx-open-launch-app extinfo:仅跳转 AI 首页,不传分享上下文 */
  390. function buildWeChatLaunchExtinfo() {
  391. return APP_UNI_AI_PATH + "?pageType=home";
  392. }
  393. function isWeChatInAppBrowser() {
  394. return /MicroMessenger/i.test(navigator.userAgent || "");
  395. }
  396. function isIOSWeChatBrowser() {
  397. var ua = navigator.userAgent || "";
  398. return /MicroMessenger/i.test(ua) && /iPhone|iPad|iPod/i.test(ua);
  399. }
  400. function isWxDebugOn() {
  401. return q("wxDebug") === "1";
  402. }
  403. function isWxForceDebug() {
  404. return q("wxForce") === "1";
  405. }
  406. function isWxPcAutoDebugHost() {
  407. var h = (location.hostname || "").toLowerCase();
  408. if (h === "localhost" || h === "127.0.0.1") return true;
  409. for (var i = 0; i < WECHAT_JS_SAFE_HOSTS.length; i++) {
  410. if (WECHAT_JS_SAFE_HOSTS[i] === h) return true;
  411. }
  412. return false;
  413. }
  414. function isWxPcBrowser() {
  415. return !isWeChatInAppBrowser();
  416. }
  417. function shouldInitWeChatJssdkOnLoad() {
  418. if (isWeChatInAppBrowser()) return true;
  419. return isWxForceDebug() || isWxPcAutoDebugHost();
  420. }
  421. function shouldFetchWxConfig(fromUserClick) {
  422. if (isWeChatInAppBrowser()) return true;
  423. if (fromUserClick) return true;
  424. return isWxForceDebug() || isWxPcAutoDebugHost();
  425. }
  426. function getWxHtmlUrl() {
  427. var forced = String(q("wxSignUrl") || "").trim();
  428. if (forced) return forced.split("#")[0];
  429. return String(location.href || "").split("#")[0];
  430. }
  431. function getWxSignPageUrlForApi() {
  432. var htmlUrl = getWxHtmlUrl();
  433. if (htmlUrl && /^https?:\/\//i.test(htmlUrl)) return htmlUrl;
  434. return WX_GET_CONFIG_SIGN_URL;
  435. }
  436. function getWxHtmlUrlBase() {
  437. var htmlUrl = getWxSignPageUrlForApi();
  438. try {
  439. var u = new URL(htmlUrl);
  440. return u.origin + u.pathname;
  441. } catch (eU) {
  442. return WX_GET_CONFIG_SIGN_URL;
  443. }
  444. }
  445. /** 签名 url 不含 query/hash,避免 payload 过长导致 wx.config / 开放标签 launch:fail */
  446. function getWxConfigSignUrl() {
  447. if (String(q("wxSignFullUrl") || "") === "1") return getWxSignPageUrlForApi();
  448. return getWxHtmlUrlBase();
  449. }
  450. function getWxGetConfigApiUrl() {
  451. return API_BASE.replace(/\/$/, "") + WECHAT_GET_WX_CONFIG_PATH;
  452. }
  453. function buildWxGetConfigRequestBody(htmlUrl) {
  454. return {
  455. url: String(htmlUrl || "").split("#")[0].trim(),
  456. };
  457. }
  458. function resolveWxConfigAppIdFromSignData(d) {
  459. if (!d || typeof d !== "object") return "";
  460. var mp =
  461. d.mpAppId ||
  462. d.mpAppid ||
  463. d.officialAppId ||
  464. d.gzhAppId ||
  465. d.serviceAppId;
  466. if (mp != null && String(mp).trim() !== "") return String(mp).trim();
  467. var fromQuery = String(q("wxMpAppId") || WECHAT_MP_APP_ID || "").trim();
  468. if (fromQuery) return fromQuery;
  469. var raw = d.appId || d.appid || d.wxAppId;
  470. return raw != null && String(raw).trim() !== "" ? String(raw).trim() : "";
  471. }
  472. function normalizeWxJssdkSignPayload(res, signUrlUsed) {
  473. if (!res || typeof res !== "object") return null;
  474. var d = res.data != null && typeof res.data === "object" ? res.data : res;
  475. if (!d || typeof d !== "object") return null;
  476. var appId = resolveWxConfigAppIdFromSignData(d);
  477. var timestamp = d.timestamp != null ? d.timestamp : d.timeStamp;
  478. var nonceStr =
  479. d.nonceStr != null && String(d.nonceStr) !== ""
  480. ? d.nonceStr
  481. : d.noncestr != null && String(d.noncestr) !== ""
  482. ? d.noncestr
  483. : d.nonce;
  484. var signature = d.signature || d.sign;
  485. if (!appId || timestamp == null || nonceStr == null || nonceStr === "" || !signature) {
  486. return null;
  487. }
  488. return {
  489. appId: String(appId),
  490. timestamp: Number(timestamp),
  491. nonceStr: String(nonceStr),
  492. signature: String(signature),
  493. signUrl: String(signUrlUsed || "")
  494. .split("#")[0]
  495. .trim(),
  496. };
  497. }
  498. function getWxErrMsg(err) {
  499. if (!err) return "";
  500. if (err.errMsg) return String(err.errMsg);
  501. if (typeof err === "string") return err;
  502. try {
  503. return JSON.stringify(err);
  504. } catch (e) {
  505. return String(err);
  506. }
  507. }
  508. function formatWxConfigErrorTip(err, signPageUrl) {
  509. var errMsg = getWxErrMsg(err);
  510. var tip = "微信 JSSDK 配置失败";
  511. if (/invalid signature/i.test(errMsg)) {
  512. return (
  513. tip +
  514. ":签名无效。请核对:①后端用与前端相同的 url 签名;②url=" +
  515. (signPageUrl || getWxHtmlUrl()) +
  516. ";③nonceStr/timestamp 与接口返回一致;④appId=" +
  517. WECHAT_MP_APP_ID
  518. );
  519. }
  520. if (/require\s*subscribe/i.test(errMsg)) {
  521. return tip + ":" + errMsg + "(服务号需用户已关注)";
  522. }
  523. return errMsg ? tip + ":" + errMsg : tip;
  524. }
  525. function setWxInitError(msg) {
  526. wxInitLastError = String(msg || "").trim();
  527. if (wxInitLastError) console.warn("[wx]", wxInitLastError);
  528. }
  529. function requestWeChatJssdkSignAndConfig(htmlUrlOptional) {
  530. var htmlUrl = String(htmlUrlOptional != null ? htmlUrlOptional : getWxConfigSignUrl())
  531. .split("#")[0]
  532. .trim();
  533. if (!htmlUrl || !/^https?:\/\//i.test(htmlUrl)) {
  534. htmlUrl = WX_GET_CONFIG_SIGN_URL;
  535. }
  536. if (isWxDebugOn()) {
  537. try {
  538. window.alert("htmlUrl(签名用,应与地址栏一致):\n" + htmlUrl);
  539. } catch (eDbg) {}
  540. }
  541. var requestUrl = getWxGetConfigApiUrl();
  542. var requestBody = buildWxGetConfigRequestBody(htmlUrl);
  543. return fetch(requestUrl, {
  544. method: "POST",
  545. mode: "cors",
  546. credentials: "omit",
  547. headers: {
  548. Accept: "application/json",
  549. "Content-Type": "application/json;charset=UTF-8",
  550. },
  551. body: JSON.stringify(requestBody),
  552. })
  553. .then(function (r) {
  554. if (r.ok) return r.json();
  555. return r
  556. .text()
  557. .catch(function () {
  558. return "";
  559. })
  560. .then(function (text) {
  561. var hint = "";
  562. try {
  563. var j = JSON.parse(text);
  564. hint = j.msg || j.message || "";
  565. } catch (eP) {
  566. if (text) hint = text.slice(0, 120);
  567. }
  568. throw new Error("getWxConfig HTTP " + r.status + (hint ? ":" + hint : ""));
  569. });
  570. })
  571. .then(function (res) {
  572. if (res && res.code != null) {
  573. var c = Number(res.code);
  574. if (c !== 200 && c !== 0 && res.success !== true) {
  575. throw new Error(res.msg || res.message || "getWxConfig code " + c);
  576. }
  577. }
  578. var sign = normalizeWxJssdkSignPayload(res, htmlUrl);
  579. if (!sign) {
  580. throw new Error("getWxConfig 缺少 appId/timestamp/nonceStr/signature(见控制台)");
  581. }
  582. if (typeof wx === "undefined") {
  583. if (isWxPcBrowser()) {
  584. console.log("[wx] PC getWxConfig 成功(无 jweixin)", sign);
  585. return true;
  586. }
  587. setWxInitError("jweixin.js 未加载");
  588. return false;
  589. }
  590. return applyWxConfigFromSign(sign, htmlUrl);
  591. });
  592. }
  593. function applyWxConfigFromSign(sign, htmlUrl) {
  594. var wxConfigParams = {
  595. debug: isWxDebugOn(),
  596. appId: String(sign.appId),
  597. timestamp: sign.timestamp,
  598. nonceStr: String(sign.nonceStr),
  599. signature: String(sign.signature),
  600. jsApiList: [],
  601. openTagList: ["wx-open-launch-app"],
  602. };
  603. return new Promise(function (resolve) {
  604. wx.config(wxConfigParams);
  605. wx.ready(function () {
  606. weChatJssdkConfigured = true;
  607. document.body.classList.add("wx-jssdk-ready");
  608. refreshWxLaunchTagAttrs();
  609. console.log("[wx.config] ready, htmlUrl=", htmlUrl);
  610. resolve(true);
  611. });
  612. wx.error(function (err) {
  613. weChatJssdkConfigured = false;
  614. document.body.classList.remove("wx-jssdk-ready");
  615. wxJssdkInitPromise = null;
  616. var errMsg = getWxErrMsg(err);
  617. setWxInitError(formatWxConfigErrorTip(err, htmlUrl));
  618. if (isWxDebugOn()) window.alert(wxInitLastError);
  619. console.warn("[wx.config]", errMsg, "htmlUrl=", htmlUrl);
  620. var baseUrl = getWxHtmlUrlBase();
  621. var fullUrl = getWxSignPageUrlForApi();
  622. if (
  623. !/invalid signature/i.test(errMsg) ||
  624. wxConfigSignRetriedBaseUrl ||
  625. baseUrl === fullUrl ||
  626. htmlUrl === baseUrl
  627. ) {
  628. resolve(false);
  629. return;
  630. }
  631. wxConfigSignRetriedBaseUrl = true;
  632. requestWeChatJssdkSignAndConfig(baseUrl).then(resolve);
  633. });
  634. });
  635. }
  636. function refreshWxLaunchTagAttrs() {
  637. var tag = document.getElementById("launch-btn");
  638. if (!tag) return;
  639. try {
  640. tag.setAttribute("appid", WECHAT_OPEN_APP_ID);
  641. tag.setAttribute("extinfo", buildWeChatLaunchExtinfo());
  642. } catch (eA) {}
  643. }
  644. function bindWeChatLaunchTagEvents() {
  645. var tag = document.getElementById("launch-btn");
  646. if (!tag || tag._wxLaunchBound) return;
  647. tag._wxLaunchBound = true;
  648. refreshWxLaunchTagAttrs();
  649. tag.addEventListener("launch", function () {
  650. console.log("[wx-open-launch-app] launch ok");
  651. });
  652. tag.addEventListener("error", function (e) {
  653. var detail = e && e.detail;
  654. var errMsg =
  655. detail && detail.errMsg
  656. ? String(detail.errMsg)
  657. : detail && detail.errmsg
  658. ? String(detail.errmsg)
  659. : "";
  660. console.warn("[wx-open-launch-app]", detail);
  661. showAppOpenFailTip(
  662. errMsg
  663. ? "未能打开 App:" + errMsg
  664. : "未能打开 App,请确认已安装最新版「U店在哪」"
  665. );
  666. });
  667. }
  668. function initWeChatOpenLaunchApp(fromUserClick) {
  669. if (!shouldFetchWxConfig(!!fromUserClick)) {
  670. return Promise.resolve(false);
  671. }
  672. if (wxJssdkInitPromise && !fromUserClick) return wxJssdkInitPromise;
  673. wxConfigSignRetriedBaseUrl = false;
  674. wxInitLastError = "";
  675. var htmlUrl = getWxConfigSignUrl();
  676. bindWeChatLaunchTagEvents();
  677. wxJssdkInitPromise = requestWeChatJssdkSignAndConfig(htmlUrl)
  678. .then(function (ok) {
  679. if (ok === true) return true;
  680. if (!wxInitLastError) {
  681. setWxInitError("wx.config 失败,可加 ?wxDebug=1 查看 htmlUrl");
  682. }
  683. return false;
  684. })
  685. .catch(function (e) {
  686. var msg = e && e.message ? e.message : "getWxConfig 请求失败";
  687. setWxInitError(msg);
  688. console.warn("[wx] requestWeChatJssdkSignAndConfig failed", msg, "htmlUrl=", htmlUrl);
  689. if (isWeChatInAppBrowser() && isWxDebugOn()) window.alert(msg);
  690. return false;
  691. })
  692. .finally(function () {
  693. if (!weChatJssdkConfigured) wxJssdkInitPromise = null;
  694. });
  695. return wxJssdkInitPromise;
  696. }
  697. function scheduleWeChatJssdkBootstrap() {
  698. if (!shouldInitWeChatJssdkOnLoad()) return;
  699. var attempts = 0;
  700. function tick() {
  701. attempts += 1;
  702. if (weChatJssdkConfigured) return;
  703. initWeChatOpenLaunchApp();
  704. if (!weChatJssdkConfigured && attempts < 8 && typeof wx === "undefined") {
  705. setTimeout(tick, 400);
  706. }
  707. }
  708. if (isIOSWeChatBrowser()) {
  709. setTimeout(tick, 350);
  710. } else {
  711. tick();
  712. }
  713. }
  714. function showFabToast(msg, ms) {
  715. var tip = String(msg || "").trim();
  716. if (!tip) return;
  717. var el = document.getElementById("openAppToast");
  718. if (el) {
  719. el.textContent = tip;
  720. el.style.display = "block";
  721. if (showFabToast._t) clearTimeout(showFabToast._t);
  722. showFabToast._t = setTimeout(function () {
  723. el.style.display = "none";
  724. }, ms || 2800);
  725. }
  726. console.log("[openApp]", tip);
  727. }
  728. function showDownloadTip() {
  729. var msg = "请到应用商店下载「U店在哪」";
  730. if (typeof uni !== "undefined" && typeof uni.showToast === "function") {
  731. uni.showToast({ title: msg, icon: "none", duration: 2500 });
  732. } else {
  733. window.alert(msg);
  734. }
  735. }
  736. function showAppOpenFailTip(msg) {
  737. var tip = msg || "未能打开 App,请确认已安装最新版「U店在哪」。";
  738. if (typeof uni !== "undefined" && typeof uni.showToast === "function") {
  739. uni.showToast({ title: msg, icon: "none", duration: 2800 });
  740. } else if (isWeChatInAppBrowser()) {
  741. window.alert(tip);
  742. } else {
  743. showFabToast(tip);
  744. }
  745. }
  746. function launchAppDeepLink(deepLink) {
  747. try {
  748. var a = document.createElement("a");
  749. a.href = deepLink;
  750. a.setAttribute("target", "_self");
  751. document.body.appendChild(a);
  752. a.click();
  753. document.body.removeChild(a);
  754. } catch (e1) {}
  755. try {
  756. window.location.href = deepLink;
  757. } catch (e2) {}
  758. }
  759. function tryOpenHBuilderAppViaScheme() {
  760. var deepLink = buildAppDeepLink();
  761. if (typeof plus !== "undefined" && plus.runtime) {
  762. var installed = null;
  763. try {
  764. if (typeof plus.runtime.isApplicationExist === "function") {
  765. installed = plus.runtime.isApplicationExist({
  766. pname: APP_ANDROID_PACKAGE,
  767. action: APP_IOS_URL_SCHEME,
  768. });
  769. }
  770. } catch (e) {
  771. console.warn(e);
  772. }
  773. try {
  774. plus.runtime.openURL(deepLink);
  775. } catch (e2) {
  776. console.warn(e2);
  777. if (installed === false) {
  778. showDownloadTip();
  779. }
  780. }
  781. return;
  782. }
  783. var done = false;
  784. function finish() {
  785. if (done) return;
  786. done = true;
  787. document.removeEventListener("visibilitychange", onVis);
  788. window.removeEventListener("pagehide", onHide);
  789. }
  790. function onVis() {
  791. if (document.visibilityState === "hidden") finish();
  792. }
  793. function onHide() {
  794. finish();
  795. }
  796. document.addEventListener("visibilitychange", onVis);
  797. window.addEventListener("pagehide", onHide);
  798. try {
  799. launchAppDeepLink(deepLink);
  800. } catch (e3) {
  801. finish();
  802. showDownloadTip();
  803. return;
  804. }
  805. window.setTimeout(function () {
  806. finish();
  807. }, 3200);
  808. }
  809. function tryOpenApp() {
  810. if (isWeChatInAppBrowser()) {
  811. if (!weChatJssdkConfigured) {
  812. initWeChatOpenLaunchApp(true).then(function (ok) {
  813. if (!ok) {
  814. showAppOpenFailTip(wxInitLastError || "微信唤起 App 初始化失败");
  815. }
  816. });
  817. }
  818. return;
  819. }
  820. tryOpenHBuilderAppViaScheme();
  821. }
  822. if (isWeChatInAppBrowser()) {
  823. document.body.classList.add("is-wechat");
  824. }
  825. bindWeChatLaunchTagEvents();
  826. scheduleWeChatJssdkBootstrap();
  827. document.getElementById("btnContinue").addEventListener("click", tryOpenApp);
  828. render(parsePayload());
  829. })();
  830. </script>
  831. </body>
  832. </html>