f
This commit is contained in:
59
src/stores/userStore.js
Normal file
59
src/stores/userStore.js
Normal file
@@ -0,0 +1,59 @@
|
||||
import { defineStore } from 'pinia'
|
||||
|
||||
export const useUserStore = defineStore('user', {
|
||||
state: () => ({
|
||||
userName: '',
|
||||
mobile: '',
|
||||
userAvatar: '',
|
||||
isLoggedIn: false,
|
||||
}),
|
||||
actions: {
|
||||
async fetchUserInfo() {
|
||||
const { data, error } = await useApiFetch('/user/detail').get().json()
|
||||
if (data.value && !error.value) {
|
||||
if (data.value.code === 200) {
|
||||
const userinfo = data.value.data.userInfo
|
||||
this.userName = userinfo.mobile || ''
|
||||
this.mobile = userinfo.mobile || ''
|
||||
this.userAvatar = userinfo.userAvatar
|
||||
this.isLoggedIn = true
|
||||
|
||||
// 保存到localStorage
|
||||
localStorage.setItem(
|
||||
'userInfo',
|
||||
JSON.stringify({
|
||||
nickName: this.userName,
|
||||
avatar: this.userAvatar,
|
||||
})
|
||||
)
|
||||
} else if (data.value.code === 100009) {
|
||||
localStorage.removeItem('token')
|
||||
localStorage.removeItem('refreshAfter')
|
||||
localStorage.removeItem('accessExpire')
|
||||
localStorage.removeItem('userInfo')
|
||||
localStorage.removeItem('agentInfo')
|
||||
|
||||
this.resetUser()
|
||||
window.location.reload()
|
||||
}
|
||||
} else {
|
||||
}
|
||||
},
|
||||
|
||||
// 更新用户信息
|
||||
updateUserInfo(userInfo) {
|
||||
if (userInfo) {
|
||||
this.userName = userInfo.mobile || userInfo.nickName || ''
|
||||
this.userAvatar = userInfo.avatar || ''
|
||||
this.isLoggedIn = true
|
||||
}
|
||||
},
|
||||
|
||||
// 重置用户信息
|
||||
resetUser() {
|
||||
this.userName = ''
|
||||
this.userAvatar = ''
|
||||
this.isLoggedIn = false
|
||||
},
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user