| | |
| | | import { storeToRefs } from 'pinia' |
| | | import { useUserStore } from '@/store/modules/user' |
| | | import { useAppMode } from '@/hooks/core/useAppMode' |
| | | const userStore = useUserStore() |
| | | |
| | | function extractRouteAuthMarks(authList) { |
| | | if (!Array.isArray(authList)) { |
| | | return [] |
| | | } |
| | | |
| | | return authList |
| | | .map((item) => { |
| | | if (typeof item === 'string') { |
| | | return item |
| | | } |
| | | if (item && typeof item === 'object') { |
| | | return item.authMark || '' |
| | | } |
| | | return '' |
| | | }) |
| | | .filter(Boolean) |
| | | } |
| | | |
| | | function extractUserButtons(info) { |
| | | return Array.isArray(info?.buttons) ? info.buttons : [] |
| | | } |
| | | |
| | | const BUTTON_ACTION_MAP = { |
| | | query: 'list', |
| | | add: 'save', |
| | | edit: 'update', |
| | | delete: 'remove' |
| | | } |
| | | |
| | | function resolveRouteResourceKey(routePath) { |
| | | const pathSegments = String(routePath || '') |
| | | .split('/') |
| | | .filter(Boolean) |
| | | |
| | | const rawSegment = pathSegments[pathSegments.length - 1] |
| | | if (!rawSegment) { |
| | | return '' |
| | | } |
| | | |
| | | return rawSegment.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase()) |
| | | } |
| | | |
| | | function matchesBackendButton(requiredAuth, buttons, routePath) { |
| | | if (buttons.includes(requiredAuth)) { |
| | | return true |
| | | } |
| | | |
| | | const action = BUTTON_ACTION_MAP[requiredAuth] |
| | | const resourceKey = resolveRouteResourceKey(routePath) |
| | | if (!action || !resourceKey) { |
| | | return false |
| | | } |
| | | |
| | | return buttons.some( |
| | | (item) => |
| | | typeof item === 'string' && item.includes(`:${resourceKey}:`) && item.endsWith(`:${action}`) |
| | | ) |
| | | } |
| | | |
| | | function hasAuthPermission( |
| | | requiredAuth, |
| | | { authList = [], buttons = [], isBackendMode = false, routePath = '' } = {} |
| | | ) { |
| | | const requiredList = Array.isArray(requiredAuth) ? requiredAuth : [requiredAuth] |
| | | |
| | | if (!requiredList.length) { |
| | | return true |
| | | } |
| | | |
| | | if (isBackendMode) { |
| | | return requiredList.some((item) => matchesBackendButton(item, buttons, routePath)) |
| | | } |
| | | |
| | | return requiredList.some((item) => authList.includes(item)) |
| | | } |
| | | |
| | | const useAuth = () => { |
| | | const userStore = useUserStore() |
| | | const route = useRoute() |
| | | const { isFrontendMode } = useAppMode() |
| | | const { isBackendMode } = useAppMode() |
| | | const { info } = storeToRefs(userStore) |
| | | const frontendAuthList = info.value?.buttons ?? [] |
| | | const backendAuthList = Array.isArray(route.meta.authList) ? route.meta.authList : [] |
| | | const authList = computed(() => extractRouteAuthMarks(route.meta.authList)) |
| | | const buttons = computed(() => extractUserButtons(info.value)) |
| | | const hasAuth = (auth) => { |
| | | if (isFrontendMode.value) { |
| | | return frontendAuthList.includes(auth) |
| | | } |
| | | return backendAuthList.some((item) => item?.authMark === auth) |
| | | return hasAuthPermission(auth, { |
| | | authList: authList.value, |
| | | buttons: buttons.value, |
| | | isBackendMode: isBackendMode.value, |
| | | routePath: route.path |
| | | }) |
| | | } |
| | | return { |
| | | hasAuth |
| | | } |
| | | } |
| | | export { useAuth } |
| | | export { |
| | | extractRouteAuthMarks, |
| | | extractUserButtons, |
| | | hasAuthPermission, |
| | | resolveRouteResourceKey, |
| | | useAuth |
| | | } |