优化
This commit is contained in:
@@ -7,11 +7,38 @@ import {
|
||||
} from '@/core/navigation/adminNavigation'
|
||||
import { useAuthStore } from '@/core/auth/auth'
|
||||
|
||||
const LOCAL_FULL_ACCESS_STORAGE_KEY = 'baiyue-admin-local-full-access'
|
||||
|
||||
class LocalFullAccessPermissionSet extends Set<string> {
|
||||
override has(_permissionCode: string): boolean {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
export function isLocalFullAccessHost(hostname: string): boolean {
|
||||
return hostname === 'localhost' || hostname === '127.0.0.1' || hostname === '::1'
|
||||
}
|
||||
|
||||
export const useAccessPreviewStore = defineStore('access-preview', () => {
|
||||
const auth = useAuthStore()
|
||||
const initialized = ref(false)
|
||||
const permissionCodes = computed<ReadonlySet<string>>(() => auth.permissionCodes)
|
||||
const permissionSignature = computed(() => [...permissionCodes.value].sort().join('|'))
|
||||
const localFullAccessAvailable = import.meta.env.DEV
|
||||
&& typeof window !== 'undefined'
|
||||
&& isLocalFullAccessHost(window.location.hostname)
|
||||
const localFullAccessEnabled = ref(
|
||||
localFullAccessAvailable
|
||||
&& window.localStorage.getItem(LOCAL_FULL_ACCESS_STORAGE_KEY) === 'true',
|
||||
)
|
||||
const permissionCodes = computed<ReadonlySet<string>>(() => (
|
||||
localFullAccessEnabled.value
|
||||
? new LocalFullAccessPermissionSet(auth.permissionCodes)
|
||||
: auth.permissionCodes
|
||||
))
|
||||
const permissionSignature = computed(() => (
|
||||
localFullAccessEnabled.value
|
||||
? '__LOCAL_FULL_ACCESS__'
|
||||
: [...permissionCodes.value].sort().join('|')
|
||||
))
|
||||
const filteredNavigation = computed(() => filterAdminNavigation(adminNavigation, permissionCodes.value))
|
||||
const firstAccessiblePath = computed(() => getFirstNavigationLeaf(filteredNavigation.value)?.path ?? null)
|
||||
|
||||
@@ -23,18 +50,33 @@ export const useAccessPreviewStore = defineStore('access-preview', () => {
|
||||
return permissionCodes.value.has(permissionCode)
|
||||
}
|
||||
|
||||
function setLocalFullAccessEnabled(enabled: boolean) {
|
||||
if (!localFullAccessAvailable) return
|
||||
localFullAccessEnabled.value = enabled
|
||||
if (enabled) window.localStorage.setItem(LOCAL_FULL_ACCESS_STORAGE_KEY, 'true')
|
||||
else window.localStorage.removeItem(LOCAL_FULL_ACCESS_STORAGE_KEY)
|
||||
}
|
||||
|
||||
function toggleLocalFullAccess() {
|
||||
setLocalFullAccessEnabled(!localFullAccessEnabled.value)
|
||||
}
|
||||
|
||||
function reset() {
|
||||
initialized.value = false
|
||||
}
|
||||
|
||||
return {
|
||||
initialized,
|
||||
localFullAccessAvailable,
|
||||
localFullAccessEnabled,
|
||||
permissionCodes,
|
||||
permissionSignature,
|
||||
filteredNavigation,
|
||||
firstAccessiblePath,
|
||||
initializeAfterLogin,
|
||||
can,
|
||||
setLocalFullAccessEnabled,
|
||||
toggleLocalFullAccess,
|
||||
reset,
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user