Files
lingji-work-fe/src/stores/modules/user/index.ts

88 lines
1.7 KiB
TypeScript
Raw Normal View History

2025-06-22 22:28:47 -04:00
/*
* @Author: RenXiaoDong
* @Date: 2025-06-22 22:26:22
*/
2025-06-20 06:10:15 -04:00
import { defineStore } from 'pinia';
2025-06-16 14:42:26 +08:00
2025-06-21 15:31:21 +08:00
interface UserInfo {
id: number;
name: string;
head_image: String;
current_enterprise_id: number;
mobile: string;
// 添加其他用户属性...
}
interface CompanyInfo {
id: number;
name: string;
// 添加其他公司属性...
}
2025-06-16 14:42:26 +08:00
interface UserState {
2025-06-21 15:31:21 +08:00
token: string;
userInfo: UserInfo | null;
companyInfo: CompanyInfo | null;
isLogin: boolean;
}
interface UserInfo {
id: number;
mobile: string;
name: string;
2025-06-16 14:42:26 +08:00
}
export const useUserStore = defineStore('user', {
state: (): UserState => ({
2025-06-20 06:10:15 -04:00
token: localStorage.getItem('accessToken') || '',
2025-06-21 15:31:21 +08:00
userInfo: null,
companyInfo: null,
2025-06-16 14:42:26 +08:00
isLogin: false,
}),
actions: {
// 设置 Token
setToken(token: string) {
this.token = `Bearer ${token}`;
localStorage.setItem('accessToken', this.token);
2025-06-16 14:42:26 +08:00
},
2025-06-22 22:28:47 -04:00
deleteToken() {
this.token = '';
localStorage.removeItem('accessToken');
},
2025-06-21 15:31:21 +08:00
// 获取 Token
getToken() {
return this.token;
2025-06-16 14:42:26 +08:00
},
// 设置用户信息
2025-06-21 15:31:21 +08:00
setUserInfo(userInfo: UserInfo | null) {
this.userInfo = userInfo;
},
// 获取用户信息
getUserInfo(): UserInfo | null {
return this.userInfo;
2025-06-21 15:31:21 +08:00
},
// 设置公司信息
2025-06-21 15:31:21 +08:00
setCompanyInfo(companyInfo: CompanyInfo | null) {
this.companyInfo = companyInfo;
},
// 获取公司信息
getCompanyInfo(): CompanyInfo | null {
return this.companyInfo;
2025-06-21 15:31:21 +08:00
},
// 登录状态
setIsLogin(isLogin: boolean) {
this.isLogin = isLogin;
2025-06-16 14:42:26 +08:00
},
getIsLogin(): boolean {
return this.isLogin;
2025-06-22 22:28:47 -04:00
},
},
2025-06-16 14:42:26 +08:00
});