增加权限系统

This commit is contained in:
yml2213
2026-05-27 05:41:59 +08:00
parent a04327fd05
commit 1458fc279c
29 changed files with 2276 additions and 46 deletions
+18 -1
View File
@@ -1,6 +1,6 @@
import { defineStore } from 'pinia'
import { fetchAdminMe, loginAdmin, type AdminUser } from '@/api/adminAuth'
import { fetchAdminMe, loginAdmin, type AdminRole, type AdminUser } from '@/api/adminAuth'
import { clearAuthStorage, getAccessToken, getRefreshToken, setAuthTokens } from '@/utils/authStorage'
export const useAdminSessionStore = defineStore('adminSession', {
@@ -10,7 +10,20 @@ export const useAdminSessionStore = defineStore('adminSession', {
adminId: Number(localStorage.getItem('admin_id') || 0),
username: localStorage.getItem('admin_username') || '',
nickname: '',
roles: [] as AdminRole[],
permissions: [] as string[],
}),
getters: {
hasPermission: (state) => {
return (code: string) => state.permissions.includes(code) || state.permissions.includes('*')
},
hasRole: (state) => {
return (code: string) => state.roles.some((r) => r.code === code)
},
isSuperAdmin: (state) => {
return state.roles.some((r) => r.code === 'super_admin') || state.permissions.includes('*')
},
},
actions: {
async login(username: string, password: string, captchaId: string, captchaCode: string) {
const result = await loginAdmin(username, password, captchaId, captchaCode)
@@ -28,6 +41,8 @@ export const useAdminSessionStore = defineStore('adminSession', {
this.adminId = 0
this.username = ''
this.nickname = ''
this.roles = []
this.permissions = []
clearAuthStorage('admin')
},
syncFromStorage() {
@@ -49,6 +64,8 @@ export const useAdminSessionStore = defineStore('adminSession', {
this.adminId = admin.id
this.username = admin.username
this.nickname = admin.nickname
this.roles = admin.roles || []
this.permissions = admin.permissions || []
localStorage.setItem('admin_id', String(admin.id))
localStorage.setItem('admin_username', admin.username)
},