feat: 实现后台管理前端全部页面与登录布局
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import { authApi } from '@/api';
|
||||
import { setToken, clearToken, getToken } from '@/api/request';
|
||||
|
||||
interface AuthUser {
|
||||
id: number;
|
||||
username: string;
|
||||
realName?: string;
|
||||
role: string;
|
||||
}
|
||||
|
||||
export const useAuthStore = defineStore('auth', {
|
||||
state: () => ({
|
||||
token: getToken() || '',
|
||||
user: null as AuthUser | null,
|
||||
}),
|
||||
getters: {
|
||||
isLoggedIn: (state) => !!state.token,
|
||||
isSuperAdmin: (state) => state.user?.role === 'SUPER_ADMIN',
|
||||
canWrite: (state) => state.user?.role === 'SUPER_ADMIN' || state.user?.role === 'OPERATOR',
|
||||
},
|
||||
actions: {
|
||||
async login(username: string, password: string) {
|
||||
const res = await authApi.login({ username, password });
|
||||
this.token = res.token;
|
||||
this.user = res.user;
|
||||
setToken(res.token);
|
||||
},
|
||||
async fetchProfile() {
|
||||
this.user = (await authApi.profile()) as AuthUser;
|
||||
},
|
||||
logout() {
|
||||
this.token = '';
|
||||
this.user = null;
|
||||
clearToken();
|
||||
},
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user