import { REQUEST_CONFIG } from '../../config'; import { WebOauth2Authorize } from '../../service/base'; import { useDomain } from '../use-domain'; import store from '../../store'; /** 缓存是否过期 */ export const useIsExpiration = async () => { // 当前时间 var timestamp = Date.parse(new Date()); // 缓存中的过期时间 var data_expiration = uni.getStorageSync('data_expiration'); // 如果缓存中没有data_expiration,说明也没有token,还未登录 if (data_expiration) { // 如果超时了,清除缓存,重新登录 if (timestamp > data_expiration) { uni.setStorageSync('data_expiration', 0); return false; } else { return true; } } return false; }; /** 小程序登录 */ export const useSmallProgramLogin = async (app: App.AppInstance) => { let res; // #ifdef MP-WEIXIN res = await smallProgramLoginByMPWEIXIN(app); // #endif // #ifdef MP-GONGZHONGHAO res = await smallProgramLoginByGONGZHONGHAO(app); // #endif return res; }; const smallProgramLoginByMPWEIXIN = async (app: App.AppInstance) => { return new Promise(async (resolve, reject) => { // 判断当前token是否过期(存在且没有过期直接返回true)不进行反复登录请求 if ((await useIsExpiration()) && uni.getStorageSync('token')) { resolve(true); return; } uni.login({ success(res) { uni.request({ url: REQUEST_CONFIG.BASE_URL + 'wsgw/' + app.globalData.channelId + '/' + app.globalData.configKey + '/' + app.globalData.hosId + '/smallProgramLogin_v2.do?cfgKey=' + app.globalData.wechatConfigKey, method: 'POST', data: { appId: app.globalData.appId, smallPro_authCode: res.code, smallPro_systemInfo: app.globalData.smallPro_systemInfo, smallPro_version: app.globalData.smallPro_version, }, header: { 'content-type': 'application/x-www-form-urlencoded', }, success(resp: any) { if (resp.data.RespCode == '10000') { store.commit('setToken', resp.data.token); store.commit('setOpenId', resp.data.openId); store.commit('setUnionId', resp.data.unionId); store.commit('setSmallProOpenId', resp.data.smallProOpenId); store.commit('setWechatOpenId', resp.data.wechatOpenid); uni.setStorageSync('isCall', 0); // publicFn.preserMember(); // 当前时间 var timestamp = Date.parse(new Date()); // 加上过期期限 var expiration = timestamp + resp.data.expireTime; uni.setStorageSync('data_expiration', expiration); resolve(resp); } else { if ( resp.data.RespCode == '-14019' && resp.data.RespMessage && resp.data.RespMessage.indexOf('非白名单用户') >= 0 ) { uni.showLoading({ title: '系统升级中...', mask: true, }); } else { uni.showLoading({ title: resp.data.RespMessage || '网络异常!', mask: true, }); } resolve(false); } }, fail(error) { uni.showLoading({ title: '网络异常!', mask: true, }); reject(error); }, }); }, fail(error) { uni.showLoading({ title: '网络异常!', mask: true, }); reject(error); }, }); }); }; const smallProgramLoginByGONGZHONGHAO = (app: App.AppInstance) => { return new Promise(async (resolve, reject) => { if ((await useIsExpiration()) && uni.getStorageSync('token')) { resolve(true); return; } const code = new URL(location.href).searchParams.get('code') || uni.getStorageSync('login_code'); if (!code) { // 没有授权登录过的,需要重定向到授权页面 location.href = WebOauth2Authorize({ appid: app.globalData.appId, redirect_uri: encodeURIComponent(`${useDomain()}/#/`), }); } else { uni.setStorageSync('login_code', code); location.href = `${useDomain()}/#/`; const { channelId, configKey, hosId, wechatConfigKey } = app.globalData; uni.request({ url: `${REQUEST_CONFIG.BASE_URL}wsgw/${channelId}/${configKey}/${hosId}/smallProgramLogin_v2.do?cfgKey=${wechatConfigKey}`, method: 'POST', data: { appId: app.globalData.appId, smallPro_authCode: code, smallPro_systemInfo: app.globalData.smallPro_systemInfo, smallPro_version: app.globalData.smallPro_version, }, header: { 'content-type': 'application/x-www-form-urlencoded', }, success(resp: any) { console.log('1111', resp); if (resp.data.RespCode == '10000') { store.commit('setToken', resp.data.token); store.commit('setOpenId', resp.data.openId); store.commit('setUnionId', resp.data.unionId); store.commit('setSmallProOpenId', resp.data.smallProOpenId); store.commit('setWechatOpenId', resp.data.wechatOpenid); uni.setStorageSync('isCall', 0); // publicFn.preserMember(); // 当前时间 var timestamp = Date.parse(new Date()); // 加上过期期限 var expiration = timestamp + resp.data.expireTime; uni.setStorageSync('data_expiration', expiration); resolve(resp); } else { if ( resp.data.RespCode == '-14019' && resp.data.RespMessage && resp.data.RespMessage.indexOf('非白名单用户') >= 0 ) { uni.showLoading({ title: '系统升级中...', mask: true, }); } else { uni.showLoading({ title: resp.data.RespMessage || '网络异常!', mask: true, }); } resolve(false); } }, fail(error) { console.log(error); uni.showLoading({ title: '网络异常!', mask: true, }); reject(error); }, }); } }); };