feat: 全局获取userinfo、数据持久化、store处理

This commit is contained in:
renxiaodong
2025-06-23 22:03:57 -04:00
parent 55198613a8
commit 59dac3bb13
10 changed files with 104 additions and 75 deletions

View File

@ -1,4 +1,5 @@
import { defineStore } from 'pinia';
import { fetchProfileInfo } from '@/api/all/login';
interface UserInfo {
id: number;
@ -19,7 +20,7 @@ interface UserState {
token: string;
userInfo: UserInfo | null;
companyInfo: CompanyInfo | null;
isLogin: boolean;
// isLogin: boolean;
}
interface UserInfo {
@ -33,7 +34,6 @@ export const useUserStore = defineStore('user', {
token: localStorage.getItem('accessToken') || '',
userInfo: null,
companyInfo: null,
isLogin: false,
}),
getters: {
isLogin(): boolean {
@ -46,6 +46,7 @@ export const useUserStore = defineStore('user', {
this.token = `Bearer ${token}`;
localStorage.setItem('accessToken', this.token);
},
deleteToken() {
this.token = '';
localStorage.removeItem('accessToken');
@ -62,18 +63,11 @@ export const useUserStore = defineStore('user', {
},
// 获取用户信息
getUserInfo(): UserInfo | null {
return this.userInfo;
},
// 设置公司信息
setCompanyInfo(companyInfo: CompanyInfo | null) {
this.companyInfo = companyInfo;
},
// 获取公司信息
getCompanyInfo(): CompanyInfo | null {
return this.companyInfo;
async fetchUserInfo() {
const { code, data } = await fetchProfileInfo();
if (code === 200) {
this.setUserInfo(data);
}
},
},
});