登录功能,多租户
This commit is contained in:
@@ -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