Files
JayuCloud/FD-Frontend/src/utils/permission.ts
T
2026-06-15 15:53:55 +08:00

151 lines
6.4 KiB
TypeScript

import { permissionApi } from '@/api/permission'
import { computed, ref } from 'vue'
import { isBootstrapAdminUser, loadCachedAdminUserInfo } from '@/utils/admin-session'
import { isAllowedForShoplessPlatformAdmin } from '@/utils/platform-admin-permissions'
/**
* Purpose: USER_PERMISSIONS_STORAGE_KEY supports permission.ts by holding or executing the user permissions storage key page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const USER_PERMISSIONS_STORAGE_KEY = 'userPermissions'
/**
* Purpose: readPermissionsFromStorage supports permission.ts by holding or executing the read permissions from storage page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const readPermissionsFromStorage = () => {
const raw = localStorage.getItem(USER_PERMISSIONS_STORAGE_KEY)
if (!raw) {
return null
}
try {
const parsed = JSON.parse(raw)
return Array.isArray(parsed) ? parsed.map(item => String(item)) : null
} catch (error) {
console.error('Failed to parse stored user permissions:', error)
localStorage.removeItem(USER_PERMISSIONS_STORAGE_KEY)
return null
}
}
/**
* Purpose: userPermissions supports permission.ts by holding or executing the user permissions page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const userPermissions = ref<string[]>(readPermissionsFromStorage() || [])
/**
* Purpose: loadingPermissions supports permission.ts by holding or executing the loading permissions page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const loadingPermissions = ref(false)
/**
* Purpose: isPlatformAdminUser supports permission.ts by holding or executing the is platform admin user page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const isPlatformAdminUser = () => Number(loadCachedAdminUserInfo()?.type) === 4
/**
* Purpose: getStoredUserPermissions supports permission.ts by holding or executing the get stored user permissions page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
export const getStoredUserPermissions = () => readPermissionsFromStorage()
/**
* Purpose: setStoredUserPermissions supports permission.ts by holding or executing the set stored user permissions page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
export const setStoredUserPermissions = (permissions: string[]) => {
userPermissions.value = Array.from(new Set((permissions || []).map(code => String(code))))
localStorage.setItem(USER_PERMISSIONS_STORAGE_KEY, JSON.stringify(userPermissions.value))
return userPermissions.value
}
/**
* Purpose: clearStoredPermissions supports permission.ts by holding or executing the clear stored permissions page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
export const clearStoredPermissions = () => {
userPermissions.value = []
localStorage.removeItem(USER_PERMISSIONS_STORAGE_KEY)
}
/**
* Purpose: fetchUserPermissionsStrict supports permission.ts by holding or executing the fetch user permissions strict page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
export const fetchUserPermissionsStrict = async () => {
loadingPermissions.value = true
try {
const permissions = await permissionApi.getUserPermissions()
const normalizedPermissions = permissions && Array.isArray(permissions)
? permissions.map(permission => permission.permissionCode)
: []
return setStoredUserPermissions(normalizedPermissions)
} finally {
loadingPermissions.value = false
}
}
/**
* Purpose: usePermission supports permission.ts by holding or executing the use permission page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
export const usePermission = () => {
const hasPermission = (permissionCode: string) => {
if (isBootstrapAdminUser(loadCachedAdminUserInfo())) {
return isAllowedForShoplessPlatformAdmin(permissionCode)
}
if (isPlatformAdminUser() && isAllowedForShoplessPlatformAdmin(permissionCode)) {
return isAllowedForShoplessPlatformAdmin(permissionCode)
}
return userPermissions.value.includes(permissionCode)
}
const hasAnyPermission = (permissionCodes: string[]) => {
if (isBootstrapAdminUser(loadCachedAdminUserInfo())) {
return permissionCodes.some(isAllowedForShoplessPlatformAdmin)
}
if (isPlatformAdminUser() && permissionCodes.some(isAllowedForShoplessPlatformAdmin)) {
return permissionCodes.some(isAllowedForShoplessPlatformAdmin)
}
return permissionCodes.some(code => userPermissions.value.includes(code))
}
const hasAllPermissions = (permissionCodes: string[]) => {
if (isBootstrapAdminUser(loadCachedAdminUserInfo())) {
return permissionCodes.every(isAllowedForShoplessPlatformAdmin)
}
if (isPlatformAdminUser() && permissionCodes.every(isAllowedForShoplessPlatformAdmin)) {
return permissionCodes.every(isAllowedForShoplessPlatformAdmin)
}
return permissionCodes.every(code => userPermissions.value.includes(code))
}
const loadUserPermissions = async () => {
try {
return await fetchUserPermissionsStrict()
} catch (error) {
console.error('加载用户权限失败:', error)
clearStoredPermissions()
return []
}
}
const clearPermissions = () => {
clearStoredPermissions()
}
return {
userPermissions: computed(() => userPermissions.value),
loadingPermissions: computed(() => loadingPermissions.value),
hasPermission,
hasAnyPermission,
hasAllPermissions,
loadUserPermissions,
clearPermissions
}
}