shareCheckInUndefined.html 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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>打卡分享</title>
  8. <style>
  9. :root {
  10. --orange: #F58220;
  11. --text: #333333;
  12. --text-secondary: #999999;
  13. --border: #EEEEEE;
  14. --bg: #FFFFFF;
  15. --safe-bottom: env(safe-area-inset-bottom, 0px);
  16. }
  17. * {
  18. margin: 0;
  19. padding: 0;
  20. box-sizing: border-box;
  21. }
  22. html {
  23. font-size: 16px;
  24. -webkit-tap-highlight-color: transparent;
  25. overflow-x: hidden;
  26. }
  27. body {
  28. font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", "Helvetica Neue", sans-serif;
  29. background: var(--bg);
  30. color: var(--text);
  31. padding-bottom: calc(88px + var(--safe-bottom));
  32. min-height: 100vh;
  33. overflow-x: hidden;
  34. }
  35. .hero.hero--empty {
  36. display: flex;
  37. flex-direction: column;
  38. align-items: center;
  39. justify-content: center;
  40. padding: 48px 24px 56px;
  41. min-height: min(52vh, 440px);
  42. background: var(--bg);
  43. }
  44. .hero--empty__illustration {
  45. display: block;
  46. width: 100%;
  47. max-width: 280px;
  48. height: auto;
  49. object-fit: contain;
  50. }
  51. .hero--empty__tip {
  52. padding: 0 16px;
  53. margin-top: 12px;
  54. font-size: 15px;
  55. line-height: 1.5;
  56. color: var(--text-secondary);
  57. text-align: center;
  58. font-weight: 400;
  59. }
  60. .fab-wrap {
  61. position: fixed;
  62. left: 0;
  63. right: 0;
  64. bottom: 0;
  65. z-index: 200;
  66. padding: 12px 24px calc(12px + var(--safe-bottom));
  67. background: linear-gradient(to top, rgba(255, 255, 255, 0.98) 70%, transparent);
  68. pointer-events: none;
  69. }
  70. .fab-dock__slot {
  71. width: 100%;
  72. max-width: 198px;
  73. height: 48px;
  74. margin: 0 auto;
  75. pointer-events: auto;
  76. }
  77. #openApp.fab {
  78. padding: 0 10px;
  79. -webkit-font-smoothing: antialiased;
  80. text-rendering: optimizeLegibility;
  81. }
  82. .fab__brand-clip {
  83. display: block;
  84. flex-shrink: 0;
  85. line-height: 0;
  86. }
  87. .fab__brand-img {
  88. display: block;
  89. height: 26px;
  90. width: auto;
  91. max-width: none;
  92. pointer-events: none;
  93. -webkit-user-drag: none;
  94. }
  95. .fab__label {
  96. font-size: 15px;
  97. line-height: 1;
  98. letter-spacing: 0.04em;
  99. white-space: nowrap;
  100. }
  101. #launch-btn {
  102. display: none;
  103. width: 100%;
  104. height: 48px;
  105. min-height: 48px;
  106. border-radius: 24px;
  107. overflow: hidden;
  108. opacity: 1;
  109. }
  110. body.is-wechat.wx-jssdk-ready #launch-btn {
  111. display: block;
  112. }
  113. body.is-wechat.wx-jssdk-ready #openApp {
  114. display: none !important;
  115. }
  116. #openAppToast {
  117. display: none;
  118. position: fixed;
  119. left: 16px;
  120. right: 16px;
  121. bottom: calc(72px + var(--safe-bottom));
  122. z-index: 10001;
  123. padding: 10px 14px;
  124. font-size: 13px;
  125. line-height: 1.45;
  126. color: #fff;
  127. text-align: center;
  128. background: rgba(0, 0, 0, 0.78);
  129. border-radius: 8px;
  130. pointer-events: none;
  131. word-break: break-all;
  132. }
  133. #openApp {
  134. touch-action: manipulation;
  135. }
  136. .fab-wrap .fab {
  137. pointer-events: auto;
  138. }
  139. .fab {
  140. display: flex;
  141. align-items: center;
  142. justify-content: center;
  143. gap: 4px;
  144. width: 100%;
  145. max-width: 198px;
  146. margin: 0 auto;
  147. height: 48px;
  148. border: none;
  149. border-radius: 24px;
  150. background: var(--orange);
  151. color: #fff;
  152. font-size: 16px;
  153. font-weight: 600;
  154. box-shadow: 0 4px 16px rgba(245, 130, 32, 0.45);
  155. cursor: pointer;
  156. }
  157. .home-indicator {
  158. height: 5px;
  159. background: #000;
  160. border-radius: 3px;
  161. width: 134px;
  162. margin: 8px auto 4px;
  163. opacity: 0.2;
  164. }
  165. </style>
  166. </head>
  167. <body>
  168. <div class="hero hero--empty" role="status" aria-live="polite">
  169. <img class="hero--empty__illustration" id="heroEmptyIllustration" src="images/empty.png" alt="" decoding="async">
  170. <p class="hero--empty__tip" id="heroEmptyTip">内容已删除</p>
  171. </div>
  172. <div id="openAppToast" role="status" aria-live="polite"></div>
  173. <div class="fab-wrap">
  174. <div class="fab-dock__slot">
  175. <!-- 非微信 / 微信 JSSDK 未就绪:scheme 唤起 -->
  176. <button type="button" class="fab" id="openApp" aria-label="APP内打开">
  177. <span class="fab__brand-clip" aria-hidden="true">
  178. <img class="fab__brand-img" src="images/uBtn.svg" alt="" decoding="async">
  179. </span>
  180. <span class="fab__label">APP内打开</span>
  181. </button>
  182. <!-- 微信内 wx.config 成功后:仅此按钮可唤起 App(须用户直接点击) -->
  183. <wx-open-launch-app id="launch-btn" appid="wxf5f1efe3a9f5012e" extinfo="">
  184. <script type="text/wxtag-template">
  185. <style>
  186. .wx-open-app-btn {
  187. display: flex;
  188. align-items: center;
  189. justify-content: center;
  190. gap: 4px;
  191. width: 100%;
  192. height: 48px;
  193. margin: 0;
  194. padding: 0 10px;
  195. border: none;
  196. border-radius: 24px;
  197. background: #F47D1F;
  198. box-shadow: 0 4px 16px rgba(245, 130, 32, 0.45);
  199. color: #fff;
  200. font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Helvetica Neue", sans-serif;
  201. -webkit-font-smoothing: antialiased;
  202. cursor: pointer;
  203. overflow: hidden;
  204. -webkit-tap-highlight-color: transparent;
  205. box-sizing: border-box;
  206. }
  207. .wx-open-app-btn .fab__brand-clip {
  208. display: block;
  209. flex-shrink: 0;
  210. line-height: 0;
  211. }
  212. .wx-open-app-btn .fab__brand-img {
  213. display: block;
  214. height: 26px;
  215. width: auto;
  216. max-width: none;
  217. pointer-events: none;
  218. -webkit-user-drag: none;
  219. }
  220. .wx-open-app-btn .fab__label {
  221. font-size: 15px;
  222. line-height: 1;
  223. letter-spacing: 0.04em;
  224. white-space: nowrap;
  225. }
  226. </style>
  227. <button class="wx-open-app-btn" aria-label="APP内打开">
  228. <span class="fab__brand-clip" aria-hidden="true">
  229. <img class="fab__brand-img" src="https://prod.ailien.shop/h5/HBuilderProjects/images/uBtn.svg" alt="" decoding="async">
  230. </span>
  231. <span class="fab__label">APP内打开</span>
  232. </button>
  233. </script>
  234. </wx-open-launch-app>
  235. </div>
  236. <div class="home-indicator" aria-hidden="true"></div>
  237. </div>
  238. <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
  239. <script>
  240. (function () {
  241. 'use strict';
  242. /**
  243. * businessStatus=99(关店,与 shareIndex.html 一致):点「APP内打开」深链为 shopro://pages/index/login?…,不再进打卡页。
  244. */
  245. var API_BASE = 'https://prod.ailien.shop/alienStore';
  246. /**
  247. * 微信 JSSDK — 与 shareIndex.html 一致
  248. * POST {API_BASE}/wx/getWxConfig,body 传 url(当前页完整地址,不含 #)
  249. */
  250. var WECHAT_MP_APP_ID = 'wx412792c77f47babd';
  251. var WECHAT_OPEN_APP_ID = 'wxf5f1efe3a9f5012e';
  252. var WECHAT_GET_WX_CONFIG_PATH = '/wx/getWxConfig';
  253. var H5_PAGE_BASE_FALLBACK = 'https://prod.ailien.shop/h5/HBuilderProjects/';
  254. var WX_GET_CONFIG_SIGN_URL = H5_PAGE_BASE_FALLBACK + 'shareCheckInUndefined.html';
  255. var WECHAT_JS_SAFE_HOSTS = ['uat.ailien.shop', 'prod.ailien.shop'];
  256. var weChatJssdkConfigured = false;
  257. var wxConfigSignRetriedBaseUrl = false;
  258. var wxInitLastError = '';
  259. var wxJssdkInitPromise = null;
  260. var APP_ANDROID_PACKAGE = 'com.alien.Udianzaizhe';
  261. var APP_DOWNLOAD_URL = 'https://a.app.qq.com/o/simple.jsp?pkgname=com.alien.Udianzaina';
  262. var APP_IOS_DOWNLOAD_URL = 'https://apps.apple.com/cn/app/u店在哪/id6752243565';
  263. var APP_IOS_URL_SCHEME = 'shopro://';
  264. var APP_UNI_STORE_PATH = 'pages/checkIn/index';
  265. function q(name) {
  266. var v = mergeSearchAndHashParams().get(name);
  267. return v == null ? '' : String(v);
  268. }
  269. function showDownloadTip() {
  270. var ua = navigator.userAgent || '';
  271. var isIOS = /iP(hone|od|ad)/i.test(ua) || (navigator.platform === 'MacIntel' && (navigator.maxTouchPoints || 0) > 1);
  272. window.location.href = isIOS ? APP_IOS_DOWNLOAD_URL : APP_DOWNLOAD_URL;
  273. }
  274. function mergeSearchAndHashParams() {
  275. var params = new URLSearchParams();
  276. function ingestAppend(querySlice) {
  277. if (!querySlice) return;
  278. var p = new URLSearchParams(querySlice);
  279. p.forEach(function (val, key) {
  280. params.append(key, val);
  281. });
  282. }
  283. function ingestSet(querySlice) {
  284. if (!querySlice) return;
  285. var p = new URLSearchParams(querySlice);
  286. p.forEach(function (val, key) {
  287. params.set(key, val);
  288. });
  289. }
  290. if (location.search && location.search.length > 1) {
  291. ingestAppend(location.search.slice(1));
  292. }
  293. var hash = location.hash || '';
  294. var qi = hash.indexOf('?');
  295. if (qi >= 0) {
  296. ingestSet(hash.slice(qi + 1));
  297. }
  298. return params;
  299. }
  300. function getRawMergedQueryForAppOpen() {
  301. var rawSearch =
  302. location.search && location.search.length > 1 ? location.search.slice(1) : '';
  303. var hash0 = location.hash || '';
  304. var hqi0 = hash0.indexOf('?');
  305. var rawHashQ = hqi0 >= 0 ? hash0.slice(hqi0 + 1) : '';
  306. if (!rawSearch && !rawHashQ) return '';
  307. if (rawHashQ && rawSearch) {
  308. return rawHashQ;
  309. }
  310. return rawHashQ || rawSearch;
  311. }
  312. /**
  313. * 关店 businessStatus=99:URL(含 hash)带 businessStatus=99 时,唤起 App 进 pages/index/login(与 shareIndex.html isClosedMerchantForAppOpen)。
  314. */
  315. function isClosedMerchantForAppOpen() {
  316. try {
  317. var bs = String(mergeSearchAndHashParams().get('businessStatus') || '').trim();
  318. if (bs === '99' || Number(bs) === 99) return true;
  319. } catch (e0) {}
  320. return false;
  321. }
  322. function getAppUniPathForCheckInShare() {
  323. if (isClosedMerchantForAppOpen()) {
  324. return 'pages/index/login';
  325. }
  326. var p = mergeSearchAndHashParams();
  327. if (!p.has('isShareCheckInSquare')) {
  328. return String(APP_UNI_STORE_PATH || 'pages/checkIn/index').replace(/^\//, '');
  329. }
  330. var v = String(p.get('isShareCheckInSquare') || '').trim().toLowerCase();
  331. if (v === '' || v === '0' || v === 'false' || v === 'no') {
  332. return String(APP_UNI_STORE_PATH || 'pages/checkIn/index').replace(/^\//, '');
  333. }
  334. return 'pages/checkIn/details';
  335. }
  336. /** businessStatus=99(商户关闭):插图与文案;否则为「内容已删除」 */
  337. function applyEmptyHeroState() {
  338. var merged = mergeSearchAndHashParams();
  339. var bsRaw = merged.get('businessStatus');
  340. var bs = bsRaw == null ? '' : String(bsRaw).trim();
  341. var isClosed = Number(bs) === 99 || bs === '99';
  342. var img = document.getElementById('heroEmptyIllustration');
  343. var tip = document.getElementById('heroEmptyTip');
  344. if (!img || !tip) return;
  345. if (isClosed) {
  346. img.src = 'images/storeNone.png';
  347. img.alt = '';
  348. tip.textContent = '抱歉商户已关闭,看看别的吧';
  349. } else {
  350. img.src = 'images/empty.png';
  351. img.alt = '';
  352. tip.textContent = '内容已删除';
  353. }
  354. img.onerror = function () {
  355. this.onerror = null;
  356. this.src = 'images/empty.png';
  357. };
  358. if (weChatJssdkConfigured) {
  359. refreshWxLaunchTagAttrs();
  360. }
  361. }
  362. function buildAppOpenQueryStringMerged() {
  363. var rawQs = getRawMergedQueryForAppOpen();
  364. if (rawQs) {
  365. return '?' + rawQs;
  366. }
  367. var params = mergeSearchAndHashParams();
  368. var sid = params.get('storeId') || params.get('id') || '';
  369. if (sid && !params.has('storeId')) {
  370. params.set('storeId', sid);
  371. }
  372. var qsOut = params.toString();
  373. return qsOut ? ('?' + qsOut) : '';
  374. }
  375. function buildAppUniPageLaunchUrl() {
  376. var path = getAppUniPathForCheckInShare().replace(/^\//, '');
  377. var qs = buildAppOpenQueryStringMerged().replace(/^\?/, '');
  378. return qs ? path + '?' + qs : path;
  379. }
  380. /**
  381. * wx-open-launch-app extinfo:pages/checkIn/index、pages/checkIn/details 或关店 pages/index/login
  382. */
  383. function buildWeChatLaunchExtinfo() {
  384. var uniPage = buildAppUniPageLaunchUrl();
  385. if (uniPage.length <= 1024) return uniPage;
  386. var path = getAppUniPathForCheckInShare().replace(/^\//, '');
  387. var rawQs = getRawMergedQueryForAppOpen();
  388. if (rawQs) {
  389. var packedRaw = path + '?' + rawQs;
  390. if (packedRaw.length <= 1024) return packedRaw;
  391. }
  392. var params = mergeSearchAndHashParams();
  393. var mini = new URLSearchParams();
  394. var storeId = params.get('storeId') || params.get('id') || '';
  395. if (storeId) mini.set('storeId', storeId);
  396. var square = params.get('isShareCheckInSquare');
  397. if (square != null && String(square).trim() !== '') {
  398. mini.set('isShareCheckInSquare', String(square).trim());
  399. }
  400. var bs = params.get('businessStatus');
  401. if (bs != null && String(bs).trim() !== '') {
  402. mini.set('businessStatus', String(bs).trim());
  403. }
  404. var shortPage = path + '?' + mini.toString();
  405. if (shortPage.length <= 1024) return shortPage;
  406. var deep = buildAppDeepLink().replace(/^shopro:\/\//i, '');
  407. return deep.length <= 1024 ? deep : shortPage.slice(0, 1024);
  408. }
  409. function buildAppDeepLink() {
  410. var path = getAppUniPathForCheckInShare().replace(/^\//, '');
  411. var s = buildAppOpenQueryStringMerged();
  412. var root = APP_IOS_URL_SCHEME.replace(/\/$/, '');
  413. if (!s) {
  414. return root + '/' + path;
  415. }
  416. return root + '/' + path + s;
  417. }
  418. function isWeChatInAppBrowser() {
  419. return /MicroMessenger/i.test(navigator.userAgent || '');
  420. }
  421. function getWxShareEntryFrom() {
  422. var from = q('from');
  423. if (from) return String(from).trim().toLowerCase();
  424. try {
  425. var h = location.hash || '';
  426. var qi = h.indexOf('?');
  427. if (qi >= 0) {
  428. var hf = new URLSearchParams(h.slice(qi + 1)).get('from');
  429. if (hf) return String(hf).trim().toLowerCase();
  430. }
  431. } catch (eH) {}
  432. try {
  433. var sf = new URLSearchParams(location.search || '').get('from');
  434. if (sf) return String(sf).trim().toLowerCase();
  435. } catch (eS) {}
  436. return '';
  437. }
  438. function isIOSWeChatBrowser() {
  439. var ua = navigator.userAgent || '';
  440. return /MicroMessenger/i.test(ua) && /iPhone|iPad|iPod/i.test(ua);
  441. }
  442. function logWxEntryDiagnostics() {
  443. if (!isWeChatInAppBrowser()) return;
  444. console.log('[wx-entry]', {
  445. entryFrom: getWxShareEntryFrom() || '(无)',
  446. wxJssdkReady: weChatJssdkConfigured,
  447. signUrl: getWxConfigSignUrl()
  448. });
  449. }
  450. function readQueryParam(name) {
  451. return q(name);
  452. }
  453. function isWxDebugOn() {
  454. return readQueryParam('wxDebug') === '1';
  455. }
  456. function isWxForceDebug() {
  457. return readQueryParam('wxForce') === '1';
  458. }
  459. function isWxConfigOnClickDebug() {
  460. return readQueryParam('wxConfigOnClick') === '1';
  461. }
  462. function isWxPcAutoDebugHost() {
  463. var h = (location.hostname || '').toLowerCase();
  464. if (h === 'localhost' || h === '127.0.0.1') return true;
  465. for (var i = 0; i < WECHAT_JS_SAFE_HOSTS.length; i++) {
  466. if (WECHAT_JS_SAFE_HOSTS[i] === h) return true;
  467. }
  468. return false;
  469. }
  470. function isWxPcBrowser() {
  471. return !isWeChatInAppBrowser();
  472. }
  473. function shouldInitWeChatJssdkOnLoad() {
  474. if (isWeChatInAppBrowser()) return true;
  475. return isWxForceDebug() || isWxPcAutoDebugHost();
  476. }
  477. function shouldFetchWxConfig(fromUserClick) {
  478. if (isWeChatInAppBrowser()) return true;
  479. if (fromUserClick) return true;
  480. return isWxForceDebug() || isWxPcAutoDebugHost();
  481. }
  482. function getWxHtmlUrl() {
  483. var forced = String(q('wxSignUrl') || '').trim();
  484. if (forced) return forced.split('#')[0];
  485. return String(location.href || '').split('#')[0];
  486. }
  487. function getWxSignPageUrlForApi() {
  488. var htmlUrl = getWxHtmlUrl();
  489. if (htmlUrl && /^https?:\/\//i.test(htmlUrl)) return htmlUrl;
  490. return WX_GET_CONFIG_SIGN_URL;
  491. }
  492. function getWxHtmlUrlBase() {
  493. var htmlUrl = getWxSignPageUrlForApi();
  494. try {
  495. var u = new URL(htmlUrl);
  496. return u.origin + u.pathname;
  497. } catch (eU) {
  498. return WX_GET_CONFIG_SIGN_URL;
  499. }
  500. }
  501. function getWxConfigSignUrl() {
  502. if (String(q('wxSignBaseOnly') || '') === '1') return getWxHtmlUrlBase();
  503. return getWxSignPageUrlForApi();
  504. }
  505. function getWxGetConfigApiUrl() {
  506. return API_BASE.replace(/\/$/, '') + WECHAT_GET_WX_CONFIG_PATH;
  507. }
  508. function buildWxGetConfigRequestBody(htmlUrl) {
  509. return {
  510. url: String(htmlUrl || '').split('#')[0].trim()
  511. };
  512. }
  513. function resolveWxConfigAppIdFromSignData(d) {
  514. if (!d || typeof d !== 'object') return '';
  515. var mp =
  516. d.mpAppId ||
  517. d.mpAppid ||
  518. d.officialAppId ||
  519. d.gzhAppId ||
  520. d.serviceAppId;
  521. if (mp != null && String(mp).trim() !== '') return String(mp).trim();
  522. var fromQuery = String(q('wxMpAppId') || WECHAT_MP_APP_ID || '').trim();
  523. if (fromQuery) return fromQuery;
  524. var raw = d.appId || d.appid || d.wxAppId;
  525. return raw != null && String(raw).trim() !== '' ? String(raw).trim() : '';
  526. }
  527. function normalizeWxJssdkSignPayload(res, signUrlUsed) {
  528. if (!res || typeof res !== 'object') return null;
  529. var d = res.data != null && typeof res.data === 'object' ? res.data : res;
  530. if (!d || typeof d !== 'object') return null;
  531. var appId = resolveWxConfigAppIdFromSignData(d);
  532. var timestamp = d.timestamp != null ? d.timestamp : d.timeStamp;
  533. var nonceStr =
  534. d.nonceStr != null && String(d.nonceStr) !== ''
  535. ? d.nonceStr
  536. : d.noncestr != null && String(d.noncestr) !== ''
  537. ? d.noncestr
  538. : d.nonce;
  539. var signature = d.signature || d.sign;
  540. if (!appId || timestamp == null || nonceStr == null || nonceStr === '' || !signature) {
  541. return null;
  542. }
  543. return {
  544. appId: String(appId),
  545. timestamp: Number(timestamp),
  546. nonceStr: String(nonceStr),
  547. signature: String(signature),
  548. signUrl: String(signUrlUsed || '')
  549. .split('#')[0]
  550. .trim()
  551. };
  552. }
  553. function getWxErrMsg(err) {
  554. if (!err) return '';
  555. if (err.errMsg) return String(err.errMsg);
  556. if (typeof err === 'string') return err;
  557. try {
  558. return JSON.stringify(err);
  559. } catch (e) {
  560. return String(err);
  561. }
  562. }
  563. function formatWxConfigErrorTip(err, signPageUrl) {
  564. var errMsg = getWxErrMsg(err);
  565. var tip = '微信 JSSDK 配置失败';
  566. if (/invalid signature/i.test(errMsg)) {
  567. return (
  568. tip +
  569. ':签名无效。请核对:①后端用与前端相同的 url 签名;②url=' +
  570. (signPageUrl || getWxHtmlUrl()) +
  571. ';③nonceStr/timestamp 与接口返回一致;④appId=' +
  572. WECHAT_MP_APP_ID
  573. );
  574. }
  575. if (/require\s*subscribe/i.test(errMsg)) {
  576. return tip + ':' + errMsg + '(服务号需用户已关注)';
  577. }
  578. return errMsg ? tip + ':' + errMsg : tip;
  579. }
  580. function setWxInitError(msg) {
  581. wxInitLastError = String(msg || '').trim();
  582. if (wxInitLastError) console.warn('[wx]', wxInitLastError);
  583. }
  584. function requestWeChatJssdkSignAndConfig(htmlUrlOptional) {
  585. var htmlUrl = String(
  586. htmlUrlOptional != null ? htmlUrlOptional : getWxConfigSignUrl()
  587. )
  588. .split('#')[0]
  589. .trim();
  590. if (!htmlUrl || !/^https?:\/\//i.test(htmlUrl)) {
  591. htmlUrl = WX_GET_CONFIG_SIGN_URL;
  592. }
  593. if (isWxDebugOn()) {
  594. try {
  595. window.alert('htmlUrl(签名用):\n' + htmlUrl);
  596. } catch (eDbg) {}
  597. }
  598. var requestUrl = getWxGetConfigApiUrl();
  599. var requestBody = buildWxGetConfigRequestBody(htmlUrl);
  600. console.log('[wx] htmlUrl=', htmlUrl);
  601. console.log('[wx] POST getWxConfig →', requestUrl, requestBody);
  602. return fetch(requestUrl, {
  603. method: 'POST',
  604. mode: 'cors',
  605. credentials: 'omit',
  606. headers: {
  607. Accept: 'application/json',
  608. 'Content-Type': 'application/json;charset=UTF-8'
  609. },
  610. body: JSON.stringify(requestBody)
  611. })
  612. .then(function (r) {
  613. if (r.ok) return r.json();
  614. return r
  615. .text()
  616. .catch(function () {
  617. return '';
  618. })
  619. .then(function (text) {
  620. var hint = '';
  621. try {
  622. var j = JSON.parse(text);
  623. hint = j.msg || j.message || '';
  624. } catch (eP) {
  625. if (text) hint = text.slice(0, 120);
  626. }
  627. throw new Error('getWxConfig HTTP ' + r.status + (hint ? ':' + hint : ''));
  628. });
  629. })
  630. .then(function (res) {
  631. if (res && res.code != null) {
  632. var c = Number(res.code);
  633. if (c !== 200 && c !== 0 && res.success !== true) {
  634. throw new Error(res.msg || res.message || 'getWxConfig code ' + c);
  635. }
  636. }
  637. var sign = normalizeWxJssdkSignPayload(res, htmlUrl);
  638. if (!sign) {
  639. console.warn('[wx] getWxConfig 响应字段不全', res, 'htmlUrl=', htmlUrl);
  640. throw new Error(
  641. 'getWxConfig 缺少 appId/timestamp/nonceStr/signature(见控制台)'
  642. );
  643. }
  644. if (sign.appId !== WECHAT_MP_APP_ID) {
  645. console.warn(
  646. '[wx] 后端 appId=' + sign.appId + ',期望服务号 ' + WECHAT_MP_APP_ID
  647. );
  648. }
  649. if (typeof wx === 'undefined') {
  650. if (isWxPcBrowser()) {
  651. console.log('[wx] PC getWxConfig 成功(无 jweixin)', sign);
  652. return true;
  653. }
  654. setWxInitError('jweixin.js 未加载');
  655. return false;
  656. }
  657. return applyWxConfigFromSign(sign, htmlUrl);
  658. });
  659. }
  660. function applyWxConfigFromSign(sign, htmlUrl) {
  661. var wxConfigParams = {
  662. debug: isWxDebugOn(),
  663. appId: String(sign.appId),
  664. timestamp: sign.timestamp,
  665. nonceStr: String(sign.nonceStr),
  666. signature: String(sign.signature),
  667. jsApiList: [],
  668. openTagList: ['wx-open-launch-app']
  669. };
  670. return new Promise(function (resolve) {
  671. wx.config(wxConfigParams);
  672. wx.ready(function () {
  673. weChatJssdkConfigured = true;
  674. document.body.classList.add('wx-jssdk-ready');
  675. refreshWxLaunchTagAttrs();
  676. console.log('[wx.config] ready, htmlUrl=', htmlUrl);
  677. resolve(true);
  678. });
  679. wx.error(function (err) {
  680. weChatJssdkConfigured = false;
  681. document.body.classList.remove('wx-jssdk-ready');
  682. wxJssdkInitPromise = null;
  683. var errMsg = getWxErrMsg(err);
  684. setWxInitError(formatWxConfigErrorTip(err, htmlUrl));
  685. if (isWxDebugOn()) window.alert(wxInitLastError);
  686. console.warn('[wx.config]', errMsg, 'htmlUrl=', htmlUrl);
  687. var baseUrl = getWxHtmlUrlBase();
  688. var fullUrl = getWxSignPageUrlForApi();
  689. if (
  690. !/invalid signature/i.test(errMsg) ||
  691. wxConfigSignRetriedBaseUrl ||
  692. baseUrl === fullUrl ||
  693. htmlUrl === baseUrl
  694. ) {
  695. resolve(false);
  696. return;
  697. }
  698. wxConfigSignRetriedBaseUrl = true;
  699. requestWeChatJssdkSignAndConfig(baseUrl).then(resolve);
  700. });
  701. });
  702. }
  703. function refreshWxLaunchTagAttrs() {
  704. var tag = document.getElementById('launch-btn');
  705. if (!tag) return;
  706. try {
  707. tag.setAttribute('appid', WECHAT_OPEN_APP_ID);
  708. tag.setAttribute('extinfo', buildWeChatLaunchExtinfo());
  709. } catch (eA) {}
  710. }
  711. function bindWeChatLaunchTagEvents() {
  712. var tag = document.getElementById('launch-btn');
  713. if (!tag || tag._wxLaunchBound) return;
  714. tag._wxLaunchBound = true;
  715. refreshWxLaunchTagAttrs();
  716. tag.addEventListener('launch', function () {
  717. console.log('[wx-open-launch-app] launch ok');
  718. // showFabToast('正在打开 U店在哪…');
  719. });
  720. tag.addEventListener('error', function (e) {
  721. var detail = e && e.detail;
  722. var errMsg =
  723. detail && detail.errMsg
  724. ? String(detail.errMsg)
  725. : detail && detail.errmsg
  726. ? String(detail.errmsg)
  727. : '';
  728. console.warn('[wx-open-launch-app]', detail);
  729. if (/launch:fail/i.test(errMsg) && !getWxShareEntryFrom()) {
  730. showDownloadTip();
  731. } else {
  732. showAppOpenFailTip(
  733. errMsg
  734. ? '未能打开 App:' + errMsg
  735. : '未能打开 App,请确认已安装最新版「U店在哪」'
  736. );
  737. }
  738. });
  739. }
  740. function initWeChatOpenLaunchApp(fromUserClick) {
  741. if (!shouldFetchWxConfig(!!fromUserClick)) {
  742. console.log('[wx] 未调用 getWxConfig');
  743. return Promise.resolve(false);
  744. }
  745. if (isWxPcBrowser()) {
  746. console.warn(
  747. '[wx] PC:请求 getWxConfig' + (fromUserClick ? '(按钮)' : '(进页)')
  748. );
  749. }
  750. if (wxJssdkInitPromise && !fromUserClick) return wxJssdkInitPromise;
  751. wxConfigSignRetriedBaseUrl = false;
  752. wxInitLastError = '';
  753. var htmlUrl = getWxConfigSignUrl();
  754. bindWeChatLaunchTagEvents();
  755. wxJssdkInitPromise = requestWeChatJssdkSignAndConfig(htmlUrl)
  756. .then(function (ok) {
  757. if (ok === true) return true;
  758. if (!wxInitLastError) {
  759. setWxInitError('wx.config 失败,可加 ?wxDebug=1 查看 htmlUrl');
  760. }
  761. return false;
  762. })
  763. .catch(function (e) {
  764. var msg = e && e.message ? e.message : 'getWxConfig 请求失败';
  765. setWxInitError(msg);
  766. console.warn('[wx] requestWeChatJssdkSignAndConfig failed', msg, 'htmlUrl=', htmlUrl);
  767. if (isWeChatInAppBrowser() && isWxDebugOn()) window.alert(msg);
  768. return false;
  769. })
  770. .finally(function () {
  771. if (!weChatJssdkConfigured) wxJssdkInitPromise = null;
  772. });
  773. return wxJssdkInitPromise;
  774. }
  775. /** 进页即尝试拉签名;jweixin 晚到时自动重试 */
  776. function scheduleWeChatJssdkBootstrap() {
  777. if (!shouldInitWeChatJssdkOnLoad()) return;
  778. var attempts = 0;
  779. function tick() {
  780. attempts += 1;
  781. if (weChatJssdkConfigured) return;
  782. initWeChatOpenLaunchApp();
  783. if (!weChatJssdkConfigured && attempts < 8 && typeof wx === 'undefined') {
  784. setTimeout(tick, 400);
  785. }
  786. }
  787. if (isIOSWeChatBrowser()) {
  788. setTimeout(tick, 350);
  789. } else {
  790. tick();
  791. }
  792. }
  793. function showFabToast(msg, ms) {
  794. var tip = String(msg || '').trim();
  795. if (!tip) return;
  796. var el = document.getElementById('openAppToast');
  797. if (el) {
  798. el.textContent = tip;
  799. el.style.display = 'block';
  800. if (showFabToast._t) clearTimeout(showFabToast._t);
  801. showFabToast._t = setTimeout(function () {
  802. el.style.display = 'none';
  803. }, ms || 2800);
  804. }
  805. console.log('[openApp]', tip);
  806. }
  807. function showAppOpenFailTip(msg) {
  808. var tip = msg || '未能打开 App,请确认已安装最新版「U店在哪」。';
  809. if (typeof uni !== 'undefined' && typeof uni.showToast === 'function') {
  810. uni.showToast({ title: tip, icon: 'none', duration: 2800 });
  811. } else if (isWeChatInAppBrowser()) {
  812. window.alert(tip);
  813. } else {
  814. console.warn('[openApp]', tip);
  815. }
  816. }
  817. function tryFetchWxConfigOnPcClick() {
  818. if (!isWxPcBrowser()) return Promise.resolve(false);
  819. showFabToast('正在请求 getWxConfig…');
  820. wxJssdkInitPromise = null;
  821. return initWeChatOpenLaunchApp(true).then(function (ok) {
  822. if (ok) {
  823. showFabToast(
  824. weChatJssdkConfigured
  825. ? 'getWxConfig 成功,wx.config 已就绪'
  826. : 'getWxConfig 成功'
  827. );
  828. } else {
  829. showFabToast(wxInitLastError || 'getWxConfig 失败');
  830. }
  831. return ok;
  832. });
  833. }
  834. function launchAppDeepLink(deepLink) {
  835. try {
  836. var a = document.createElement('a');
  837. a.href = deepLink;
  838. a.setAttribute('target', '_self');
  839. document.body.appendChild(a);
  840. a.click();
  841. document.body.removeChild(a);
  842. } catch (e1) {}
  843. try {
  844. window.location.href = deepLink;
  845. } catch (e2) {}
  846. }
  847. function tryOpenHBuilderApp() {
  848. /* 微信内须直接点击 wx-open-launch-app,scheme 会被拦截 */
  849. if (isWeChatInAppBrowser()) return;
  850. tryFetchWxConfigOnPcClick().then(function () {
  851. if (isWxConfigOnClickDebug() && !isWxForceDebug() && !isWxPcAutoDebugHost()) {
  852. return;
  853. }
  854. tryOpenHBuilderAppViaScheme();
  855. });
  856. }
  857. function tryOpenHBuilderAppViaScheme() {
  858. // showFabToast('正在打开 U店在哪…');
  859. var deepLink = buildAppDeepLink();
  860. if (typeof plus !== 'undefined' && plus.runtime) {
  861. var installed = null;
  862. try {
  863. if (typeof plus.runtime.isApplicationExist === 'function') {
  864. installed = plus.runtime.isApplicationExist({
  865. pname: APP_ANDROID_PACKAGE,
  866. action: APP_IOS_URL_SCHEME
  867. });
  868. }
  869. } catch (e) {
  870. console.warn(e);
  871. }
  872. if (installed === false) {
  873. showDownloadTip();
  874. return;
  875. }
  876. try {
  877. plus.runtime.openURL(deepLink);
  878. } catch (e2) {
  879. console.warn(e2);
  880. showDownloadTip();
  881. }
  882. return;
  883. }
  884. var done = false;
  885. function finish() {
  886. if (done) return;
  887. done = true;
  888. document.removeEventListener('visibilitychange', onVis);
  889. window.removeEventListener('pagehide', onHide);
  890. }
  891. function onVis() {
  892. if (document.visibilityState === 'hidden') finish();
  893. }
  894. function onHide() {
  895. finish();
  896. }
  897. document.addEventListener('visibilitychange', onVis);
  898. window.addEventListener('pagehide', onHide);
  899. try {
  900. launchAppDeepLink(deepLink);
  901. } catch (e3) {
  902. finish();
  903. showDownloadTip();
  904. return;
  905. }
  906. window.setTimeout(function () {
  907. if (done) return;
  908. finish();
  909. showDownloadTip();
  910. }, 2600);
  911. }
  912. function boot() {
  913. var launchTag = document.getElementById('launch-btn');
  914. if (launchTag) launchTag.setAttribute('appid', WECHAT_OPEN_APP_ID);
  915. bindWeChatLaunchTagEvents();
  916. if (isWeChatInAppBrowser()) {
  917. document.body.classList.add('is-wechat');
  918. logWxEntryDiagnostics();
  919. }
  920. if (shouldInitWeChatJssdkOnLoad()) {
  921. if (isWxPcBrowser() && (isWxForceDebug() || isWxPcAutoDebugHost())) {
  922. document.body.classList.add('wx-pc-debug');
  923. }
  924. scheduleWeChatJssdkBootstrap();
  925. }
  926. document.addEventListener('WeixinOpenTagsError', function (e) {
  927. console.warn('[WeixinOpenTagsError]', e && e.detail);
  928. });
  929. applyEmptyHeroState();
  930. if (isWeChatInAppBrowser()) {
  931. var openBtnWx = document.getElementById('openApp');
  932. if (openBtnWx) {
  933. openBtnWx.addEventListener('click', function () {
  934. if (!weChatJssdkConfigured) {
  935. showFabToast(
  936. wxInitLastError || '微信 SDK 初始化中,请稍候再点底部按钮'
  937. );
  938. return;
  939. }
  940. });
  941. }
  942. } else {
  943. var openBtn = document.getElementById('openApp');
  944. if (openBtn) {
  945. openBtn.addEventListener('click', tryOpenHBuilderApp);
  946. }
  947. }
  948. }
  949. if (document.readyState === 'complete') {
  950. boot();
  951. } else {
  952. window.onload = boot;
  953. }
  954. })();
  955. </script>
  956. </body>
  957. </html>