| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044 |
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">
- <meta name="format-detection" content="telephone=no">
- <title>打卡分享</title>
- <style>
- :root {
- --orange: #F58220;
- --text: #333333;
- --text-secondary: #999999;
- --border: #EEEEEE;
- --bg: #FFFFFF;
- --safe-bottom: env(safe-area-inset-bottom, 0px);
- }
- * {
- margin: 0;
- padding: 0;
- box-sizing: border-box;
- }
- html {
- font-size: 16px;
- -webkit-tap-highlight-color: transparent;
- overflow-x: hidden;
- }
- body {
- font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", "Helvetica Neue", sans-serif;
- background: var(--bg);
- color: var(--text);
- padding-bottom: calc(88px + var(--safe-bottom));
- min-height: 100vh;
- overflow-x: hidden;
- }
- .hero.hero--empty {
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- padding: 48px 24px 56px;
- min-height: min(52vh, 440px);
- background: var(--bg);
- }
- .hero--empty__illustration {
- display: block;
- width: 100%;
- max-width: 280px;
- height: auto;
- object-fit: contain;
- }
- .hero--empty__tip {
- padding: 0 16px;
- margin-top: 12px;
- font-size: 15px;
- line-height: 1.5;
- color: var(--text-secondary);
- text-align: center;
- font-weight: 400;
- }
- .fab-wrap {
- position: fixed;
- left: 0;
- right: 0;
- bottom: 0;
- z-index: 200;
- padding: 12px 24px calc(12px + var(--safe-bottom));
- background: linear-gradient(to top, rgba(255, 255, 255, 0.98) 70%, transparent);
- pointer-events: none;
- }
- .fab-dock__slot {
- width: 100%;
- max-width: 198px;
- height: 48px;
- margin: 0 auto;
- pointer-events: auto;
- }
- #openApp.fab {
- padding: 0 10px;
- -webkit-font-smoothing: antialiased;
- text-rendering: optimizeLegibility;
- }
- .fab__brand-clip {
- display: block;
- flex-shrink: 0;
- line-height: 0;
- }
- .fab__brand-img {
- display: block;
- height: 26px;
- width: auto;
- max-width: none;
- pointer-events: none;
- -webkit-user-drag: none;
- }
- .fab__label {
- font-size: 15px;
- line-height: 1;
- letter-spacing: 0.04em;
- white-space: nowrap;
- }
- #launch-btn {
- display: none;
- width: 100%;
- height: 48px;
- min-height: 48px;
- border-radius: 24px;
- overflow: hidden;
- opacity: 1;
- }
- body.is-wechat.wx-jssdk-ready #launch-btn {
- display: block;
- }
- body.is-wechat.wx-jssdk-ready #openApp {
- display: none !important;
- }
- #openAppToast {
- display: none;
- position: fixed;
- left: 16px;
- right: 16px;
- bottom: calc(72px + var(--safe-bottom));
- z-index: 10001;
- padding: 10px 14px;
- font-size: 13px;
- line-height: 1.45;
- color: #fff;
- text-align: center;
- background: rgba(0, 0, 0, 0.78);
- border-radius: 8px;
- pointer-events: none;
- word-break: break-all;
- }
- #openApp {
- touch-action: manipulation;
- }
- .fab-wrap .fab {
- pointer-events: auto;
- }
- .fab {
- display: flex;
- align-items: center;
- justify-content: center;
- gap: 4px;
- width: 100%;
- max-width: 198px;
- margin: 0 auto;
- height: 48px;
- border: none;
- border-radius: 24px;
- background: var(--orange);
- color: #fff;
- font-size: 16px;
- font-weight: 600;
- box-shadow: 0 4px 16px rgba(245, 130, 32, 0.45);
- cursor: pointer;
- }
- .home-indicator {
- height: 5px;
- background: #000;
- border-radius: 3px;
- width: 134px;
- margin: 8px auto 4px;
- opacity: 0.2;
- }
- </style>
- </head>
- <body>
- <div class="hero hero--empty" role="status" aria-live="polite">
- <img class="hero--empty__illustration" id="heroEmptyIllustration" src="images/empty.png" alt="" decoding="async">
- <p class="hero--empty__tip" id="heroEmptyTip">内容已删除</p>
- </div>
- <div id="openAppToast" role="status" aria-live="polite"></div>
- <div class="fab-wrap">
- <div class="fab-dock__slot">
- <!-- 非微信 / 微信 JSSDK 未就绪:scheme 唤起 -->
- <button type="button" class="fab" id="openApp" aria-label="APP内打开">
- <span class="fab__brand-clip" aria-hidden="true">
- <img class="fab__brand-img" src="images/uBtn.svg" alt="" decoding="async">
- </span>
- <span class="fab__label">APP内打开</span>
- </button>
- <!-- 微信内 wx.config 成功后:仅此按钮可唤起 App(须用户直接点击) -->
- <wx-open-launch-app id="launch-btn" appid="wxf5f1efe3a9f5012e" extinfo="">
- <script type="text/wxtag-template">
- <style>
- .wx-open-app-btn {
- display: flex;
- align-items: center;
- justify-content: center;
- gap: 4px;
- width: 100%;
- height: 48px;
- margin: 0;
- padding: 0 10px;
- border: none;
- border-radius: 24px;
- background: #F47D1F;
- box-shadow: 0 4px 16px rgba(245, 130, 32, 0.45);
- color: #fff;
- font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Helvetica Neue", sans-serif;
- -webkit-font-smoothing: antialiased;
- cursor: pointer;
- overflow: hidden;
- -webkit-tap-highlight-color: transparent;
- box-sizing: border-box;
- }
- .wx-open-app-btn .fab__brand-clip {
- display: block;
- flex-shrink: 0;
- line-height: 0;
- }
- .wx-open-app-btn .fab__brand-img {
- display: block;
- height: 26px;
- width: auto;
- max-width: none;
- pointer-events: none;
- -webkit-user-drag: none;
- }
- .wx-open-app-btn .fab__label {
- font-size: 15px;
- line-height: 1;
- letter-spacing: 0.04em;
- white-space: nowrap;
- }
- </style>
- <button class="wx-open-app-btn" aria-label="APP内打开">
- <span class="fab__brand-clip" aria-hidden="true">
- <img class="fab__brand-img" src="https://prod.ailien.shop/h5/HBuilderProjects/images/uBtn.svg" alt="" decoding="async">
- </span>
- <span class="fab__label">APP内打开</span>
- </button>
- </script>
- </wx-open-launch-app>
- </div>
- <div class="home-indicator" aria-hidden="true"></div>
- </div>
- <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
- <script>
- (function () {
- 'use strict';
- /**
- * businessStatus=99(关店,与 shareIndex.html 一致):点「APP内打开」深链为 shopro://pages/index/login?…,不再进打卡页。
- */
- var API_BASE = 'https://prod.ailien.shop/alienStore';
- /**
- * 微信 JSSDK — 与 shareIndex.html 一致
- * POST {API_BASE}/wx/getWxConfig,body 传 url(当前页完整地址,不含 #)
- */
- var WECHAT_MP_APP_ID = 'wx412792c77f47babd';
- var WECHAT_OPEN_APP_ID = 'wxf5f1efe3a9f5012e';
- var WECHAT_GET_WX_CONFIG_PATH = '/wx/getWxConfig';
- var H5_PAGE_BASE_FALLBACK = 'https://prod.ailien.shop/h5/HBuilderProjects/';
- var WX_GET_CONFIG_SIGN_URL = H5_PAGE_BASE_FALLBACK + 'shareCheckInUndefined.html';
- var WECHAT_JS_SAFE_HOSTS = ['uat.ailien.shop', 'prod.ailien.shop'];
- var weChatJssdkConfigured = false;
- var wxConfigSignRetriedBaseUrl = false;
- var wxInitLastError = '';
- var wxJssdkInitPromise = null;
- var APP_ANDROID_PACKAGE = 'com.alien.Udianzaizhe';
- var APP_IOS_URL_SCHEME = 'shopro://';
- var APP_UNI_STORE_PATH = 'pages/checkIn/index';
- function q(name) {
- var v = mergeSearchAndHashParams().get(name);
- return v == null ? '' : String(v);
- }
- function showDownloadTip() {
- var msg = '请到应用商店下载';
- if (typeof uni !== 'undefined' && typeof uni.showToast === 'function') {
- uni.showToast({ title: msg, icon: 'none', duration: 2500 });
- } else {
- window.alert(msg);
- }
- }
- function mergeSearchAndHashParams() {
- var params = new URLSearchParams();
- function ingestAppend(querySlice) {
- if (!querySlice) return;
- var p = new URLSearchParams(querySlice);
- p.forEach(function (val, key) {
- params.append(key, val);
- });
- }
- function ingestSet(querySlice) {
- if (!querySlice) return;
- var p = new URLSearchParams(querySlice);
- p.forEach(function (val, key) {
- params.set(key, val);
- });
- }
- if (location.search && location.search.length > 1) {
- ingestAppend(location.search.slice(1));
- }
- var hash = location.hash || '';
- var qi = hash.indexOf('?');
- if (qi >= 0) {
- ingestSet(hash.slice(qi + 1));
- }
- return params;
- }
- function getRawMergedQueryForAppOpen() {
- var rawSearch =
- location.search && location.search.length > 1 ? location.search.slice(1) : '';
- var hash0 = location.hash || '';
- var hqi0 = hash0.indexOf('?');
- var rawHashQ = hqi0 >= 0 ? hash0.slice(hqi0 + 1) : '';
- if (!rawSearch && !rawHashQ) return '';
- if (rawHashQ && rawSearch) {
- return rawHashQ;
- }
- return rawHashQ || rawSearch;
- }
- /**
- * 关店 businessStatus=99:URL(含 hash)带 businessStatus=99 时,唤起 App 进 pages/index/login(与 shareIndex.html isClosedMerchantForAppOpen)。
- */
- function isClosedMerchantForAppOpen() {
- try {
- var bs = String(mergeSearchAndHashParams().get('businessStatus') || '').trim();
- if (bs === '99' || Number(bs) === 99) return true;
- } catch (e0) {}
- return false;
- }
- function getAppUniPathForCheckInShare() {
- if (isClosedMerchantForAppOpen()) {
- return 'pages/index/login';
- }
- var p = mergeSearchAndHashParams();
- if (!p.has('isShareCheckInSquare')) {
- return String(APP_UNI_STORE_PATH || 'pages/checkIn/index').replace(/^\//, '');
- }
- var v = String(p.get('isShareCheckInSquare') || '').trim().toLowerCase();
- if (v === '' || v === '0' || v === 'false' || v === 'no') {
- return String(APP_UNI_STORE_PATH || 'pages/checkIn/index').replace(/^\//, '');
- }
- return 'pages/checkIn/details';
- }
- /** businessStatus=99(商户关闭):插图与文案;否则为「内容已删除」 */
- function applyEmptyHeroState() {
- var merged = mergeSearchAndHashParams();
- var bsRaw = merged.get('businessStatus');
- var bs = bsRaw == null ? '' : String(bsRaw).trim();
- var isClosed = Number(bs) === 99 || bs === '99';
- var img = document.getElementById('heroEmptyIllustration');
- var tip = document.getElementById('heroEmptyTip');
- if (!img || !tip) return;
- if (isClosed) {
- img.src = 'images/storeNone.png';
- img.alt = '';
- tip.textContent = '抱歉商户已关闭,看看别的吧';
- } else {
- img.src = 'images/empty.png';
- img.alt = '';
- tip.textContent = '内容已删除';
- }
- img.onerror = function () {
- this.onerror = null;
- this.src = 'images/empty.png';
- };
- if (weChatJssdkConfigured) {
- refreshWxLaunchTagAttrs();
- }
- }
- function buildAppOpenQueryStringMerged() {
- var rawQs = getRawMergedQueryForAppOpen();
- if (rawQs) {
- return '?' + rawQs;
- }
- var params = mergeSearchAndHashParams();
- var sid = params.get('storeId') || params.get('id') || '';
- if (sid && !params.has('storeId')) {
- params.set('storeId', sid);
- }
- var qsOut = params.toString();
- return qsOut ? ('?' + qsOut) : '';
- }
- function buildAppUniPageLaunchUrl() {
- var path = getAppUniPathForCheckInShare().replace(/^\//, '');
- var qs = buildAppOpenQueryStringMerged().replace(/^\?/, '');
- return qs ? path + '?' + qs : path;
- }
- /**
- * wx-open-launch-app extinfo:pages/checkIn/index、pages/checkIn/details 或关店 pages/index/login
- */
- function buildWeChatLaunchExtinfo() {
- var uniPage = buildAppUniPageLaunchUrl();
- if (uniPage.length <= 1024) return uniPage;
- var path = getAppUniPathForCheckInShare().replace(/^\//, '');
- var rawQs = getRawMergedQueryForAppOpen();
- if (rawQs) {
- var packedRaw = path + '?' + rawQs;
- if (packedRaw.length <= 1024) return packedRaw;
- }
- var params = mergeSearchAndHashParams();
- var mini = new URLSearchParams();
- var storeId = params.get('storeId') || params.get('id') || '';
- if (storeId) mini.set('storeId', storeId);
- var square = params.get('isShareCheckInSquare');
- if (square != null && String(square).trim() !== '') {
- mini.set('isShareCheckInSquare', String(square).trim());
- }
- var bs = params.get('businessStatus');
- if (bs != null && String(bs).trim() !== '') {
- mini.set('businessStatus', String(bs).trim());
- }
- var shortPage = path + '?' + mini.toString();
- if (shortPage.length <= 1024) return shortPage;
- var deep = buildAppDeepLink().replace(/^shopro:\/\//i, '');
- return deep.length <= 1024 ? deep : shortPage.slice(0, 1024);
- }
- function buildAppDeepLink() {
- var path = getAppUniPathForCheckInShare().replace(/^\//, '');
- var s = buildAppOpenQueryStringMerged();
- var root = APP_IOS_URL_SCHEME.replace(/\/$/, '');
- if (!s) {
- return root + '/' + path;
- }
- return root + '/' + path + s;
- }
- function isWeChatInAppBrowser() {
- return /MicroMessenger/i.test(navigator.userAgent || '');
- }
- function getWxShareEntryFrom() {
- var from = q('from');
- if (from) return String(from).trim().toLowerCase();
- try {
- var h = location.hash || '';
- var qi = h.indexOf('?');
- if (qi >= 0) {
- var hf = new URLSearchParams(h.slice(qi + 1)).get('from');
- if (hf) return String(hf).trim().toLowerCase();
- }
- } catch (eH) {}
- try {
- var sf = new URLSearchParams(location.search || '').get('from');
- if (sf) return String(sf).trim().toLowerCase();
- } catch (eS) {}
- return '';
- }
- function isIOSWeChatBrowser() {
- var ua = navigator.userAgent || '';
- return /MicroMessenger/i.test(ua) && /iPhone|iPad|iPod/i.test(ua);
- }
- function logWxEntryDiagnostics() {
- if (!isWeChatInAppBrowser()) return;
- console.log('[wx-entry]', {
- entryFrom: getWxShareEntryFrom() || '(无)',
- wxJssdkReady: weChatJssdkConfigured,
- signUrl: getWxConfigSignUrl()
- });
- }
- function readQueryParam(name) {
- return q(name);
- }
- function isWxDebugOn() {
- return readQueryParam('wxDebug') === '1';
- }
- function isWxForceDebug() {
- return readQueryParam('wxForce') === '1';
- }
- function isWxConfigOnClickDebug() {
- return readQueryParam('wxConfigOnClick') === '1';
- }
- function isWxPcAutoDebugHost() {
- var h = (location.hostname || '').toLowerCase();
- if (h === 'localhost' || h === '127.0.0.1') return true;
- for (var i = 0; i < WECHAT_JS_SAFE_HOSTS.length; i++) {
- if (WECHAT_JS_SAFE_HOSTS[i] === h) return true;
- }
- return false;
- }
- function isWxPcBrowser() {
- return !isWeChatInAppBrowser();
- }
- function shouldInitWeChatJssdkOnLoad() {
- if (isWeChatInAppBrowser()) return true;
- return isWxForceDebug() || isWxPcAutoDebugHost();
- }
- function shouldFetchWxConfig(fromUserClick) {
- if (isWeChatInAppBrowser()) return true;
- if (fromUserClick) return true;
- return isWxForceDebug() || isWxPcAutoDebugHost();
- }
- function getWxHtmlUrl() {
- var forced = String(q('wxSignUrl') || '').trim();
- if (forced) return forced.split('#')[0];
- return String(location.href || '').split('#')[0];
- }
- function getWxSignPageUrlForApi() {
- var htmlUrl = getWxHtmlUrl();
- if (htmlUrl && /^https?:\/\//i.test(htmlUrl)) return htmlUrl;
- return WX_GET_CONFIG_SIGN_URL;
- }
- function getWxHtmlUrlBase() {
- var htmlUrl = getWxSignPageUrlForApi();
- try {
- var u = new URL(htmlUrl);
- return u.origin + u.pathname;
- } catch (eU) {
- return WX_GET_CONFIG_SIGN_URL;
- }
- }
- function getWxConfigSignUrl() {
- if (String(q('wxSignBaseOnly') || '') === '1') return getWxHtmlUrlBase();
- return getWxSignPageUrlForApi();
- }
- function getWxGetConfigApiUrl() {
- return API_BASE.replace(/\/$/, '') + WECHAT_GET_WX_CONFIG_PATH;
- }
- function buildWxGetConfigRequestBody(htmlUrl) {
- return {
- url: String(htmlUrl || '').split('#')[0].trim()
- };
- }
- function resolveWxConfigAppIdFromSignData(d) {
- if (!d || typeof d !== 'object') return '';
- var mp =
- d.mpAppId ||
- d.mpAppid ||
- d.officialAppId ||
- d.gzhAppId ||
- d.serviceAppId;
- if (mp != null && String(mp).trim() !== '') return String(mp).trim();
- var fromQuery = String(q('wxMpAppId') || WECHAT_MP_APP_ID || '').trim();
- if (fromQuery) return fromQuery;
- var raw = d.appId || d.appid || d.wxAppId;
- return raw != null && String(raw).trim() !== '' ? String(raw).trim() : '';
- }
- function normalizeWxJssdkSignPayload(res, signUrlUsed) {
- if (!res || typeof res !== 'object') return null;
- var d = res.data != null && typeof res.data === 'object' ? res.data : res;
- if (!d || typeof d !== 'object') return null;
- var appId = resolveWxConfigAppIdFromSignData(d);
- var timestamp = d.timestamp != null ? d.timestamp : d.timeStamp;
- var nonceStr =
- d.nonceStr != null && String(d.nonceStr) !== ''
- ? d.nonceStr
- : d.noncestr != null && String(d.noncestr) !== ''
- ? d.noncestr
- : d.nonce;
- var signature = d.signature || d.sign;
- if (!appId || timestamp == null || nonceStr == null || nonceStr === '' || !signature) {
- return null;
- }
- return {
- appId: String(appId),
- timestamp: Number(timestamp),
- nonceStr: String(nonceStr),
- signature: String(signature),
- signUrl: String(signUrlUsed || '')
- .split('#')[0]
- .trim()
- };
- }
- function getWxErrMsg(err) {
- if (!err) return '';
- if (err.errMsg) return String(err.errMsg);
- if (typeof err === 'string') return err;
- try {
- return JSON.stringify(err);
- } catch (e) {
- return String(err);
- }
- }
- function formatWxConfigErrorTip(err, signPageUrl) {
- var errMsg = getWxErrMsg(err);
- var tip = '微信 JSSDK 配置失败';
- if (/invalid signature/i.test(errMsg)) {
- return (
- tip +
- ':签名无效。请核对:①后端用与前端相同的 url 签名;②url=' +
- (signPageUrl || getWxHtmlUrl()) +
- ';③nonceStr/timestamp 与接口返回一致;④appId=' +
- WECHAT_MP_APP_ID
- );
- }
- if (/require\s*subscribe/i.test(errMsg)) {
- return tip + ':' + errMsg + '(服务号需用户已关注)';
- }
- return errMsg ? tip + ':' + errMsg : tip;
- }
- function setWxInitError(msg) {
- wxInitLastError = String(msg || '').trim();
- if (wxInitLastError) console.warn('[wx]', wxInitLastError);
- }
- function requestWeChatJssdkSignAndConfig(htmlUrlOptional) {
- var htmlUrl = String(
- htmlUrlOptional != null ? htmlUrlOptional : getWxConfigSignUrl()
- )
- .split('#')[0]
- .trim();
- if (!htmlUrl || !/^https?:\/\//i.test(htmlUrl)) {
- htmlUrl = WX_GET_CONFIG_SIGN_URL;
- }
- if (isWxDebugOn()) {
- try {
- window.alert('htmlUrl(签名用):\n' + htmlUrl);
- } catch (eDbg) {}
- }
- var requestUrl = getWxGetConfigApiUrl();
- var requestBody = buildWxGetConfigRequestBody(htmlUrl);
- console.log('[wx] htmlUrl=', htmlUrl);
- console.log('[wx] POST getWxConfig →', requestUrl, requestBody);
- return fetch(requestUrl, {
- method: 'POST',
- mode: 'cors',
- credentials: 'omit',
- headers: {
- Accept: 'application/json',
- 'Content-Type': 'application/json;charset=UTF-8'
- },
- body: JSON.stringify(requestBody)
- })
- .then(function (r) {
- if (r.ok) return r.json();
- return r
- .text()
- .catch(function () {
- return '';
- })
- .then(function (text) {
- var hint = '';
- try {
- var j = JSON.parse(text);
- hint = j.msg || j.message || '';
- } catch (eP) {
- if (text) hint = text.slice(0, 120);
- }
- throw new Error('getWxConfig HTTP ' + r.status + (hint ? ':' + hint : ''));
- });
- })
- .then(function (res) {
- if (res && res.code != null) {
- var c = Number(res.code);
- if (c !== 200 && c !== 0 && res.success !== true) {
- throw new Error(res.msg || res.message || 'getWxConfig code ' + c);
- }
- }
- var sign = normalizeWxJssdkSignPayload(res, htmlUrl);
- if (!sign) {
- console.warn('[wx] getWxConfig 响应字段不全', res, 'htmlUrl=', htmlUrl);
- throw new Error(
- 'getWxConfig 缺少 appId/timestamp/nonceStr/signature(见控制台)'
- );
- }
- if (sign.appId !== WECHAT_MP_APP_ID) {
- console.warn(
- '[wx] 后端 appId=' + sign.appId + ',期望服务号 ' + WECHAT_MP_APP_ID
- );
- }
- if (typeof wx === 'undefined') {
- if (isWxPcBrowser()) {
- console.log('[wx] PC getWxConfig 成功(无 jweixin)', sign);
- return true;
- }
- setWxInitError('jweixin.js 未加载');
- return false;
- }
- return applyWxConfigFromSign(sign, htmlUrl);
- });
- }
- function applyWxConfigFromSign(sign, htmlUrl) {
- var wxConfigParams = {
- debug: isWxDebugOn(),
- appId: String(sign.appId),
- timestamp: sign.timestamp,
- nonceStr: String(sign.nonceStr),
- signature: String(sign.signature),
- jsApiList: [],
- openTagList: ['wx-open-launch-app']
- };
- return new Promise(function (resolve) {
- wx.config(wxConfigParams);
- wx.ready(function () {
- weChatJssdkConfigured = true;
- document.body.classList.add('wx-jssdk-ready');
- refreshWxLaunchTagAttrs();
- console.log('[wx.config] ready, htmlUrl=', htmlUrl);
- resolve(true);
- });
- wx.error(function (err) {
- weChatJssdkConfigured = false;
- document.body.classList.remove('wx-jssdk-ready');
- wxJssdkInitPromise = null;
- var errMsg = getWxErrMsg(err);
- setWxInitError(formatWxConfigErrorTip(err, htmlUrl));
- if (isWxDebugOn()) window.alert(wxInitLastError);
- console.warn('[wx.config]', errMsg, 'htmlUrl=', htmlUrl);
- var baseUrl = getWxHtmlUrlBase();
- var fullUrl = getWxSignPageUrlForApi();
- if (
- !/invalid signature/i.test(errMsg) ||
- wxConfigSignRetriedBaseUrl ||
- baseUrl === fullUrl ||
- htmlUrl === baseUrl
- ) {
- resolve(false);
- return;
- }
- wxConfigSignRetriedBaseUrl = true;
- requestWeChatJssdkSignAndConfig(baseUrl).then(resolve);
- });
- });
- }
- function refreshWxLaunchTagAttrs() {
- var tag = document.getElementById('launch-btn');
- if (!tag) return;
- try {
- tag.setAttribute('appid', WECHAT_OPEN_APP_ID);
- tag.setAttribute('extinfo', buildWeChatLaunchExtinfo());
- } catch (eA) {}
- }
- function bindWeChatLaunchTagEvents() {
- var tag = document.getElementById('launch-btn');
- if (!tag || tag._wxLaunchBound) return;
- tag._wxLaunchBound = true;
- refreshWxLaunchTagAttrs();
- tag.addEventListener('launch', function () {
- console.log('[wx-open-launch-app] launch ok');
- // showFabToast('正在打开 U店在哪…');
- });
- tag.addEventListener('error', function (e) {
- var detail = e && e.detail;
- var errMsg =
- detail && detail.errMsg
- ? String(detail.errMsg)
- : detail && detail.errmsg
- ? String(detail.errmsg)
- : '';
- console.warn('[wx-open-launch-app]', detail);
- if (/launch:fail/i.test(errMsg) && !getWxShareEntryFrom()) {
- showAppOpenFailTip(
- '当前环境无法直接打开 App,请使用 App「分享到微信」后点分享卡片进入'
- );
- } else {
- showAppOpenFailTip(
- errMsg
- ? '未能打开 App:' + errMsg
- : '未能打开 App,请确认已安装最新版「U店在哪」'
- );
- }
- });
- }
- function initWeChatOpenLaunchApp(fromUserClick) {
- if (!shouldFetchWxConfig(!!fromUserClick)) {
- console.log('[wx] 未调用 getWxConfig');
- return Promise.resolve(false);
- }
- if (isWxPcBrowser()) {
- console.warn(
- '[wx] PC:请求 getWxConfig' + (fromUserClick ? '(按钮)' : '(进页)')
- );
- }
- if (wxJssdkInitPromise && !fromUserClick) return wxJssdkInitPromise;
- wxConfigSignRetriedBaseUrl = false;
- wxInitLastError = '';
- var htmlUrl = getWxConfigSignUrl();
- bindWeChatLaunchTagEvents();
- wxJssdkInitPromise = requestWeChatJssdkSignAndConfig(htmlUrl)
- .then(function (ok) {
- if (ok === true) return true;
- if (!wxInitLastError) {
- setWxInitError('wx.config 失败,可加 ?wxDebug=1 查看 htmlUrl');
- }
- return false;
- })
- .catch(function (e) {
- var msg = e && e.message ? e.message : 'getWxConfig 请求失败';
- setWxInitError(msg);
- console.warn('[wx] requestWeChatJssdkSignAndConfig failed', msg, 'htmlUrl=', htmlUrl);
- if (isWeChatInAppBrowser() && isWxDebugOn()) window.alert(msg);
- return false;
- })
- .finally(function () {
- if (!weChatJssdkConfigured) wxJssdkInitPromise = null;
- });
- return wxJssdkInitPromise;
- }
- /** 进页即尝试拉签名;jweixin 晚到时自动重试 */
- function scheduleWeChatJssdkBootstrap() {
- if (!shouldInitWeChatJssdkOnLoad()) return;
- var attempts = 0;
- function tick() {
- attempts += 1;
- if (weChatJssdkConfigured) return;
- initWeChatOpenLaunchApp();
- if (!weChatJssdkConfigured && attempts < 8 && typeof wx === 'undefined') {
- setTimeout(tick, 400);
- }
- }
- if (isIOSWeChatBrowser()) {
- setTimeout(tick, 350);
- } else {
- tick();
- }
- }
- function showFabToast(msg, ms) {
- var tip = String(msg || '').trim();
- if (!tip) return;
- var el = document.getElementById('openAppToast');
- if (el) {
- el.textContent = tip;
- el.style.display = 'block';
- if (showFabToast._t) clearTimeout(showFabToast._t);
- showFabToast._t = setTimeout(function () {
- el.style.display = 'none';
- }, ms || 2800);
- }
- console.log('[openApp]', tip);
- }
- function showAppOpenFailTip(msg) {
- var tip = msg || '未能打开 App,请确认已安装最新版「U店在哪」。';
- if (typeof uni !== 'undefined' && typeof uni.showToast === 'function') {
- uni.showToast({ title: tip, icon: 'none', duration: 2800 });
- } else if (isWeChatInAppBrowser()) {
- window.alert(tip);
- } else {
- console.warn('[openApp]', tip);
- }
- }
- function tryFetchWxConfigOnPcClick() {
- if (!isWxPcBrowser()) return Promise.resolve(false);
- showFabToast('正在请求 getWxConfig…');
- wxJssdkInitPromise = null;
- return initWeChatOpenLaunchApp(true).then(function (ok) {
- if (ok) {
- showFabToast(
- weChatJssdkConfigured
- ? 'getWxConfig 成功,wx.config 已就绪'
- : 'getWxConfig 成功'
- );
- } else {
- showFabToast(wxInitLastError || 'getWxConfig 失败');
- }
- return ok;
- });
- }
- function launchAppDeepLink(deepLink) {
- try {
- var a = document.createElement('a');
- a.href = deepLink;
- a.setAttribute('target', '_self');
- document.body.appendChild(a);
- a.click();
- document.body.removeChild(a);
- } catch (e1) {}
- try {
- window.location.href = deepLink;
- } catch (e2) {}
- }
- function tryOpenHBuilderApp() {
- /* 微信内须直接点击 wx-open-launch-app,scheme 会被拦截 */
- if (isWeChatInAppBrowser()) return;
- tryFetchWxConfigOnPcClick().then(function () {
- if (isWxConfigOnClickDebug() && !isWxForceDebug() && !isWxPcAutoDebugHost()) {
- return;
- }
- tryOpenHBuilderAppViaScheme();
- });
- }
- function tryOpenHBuilderAppViaScheme() {
- // showFabToast('正在打开 U店在哪…');
- var deepLink = buildAppDeepLink();
- if (typeof plus !== 'undefined' && plus.runtime) {
- var installed = null;
- try {
- if (typeof plus.runtime.isApplicationExist === 'function') {
- installed = plus.runtime.isApplicationExist({
- pname: APP_ANDROID_PACKAGE,
- action: APP_IOS_URL_SCHEME
- });
- }
- } catch (e) {
- console.warn(e);
- }
- if (installed === false) {
- showDownloadTip();
- return;
- }
- try {
- plus.runtime.openURL(deepLink);
- } catch (e2) {
- console.warn(e2);
- showDownloadTip();
- }
- return;
- }
- var done = false;
- function finish() {
- if (done) return;
- done = true;
- document.removeEventListener('visibilitychange', onVis);
- window.removeEventListener('pagehide', onHide);
- }
- function onVis() {
- if (document.visibilityState === 'hidden') finish();
- }
- function onHide() {
- finish();
- }
- document.addEventListener('visibilitychange', onVis);
- window.addEventListener('pagehide', onHide);
- try {
- launchAppDeepLink(deepLink);
- } catch (e3) {
- finish();
- showDownloadTip();
- return;
- }
- window.setTimeout(function () {
- finish();
- }, 2600);
- }
- function boot() {
- var launchTag = document.getElementById('launch-btn');
- if (launchTag) launchTag.setAttribute('appid', WECHAT_OPEN_APP_ID);
- bindWeChatLaunchTagEvents();
- if (isWeChatInAppBrowser()) {
- document.body.classList.add('is-wechat');
- logWxEntryDiagnostics();
- }
- if (shouldInitWeChatJssdkOnLoad()) {
- if (isWxPcBrowser() && (isWxForceDebug() || isWxPcAutoDebugHost())) {
- document.body.classList.add('wx-pc-debug');
- }
- scheduleWeChatJssdkBootstrap();
- }
- document.addEventListener('WeixinOpenTagsError', function (e) {
- console.warn('[WeixinOpenTagsError]', e && e.detail);
- });
- applyEmptyHeroState();
- if (isWeChatInAppBrowser()) {
- var openBtnWx = document.getElementById('openApp');
- if (openBtnWx) {
- openBtnWx.addEventListener('click', function () {
- if (!weChatJssdkConfigured) {
- showFabToast(
- wxInitLastError || '微信 SDK 初始化中,请稍候再点底部按钮'
- );
- return;
- }
- });
- }
- } else {
- var openBtn = document.getElementById('openApp');
- if (openBtn) {
- openBtn.addEventListener('click', tryOpenHBuilderApp);
- }
- }
- }
- if (document.readyState === 'complete') {
- boot();
- } else {
- window.onload = boot;
- }
- })();
- </script>
- </body>
- </html>
|