feat(admin): 侧边栏顶部加亚马逊/京东平台tab切换(2026-11-16 用户定案「不混一起」)
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
/** 平台 tab 口径的唯一清单(2026-11-16 用户定案「亚马逊/京东不混一起」)。
|
||||
* 仿 company-scope.ts 的形状:单独成文件而不是塞进 routes.ts——护栏用例要从这里
|
||||
* 反读清单与注册表对撞,放在一起会成环。 */
|
||||
|
||||
export type PlatformTabId = 'amazon' | 'jd'
|
||||
|
||||
export interface PlatformTabDef {
|
||||
id: PlatformTabId
|
||||
label: string
|
||||
}
|
||||
|
||||
/** 侧边栏顶部平台 tab 的固定顺序:亚马逊在前(存量主体),京东在后。 */
|
||||
export const PLATFORM_TABS: readonly PlatformTabDef[] = [
|
||||
{ id: 'amazon', label: '亚马逊' },
|
||||
{ id: 'jd', label: '京东' },
|
||||
]
|
||||
|
||||
/** 京东侧菜单的 menuKey 唯一清单(与 routes.ts 的 menuKey 逐字一致,用例钉住)。
|
||||
* 2026-11-16 用户按截图圈定四项;后续京东新页面上线把 key 加进这里即可,
|
||||
* 清单外的 key(含店铺管理/生成记录等存量页)一律归亚马逊。 */
|
||||
export const JD_MENU_KEYS: readonly string[] = [
|
||||
'admin_users',
|
||||
'admin_group_manage',
|
||||
'admin_companies',
|
||||
'admin_jd_database',
|
||||
]
|
||||
|
||||
/** menuKey → 平台归属:命中京东清单即 jd,其余(未知 key 在内)默认亚马逊。 */
|
||||
export function platformOfMenuKey(key: string): PlatformTabId {
|
||||
return JD_MENU_KEYS.includes(key) ? 'jd' : 'amazon'
|
||||
}
|
||||
@@ -6,6 +6,10 @@ import { useAdminSessionStore } from '@/stores/admin-session'
|
||||
import { joinAdminPath } from '@/config/app'
|
||||
import { pageTitleOf, topbarUserOf } from '@/layout/topbar-model'
|
||||
import { groupKeysForActive, toSidebarEntries } from '@/layout/menu-mapper'
|
||||
import { filterMenuTreeByPlatform, visiblePlatformsFor } from '@/layout/platform-menu-filter'
|
||||
import { PLATFORM_TABS, platformOfMenuKey } from '@/config/platform-scope'
|
||||
import { usePlatformTabStore } from '@/stores/platform-tab'
|
||||
import { firstVisiblePath } from '@/router/helpers'
|
||||
import {
|
||||
EMPTY_MENU_DESCRIPTION,
|
||||
EMPTY_MENU_TITLE,
|
||||
@@ -31,7 +35,15 @@ const router = useRouter()
|
||||
const session = useAdminSessionStore()
|
||||
|
||||
const activePath = computed(() => route.path)
|
||||
const menuEntries = computed(() => toSidebarEntries(session.menuTree))
|
||||
const platformTab = usePlatformTabStore()
|
||||
// 平台 tab(亚马逊/京东,2026-11-16「不混」定案)只做显示分组:准入仍是 session.menuTree
|
||||
// (后端按权限裁剪)+路由守卫,这里只把当前用户已被授权的菜单按平台分侧展示。
|
||||
const menuEntries = computed(() =>
|
||||
toSidebarEntries(filterMenuTreeByPlatform(session.menuTree, platformTab.active)),
|
||||
)
|
||||
const visibleTabIds = computed(() => visiblePlatformsFor(session.menuTree))
|
||||
const visibleTabDefs = computed(() => PLATFORM_TABS.filter((tab) => visibleTabIds.value.includes(tab.id)))
|
||||
const showPlatformTabs = computed(() => visibleTabIds.value.length > 1)
|
||||
const openedKeys = computed(() => groupKeysForActive(menuEntries.value, route.path))
|
||||
const emptyMenu = computed(() => shouldShowEmptyMenu(menuEntries.value.length > 0, session.loading, session.initialized))
|
||||
const pageTitle = computed(() => pageTitleOf(route.meta.title))
|
||||
@@ -45,6 +57,30 @@ watch(
|
||||
{ immediate: true },
|
||||
)
|
||||
|
||||
// 直达 URL/面包屑跳到另一平台的页面时,tab 指示自动跟过去(与侧边栏保持一致)。
|
||||
watch(
|
||||
() => route.meta.menuKey as string | undefined,
|
||||
(menuKey) => {
|
||||
if (!menuKey) return
|
||||
const target = platformOfMenuKey(menuKey)
|
||||
if (target !== platformTab.active && visibleTabIds.value.includes(target)) {
|
||||
platformTab.setActive(target)
|
||||
}
|
||||
},
|
||||
{ immediate: true },
|
||||
)
|
||||
|
||||
// 切到另一侧 tab 时当前页多半不在那边:跳到该侧第一个可见页;该侧为空(tab 不可见)则不动。
|
||||
watch(
|
||||
() => platformTab.active,
|
||||
(tab) => {
|
||||
const menuKey = route.meta.menuKey as string | undefined
|
||||
if (!menuKey || platformOfMenuKey(menuKey) === tab) return
|
||||
const first = firstVisiblePath(filterMenuTreeByPlatform(session.menuTree, tab))
|
||||
if (first) void router.replace(first)
|
||||
},
|
||||
)
|
||||
|
||||
async function signOut() {
|
||||
try {
|
||||
await runLogout({
|
||||
@@ -77,6 +113,19 @@ async function signOut() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<nav v-if="showPlatformTabs" class="admin-platform-tabs" role="tablist" aria-label="按平台切换菜单">
|
||||
<button
|
||||
v-for="tab in visibleTabDefs"
|
||||
:key="tab.id"
|
||||
type="button"
|
||||
role="tab"
|
||||
class="admin-platform-tab"
|
||||
:class="{ 'is-active': tab.id === platformTab.active }"
|
||||
:aria-selected="tab.id === platformTab.active"
|
||||
@click="platformTab.setActive(tab.id)"
|
||||
>{{ tab.label }}</button>
|
||||
</nav>
|
||||
|
||||
<el-scrollbar class="admin-menu-scroll">
|
||||
<el-menu :default-active="activePath" :default-openeds="openedKeys" router class="admin-menu">
|
||||
<template v-for="entry in menuEntries" :key="entry.key">
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import type { AdminMenuNode } from '../types/admin'
|
||||
import { PLATFORM_TABS, platformOfMenuKey } from '../config/platform-scope.ts'
|
||||
import type { PlatformTabId } from '../config/platform-scope.ts'
|
||||
import { firstVisiblePath } from '../router/helpers.ts'
|
||||
|
||||
/**
|
||||
* 按平台 tab 过滤后端菜单树(2026-11-16「亚马逊/京东不混」):**只做显示分组,不做鉴权**——
|
||||
* 准入仍由 session.menuTree(后端按权限裁剪)与路由守卫负责,这里只是把当前用户已被授权的
|
||||
* 菜单按平台分到两个 tab 下展示。
|
||||
* 规则:叶子按 platformOfMenuKey(key) 留下;分组节点(admin_group_*)自身的 key 不参与平台
|
||||
* 判定,按「留住的子项数 > 0」决定去留——否则京东 tab 下「账号与权限」这类混合分组会被整组
|
||||
* 误剪。节点的 key/name/route/children 形状原样透传(浅拷贝不改写),toSidebarEntries、
|
||||
* groupKeysForActive 与面包屑都不受影响。
|
||||
*/
|
||||
export function filterMenuTreeByPlatform(
|
||||
nodes: AdminMenuNode[] | null | undefined,
|
||||
platform: PlatformTabId,
|
||||
): AdminMenuNode[] {
|
||||
const filtered: AdminMenuNode[] = []
|
||||
for (const node of nodes || []) {
|
||||
const children = filterMenuTreeByPlatform(node.children, platform)
|
||||
if (children.length > 0) {
|
||||
filtered.push({ ...node, children })
|
||||
continue
|
||||
}
|
||||
if ((node.children || []).length > 0) continue
|
||||
if (platformOfMenuKey(node.key || '') === platform) filtered.push({ ...node })
|
||||
}
|
||||
return filtered
|
||||
}
|
||||
|
||||
/** 当前用户在哪些平台下至少有一个可点页面(tab 显示依据);顺序固定随 PLATFORM_TABS。
|
||||
* 「可点」沿用 firstVisiblePath 这一把尺子(有 route 即可点),不另写第二处判定。 */
|
||||
export function visiblePlatformsFor(nodes: AdminMenuNode[] | null | undefined): PlatformTabId[] {
|
||||
return PLATFORM_TABS
|
||||
.filter((tab) => firstVisiblePath(filterMenuTreeByPlatform(nodes, tab.id)) !== '')
|
||||
.map((tab) => tab.id)
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { PLATFORM_TABS } from '@/config/platform-scope'
|
||||
import type { PlatformTabId } from '@/config/platform-scope'
|
||||
|
||||
const STORAGE_KEY = 'admin-platform-tab'
|
||||
const DEFAULT_TAB: PlatformTabId = 'amazon'
|
||||
|
||||
function isKnownTab(id: unknown): id is PlatformTabId {
|
||||
return PLATFORM_TABS.some((tab) => tab.id === id)
|
||||
}
|
||||
|
||||
/** 读取持久化偏好;无存储/值非法/存储不可用一律回落亚马逊(隐私模式不阻断交互)。 */
|
||||
function readStoredTab(): PlatformTabId {
|
||||
try {
|
||||
const raw = typeof localStorage === 'undefined' ? null : localStorage.getItem(STORAGE_KEY)
|
||||
return isKnownTab(raw) ? raw : DEFAULT_TAB
|
||||
} catch {
|
||||
return DEFAULT_TAB
|
||||
}
|
||||
}
|
||||
|
||||
function writeStoredTab(id: PlatformTabId): void {
|
||||
try {
|
||||
if (typeof localStorage !== 'undefined') localStorage.setItem(STORAGE_KEY, id)
|
||||
} catch {
|
||||
// localStorage 不可用(隐私模式等)时忽略
|
||||
}
|
||||
}
|
||||
|
||||
/** 侧边栏平台 tab(亚马逊/京东)状态:纯显示分组不参与鉴权(2026-11-16 用户定案)。
|
||||
* 菜单可见性仍由 session.menuTree(后端按权限裁剪)决定,这里只记「当前看哪一侧」。 */
|
||||
export const usePlatformTabStore = defineStore('platform-tab', {
|
||||
state: () => ({
|
||||
active: readStoredTab(),
|
||||
}),
|
||||
actions: {
|
||||
setActive(id: PlatformTabId) {
|
||||
this.active = id
|
||||
writeStoredTab(id)
|
||||
},
|
||||
},
|
||||
})
|
||||
@@ -65,6 +65,22 @@ button, input, textarea, select { font: inherit; }
|
||||
.admin-brand-copy strong { color: var(--admin-text); font-size: 15px; font-weight: 600; letter-spacing: .5px; white-space: nowrap; }
|
||||
.admin-brand-copy span { color: #77899b; font-size: 11px; white-space: nowrap; }
|
||||
|
||||
/* 侧边栏顶部平台 tab(亚马逊/京东,2026-11-16「不混」定案):纯显示分组,只对当前用户
|
||||
* 已授权菜单分侧展示;单侧无可见菜单时整条隐藏(AdminLayout showPlatformTabs)。 */
|
||||
.admin-platform-tabs { display: flex; gap: 6px; padding: 10px 12px 0; }
|
||||
.admin-platform-tab {
|
||||
flex: 1; height: 34px; text-align: center; font-size: 13px; font-weight: 600;
|
||||
color: var(--admin-sidebar-text); background: transparent;
|
||||
border: 1px solid var(--admin-border); border-radius: 9px; cursor: pointer;
|
||||
}
|
||||
.admin-platform-tab:hover { color: #1e3b57; background: rgba(47, 93, 139, 0.16); }
|
||||
.admin-platform-tab.is-active {
|
||||
color: var(--admin-primary-strong);
|
||||
background: linear-gradient(180deg, #dce9f5, #edf4fa);
|
||||
border-color: #b6cbe0;
|
||||
box-shadow: inset 0 2px 0 #5f85ad;
|
||||
}
|
||||
|
||||
.admin-menu-scroll { flex: 1; padding: 12px 10px; }
|
||||
.admin-menu { border-right: 0; background: transparent; }
|
||||
.admin-menu .el-sub-menu__title,
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import type { AdminMenuNode } from '../src/types/admin.ts'
|
||||
import { JD_MENU_KEYS, PLATFORM_TABS, platformOfMenuKey } from '../src/config/platform-scope.ts'
|
||||
import { filterMenuTreeByPlatform, visiblePlatformsFor } from '../src/layout/platform-menu-filter.ts'
|
||||
import { adminPages } from '../src/router/routes.ts'
|
||||
|
||||
/** 2026-11-16 用户定案「亚马逊/京东不混一起」:侧边栏顶部加平台 tab,京东侧=截图圈定四项
|
||||
* (用户管理/分组管理/公司管理/京东数据库),其余归亚马逊。tab 只做显示分组——菜单可见性
|
||||
* 仍由 session.menuTree(后端按权限裁剪)决定,这里钉的是「分侧归属」与「空侧隐藏」两条形状。
|
||||
* 辅助定义在所有 test 之前:node:test 的用例回调按声明序执行,挪到文件尾会踩 TDZ。 */
|
||||
|
||||
const node = (key: string, name: string, route?: string, children?: AdminMenuNode[]): AdminMenuNode => {
|
||||
const out: AdminMenuNode = { key, name }
|
||||
if (route) out.route = route
|
||||
if (children) out.children = children
|
||||
return out
|
||||
}
|
||||
|
||||
/** 侧边栏实测形状的缩影:账号与权限(混编京东侧账号菜单与亚马逊侧配置/密钥菜单)+数据管理。 */
|
||||
const TREE: AdminMenuNode[] = [
|
||||
node('admin_group_account', '账号与权限', undefined, [
|
||||
node('admin_columns', '菜单权限配置', '/account/menus'),
|
||||
node('admin_users', '用户管理', '/account/users'),
|
||||
node('admin_group_manage', '分组管理', '/account/groups'),
|
||||
node('admin_user_secrets', '密钥管理', '/account/user-secrets'),
|
||||
node('admin_user_secret_usage', '密钥用量统计', '/account/user-secret-usage'),
|
||||
node('admin_companies', '公司管理', '/account/companies'),
|
||||
]),
|
||||
node('admin_group_data', '数据管理', undefined, [
|
||||
node('admin_dedupe_total_data', '去重数据汇总', '/asin-center/registry'),
|
||||
node('admin_invalid_asin_data', '品牌数据库', '/asin-center/invalid'),
|
||||
node('admin_query_asin', '查询ASIN', '/asin-center/query'),
|
||||
node('admin_appearance_patent_clean_items', '外观专利数据库', '/patent-center/clean-items'),
|
||||
node('admin_jd_database', '京东数据库', '/jd-center/database'),
|
||||
node('admin_product_categories', '商品类目', '/asin-center/categories'),
|
||||
]),
|
||||
]
|
||||
|
||||
function groupChildKeys(filtered: AdminMenuNode[], groupKey: string): string[] {
|
||||
const group = filtered.find((item) => item.key === groupKey)
|
||||
return (group?.children || []).map((child) => child.key)
|
||||
}
|
||||
|
||||
test('menuKey 平台归属:京东清单四项即 jd,其余(含分组 key 与未知 key)一律 amazon', () => {
|
||||
for (const key of JD_MENU_KEYS) assert.equal(platformOfMenuKey(key), 'jd', key)
|
||||
assert.equal(platformOfMenuKey('admin_query_asin'), 'amazon')
|
||||
assert.equal(platformOfMenuKey('admin_columns'), 'amazon')
|
||||
assert.equal(platformOfMenuKey('admin_group_account'), 'amazon')
|
||||
assert.equal(platformOfMenuKey('admin_jd_unknown_future'), 'amazon')
|
||||
assert.equal(platformOfMenuKey(''), 'amazon')
|
||||
})
|
||||
|
||||
test('京东 tab:账号与权限只余圈定三项,数据管理只余京东数据库;顺序与 route 原样透传', () => {
|
||||
const filtered = filterMenuTreeByPlatform(TREE, 'jd')
|
||||
assert.deepEqual(filtered.map((item) => item.key), ['admin_group_account', 'admin_group_data'])
|
||||
assert.deepEqual(groupChildKeys(filtered, 'admin_group_account'),
|
||||
['admin_users', 'admin_group_manage', 'admin_companies'])
|
||||
assert.deepEqual(groupChildKeys(filtered, 'admin_group_data'), ['admin_jd_database'])
|
||||
const jd = filtered[1].children![0]
|
||||
assert.equal(jd.route, '/jd-center/database')
|
||||
assert.equal(jd.name, '京东数据库')
|
||||
})
|
||||
|
||||
test('亚马逊 tab:其余菜单全数保留且分组结构不变', () => {
|
||||
const filtered = filterMenuTreeByPlatform(TREE, 'amazon')
|
||||
assert.deepEqual(groupChildKeys(filtered, 'admin_group_account'),
|
||||
['admin_columns', 'admin_user_secrets', 'admin_user_secret_usage'])
|
||||
assert.deepEqual(groupChildKeys(filtered, 'admin_group_data'),
|
||||
['admin_dedupe_total_data', 'admin_invalid_asin_data', 'admin_query_asin',
|
||||
'admin_appearance_patent_clean_items', 'admin_product_categories'])
|
||||
})
|
||||
|
||||
test('子项全归另一平台的分组整组隐藏(空分组不画);未知 key 默认归亚马逊侧', () => {
|
||||
const mixed: AdminMenuNode[] = [
|
||||
node('admin_group_shop', '店铺管理', undefined, [
|
||||
node('admin_shop_manage', '店铺管理', '/shop-center/shops'),
|
||||
node('admin_shop_keys', '店铺密钥管理', '/shop-center/keys'),
|
||||
]),
|
||||
node('admin_jd_database', '京东数据库', '/jd-center/database'),
|
||||
node('admin_history', '生成记录', '/records/history'),
|
||||
]
|
||||
const jd = filterMenuTreeByPlatform(mixed, 'jd')
|
||||
assert.deepEqual(jd.map((item) => item.key), ['admin_jd_database'],
|
||||
'店铺管理组子项全在亚马逊侧 ⇒ 京东 tab 整组剪掉;生成记录未知 key 默认亚马逊')
|
||||
const amazon = filterMenuTreeByPlatform(mixed, 'amazon')
|
||||
assert.deepEqual(amazon.map((item) => item.key), ['admin_group_shop', 'admin_history'])
|
||||
})
|
||||
|
||||
test('tab 显示依据:两侧都有可见页才画 tab 条,仅一侧授权时另一侧自动隐藏', () => {
|
||||
assert.deepEqual(visiblePlatformsFor(TREE), ['amazon', 'jd'], '顺序固定随 PLATFORM_TABS')
|
||||
const amazonOnly: AdminMenuNode[] = [
|
||||
node('admin_group_data', '数据管理', undefined, [
|
||||
node('admin_dedupe_total_data', '去重数据汇总', '/asin-center/registry'),
|
||||
]),
|
||||
]
|
||||
assert.deepEqual(visiblePlatformsFor(amazonOnly), ['amazon'],
|
||||
'只授权亚马逊侧(无京东数据库)的用户只看到亚马逊 tab')
|
||||
const jdOnly: AdminMenuNode[] = [
|
||||
node('admin_group_data', '数据管理', undefined, [node('admin_jd_database', '京东数据库', '/jd-center/database')]),
|
||||
]
|
||||
assert.deepEqual(visiblePlatformsFor(jdOnly), ['jd'])
|
||||
assert.deepEqual(visiblePlatformsFor([]), [])
|
||||
assert.deepEqual(visiblePlatformsFor(null), [])
|
||||
})
|
||||
|
||||
test('平台 tab 定义与京东清单钉死(2026-11-16 用户圈定口径,防清单漂移)', () => {
|
||||
assert.deepEqual(PLATFORM_TABS, [
|
||||
{ id: 'amazon', label: '亚马逊' },
|
||||
{ id: 'jd', label: '京东' },
|
||||
])
|
||||
assert.deepEqual([...JD_MENU_KEYS].sort(), ['admin_companies', 'admin_group_manage', 'admin_jd_database', 'admin_users'])
|
||||
})
|
||||
|
||||
test('JD_MENU_KEYS 每一条都必须在路由注册表登记(同 company-menu-route 护栏形状:少登记=看得见点进去404)', () => {
|
||||
const registered = new Set(adminPages.map((page) => page.menuKey))
|
||||
for (const key of JD_MENU_KEYS) {
|
||||
assert.ok(registered.has(key), `京东侧菜单 key "${key}" 未在 src/router/routes.ts 登记 menuKey`)
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user