|
|
@@ -0,0 +1,157 @@
|
|
|
+import { REQUEST_CONFIG } from '@kasite/uni-app-base/config';
|
|
|
+import { WebOauthAuthorize } from '@kasite/uni-app-base/service/base';
|
|
|
+import { useDomain } from '@kasite/uni-app-base/hook/system/use-domain';
|
|
|
+import store from '@kasite/uni-app-base/store';
|
|
|
+
|
|
|
+/** 缓存是否过期 */
|
|
|
+export const useIsExpirationDiy = 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 useSmallProgramLoginDiy = async (app: App.AppInstance) => {
|
|
|
+ let res;
|
|
|
+ // #ifdef MP-WEIXIN
|
|
|
+ res = await smallProgramLoginByMPWEIXINDiy(app);
|
|
|
+ // #endif
|
|
|
+
|
|
|
+ // #ifdef MP-GONGZHONGHAO
|
|
|
+ res = await smallProgramLoginByGONGZHONGHAODiy(app);
|
|
|
+ // #endif
|
|
|
+
|
|
|
+ return res;
|
|
|
+};
|
|
|
+
|
|
|
+const smallProgramLoginByMPWEIXINDiy = async (app: App.AppInstance) => {
|
|
|
+ return new Promise(async (resolve, reject) => {
|
|
|
+ // 判断当前token是否过期(存在且没有过期直接返回true)不进行反复登录请求
|
|
|
+ if ((await useIsExpirationDiy()) && 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 smallProgramLoginByGONGZHONGHAODiy = (app: App.AppInstance) => {
|
|
|
+ return new Promise(async (resolve, reject) => {
|
|
|
+ // if ((await useIsExpirationDiy()) && uni.getStorageSync('token')) {
|
|
|
+ // resolve(true);
|
|
|
+ // if (!store.state.token) {
|
|
|
+ // store.commit('setToken', uni.getStorageSync('token'));
|
|
|
+ // }
|
|
|
+ // return;
|
|
|
+ // }
|
|
|
+ const token = new URL(location.href).searchParams.get('token');
|
|
|
+ const openid = new URL(location.href).searchParams.get('openid');
|
|
|
+ console.log(token);
|
|
|
+ const { channelId, configKey, hosId, wechatConfigKey } = app.globalData;
|
|
|
+ if (!token) {
|
|
|
+ // 没有授权登录过的,需要重定向到授权页面
|
|
|
+ const url = WebOauthAuthorize({
|
|
|
+ hosId,
|
|
|
+ wechatConfigKey,
|
|
|
+ toUrl: `${useDomain(true)}/#/`, //encodeURIComponent(`${useDomain(true)}/#/`),
|
|
|
+ });
|
|
|
+ location.href = url;
|
|
|
+ } else {
|
|
|
+ store.commit('setToken', token);
|
|
|
+ store.commit('setOpenId', openid);
|
|
|
+ // 当前时间
|
|
|
+ var timestamp = Date.parse(new Date());
|
|
|
+ // 加上过期期限
|
|
|
+ var expiration = timestamp + 24 * 60 * 60 * 1000;
|
|
|
+ uni.setStorageSync('data_expiration', expiration);
|
|
|
+ // location.href = `${useDomain(true)}/#/`;
|
|
|
+ resolve(true);
|
|
|
+ }
|
|
|
+ });
|
|
|
+};
|