根据token静默登录

This commit is contained in:
duan
2024-07-20 15:33:54 +08:00
parent 638ffcb824
commit 85bba7828c
5 changed files with 41 additions and 40 deletions

31
App.vue
View File

@@ -19,32 +19,11 @@
// console.log('App Hide');
},
methods: {
async userlogin() {
uni.login({
provider: 'weixin',
success: (data) => {
uni.getUserInfo({
provider: 'weixin',
success: async (infoRes) => {
uni.cache.set('weixincode', data.code);
let res = await this.api.userwxlogin({
code: uni.cache.get('weixincode'), //临时登录凭证
rawData: infoRes.rawData,
})
if (res.code == 0) {
uni.cache.set('token', res.data.token);
uni.cache.set('miniAppOpenId', res.data.userInfo
.miniAppOpenId)
uni.cache.set('userInfo', res.data.userInfo);
}
},
fail: (err) => {}
});
}
});
},
userlogin() {
if (!uni.cache.get('token')) {
this.$store.dispatch("loginEvent"); //获取shapid
}
}
}
};
</script>

View File

@@ -1,7 +1,7 @@
import {
fill
} from "lodash"
import store from "../store"
const preCacheKeyClearFetch = 'storage:clear:fetch:'
uni.pro.interceptor('request', {
@@ -104,17 +104,14 @@ async function request(options) {
if (res.code != 0) {
if (res.code == -4) {
uni.showToast({
title: res.message || res.msg,
icon: "none"
// success: () => {
// setTimeout(res => {
// uni.cache.set('token', '');
// uni.redirectTo({
// url: '/pages/login/login'
// });
// }, 1000)
// }
title: '',
// title: res.message || res.msg,
icon: "none",
success: () => {
setTimeout(res => {
store.dispatch("loginEvent"); //获取shapid
}, 1000)
}
})
} else if (res.code == 482) {
let nowTime = new Date() / 1000 | 0

View File

@@ -14,7 +14,7 @@
</image>
<view class="contentboxitemright_itembox flex-colum">
<text>会员</text>
<text>入会权益</text>
<text>入会权益</text>
</view>
</view>
<view class="contentboxitemright_item flex-between" @click="memberindex(0)">

View File

@@ -170,7 +170,7 @@
let res = await this.api.upUserInfo({
headImg: this.userHeadImg,
nickName: this.nickName,
telephone: this.phonetitle
telephone: this.phonetitle == '请授权手机号' ? '' : this.phonetitle
})
if (res.code == 0) {
uni.navigateBack()

View File

@@ -61,6 +61,31 @@ const store = new Vuex.Store({
},
},
actions: {
loginEvent: () => {
uni.login({
provider: 'weixin',
success: (data) => {
uni.getUserInfo({
provider: 'weixin',
success: async (infoRes) => {
uni.cache.set('weixincode', data.code);
let res = await Api.userwxlogin({
code: uni.cache.get('weixincode'), //临时登录凭证
rawData: infoRes.rawData,
})
if (res.code == 0) {
uni.cache.set('token', res.data.token);
uni.cache.set('miniAppOpenId', res.data.userInfo
.miniAppOpenId)
uni.cache.set('userInfo', res.data.userInfo);
}
},
fail: (err) => {}
});
}
});
},
set_shopid: async ({
commit
}, data) => {