2021-08-13 14:22:35 +08:00
|
|
|
import { defineStore } from 'pinia';
|
|
|
|
import type { UserInfo } from '@/interface';
|
|
|
|
import { store } from '../../index';
|
2021-05-29 03:02:15 +08:00
|
|
|
|
|
|
|
interface AuthState {
|
2021-08-13 14:22:35 +08:00
|
|
|
/** 用户token */
|
2021-05-29 03:02:15 +08:00
|
|
|
token: string;
|
2021-08-13 14:22:35 +08:00
|
|
|
/** 用户信息 */
|
2021-05-29 03:02:15 +08:00
|
|
|
userInfo: UserInfo;
|
|
|
|
}
|
|
|
|
|
2021-08-13 14:22:35 +08:00
|
|
|
const authStore = defineStore({
|
|
|
|
/** 区分不通状态的唯一标识 */
|
|
|
|
id: 'auth-store',
|
|
|
|
/** 状态 */
|
|
|
|
state: (): AuthState => {
|
|
|
|
return {
|
|
|
|
token: '',
|
|
|
|
userInfo: {
|
|
|
|
userId: '',
|
|
|
|
userName: '',
|
|
|
|
userPhone: ''
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
getters: {
|
|
|
|
/** 是否登录 */
|
|
|
|
isLogin: state => Boolean(state.token)
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
/** 重置auth状态 */
|
|
|
|
resetAuthState() {
|
|
|
|
this.$reset();
|
2021-05-29 03:02:15 +08:00
|
|
|
}
|
2021-08-13 14:22:35 +08:00
|
|
|
}
|
|
|
|
});
|
2021-05-29 03:02:15 +08:00
|
|
|
|
2021-08-13 14:22:35 +08:00
|
|
|
export default function useAuthStore() {
|
|
|
|
return authStore(store);
|
2021-05-29 03:02:15 +08:00
|
|
|
}
|