登录功能,多租户

This commit is contained in:
zhonghua1
2025-12-16 22:36:47 +08:00
parent 2aef477d33
commit eef923a2da
4 changed files with 87 additions and 54 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name" : "AIDriverEEAudio6", "name" : "AIDriverEEAudio6",
"appid" : "__UNI__AIDriverEEAudio6", "appid" : "wx534c58a9db510fba",
"description" : "云端一体应用快速开发基本项目模版", "description" : "云端一体应用快速开发基本项目模版",
"versionName" : "1.0.0", "versionName" : "1.0.0",
"versionCode" : "100", "versionCode" : "100",
@@ -48,17 +48,11 @@
"h5" : { "h5" : {
"devServer" : { "devServer" : {
"port" : 8080, "port" : 8080,
"https" : false,
"disableHostCheck" : true,
"proxy" : { "proxy" : {
"^/api" : { "^/api" : {
// 注意此代理target需要与 common/env.js 中的 apiEnv 配置保持一致
// 如果 apiEnv 为 'local'target 应为 "http://localhost:8090"
// 如果 apiEnv 为 'prod'target 应为 "https://api.huayang-star.com"
"target" : "http://localhost:8090", "target" : "http://localhost:8090",
"changeOrigin" : true, "changeOrigin" : true,
"secure" : false, "secure" : false
"ws" : false
} }
} }
} }

View File

@@ -1,7 +1,7 @@
import pagesJson from '@/pages.json' import pagesJson from '@/pages.json'
import config from '@/uni_modules/uni-id-pages/config.js' 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 db = uniCloud.database();
const usersTable = db.collection('uni-id-users') const usersTable = db.collection('uni-id-users')
@@ -71,14 +71,29 @@ export const mutations = {
return data return data
}, },
async logout() { async logout() {
// 1. 已经过期就不需要调用服务端的注销接口 2.即使调用注销接口失败,不能阻塞客户端 // 优先调用后端自定义登出接口(/api/sys/auth/logout失败也不影响本地清理
if(uniCloud.getCurrentUserInfo().tokenExpired > Date.now()){ const backendUserId = uni.getStorageSync('backend-user-id')
if (backendUserId) {
try { try {
await uniIdCo.logout() await uni.request({
url: getApiUrl('/api/sys/auth/logout'),
method: 'POST',
header: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: {
userId: backendUserId
}
})
} catch (e) { } catch (e) {
console.error(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.removeStorageSync('uni_id_token');
uni.setStorageSync('uni_id_token_expired', 0) uni.setStorageSync('uni_id_token_expired', 0)
this.setUserInfo({},{cover:true}) this.setUserInfo({},{cover:true})

View File

@@ -15,13 +15,10 @@ export default {
// "google", // "google",
// "alipay", // "alipay",
// "douyin", // "douyin",
// #ifdef APP // #ifdef APP
'univerify', 'univerify',
// #endif // #endif
// #ifndef MP-HARMONY // 去掉微信登录
'weixin',
// #endif
'username', 'username',
// #ifdef APP // #ifdef APP
'apple', 'apple',

View File

@@ -36,11 +36,7 @@
<script> <script>
import mixin from '@/uni_modules/uni-id-pages/common/login-page.mixin.js'; import mixin from '@/uni_modules/uni-id-pages/common/login-page.mixin.js';
const uniIdCo = uniCloud.importObject("uni-id-co", { import { getApiUrl } from '@/common/config.js'
errorOptions: {
type: 'toast'
}
})
export default { export default {
mixins: [mixin], mixins: [mixin],
data() { data() {
@@ -77,7 +73,7 @@
}) })
}, },
/** /**
* 密码登录 * 密码登录(改为调用后端 /api/sys/auth/login 接口)
*/ */
pwdLogin() { pwdLogin() {
if (!this.password.length) { if (!this.password.length) {
@@ -96,40 +92,71 @@
duration: 3000 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) { if (this.needAgreements && !this.agree) {
return this.$refs.agreements.popup(this.pwdLogin) return this.$refs.agreements.popup(this.pwdLogin)
} }
let data = { // 组装后端登录参数(根据后端 LoginRequest 约定,这里假定为 userName + password
"password": this.password, const payload = {
"captcha": this.captcha userName: this.username,
password: this.password
} }
if (/^1\d{10}$/.test(this.username)) { uni.showLoading({
data.mobile = this.username title: '正在登录...',
} else if (/@/.test(this.username)) { mask: true
data.email = this.username })
} else {
data.username = this.username 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
})
} }
uniIdCo.login(data).then(e => { const loginData = body.data
this.loginSuccess(e) // 持久化后端登录态,供后续接口使用
}).catch(e => { try {
if (e.errCode == 'uni-id-captcha-required') { uni.setStorageSync('backend-login-response', loginData)
this.needCaptcha = true uni.setStorageSync('backend-token', loginData.token || '')
} else if (this.needCaptcha) { uni.setStorageSync('backend-user-id', loginData.userId)
//登录失败,自动重新获取验证码 } catch (e) {
this.$refs.captcha.getImageCaptcha() 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()
} }
}) })
}, },