mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-28 10:03:54 +08:00
update: mobile 缓存用户信息
This commit is contained in:
@@ -15,7 +15,7 @@ import { computed, onMounted, ref } from 'vue'
|
|||||||
import { login } from '@/api/auth'
|
import { login } from '@/api/auth'
|
||||||
// 导入国际化相关功能
|
// 导入国际化相关功能
|
||||||
import { changeLanguage, getCurrentLanguage, getSupportedLanguages, initI18n, t } from '@/i18n'
|
import { changeLanguage, getCurrentLanguage, getSupportedLanguages, initI18n, t } from '@/i18n'
|
||||||
import { useConfigStore } from '@/store'
|
import { useConfigStore, useUserStore } from '@/store'
|
||||||
// 导入SM2加密工具
|
// 导入SM2加密工具
|
||||||
import { getEnvBaseUrl, sm2Encrypt } from '@/utils'
|
import { getEnvBaseUrl, sm2Encrypt } from '@/utils'
|
||||||
import { toast } from '@/utils/toast'
|
import { toast } from '@/utils/toast'
|
||||||
@@ -61,6 +61,7 @@ const loginType = ref<'username' | 'mobile'>('username')
|
|||||||
|
|
||||||
// 获取配置store
|
// 获取配置store
|
||||||
const configStore = useConfigStore()
|
const configStore = useConfigStore()
|
||||||
|
const userStore = useUserStore()
|
||||||
|
|
||||||
// 区号选择相关
|
// 区号选择相关
|
||||||
const showAreaCodeSheet = ref(false)
|
const showAreaCodeSheet = ref(false)
|
||||||
@@ -227,6 +228,7 @@ async function handleLogin() {
|
|||||||
const response = await login(loginData)
|
const response = await login(loginData)
|
||||||
// 存储token
|
// 存储token
|
||||||
uni.setStorageSync('token', JSON.stringify(response))
|
uni.setStorageSync('token', JSON.stringify(response))
|
||||||
|
await userStore.getUserInfo()
|
||||||
|
|
||||||
toast.success(t('message.loginSuccess'))
|
toast.success(t('message.loginSuccess'))
|
||||||
|
|
||||||
@@ -581,7 +583,6 @@ onMounted(async () => {
|
|||||||
|
|
||||||
.input-wrapper {
|
.input-wrapper {
|
||||||
position: relative;
|
position: relative;
|
||||||
background: #f8f9fa;
|
|
||||||
border-radius: 16rpx;
|
border-radius: 16rpx;
|
||||||
padding: 20rpx 16rpx;
|
padding: 20rpx 16rpx;
|
||||||
border: 2rpx solid #e9ecef;
|
border: 2rpx solid #e9ecef;
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ const userInfoState: UserInfo & { avatar?: string, token?: string } = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const useUserStore = defineStore(
|
export const useUserStore = defineStore(
|
||||||
'user',
|
'userInfo',
|
||||||
() => {
|
() => {
|
||||||
// 定义用户信息
|
// 定义用户信息
|
||||||
const userInfo = ref<UserInfo & { avatar?: string, token?: string }>({ ...userInfoState })
|
const userInfo = ref<UserInfo & { avatar?: string, token?: string }>({ ...userInfoState })
|
||||||
@@ -51,16 +51,8 @@ export const useUserStore = defineStore(
|
|||||||
*/
|
*/
|
||||||
const getUserInfo = async () => {
|
const getUserInfo = async () => {
|
||||||
const userData = await _getUserInfo()
|
const userData = await _getUserInfo()
|
||||||
const authInfo = JSON.parse(uni.getStorageSync('token') || '{}')
|
setUserInfo(userData)
|
||||||
const userInfoWithExtras = {
|
return userData
|
||||||
...userData,
|
|
||||||
avatar: userInfoState.avatar,
|
|
||||||
token: authInfo.token || '',
|
|
||||||
}
|
|
||||||
setUserInfo(userInfoWithExtras)
|
|
||||||
uni.setStorageSync('userInfo', userInfoWithExtras)
|
|
||||||
// TODO 这里可以增加获取用户路由的方法 根据用户的角色动态生成路由
|
|
||||||
return userInfoWithExtras
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 退出登录 并 删除用户信息
|
* 退出登录 并 删除用户信息
|
||||||
@@ -79,6 +71,12 @@ export const useUserStore = defineStore(
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
persist: true,
|
persist: {
|
||||||
|
key: 'userInfo',
|
||||||
|
serializer: {
|
||||||
|
serialize: state => JSON.stringify(state.userInfo),
|
||||||
|
deserialize: value => ({ userInfo: JSON.parse(value) }),
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user