feat(jd): 京东工具台纳入后台菜单权限——V209 app菜单组jd_tools+两子行, 预授权沿V163全授组行; 用户端工具台卡片/首页入口按权限显隐(接口失败兜底全显, 同亚马逊形状); 首页computePermissionState抽薄home-permission.ts并修只授京东误亮亚马逊的兜底缺陷
This commit is contained in:
@@ -64,9 +64,10 @@
|
||||
data-column-key="wb"
|
||||
>视频</router-link>
|
||||
|
||||
<!-- 京东入口:一级入口,不挂在亚马逊/视频的权限树里(京东工具台自带两个菜单,
|
||||
权限收口在工具台内部;入口恒显示,避免账号还没配京东菜单时入口凭空消失)。 -->
|
||||
<!-- 京东入口(V209 起纳入 app 菜单权限):jd_tools 组行或其子项/祖先命中即可见。
|
||||
权限接口异常时退回显示(与亚马逊/视频入口同一兜底),不空白。 -->
|
||||
<router-link
|
||||
v-if="canShow.jd"
|
||||
:to="jdHref"
|
||||
class="entrance-btn jd"
|
||||
:class="{ disabled: !jdHref }"
|
||||
@@ -81,7 +82,8 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, ref } from 'vue'
|
||||
import { restoreLoginUser } from '@/shared/auth/ensure-auth'
|
||||
import { getCurrentUserAppColumnRaw, readCachedAppColumnPermissions, type PermissionMenuItem } from '@/shared/api/permission'
|
||||
import { getCurrentUserAppColumnRaw, readCachedAppColumnPermissions } from '@/shared/api/permission'
|
||||
import { computePermissionState, type PermissionState } from './home-permission'
|
||||
import { useVersionUpdate } from '@/shared/composables/useVersionUpdate'
|
||||
import UpdateProgressBar from '@/shared/components/UpdateProgressBar.vue'
|
||||
import UpdateLogList from '@/shared/components/UpdateLogList.vue'
|
||||
@@ -116,10 +118,10 @@ const {
|
||||
// 桌面端原 Flask /logout 已随瘦身下线:统一跳登录页并清本地 token(/login?logout=1)
|
||||
const logoutHref = computed(() => `${resolvePageHref('/new_web_source/login.html')}?logout=1`)
|
||||
|
||||
// 桌面端首页仅保留一级入口:亚马逊 / 视频;图片已收进视频页(/image-video)二级菜单。
|
||||
// 桌面端首页仅保留一级入口:亚马逊 / 视频 / 京东;图片已收进视频页(/image-video)二级菜单。
|
||||
// 入口先乐观显示(接口返回后按权限隐藏),避免权限接口慢时首页入口空白"点不了"。
|
||||
type PermissionState = { amazon: boolean; video: boolean }
|
||||
const permissionState = ref<PermissionState>({ amazon: true, video: true })
|
||||
// 权限计算在 ./home-permission(纯函数,tests/desktop-home-permission.test.ts 直测)。
|
||||
const permissionState = ref<PermissionState>({ amazon: true, video: true, jd: true })
|
||||
const permissionUnavailable = ref(false)
|
||||
|
||||
const amazonHref = computed(() => resolvePageHref('/new_web_source/amazon-console.html'))
|
||||
@@ -128,7 +130,7 @@ const videoHref = computed(() => resolvePageHref('/new_web_source/image-video.ht
|
||||
const jdHref = computed(() => resolvePageHref('/new_web_source/jd-console.html'))
|
||||
|
||||
const canShow = computed(() =>
|
||||
permissionUnavailable.value ? { amazon: true, video: true } : permissionState.value,
|
||||
permissionUnavailable.value ? { amazon: true, video: true, jd: true } : permissionState.value,
|
||||
)
|
||||
|
||||
function getUid() {
|
||||
@@ -137,36 +139,6 @@ function getUid() {
|
||||
return Number.isFinite(numeric) && numeric > 0 ? String(numeric) : ''
|
||||
}
|
||||
|
||||
function computePermissionState(items: PermissionMenuItem[]): PermissionState {
|
||||
const allowedKeys: string[] = []
|
||||
const allowedColumnKeys: string[] = []
|
||||
const allowedRootKeys: string[] = []
|
||||
let hasRootMetadata = false
|
||||
const push = (list: string[], value: unknown) => {
|
||||
const key = String(value || '').trim().toLowerCase()
|
||||
if (key && list.indexOf(key) < 0) list.push(key)
|
||||
}
|
||||
;(items || []).forEach((item) => {
|
||||
push(allowedColumnKeys, item.columnKey)
|
||||
push(allowedColumnKeys, item.column_key)
|
||||
const root = item.rootColumnKey || item.root_column_key
|
||||
if (root) {
|
||||
hasRootMetadata = true
|
||||
push(allowedRootKeys, root)
|
||||
}
|
||||
;[item.columnKey, item.column_key, item.routePath, item.route_path].forEach((v) => push(allowedKeys, v))
|
||||
})
|
||||
const videoVisible = allowedKeys.indexOf('wb') >= 0
|
||||
let amazonVisible =
|
||||
allowedRootKeys.indexOf('brand_front_tools') >= 0 ||
|
||||
allowedRootKeys.indexOf('brand_operation_tools') >= 0 ||
|
||||
allowedRootKeys.indexOf('brand_logistics_tools') >= 0
|
||||
if (!hasRootMetadata) {
|
||||
amazonVisible = allowedColumnKeys.some((key) => key !== 'wb')
|
||||
}
|
||||
return { amazon: amazonVisible, video: videoVisible }
|
||||
}
|
||||
|
||||
async function loadPermissions() {
|
||||
const currentUid = getUid()
|
||||
if (!currentUid) {
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* 桌面首页一级入口的权限计算(亚马逊 / 视频 / 京东)。
|
||||
*
|
||||
* 2026-11-17 自 DesktopHomePage.vue 原样抽薄(逻辑逐字未动):这段判据此前只活在 SFC 里、
|
||||
* 零测试;V209 把京东纳入 app 菜单权限并顺手收紧"只授京东的账号被误判亚马逊可见"的兜底,
|
||||
* 必须有用例钉住,故抽成纯函数模块由 tests/desktop-home-permission.test.ts 直测。
|
||||
* 数据源不变:shared/api/permission 的原始菜单项(含 localStorage 缓存路径),页面不得直连 /newApi。
|
||||
*/
|
||||
import type { PermissionMenuItem } from '@/shared/api/permission'
|
||||
|
||||
/** 京东侧权限键(V209 app 菜单):组行 jd_tools + 子行 jd-collect/jd-publish(route slug jd-tools)。 */
|
||||
export const JD_PERMISSION_KEYS: ReadonlySet<string> = new Set([
|
||||
'jd_tools',
|
||||
'jd-tools',
|
||||
'jd-collect',
|
||||
'jd-publish',
|
||||
])
|
||||
|
||||
export type PermissionState = { amazon: boolean; video: boolean; jd: boolean }
|
||||
|
||||
export function computePermissionState(items: PermissionMenuItem[]): PermissionState {
|
||||
const allowedKeys: string[] = []
|
||||
const allowedColumnKeys: string[] = []
|
||||
const allowedRootKeys: string[] = []
|
||||
let hasRootMetadata = false
|
||||
const push = (list: string[], value: unknown) => {
|
||||
const key = String(value || '').trim().toLowerCase()
|
||||
if (key && list.indexOf(key) < 0) list.push(key)
|
||||
}
|
||||
;(items || []).forEach((item) => {
|
||||
push(allowedColumnKeys, item.columnKey)
|
||||
push(allowedColumnKeys, item.column_key)
|
||||
const root = item.rootColumnKey || item.root_column_key
|
||||
if (root) {
|
||||
hasRootMetadata = true
|
||||
push(allowedRootKeys, root)
|
||||
}
|
||||
;[item.columnKey, item.column_key, item.routePath, item.route_path].forEach((v) => push(allowedKeys, v))
|
||||
})
|
||||
const videoVisible = allowedKeys.indexOf('wb') >= 0
|
||||
// 京东入口:组键命中(V209 预授权授的就是组行),或只授了子项时靠行的 root_column_key 祖先元数据命中。
|
||||
const jdVisible = allowedKeys.indexOf('jd_tools') >= 0 || allowedRootKeys.indexOf('jd_tools') >= 0
|
||||
let amazonVisible =
|
||||
allowedRootKeys.indexOf('brand_front_tools') >= 0 ||
|
||||
allowedRootKeys.indexOf('brand_operation_tools') >= 0 ||
|
||||
allowedRootKeys.indexOf('brand_logistics_tools') >= 0
|
||||
if (!hasRootMetadata) {
|
||||
// 旧权限接口不带 root 元数据时的兜底。原式只排除 'wb':V209 后"只授了京东"的账号会
|
||||
// 在这里被误判亚马逊可见(进去是空卡片墙),顺手把京东键一并排除——同形债不再留第二份。
|
||||
amazonVisible = allowedColumnKeys.some((key) => key !== 'wb' && !JD_PERMISSION_KEYS.has(key))
|
||||
}
|
||||
return { amazon: amazonVisible, video: videoVisible, jd: jdVisible }
|
||||
}
|
||||
@@ -31,11 +31,11 @@
|
||||
<i class="group-dot"></i>
|
||||
<span class="group-name">京东工具</span>
|
||||
<span class="group-desc">采集与上架,两条主线</span>
|
||||
<span class="group-count">{{ JD_TOOLS.length }} 个工具</span>
|
||||
<span class="group-count">{{ visibleTools.length }} 个工具</span>
|
||||
</header>
|
||||
|
||||
<div class="card-grid">
|
||||
<template v-for="tool in JD_TOOLS" :key="tool.id">
|
||||
<template v-for="tool in visibleTools" :key="tool.id">
|
||||
<router-link
|
||||
v-if="tool.href"
|
||||
:to="tool.href"
|
||||
@@ -89,11 +89,15 @@ import { onBeforeUnmount, onMounted, ref } from 'vue'
|
||||
|
||||
import NotificationBell from '@/shared/components/NotificationBell.vue'
|
||||
import { resolvePageHref } from '@/shared/page-prefix'
|
||||
import { JD_TOOLS, type JdToolInfo } from '@/pages/jd/tool-catalog'
|
||||
import { getCurrentUserAppColumnKeys } from '@/shared/api/permission'
|
||||
import { filterJdToolsByPermission, JD_TOOLS, type JdToolInfo } from '@/pages/jd/tool-catalog'
|
||||
|
||||
/** 返回桌面入口首页(亚马逊 / 京东 / 视频 一级入口) */
|
||||
const homeHref = resolvePageHref('/new_web_source/home.html')
|
||||
|
||||
/** 卡片显隐先乐观全量(接口返回后按菜单权限过滤,同 AmazonConsolePage 形状) */
|
||||
const visibleTools = ref<JdToolInfo[]>([...JD_TOOLS])
|
||||
|
||||
const username = ref('admin')
|
||||
const toastText = ref('')
|
||||
let toastTimer: number | undefined
|
||||
@@ -114,9 +118,21 @@ function onPendingClick(tool: JdToolInfo) {
|
||||
showToast(`${tool.name}暂未开通,敬请期待`)
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
onMounted(async () => {
|
||||
const localName = (window.localStorage.getItem('username') || '').trim()
|
||||
if (localName) username.value = localName
|
||||
// 判据与 AmazonConsolePage 同形:权限接口异常 ⇒ permissionUnavailable ⇒ 展示全部工具,
|
||||
// 正常 ⇒ 按 app 菜单权限过滤(V209:组键 jd_tools 整放,否则逐项 jd-collect/jd-publish)
|
||||
let allowedKeys: string[] = []
|
||||
let permissionUnavailable = false
|
||||
try {
|
||||
allowedKeys = await getCurrentUserAppColumnKeys()
|
||||
} catch (_error) {
|
||||
permissionUnavailable = true
|
||||
}
|
||||
visibleTools.value = permissionUnavailable
|
||||
? [...JD_TOOLS]
|
||||
: filterJdToolsByPermission(JD_TOOLS, allowedKeys)
|
||||
})
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
|
||||
@@ -6,6 +6,12 @@
|
||||
* 2. 商品上架:功能未落地,先按"假菜单"占位(点击提示暂未开通),不发 href。
|
||||
*
|
||||
* 卡片形状照亚马逊工具台(tool-catalog.ts)的 ToolInfo,样式复用同一套深色卡片墙。
|
||||
*
|
||||
* 菜单权限(V209):permissionKeys 与后端 columns(menu_type='app') 的 column_key 逐字一致
|
||||
* (jd_tools 组行 / jd-collect / jd-publish),跨端契约见 tests/jd-console-tool-catalog.test.ts。
|
||||
* 卡片显隐过滤用本文件的 filterJdToolsByPermission——判据与亚马逊 filterGroupsByPermission
|
||||
* 同形(组键命中整放,否则逐项),不直接导入是因为 ToolGroup.key 是 'front'|'ops'|'logi'
|
||||
* 字面量联合且组形状带颜色/工作流等必填件,京东借不了那个类型;改语义两头都要过用例。
|
||||
*/
|
||||
|
||||
export type JdToolInfo = {
|
||||
@@ -19,8 +25,13 @@ export type JdToolInfo = {
|
||||
tag?: string
|
||||
/** 功能页地址;未提供表示暂未开通(占位菜单) */
|
||||
href?: string
|
||||
/** 菜单权限键(小写),与后端 app 菜单 column_key 逐字一致 */
|
||||
permissionKeys: string[]
|
||||
}
|
||||
|
||||
/** app 菜单组键(columns.column_key='jd_tools'):勾中整组放行,同亚马逊 brand_*_tools 语义 */
|
||||
export const JD_GROUP_COLUMN_KEY = 'jd_tools'
|
||||
|
||||
export const JD_TOOLS: ReadonlyArray<JdToolInfo> = [
|
||||
{
|
||||
id: 'jd-collect',
|
||||
@@ -28,15 +39,33 @@ export const JD_TOOLS: ReadonlyArray<JdToolInfo> = [
|
||||
desc: '一键抓取已登录京麦后台的店铺商品(列表 + 详情全部属性 + 图片),导入后台「京东数据库」',
|
||||
tag: '核心',
|
||||
href: '/jd-collect',
|
||||
permissionKeys: ['jd-collect'],
|
||||
},
|
||||
{
|
||||
id: 'jd-publish',
|
||||
name: '商品上架',
|
||||
desc: '把京东数据库里的商品批量上架到京麦店铺(建设中,敬请期待)',
|
||||
tag: '规划中',
|
||||
permissionKeys: ['jd-publish'],
|
||||
},
|
||||
]
|
||||
|
||||
/**
|
||||
* 按菜单权限过滤京东工具卡片(与亚马逊 tool-catalog.ts::filterGroupsByPermission 同形判据):
|
||||
* 组键 jd_tools 命中则整组放行(预授权授的就是组行,读侧由后端展开子孙);
|
||||
* 否则只放行 permissionKeys 命中的卡片;空权限集 ⇒ 全隐藏。
|
||||
*/
|
||||
export function filterJdToolsByPermission(
|
||||
tools: ReadonlyArray<JdToolInfo>,
|
||||
allowedKeys: ReadonlyArray<string>,
|
||||
): JdToolInfo[] {
|
||||
const allowedSet = new Set(allowedKeys.map((key) => String(key || '').trim().toLowerCase()).filter(Boolean))
|
||||
if (allowedSet.has(JD_GROUP_COLUMN_KEY)) {
|
||||
return [...tools]
|
||||
}
|
||||
return tools.filter((tool) => tool.permissionKeys.some((key) => allowedSet.has(key.toLowerCase())))
|
||||
}
|
||||
|
||||
export const JD_TOOL_MAP: ReadonlyMap<string, JdToolInfo> = new Map(
|
||||
JD_TOOLS.map((tool) => [tool.id, tool] as const),
|
||||
)
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
|
||||
import { computePermissionState } from '../src/pages/home/home-permission.ts'
|
||||
|
||||
/**
|
||||
* 桌面首页一级入口权限计算(V209 把京东纳入 app 菜单权限)。
|
||||
* 判据形状:组行直授 / 子行直授带 root 元数据 / 无 root 元数据旧接口兜底,三条路都过。
|
||||
*/
|
||||
|
||||
type Item = Record<string, unknown>
|
||||
|
||||
function item(columnKey: string, rootColumnKey?: string, routePath?: string): Item {
|
||||
const it: Item = { id: columnKey, column_key: columnKey }
|
||||
if (rootColumnKey) it.root_column_key = rootColumnKey
|
||||
if (routePath) it.route_path = routePath
|
||||
return it
|
||||
}
|
||||
|
||||
test('组行 jd_tools 直授 ⇒ 京东入口可见', () => {
|
||||
const state = computePermissionState([item('jd_tools', 'jd_tools', 'jd-tools')])
|
||||
assert.equal(state.jd, true)
|
||||
})
|
||||
|
||||
test('只授子行(带 root_column_key=jd_tools 祖先元数据)⇒ 京东入口仍可见', () => {
|
||||
// V209 预授权授的是组行,但管理员收组后单授采集卡是合法配置:入口不能凭空消失。
|
||||
const state = computePermissionState([item('jd-collect', 'jd_tools', 'jd-collect')])
|
||||
assert.equal(state.jd, true)
|
||||
})
|
||||
|
||||
test('权限集与京东无关 ⇒ 京东入口隐藏', () => {
|
||||
const state = computePermissionState([item('wb', 'wb', 'image-video'), item('collect-data', 'brand_front_tools')])
|
||||
assert.equal(state.jd, false)
|
||||
assert.equal(state.video, true)
|
||||
})
|
||||
|
||||
test('只授京东 ⇒ 亚马逊入口不得假显示(有 root 元数据路)', () => {
|
||||
const state = computePermissionState([item('jd_tools', 'jd_tools', 'jd-tools')])
|
||||
assert.equal(state.jd, true)
|
||||
assert.equal(state.amazon, false)
|
||||
})
|
||||
|
||||
test('只授京东 ⇒ 亚马逊入口不得假显示(旧接口无 root 元数据兜底路,V209 顺手钉的判据)', () => {
|
||||
// 原实现兜底只排除 'wb':V209 后 jd 键混进兜底集合会被误判 amazon=true(点进去空卡片墙)。
|
||||
const state = computePermissionState([item('jd_tools'), item('jd-collect')])
|
||||
assert.equal(state.amazon, false, '无 root 元数据时,仅授京东键不得点亮亚马逊入口')
|
||||
assert.equal(state.jd, true)
|
||||
})
|
||||
|
||||
test('旧接口兜底不误伤既有语义:亚马逊子键仍点亮亚马逊入口', () => {
|
||||
const state = computePermissionState([item('collect-data')])
|
||||
assert.equal(state.amazon, true)
|
||||
})
|
||||
|
||||
test('大小写/空白归一:JD_TOOLS 照命中', () => {
|
||||
const state = computePermissionState([{ id: 1, column_key: ' JD_TOOLS ' }])
|
||||
assert.equal(state.jd, true)
|
||||
})
|
||||
@@ -3,7 +3,12 @@ import test from 'node:test'
|
||||
import { existsSync, readFileSync } from 'node:fs'
|
||||
import { join } from 'node:path'
|
||||
|
||||
import { JD_TOOLS, JD_TOOL_MAP } from '../src/pages/jd/tool-catalog.ts'
|
||||
import {
|
||||
filterJdToolsByPermission,
|
||||
JD_GROUP_COLUMN_KEY,
|
||||
JD_TOOLS,
|
||||
JD_TOOL_MAP,
|
||||
} from '../src/pages/jd/tool-catalog.ts'
|
||||
|
||||
/** 京东一级入口(v1.5):首页「京东」→ 京东工作台两个菜单(获取店铺数据 / 商品上架占位)。 */
|
||||
|
||||
@@ -54,3 +59,40 @@ test('获取店铺数据页是一键触发 + 服务端进度(任务面板改
|
||||
assert.ok(page.includes('jd-shop-collect-run'), '建单后必须推送本机客户端队列')
|
||||
assert.ok(page.includes('TaskCenterPanel'), '改版后右侧必须是共享任务面板(含历史任务层)')
|
||||
})
|
||||
|
||||
/* ===== V209 菜单权限:permissionKeys 与后端 app 菜单逐字对撞 + 过滤判据 ===== */
|
||||
|
||||
test('两张卡片都声明 permissionKeys 且与 V209 迁移 column_key 逐字一致', () => {
|
||||
// 跨仓对撞(后管 company-menu-route.test.ts 同族判据):迁移插的 app 菜单键与前端
|
||||
// permissionKeys 任何一侧改名而另一侧不动 ⇒ 这里红,而不是线上静默"勾了不显示"。
|
||||
const sqlPath = join(ROOT, '..', 'backend-java', 'src', 'main', 'resources', 'db', 'V209__jd_app_menu.sql')
|
||||
assert.ok(existsSync(sqlPath), `迁移缺失:${sqlPath}(V209 与前端权限键必须同窗改动)`)
|
||||
const sql = readFileSync(sqlPath, 'utf8')
|
||||
const frontendKeys = new Set<string>([JD_GROUP_COLUMN_KEY, ...JD_TOOLS.flatMap((tool) => tool.permissionKeys)])
|
||||
for (const key of frontendKeys) {
|
||||
assert.ok(sql.includes(`'${key}'`), `V209 迁移缺 column_key:${key}`)
|
||||
}
|
||||
for (const route of ['jd-tools', 'jd-collect', 'jd-publish']) {
|
||||
assert.ok(sql.includes(`'${route}'`), `V209 迁移缺 route_path:${route}`)
|
||||
}
|
||||
for (const tool of JD_TOOLS) {
|
||||
assert.ok(tool.permissionKeys.length > 0, `卡片 ${tool.id} 必须声明 permissionKeys`)
|
||||
}
|
||||
})
|
||||
|
||||
test('按菜单权限过滤京东卡片(与亚马逊 filterGroupsByPermission 同形判据)', () => {
|
||||
// 空权限集 ⇒ 全隐藏
|
||||
assert.deepEqual(filterJdToolsByPermission(JD_TOOLS, []), [])
|
||||
// 组键命中 ⇒ 整组放行(V209 预授权授的就是组行)
|
||||
assert.equal(filterJdToolsByPermission(JD_TOOLS, ['jd_tools']).length, 2)
|
||||
// 只授采集 ⇒ 只剩采集卡
|
||||
const collectOnly = filterJdToolsByPermission(JD_TOOLS, ['jd-collect'])
|
||||
assert.deepEqual(collectOnly.map((tool) => tool.id), ['jd-collect'])
|
||||
// 只授上架 ⇒ 只剩上架卡(占位卡转正前同样受控)
|
||||
const publishOnly = filterJdToolsByPermission(JD_TOOLS, ['jd-publish'])
|
||||
assert.deepEqual(publishOnly.map((tool) => tool.id), ['jd-publish'])
|
||||
// 大小写/空白归一(亚马逊侧同款 normalize:trim + lowercase)
|
||||
assert.equal(filterJdToolsByPermission(JD_TOOLS, [' JD_TOOLS ']).length, 2)
|
||||
// 无关键不放行任何卡片
|
||||
assert.deepEqual(filterJdToolsByPermission(JD_TOOLS, ['collect-data', 'wb']), [])
|
||||
})
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* 京东工作台卡片按 app 菜单权限(V209)显隐的挂载用例。
|
||||
*
|
||||
* 四态判据,与 AmazonConsolePage 同形:
|
||||
* ① 组键 jd_tools 命中 ⇒ 两张卡都在(V209 预授权授的就是组行,线上默认形态);
|
||||
* ② 只授 jd-collect ⇒ 只剩采集卡(上架占位同样受控,转正后行为不变);
|
||||
* ③ 权限集与京东无关 ⇒ 两张卡都隐藏;
|
||||
* ④ 权限接口异常 ⇒ 展示全部(现用人不因接口抖动丢功能)。
|
||||
*
|
||||
* 跑法:cd frontend-vue && npx vitest run tests/mount/jd-console-permission.mount.test.ts
|
||||
*/
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { mount, type VueWrapper } from '@vue/test-utils'
|
||||
|
||||
vi.mock('@/shared/api/permission', () => ({
|
||||
getCurrentUserAppColumnKeys: vi.fn(),
|
||||
}))
|
||||
|
||||
import { getCurrentUserAppColumnKeys } from '@/shared/api/permission'
|
||||
import JdConsolePage from '@/pages/jd/JdConsolePage.vue'
|
||||
|
||||
let wrapper: VueWrapper | null = null
|
||||
|
||||
const mockKeys = (keys: string[]) =>
|
||||
vi.mocked(getCurrentUserAppColumnKeys).mockResolvedValue(keys)
|
||||
|
||||
async function mountConsole() {
|
||||
wrapper = mount(JdConsolePage, {
|
||||
global: {
|
||||
stubs: {
|
||||
NotificationBell: true,
|
||||
RouterLink: { template: '<a class="router-link-stub"><slot /></a>' },
|
||||
'router-link': { template: '<a class="router-link-stub"><slot /></a>' },
|
||||
},
|
||||
},
|
||||
})
|
||||
// onMounted 里权限是异步 await,等过滤落地后再断言
|
||||
await vi.waitFor(() => {
|
||||
expect(vi.mocked(getCurrentUserAppColumnKeys).mock.calls.length).toBeGreaterThan(0)
|
||||
})
|
||||
return wrapper
|
||||
}
|
||||
|
||||
describe('京东工作台按菜单权限显隐(V209)', () => {
|
||||
beforeEach(() => {
|
||||
window.localStorage.setItem('username', 'admin')
|
||||
vi.mocked(getCurrentUserAppColumnKeys).mockReset()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
wrapper?.unmount()
|
||||
wrapper = null
|
||||
window.localStorage.clear()
|
||||
})
|
||||
|
||||
it('组键命中 ⇒ 两张卡都在', async () => {
|
||||
mockKeys(['jd_tools', 'jd-tools'])
|
||||
const w = await mountConsole()
|
||||
await vi.waitFor(() => expect(w.findAll('.tool-card').length).toBe(2))
|
||||
})
|
||||
|
||||
it('只授 jd-collect ⇒ 只剩获取店铺数据卡', async () => {
|
||||
mockKeys(['jd-collect'])
|
||||
const w = await mountConsole()
|
||||
await vi.waitFor(() => expect(w.findAll('.tool-card').length).toBe(1))
|
||||
// 断言圈在卡片节点上:hero 文案("店铺采集 · 商品上架")与计数是页面静态区,
|
||||
// 不参与权限过滤,拿整页 text() 判"商品上架消失"会误红。
|
||||
const cards = w.findAll('.tool-card')
|
||||
expect(cards[0].text()).toContain('获取店铺数据')
|
||||
expect(cards.every((card) => !card.classes().includes('tool-card--pending'))).toBe(true)
|
||||
expect(w.find('.group-count').text()).toContain('1 个工具')
|
||||
})
|
||||
|
||||
it('权限集与京东无关 ⇒ 两张卡都隐藏', async () => {
|
||||
mockKeys(['wb', 'collect-data'])
|
||||
const w = await mountConsole()
|
||||
await vi.waitFor(() => expect(w.findAll('.tool-card').length).toBe(0))
|
||||
expect(w.findAll('.tool-card').length).toBe(0)
|
||||
})
|
||||
|
||||
it('权限接口异常 ⇒ 展示全部(兜底不让现用人丢入口)', async () => {
|
||||
vi.mocked(getCurrentUserAppColumnKeys).mockRejectedValue(new Error('获取菜单权限失败'))
|
||||
const w = await mountConsole()
|
||||
await vi.waitFor(() => expect(w.findAll('.tool-card').length).toBe(2))
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user