| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- import { REQUEST_CONFIG } from '../../config';
- import { WebOauth2Authorize } from '../../service/base';
- import { useDomain } from '../use-domain';
- /** 缓存是否过期 */
- 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') {
- uni.setStorageSync('token', resp.data.token);
- getApp().globalData.token = resp.data.token;
- uni.setStorageSync('openid', resp.data.openId);
- uni.setStorageSync('unionid', resp.data.unionId);
- uni.setStorageSync('smallProOpenId', resp.data.smallProOpenId);
- uni.setStorageSync('wechatOpenid', 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') {
- uni.setStorageSync('token', resp.data.token);
- getApp().globalData.token = resp.data.token;
- uni.setStorageSync('openid', resp.data.openId);
- uni.setStorageSync('unionid', resp.data.unionId);
- uni.setStorageSync('smallProOpenId', resp.data.smallProOpenId);
- uni.setStorageSync('wechatOpenid', 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);
- },
- });
- }
- });
- };
|