| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- 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;
- // }
- // 兼容解析:优先读取标准URL query,其次读取hash路由中的query
- const getUrlParam = (key: string) => {
- let value = new URL(location.href).searchParams.get(key);
- if (!value) {
- const hash = location.hash || '';
- const qIndex = hash.indexOf('?');
- if (qIndex !== -1) {
- const hashQuery = hash.slice(qIndex + 1);
- value = new URLSearchParams(hashQuery).get(key);
- }
- }
- return value;
- };
- const token = getUrlParam('token');
- const openid = getUrlParam('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);
- }
- });
- };
|