提交 fca76138 编写于 作者: Anne_LXM's avatar Anne_LXM

更新 优化时序,确保登录状态变化事件(uni-id-pages-logout、uni-id-pages-login-success)发出前store下的userInfo._id值已刷新

上级 ae3b80c2
...@@ -6,13 +6,12 @@ const db = uniCloud.database(); ...@@ -6,13 +6,12 @@ const db = uniCloud.database();
const usersTable = db.collection('uni-id-users') const usersTable = db.collection('uni-id-users')
let hostUserInfo = uni.getStorageSync('uni-id-pages-userInfo')||{} let hostUserInfo = uni.getStorageSync('uni-id-pages-userInfo')||{}
// console.log( hostUserInfo);
const data = { const data = {
userInfo: hostUserInfo, userInfo: hostUserInfo,
hasLogin: Object.keys(hostUserInfo).length != 0 hasLogin: Object.keys(hostUserInfo).length != 0
} }
// console.log('data', data);
// 定义 mutations, 修改属性 // 定义 mutations, 修改属性
export const mutations = { export const mutations = {
// data不为空,表示传递要更新的值(注意不是覆盖是合并),什么也不传时,直接查库获取更新 // data不为空,表示传递要更新的值(注意不是覆盖是合并),什么也不传时,直接查库获取更新
...@@ -37,6 +36,10 @@ export const mutations = { ...@@ -37,6 +36,10 @@ export const mutations = {
}) })
} else { } else {
// 不等待联网查询,立即更新用户_id确保store.userInfo中的_id是最新的
const _id = uniCloud.getCurrentUserInfo().uid
this.setUserInfo({_id},{cover:true})
// 查库获取用户信息,更新store.userInfo
const uniIdCo = uniCloud.importObject("uni-id-co", { const uniIdCo = uniCloud.importObject("uni-id-co", {
customUI: true customUI: true
}) })
...@@ -58,7 +61,7 @@ export const mutations = { ...@@ -58,7 +61,7 @@ export const mutations = {
} }
} }
}, },
async setUserInfo(data, {cover}={cover:false}) { setUserInfo(data, {cover}={cover:false}) {
// console.log('set-userInfo', data); // console.log('set-userInfo', data);
let userInfo = cover?data:Object.assign(store.userInfo,data) let userInfo = cover?data:Object.assign(store.userInfo,data)
store.userInfo = Object.assign({},userInfo) store.userInfo = Object.assign({},userInfo)
...@@ -78,13 +81,12 @@ export const mutations = { ...@@ -78,13 +81,12 @@ export const mutations = {
} }
uni.removeStorageSync('uni_id_token'); uni.removeStorageSync('uni_id_token');
uni.setStorageSync('uni_id_token_expired', 0) uni.setStorageSync('uni_id_token_expired', 0)
this.setUserInfo({},{cover:true})
uni.$emit('uni-id-pages-logout')
uni.redirectTo({ uni.redirectTo({
url: `/${pagesJson.uniIdRouter && pagesJson.uniIdRouter.loginPage ? pagesJson.uniIdRouter.loginPage: 'uni_modules/uni-id-pages/pages/login/login-withoutpwd'}`, url: `/${pagesJson.uniIdRouter && pagesJson.uniIdRouter.loginPage ? pagesJson.uniIdRouter.loginPage: 'uni_modules/uni-id-pages/pages/login/login-withoutpwd'}`,
}); });
uni.$emit('uni-id-pages-logout')
this.setUserInfo({},{cover:true})
}, },
loginBack (e = {}) { loginBack (e = {}) {
const {uniIdRedirectUrl = ''} = e const {uniIdRedirectUrl = ''} = e
let delta = 0; //判断需要返回几层 let delta = 0; //判断需要返回几层
...@@ -139,6 +141,7 @@ export const mutations = { ...@@ -139,6 +141,7 @@ export const mutations = {
duration: 3000 duration: 3000
}); });
} }
// 异步调用(更新用户信息)防止获取头像等操作阻塞页面返回
this.updateUserInfo() this.updateUserInfo()
uni.$emit('uni-id-pages-login-success') uni.$emit('uni-id-pages-login-success')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册