角色 保存
This commit is contained in:
@@ -75,9 +75,9 @@
|
||||
})
|
||||
},
|
||||
/**
|
||||
* 密码登录(改为调用后端 /api/sys/auth/login 接口)
|
||||
* 密码登录(调用后端 /api/sys/auth/login 接口)
|
||||
*/
|
||||
pwdLogin() {
|
||||
async pwdLogin() {
|
||||
if (!this.password.length) {
|
||||
this.focusPassword = true
|
||||
return uni.showToast({
|
||||
@@ -98,7 +98,7 @@
|
||||
return this.$refs.agreements.popup(this.pwdLogin)
|
||||
}
|
||||
|
||||
// 组装后端登录参数(根据后端 LoginRequest 约定,这里假定为 userName + password)
|
||||
// 组装后端登录参数
|
||||
const payload = {
|
||||
userName: this.username,
|
||||
password: this.password
|
||||
@@ -109,79 +109,100 @@
|
||||
mask: true
|
||||
})
|
||||
|
||||
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)
|
||||
|
||||
// 保存 tenantId(从 LoginResponse 中获取,后端 UserServiceImpl.login 从 user.getTenantId() 赋值)
|
||||
// 后续所有请求会自动在 X-Tenant-Id header 中携带此值
|
||||
if (loginData.tenantId) {
|
||||
uni.setStorageSync('backend-tenant-id', loginData.tenantId)
|
||||
console.log('[Login] tenantId 已保存:', loginData.tenantId)
|
||||
} else {
|
||||
console.warn('[Login] 登录响应中未包含 tenantId,后续请求可能失败')
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('缓存登录信息失败:', e)
|
||||
}
|
||||
|
||||
// 同步更新前端 store,保证“我的”页等能识别为已登录状态
|
||||
try {
|
||||
mutations.setUserInfo({
|
||||
username: loginData.userName,
|
||||
mobile: loginData.phone,
|
||||
email: loginData.email,
|
||||
avatar_file: loginData.avatar ? { url: loginData.avatar } : undefined
|
||||
}, { cover: true })
|
||||
} catch (e) {
|
||||
console.error('更新本地用户信息失败:', e)
|
||||
}
|
||||
|
||||
uni.showToast({
|
||||
title: body.message || '登录成功',
|
||||
icon: 'none',
|
||||
duration: 800
|
||||
try {
|
||||
// 使用 uni.request 直接调用登录接口(登录接口不需要 token)
|
||||
const res = await new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
url: getApiUrl('/api/sys/auth/login'),
|
||||
method: 'POST',
|
||||
header: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
data: payload,
|
||||
success: resolve,
|
||||
fail: reject
|
||||
})
|
||||
})
|
||||
|
||||
// 登录成功后跳转到 tabBar 首页(接待页)
|
||||
setTimeout(() => {
|
||||
uni.switchTab({
|
||||
url: '/pages/reception/reception'
|
||||
})
|
||||
}, 300)
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('调用登录接口失败:', err)
|
||||
uni.showToast({
|
||||
title: '网络异常,登录失败',
|
||||
const body = res.data || {}
|
||||
if (!body.success || !body.data) {
|
||||
return uni.showToast({
|
||||
title: body.message || '登录失败',
|
||||
icon: 'none',
|
||||
duration: 3000
|
||||
})
|
||||
},
|
||||
complete: () => {
|
||||
uni.hideLoading()
|
||||
}
|
||||
})
|
||||
|
||||
const loginData = body.data
|
||||
|
||||
// 保存 token(重要:后续所有请求会自动在 header 中携带此 token)
|
||||
if (loginData.token) {
|
||||
uni.setStorageSync('backend-token', loginData.token)
|
||||
console.log('[Login] token 已保存,后续请求将自动在 header 中携带')
|
||||
} else {
|
||||
console.warn('[Login] 登录响应中未包含 token')
|
||||
}
|
||||
|
||||
// 持久化其他登录信息
|
||||
try {
|
||||
uni.setStorageSync('backend-login-response', loginData)
|
||||
uni.setStorageSync('backend-user-id', loginData.userId)
|
||||
|
||||
// 保存 tenantId(后续所有请求会自动在 X-Tenant-Id header 中携带)
|
||||
if (loginData.tenantId) {
|
||||
uni.setStorageSync('backend-tenant-id', loginData.tenantId)
|
||||
console.log('[Login] tenantId 已保存:', loginData.tenantId)
|
||||
} else {
|
||||
console.warn('[Login] 登录响应中未包含 tenantId,后续请求可能失败')
|
||||
}
|
||||
|
||||
// 保存 roleName 和 scenario,后续所有请求会自动在 header 中携带
|
||||
if (loginData.roleName) {
|
||||
uni.setStorageSync('backend-role-name', loginData.roleName)
|
||||
console.log('[Login] roleName 已保存:', loginData.roleName)
|
||||
}
|
||||
if (loginData.scenario) {
|
||||
uni.setStorageSync('backend-scenario', loginData.scenario)
|
||||
console.log('[Login] scenario 已保存:', loginData.scenario)
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('缓存登录信息失败:', e)
|
||||
}
|
||||
|
||||
// 同步更新前端 store,保证"我的"页等能识别为已登录状态
|
||||
try {
|
||||
mutations.setUserInfo({
|
||||
username: loginData.userName,
|
||||
mobile: loginData.phone,
|
||||
email: loginData.email,
|
||||
avatar_file: loginData.avatar ? { url: loginData.avatar } : undefined
|
||||
}, { cover: true })
|
||||
} catch (e) {
|
||||
console.error('更新本地用户信息失败:', e)
|
||||
}
|
||||
|
||||
uni.showToast({
|
||||
title: body.message || '登录成功',
|
||||
icon: 'none',
|
||||
duration: 800
|
||||
})
|
||||
|
||||
// 登录成功后跳转到 tabBar 首页(接待页)
|
||||
setTimeout(() => {
|
||||
uni.switchTab({
|
||||
url: '/pages/reception/reception'
|
||||
})
|
||||
}, 300)
|
||||
} catch (err) {
|
||||
console.error('调用登录接口失败:', err)
|
||||
uni.showToast({
|
||||
title: '网络异常,登录失败',
|
||||
icon: 'none',
|
||||
duration: 3000
|
||||
})
|
||||
} finally {
|
||||
uni.hideLoading()
|
||||
}
|
||||
},
|
||||
/* 前往注册 */
|
||||
toRegister() {
|
||||
|
||||
Reference in New Issue
Block a user