登录功能,多租户
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'}`,
|
||||
});
|
||||
|
||||
@@ -15,13 +15,10 @@ export default {
|
||||
// "google",
|
||||
// "alipay",
|
||||
// "douyin",
|
||||
|
||||
// #ifdef APP
|
||||
'univerify',
|
||||
// #endif
|
||||
// #ifndef MP-HARMONY
|
||||
'weixin',
|
||||
// #endif
|
||||
// 去掉微信登录
|
||||
'username',
|
||||
// #ifdef APP
|
||||
'apple',
|
||||
|
||||
@@ -36,11 +36,7 @@
|
||||
|
||||
<script>
|
||||
import mixin from '@/uni_modules/uni-id-pages/common/login-page.mixin.js';
|
||||
const uniIdCo = uniCloud.importObject("uni-id-co", {
|
||||
errorOptions: {
|
||||
type: 'toast'
|
||||
}
|
||||
})
|
||||
import { getApiUrl } from '@/common/config.js'
|
||||
export default {
|
||||
mixins: [mixin],
|
||||
data() {
|
||||
@@ -77,7 +73,7 @@
|
||||
})
|
||||
},
|
||||
/**
|
||||
* 密码登录
|
||||
* 密码登录(改为调用后端 /api/sys/auth/login 接口)
|
||||
*/
|
||||
pwdLogin() {
|
||||
if (!this.password.length) {
|
||||
@@ -96,40 +92,71 @@
|
||||
duration: 3000
|
||||
});
|
||||
}
|
||||
if (this.needCaptcha && this.captcha.length != 4) {
|
||||
this.$refs.captcha.getImageCaptcha()
|
||||
return uni.showToast({
|
||||
title: '请输入验证码',
|
||||
icon: 'none',
|
||||
duration: 3000
|
||||
});
|
||||
}
|
||||
|
||||
if (this.needAgreements && !this.agree) {
|
||||
return this.$refs.agreements.popup(this.pwdLogin)
|
||||
}
|
||||
|
||||
let data = {
|
||||
"password": this.password,
|
||||
"captcha": this.captcha
|
||||
// 组装后端登录参数(根据后端 LoginRequest 约定,这里假定为 userName + password)
|
||||
const payload = {
|
||||
userName: this.username,
|
||||
password: this.password
|
||||
}
|
||||
|
||||
if (/^1\d{10}$/.test(this.username)) {
|
||||
data.mobile = this.username
|
||||
} else if (/@/.test(this.username)) {
|
||||
data.email = this.username
|
||||
} else {
|
||||
data.username = this.username
|
||||
}
|
||||
uni.showLoading({
|
||||
title: '正在登录...',
|
||||
mask: true
|
||||
})
|
||||
|
||||
uniIdCo.login(data).then(e => {
|
||||
this.loginSuccess(e)
|
||||
}).catch(e => {
|
||||
if (e.errCode == 'uni-id-captcha-required') {
|
||||
this.needCaptcha = true
|
||||
} else if (this.needCaptcha) {
|
||||
//登录失败,自动重新获取验证码
|
||||
this.$refs.captcha.getImageCaptcha()
|
||||
uni.request({
|
||||
url: getApiUrl('/api/sys/auth/login'),
|
||||
method: 'POST',
|
||||
header: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
data: payload,
|
||||
success: (res) => {
|
||||
const body = res.data || {}
|
||||
if (!body.success || !body.data) {
|
||||
return uni.showToast({
|
||||
title: body.message || '登录失败',
|
||||
icon: 'none',
|
||||
duration: 3000
|
||||
})
|
||||
}
|
||||
|
||||
const loginData = body.data
|
||||
// 持久化后端登录态,供后续接口使用
|
||||
try {
|
||||
uni.setStorageSync('backend-login-response', loginData)
|
||||
uni.setStorageSync('backend-token', loginData.token || '')
|
||||
uni.setStorageSync('backend-user-id', loginData.userId)
|
||||
} catch (e) {
|
||||
console.error('缓存登录信息失败:', e)
|
||||
}
|
||||
|
||||
uni.showToast({
|
||||
title: body.message || '登录成功',
|
||||
icon: 'none',
|
||||
duration: 1500
|
||||
})
|
||||
|
||||
// 登录成功后跳转到首页(这里使用项目首页路径,如需调整可改为你期望的页面)
|
||||
setTimeout(() => {
|
||||
uni.reLaunch({
|
||||
url: '/pages/list/list'
|
||||
})
|
||||
}, 300)
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('调用登录接口失败:', err)
|
||||
uni.showToast({
|
||||
title: '网络异常,登录失败',
|
||||
icon: 'none',
|
||||
duration: 3000
|
||||
})
|
||||
},
|
||||
complete: () => {
|
||||
uni.hideLoading()
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user