fix bug
This commit is contained in:
@@ -57,11 +57,11 @@ export const useStore = defineStore('main2', {
|
||||
});
|
||||
},
|
||||
checkLogin(){ // 检查是否登录
|
||||
let access_token = uni.getStorageSync('access_token')
|
||||
// let visitState = uni.getStorageSync('visitState')
|
||||
console.log(access_token)
|
||||
if(access_token) {
|
||||
this.access_token = access_token
|
||||
// let access_token = uni.getStorageSync('access_token')
|
||||
let u_info = uni.getStorageSync('userInfo')
|
||||
// console.log("u_info is ",u_info)
|
||||
if(u_info) {
|
||||
this.access_token = uni.getStorageSync('access_token')
|
||||
this.isLogin = true
|
||||
this.userInfo = uni.getStorageSync('userInfo')
|
||||
this.operation_info = uni.getStorageSync('operation_info')
|
||||
@@ -181,6 +181,87 @@ export const useStore = defineStore('main2', {
|
||||
})
|
||||
},duration)
|
||||
|
||||
},
|
||||
async getAccess_token(params={}) {
|
||||
//清除token缓存
|
||||
wx.clearStorageSync();
|
||||
//访问接口
|
||||
const data={
|
||||
username:params.code,//手机号按钮获取的code
|
||||
type:'wechat_mini_app',
|
||||
identity:'user',
|
||||
password:'111'
|
||||
}
|
||||
// console.log('登录参数:',data);return;
|
||||
const res = await api.getToken(data)
|
||||
if(res.code === 200) {
|
||||
// newUser.value = res.data.newUser
|
||||
// res.data.newUser = true // 开发阶段专用,正式版请删除或注释
|
||||
if(res.data.newUser) { // 新用户,不做任何缓存动作,跳转登录页
|
||||
let userId = res.data.userId
|
||||
let res_p=await new Promise(resolve=>{
|
||||
uni.login({
|
||||
provider: 'weixin', //使用微信登录
|
||||
success: function (loginRes) {
|
||||
resolve(loginRes);
|
||||
// console.log(loginRes,'微信登录返回信息');
|
||||
}
|
||||
});
|
||||
});
|
||||
// 正式版记住放开下面这行注释掉的代码
|
||||
uni.setStorageSync('access_token','Bearer '+res.data.access_token);// 为了wx登录成功,必须token单独缓存一次
|
||||
let res2= await api.wx_login({code:res_p.code},userId);
|
||||
if(res2.code === 0) {
|
||||
// this.checkLogin()
|
||||
// this.toLogin(encodeURIComponent(JSON.stringify(res.data))) // 跳转登录页,但是我不想这么做
|
||||
this.toProfile(encodeURIComponent(JSON.stringify(res.data)))
|
||||
}
|
||||
}
|
||||
else{
|
||||
let res_form = {}
|
||||
uni.setStorageSync('avatarUrl', res.data.avatar);//avatarUrl
|
||||
res_form = res.data
|
||||
res_form.access_token = 'Bearer '+res.data.access_token
|
||||
let userId = res.data.userId
|
||||
uni.setStorageSync('access_token',res_form.access_token);// token单独缓存一次
|
||||
this.wx_login(userId, res_form)
|
||||
}
|
||||
}
|
||||
else{
|
||||
uni.showToast({
|
||||
icon:"error",
|
||||
title:res.msg,
|
||||
duration:2000
|
||||
})
|
||||
}
|
||||
},
|
||||
async wx_login(userId, res_form) {
|
||||
let res=await new Promise(resolve=>{
|
||||
uni.login({
|
||||
provider: 'weixin', //使用微信登录
|
||||
success: function (loginRes) {
|
||||
resolve(loginRes);
|
||||
}
|
||||
});
|
||||
});
|
||||
let res2 = await api.wx_login({code:res.code},userId);
|
||||
if(res2.code === 0) {
|
||||
uni.setStorageSync('userInfo',JSON.stringify(res_form))
|
||||
this.checkLogin()
|
||||
}
|
||||
else{
|
||||
uni.clearStorage();
|
||||
}
|
||||
},
|
||||
toLogin(token_info) {
|
||||
uni.navigateTo({
|
||||
url:"/pages/login/login?token_info="+token_info
|
||||
})
|
||||
},
|
||||
toProfile(token_info) {
|
||||
uni.navigateTo({
|
||||
url:"/pages/user/userInfo?token_info="+token_info
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
Reference in New Issue
Block a user