feat: 全局获取userinfo、数据持久化、store处理
This commit is contained in:
@ -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);
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user