修复菜单消失
This commit is contained in:
@@ -71,6 +71,8 @@ type NavItem = {
|
||||
key: string
|
||||
label: string
|
||||
href?: string
|
||||
columnKey?: string
|
||||
aliases?: ReadonlyArray<string>
|
||||
}
|
||||
|
||||
type NavGroup = {
|
||||
@@ -79,17 +81,21 @@ type NavGroup = {
|
||||
items: ReadonlyArray<NavItem>
|
||||
}
|
||||
|
||||
const props = defineProps<{
|
||||
const props = withDefaults(defineProps<{
|
||||
active: ActiveNavKey
|
||||
showNav?: boolean
|
||||
showHomeLink?: boolean
|
||||
showActions?: boolean
|
||||
}>()
|
||||
}>(), {
|
||||
showNav: true,
|
||||
showHomeLink: true,
|
||||
showActions: true,
|
||||
})
|
||||
|
||||
const active = props.active
|
||||
const showNav = props.showNav ?? true
|
||||
const showHomeLink = props.showHomeLink ?? true
|
||||
const showActions = props.showActions ?? true
|
||||
const showNav = props.showNav
|
||||
const showHomeLink = props.showHomeLink
|
||||
const showActions = props.showActions
|
||||
const allowedColumnKeys = ref<string[] | null>(null)
|
||||
|
||||
const navGroups: ReadonlyArray<NavGroup> = [
|
||||
@@ -115,7 +121,7 @@ const navGroups: ReadonlyArray<NavGroup> = [
|
||||
{ key: 'delete-brand', label: '删除ASIN', href: '/new_web_source/delete-brand.html' },
|
||||
{ key: 'product-risk', label: '商品风险解决', href: '/new_web_source/product-risk.html' },
|
||||
{ key: 'shop-match', label: '定时匹配', href: '/new_web_source/shop-match.html' },
|
||||
{ key: 'pricing', label: '跟价', href: '/new_web_source/price-track.html' },
|
||||
{ key: 'pricing', label: '跟价', href: '/new_web_source/price-track.html', aliases: ['price-track'] },
|
||||
{ key: 'patrol-delete', label: '巡店删除', href: '/new_web_source/patrol-delete.html' },
|
||||
{ key: 'query-asin', label: '查询ASIN', href: '/new_web_source/query-asin.html' },
|
||||
{ key: 'withdraw', label: '取款' },
|
||||
@@ -132,12 +138,29 @@ const navGroups: ReadonlyArray<NavGroup> = [
|
||||
},
|
||||
]
|
||||
|
||||
function getItemPermissionKeys(item: NavItem) {
|
||||
return [item.key, item.columnKey, ...(item.aliases || [])]
|
||||
.map((key) => String(key || '').trim().toLowerCase())
|
||||
.filter(Boolean)
|
||||
}
|
||||
|
||||
const visibleNavGroups = computed(() => {
|
||||
if (allowedColumnKeys.value === null) {
|
||||
return navGroups
|
||||
}
|
||||
const allowedSet = new Set(allowedColumnKeys.value)
|
||||
return navGroups.filter((group) => allowedSet.has(group.columnKey))
|
||||
|
||||
const groups = navGroups.flatMap((group) => {
|
||||
if (allowedSet.has(group.columnKey)) {
|
||||
return [group]
|
||||
}
|
||||
const items = group.items.filter((item) =>
|
||||
getItemPermissionKeys(item).some((key) => allowedSet.has(key)),
|
||||
)
|
||||
return items.length ? [{ ...group, items }] : []
|
||||
})
|
||||
|
||||
return groups.length ? groups : navGroups
|
||||
})
|
||||
|
||||
onMounted(async () => {
|
||||
|
||||
@@ -4,7 +4,9 @@ export interface PermissionMenuItem {
|
||||
id: number | string
|
||||
name?: string
|
||||
column_key?: string
|
||||
columnKey?: string
|
||||
route_path?: string
|
||||
routePath?: string
|
||||
menu_type?: string
|
||||
sort_order?: number
|
||||
created_at?: string
|
||||
@@ -12,8 +14,10 @@ export interface PermissionMenuItem {
|
||||
|
||||
interface PermissionMenuResponse {
|
||||
success: boolean
|
||||
data?: PermissionMenuItem[]
|
||||
items?: PermissionMenuItem[]
|
||||
error?: string
|
||||
message?: string
|
||||
}
|
||||
|
||||
function getCurrentUserId() {
|
||||
@@ -29,6 +33,23 @@ function getAppPermissionCacheKey(uid: number) {
|
||||
return `app_column_permissions:${String(uid)}`
|
||||
}
|
||||
|
||||
function getAuthToken() {
|
||||
return typeof window === 'undefined' ? '' : window.localStorage.getItem('aiimage_auth_token') || ''
|
||||
}
|
||||
|
||||
function normalizeColumnKeys(items: PermissionMenuItem[] | undefined) {
|
||||
const keys = new Set<string>()
|
||||
for (const item of items || []) {
|
||||
for (const value of [item.column_key, item.columnKey, item.route_path, item.routePath]) {
|
||||
const key = String(value || '').trim().toLowerCase()
|
||||
if (key) {
|
||||
keys.add(key)
|
||||
}
|
||||
}
|
||||
}
|
||||
return Array.from(keys)
|
||||
}
|
||||
|
||||
export async function getCurrentUserAppColumnKeys() {
|
||||
const uid = getCurrentUserId()
|
||||
const cacheKey = getAppPermissionCacheKey(uid)
|
||||
@@ -36,28 +57,35 @@ export async function getCurrentUserAppColumnKeys() {
|
||||
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)
|
||||
const cachedKeys = normalizeColumnKeys(cachedItems)
|
||||
if (cachedKeys.length) {
|
||||
return cachedKeys
|
||||
}
|
||||
}
|
||||
} catch (_error) {}
|
||||
|
||||
const headers: Record<string, string> = {}
|
||||
const token = getAuthToken()
|
||||
if (token) {
|
||||
headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
|
||||
const res = await requestGetJson<PermissionMenuResponse>(
|
||||
`/api/admin/user/${encodeURIComponent(String(uid))}/column-permissions`,
|
||||
`/newApi/api/admin/permission-users/${encodeURIComponent(String(uid))}/column-permissions`,
|
||||
{
|
||||
params: { menu_type: 'app' },
|
||||
params: { menuType: 'app' },
|
||||
headers,
|
||||
},
|
||||
)
|
||||
|
||||
if (!res.success) {
|
||||
throw new Error(res.error || '获取菜单权限失败')
|
||||
throw new Error(res.error || res.message || '获取菜单权限失败')
|
||||
}
|
||||
|
||||
const items = res.data || res.items || []
|
||||
try {
|
||||
window.localStorage.setItem(cacheKey, JSON.stringify(res.items || []))
|
||||
window.localStorage.setItem(cacheKey, JSON.stringify(items))
|
||||
} catch (_error) {}
|
||||
|
||||
return (res.items || [])
|
||||
.map((item) => String(item.column_key || '').trim().toLowerCase())
|
||||
.filter(Boolean)
|
||||
return normalizeColumnKeys(items)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user