改造后台权限和APP权限
This commit is contained in:
63
frontend-vue/src/shared/api/permission.ts
Normal file
63
frontend-vue/src/shared/api/permission.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { requestGetJson } from '@/shared/api/http'
|
||||
|
||||
export interface PermissionMenuItem {
|
||||
id: number | string
|
||||
name?: string
|
||||
column_key?: string
|
||||
route_path?: string
|
||||
menu_type?: string
|
||||
sort_order?: number
|
||||
created_at?: string
|
||||
}
|
||||
|
||||
interface PermissionMenuResponse {
|
||||
success: boolean
|
||||
items?: PermissionMenuItem[]
|
||||
error?: string
|
||||
}
|
||||
|
||||
function getCurrentUserId() {
|
||||
const raw = typeof window === 'undefined' ? '' : window.localStorage.getItem('uid') || ''
|
||||
const value = Number(raw)
|
||||
if (!Number.isFinite(value) || value <= 0) {
|
||||
throw new Error('未获取到用户ID')
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
function getAppPermissionCacheKey(uid: number) {
|
||||
return `app_column_permissions:${String(uid)}`
|
||||
}
|
||||
|
||||
export async function getCurrentUserAppColumnKeys() {
|
||||
const uid = getCurrentUserId()
|
||||
const cacheKey = getAppPermissionCacheKey(uid)
|
||||
|
||||
try {
|
||||
const cachedItems = JSON.parse(window.localStorage.getItem(cacheKey) || 'null') as PermissionMenuItem[] | null
|
||||
if (Array.isArray(cachedItems)) {
|
||||
return cachedItems
|
||||
.map((item) => String(item.column_key || '').trim().toLowerCase())
|
||||
.filter(Boolean)
|
||||
}
|
||||
} catch (_error) {}
|
||||
|
||||
const res = await requestGetJson<PermissionMenuResponse>(
|
||||
`/api/admin/user/${encodeURIComponent(String(uid))}/column-permissions`,
|
||||
{
|
||||
params: { menu_type: 'app' },
|
||||
},
|
||||
)
|
||||
|
||||
if (!res.success) {
|
||||
throw new Error(res.error || '获取菜单权限失败')
|
||||
}
|
||||
|
||||
try {
|
||||
window.localStorage.setItem(cacheKey, JSON.stringify(res.items || []))
|
||||
} catch (_error) {}
|
||||
|
||||
return (res.items || [])
|
||||
.map((item) => String(item.column_key || '').trim().toLowerCase())
|
||||
.filter(Boolean)
|
||||
}
|
||||
Reference in New Issue
Block a user