|
|
@@ -3,28 +3,20 @@ import {defineStore} from 'pinia';
|
|
|
import {useTabStore} from './tab';
|
|
|
|
|
|
type State = {
|
|
|
- user: {
|
|
|
- id: string,
|
|
|
- realName: string,
|
|
|
- token: string,
|
|
|
- userName: string,
|
|
|
- department: string,
|
|
|
- role: string,
|
|
|
- }
|
|
|
+ id: string,
|
|
|
+ realName: string,
|
|
|
+ token: string,
|
|
|
};
|
|
|
|
|
|
type Action = {
|
|
|
- init: (state: State['user']) => void;
|
|
|
+ init: (state: State) => void;
|
|
|
logout: () => void;
|
|
|
};
|
|
|
|
|
|
-const defaultUser: State['user'] = Object.freeze({
|
|
|
+const defaultUser: State = Object.freeze({
|
|
|
id: '',
|
|
|
realName: '',
|
|
|
token: '',
|
|
|
- userName: '',
|
|
|
- department: '',
|
|
|
- role: '',
|
|
|
});
|
|
|
|
|
|
export const useUserStore = defineStore<string, State, any, Action>(
|
|
|
@@ -32,23 +24,23 @@ export const useUserStore = defineStore<string, State, any, Action>(
|
|
|
{
|
|
|
state() {
|
|
|
const userLocalStorage = sessionStorage.getItem(USER_STORAGE_KEY);
|
|
|
- const user: State['user'] = {...defaultUser};
|
|
|
+ const user: State = {...defaultUser};
|
|
|
if (userLocalStorage) {
|
|
|
Object.assign(
|
|
|
user,
|
|
|
- JSON.parse(userLocalStorage) as State['user'],
|
|
|
+ JSON.parse(userLocalStorage) as State,
|
|
|
);
|
|
|
}
|
|
|
|
|
|
- return {user};
|
|
|
+ return user;
|
|
|
},
|
|
|
actions: {
|
|
|
init(state) {
|
|
|
- this.user = state;
|
|
|
+ this.$patch(state);
|
|
|
},
|
|
|
logout() {
|
|
|
// 清空用户信息
|
|
|
- this.user = {...defaultUser};
|
|
|
+ this.$patch({...defaultUser});
|
|
|
// 清空tab信息
|
|
|
const tabStore = useTabStore();
|
|
|
tabStore.clear();
|