登录功能,多租户
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import pagesJson from '@/pages.json'
|
||||
import config from '@/uni_modules/uni-id-pages/config.js'
|
||||
import { getApiUrl } from '@/common/config.js'
|
||||
|
||||
const uniIdCo = uniCloud.importObject("uni-id-co")
|
||||
const db = uniCloud.database();
|
||||
const usersTable = db.collection('uni-id-users')
|
||||
|
||||
@@ -71,18 +71,33 @@ export const mutations = {
|
||||
return data
|
||||
},
|
||||
async logout() {
|
||||
// 1. 已经过期就不需要调用服务端的注销接口 2.即使调用注销接口失败,不能阻塞客户端
|
||||
if(uniCloud.getCurrentUserInfo().tokenExpired > Date.now()){
|
||||
try{
|
||||
await uniIdCo.logout()
|
||||
}catch(e){
|
||||
console.error(e);
|
||||
// 优先调用后端自定义登出接口(/api/sys/auth/logout),失败也不影响本地清理
|
||||
const backendUserId = uni.getStorageSync('backend-user-id')
|
||||
if (backendUserId) {
|
||||
try {
|
||||
await uni.request({
|
||||
url: getApiUrl('/api/sys/auth/logout'),
|
||||
method: 'POST',
|
||||
header: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
data: {
|
||||
userId: backendUserId
|
||||
}
|
||||
})
|
||||
} catch (e) {
|
||||
console.error('后端登出接口调用失败:', e)
|
||||
}
|
||||
}
|
||||
|
||||
// 清理本地登录状态(后端 token + 旧 uni-id token)
|
||||
uni.removeStorageSync('backend-token')
|
||||
uni.removeStorageSync('backend-login-response')
|
||||
uni.removeStorageSync('backend-user-id')
|
||||
uni.removeStorageSync('uni_id_token');
|
||||
uni.setStorageSync('uni_id_token_expired', 0)
|
||||
this.setUserInfo({},{cover:true})
|
||||
uni.$emit('uni-id-pages-logout')
|
||||
this.setUserInfo({},{cover:true})
|
||||
uni.$emit('uni-id-pages-logout')
|
||||
uni.redirectTo({
|
||||
url: `/${pagesJson.uniIdRouter && pagesJson.uniIdRouter.loginPage ? pagesJson.uniIdRouter.loginPage: 'uni_modules/uni-id-pages/pages/login/login-withoutpwd'}`,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user