const TOKEN_KEY = 'AppToken'; let isBinding = false; let pendingBindRefresh = false; let lastBindPromptTime = 0; const BIND_PROMPT_INTERVAL = 3000; const WHITELIST_APIS = [ '/app/app/login', '/app/app/loginByWeChat', '/app/app/loginByPhone', '/app/app/loginByApple', '/app/app/loginByUserId', '/app/app/register', '/app/app/registerByPhone', '/app/app/checkLogin', '/app/app/bindWeChat', '/app/app/setPhone', '/app/app/setIPhoneNumber', '/app/app/sendCode', '/app/app/registerSendCode', '/app/wx/loginByMp', '/app/wx/h5/mp/loginByMp', '/app/user/getUserInfo', '/app/app/logout', '/app/common/', '/app/app/getAppVersion', '/app/app/updatePushId', ]; const WHITELIST_PAGES = [ 'pages/auth/login', 'pages/auth/loginIndex', 'pages/auth/bindMobile', 'pages/auth/h5WxLogin', 'pages/auth/wechatList', 'pages/auth/register', ]; function isLogin() { return !!uni.getStorageSync(TOKEN_KEY); } function getUserInfoFromStorage() { let userInfo = uni.getStorageSync('userInfo'); if (userInfo && JSON.stringify(userInfo) !== '{}') { try { userInfo = typeof userInfo === 'string' ? JSON.parse(userInfo) : userInfo; } catch (e) { userInfo = {}; } } else { userInfo = {}; } return userInfo; } export function hasMaOpenId(userInfo) { if (!userInfo) { return false; } return !!(userInfo.maOpenId || userInfo.maopenid); } export function needBindMaOpenId() { if (!isLogin()) { return false; } const userInfo = getUserInfoFromStorage(); return !!(userInfo && userInfo.userId && !hasMaOpenId(userInfo)); } export function isWhitelistedApi(router) { if (!router) { return true; } return WHITELIST_APIS.some((api) => router.indexOf(api) !== -1); } export function isWhitelistedPage(route) { if (!route) { return false; } const path = route.replace(/^\//, ''); return WHITELIST_PAGES.some((page) => path.indexOf(page) !== -1); } export function getBindMiniProgramPath() { const app = getApp(); const userInfo = getUserInfoFromStorage(); const userId = userInfo && userInfo.userId ? userInfo.userId : ''; const basePath = (app && app.globalData && app.globalData.bindMiniProgramPath) || '/pages/auth/bindApp'; const connector = basePath.indexOf('?') !== -1 ? '&' : '?'; return `${basePath}${connector}userId=${userId}&source=app`; } export function launchBindMiniProgram() { // #ifdef APP-PLUS if (!needBindMaOpenId()) { return Promise.resolve(true); } return new Promise((resolve) => { plus.share.getServices(function(res) { let sweixin = null; for (let i = 0; i < res.length; i++) { if (res[i].id === 'weixin') { sweixin = res[i]; break; } } if (!sweixin) { uni.showToast({ title: '请先安装微信后再绑定小程序', icon: 'none', duration: 3000 }); resolve(false); return; } pendingBindRefresh = true; sweixin.launchMiniProgram({ // id: getApp().globalData.miniprogamId, // path: getBindMiniProgramPath(), // type: 0, id:"gh_b51445318864",//gh_7a6a32e5ef61 御君方互医 path:'pages/user/index', type:0 }, function() { resolve(true); }, function(e) { console.log('launchBindMiniProgram fail', e); pendingBindRefresh = false; uni.showToast({ title: '微信唤起失败,请检查是否安装微信', icon: 'none' }); resolve(false); }); }, function() { uni.showToast({ title: '微信唤起失败,请检查是否安装微信', icon: 'none' }); resolve(false); }); }); // #endif // #ifndef APP-PLUS return Promise.resolve(true); // #endif } export function promptBindMaOpenId() { const now = Date.now(); if (isBinding) { return false; } if (now - lastBindPromptTime < BIND_PROMPT_INTERVAL) { return false; } if (!needBindMaOpenId()) { return true; } isBinding = true; lastBindPromptTime = now; uni.showModal({ title: '提示', content: '请先绑定小程序后再继续使用', showCancel: false, confirmText: '去绑定', success: () => { launchBindMiniProgram().finally(() => { isBinding = false; }); }, fail: () => { isBinding = false; } }); return false; } export function checkMaOpenIdForRequest(router) { // #ifndef APP-PLUS return true; // #endif if (isWhitelistedApi(router)) { return true; } if (!needBindMaOpenId()) { return true; } promptBindMaOpenId(); return false; } export function checkMaOpenIdBind() { if (!needBindMaOpenId()) { return true; } return promptBindMaOpenId(); } export function initMaOpenIdNavigationInterceptor() { // #ifndef APP-PLUS return; // #endif const interceptor = { invoke(args) { const targetUrl = args.url || ''; if (isWhitelistedPage(targetUrl)) { return args; } const pages = getCurrentPages(); const curPage = pages[pages.length - 1]; const curRoute = curPage && curPage.route || ''; if (isWhitelistedPage(curRoute)) { return args; } if (!needBindMaOpenId()) { return args; } promptBindMaOpenId(); return false; } }; ['navigateTo', 'redirectTo', 'reLaunch', 'switchTab'].forEach((method) => { uni.addInterceptor(method, interceptor); }); } export function refreshUserInfoAfterBind() { if (!isLogin()) { pendingBindRefresh = false; return; } const shouldRefresh = pendingBindRefresh || needBindMaOpenId(); if (!shouldRefresh) { return; } const wasPending = pendingBindRefresh; pendingBindRefresh = false; // 延迟加载,避免与 request.js 循环依赖 const userApi = require('@/api/user.js'); userApi.getUserInfo().then((res) => { if (res.code == 200 && res.user) { uni.setStorageSync('userInfo', JSON.stringify(res.user)); uni.setStorageSync('userData', JSON.stringify(res.user)); uni.$emit('refreshUserInfo', {}); if (wasPending && !hasMaOpenId(res.user)) { setTimeout(() => { promptBindMaOpenId(); }, 500); } } }); }