Files
crawler-plugin/admin-frontend-vue/src/pages/jd/JdDatabasePage.vue
T
huangzd1997 2a194d3340 feat(admin-jd): 京东数据库「分组」控件恒渲染下拉(2026-11-23 用户口径「就算一个分组也要下拉,后续分组管理加了新的就要有新的选项」)
- 单组不再退回锁定芯片:删 canSwitchGroup 与 .jd-group-locked 分支/样式(jd-database-page.css+main.css 两处),分组管理新增组后重进页面即多出选项
- 多组切换、作用域语义(重置不清组)、无可见组不渲染三件行为不变
- 挂载测试 7 处断言随口径改写:单组出下拉+选项清单=本组+旧芯片退役反钉
- 判据:vitest jd-database-page.mount 60/60 绿;vue-tsc rc=0;ops 快检四面绿
2026-11-23 17:57:18 +08:00

968 lines
45 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
/** 京东公共商品库(v1.6 蓝图 Phase 5 重写装配:侧栏+卡片网格+居中详情弹窗)。
* V203(2026-11-16 用户口径):分类按「数据权限分组」隔离——不同组看不到彼此的分类,
* 能看多组的人用「分组」下拉切换当前组;单组也出下拉(2026-11-23 用户口径
* 「就算一个分组也要下拉,后续分组管理加了新的就要有新的选项」,旧「单组锁定芯片」退役)。
* 2026-11-17 用户口径:店铺分类弹窗下线,工具栏「归入分类」——勾选商品(翻页累计记忆)
* 后选目标分类归入或取消归属;建/改名/删分类收进侧栏「分类管理」对话框(带名称搜索)。
* v1.7(2026-11-21 对齐原型《商品数据商城》):
* ① 搜索合并——顶部单搜索框(商品编码 OR 标题,OR 语义在后端 SQL;旧两框退役),
* Enter 查询、× 清除即查;
* ② 筛选收弹层——「筛选」按钮下方浮层(价格区间+同步起止+重置/应用),分组下拉与
* 归属三档保留顶部(v1.6 用户口径的两件保留不动);
* ③ 筛选回显条——主面板顶部条件芯片(类目/分类/店铺/价格/同步区间+共 N 条+清除筛选,
* 清除保留分组作用域),无激活筛选不渲染;
* ④ 顶部分页——「第 X/Y 页 · 共 N 条」+首页/上一页/刷新/下一页/末页+档位
* 50/100/200/500/1000(与后端 MAX_PAGE_SIZE=1000 逐字同步),底部 OldPagination 从本页
* 撤下(组件本体不动,他页在用);
* ⑤ 底部已选条——主面板底部常驻「已选 N 件」悬浮条(能勾选的档才渲染);
* ⑥ CSV 导出+已下载角标已下线(2026-11-21 用户拍板「删除掉」导出按钮):随按钮一并
* 摘除 onExportCsv/exportJdProductsCsv/downloadedIds 记忆与卡片「已下载」角标;后端
* /export 端点暂留,下一 jar 发版窗再收。 */
import { computed, onMounted, reactive, ref } from 'vue'
import {
batchAssignJdProductGroup,
createJdProductGroup,
deleteJdProductGroup,
deleteJdProducts,
fetchJdCategoryScope,
fetchJdProductCategories,
fetchJdProductGroups,
fetchJdProductList,
removeJdGroupShop,
renameJdGroupShop,
updateJdProductGroup,
type JdCategoryGroupOption,
type JdProductCategoryOption,
type JdProductGroup,
} from './jd-product-api.ts'
import {
applyJdOwnershipSelection,
createJdProductFilterState,
finitePriceOrNull,
JD_OWNERSHIP_CATEGORY_PREFIX,
jdOwnershipSelectValue,
jdProductFilterActive,
JD_PRODUCT_PAGE_SIZE_OPTIONS,
} from './jd-product-filter.ts'
import { JD_PRODUCT_DEFAULT_SIZE, type JdProductItem } from './jd-product-model.ts'
import JdCategorySidebar from './JdCategorySidebar.vue'
import JdProductCard from './JdProductCard.vue'
import JdProductDetailModal from './JdProductDetailModal.vue'
import JdProductEditDrawer from './JdProductEditDrawer.vue'
import JdBatchAssignDialog from './JdBatchAssignDialog.vue'
import JdCategoryManageDialog from './JdCategoryManageDialog.vue'
import JdShopPickerDialog from './JdShopPickerDialog.vue'
import { showAdminFeedback } from '@/components/admin-feedback-ui'
import { openAdminConfirm } from '@/components/admin-confirm'
import { loadFailureMessage } from '@/components/load-failure-model'
import LoadFailureBar from '@/components/LoadFailureBar.vue'
import { useAdminSessionStore } from '@/stores/admin-session'
import { isAdminRole, isCompanySuperAdminRole } from '@/types/admin'
import { orderedPair } from '@/utils/datetime'
/** 写权限口径(2026-11-23 客户口径「删除权限只有公司管理员有;管理人员只有分类权限」,
* 推翻 2026-11-17「超管+管理员都可以」——当时正好弄反):
* 商品行写(编辑/删除/批量删除/传图)只对超管级(超级管理员/公司超级管理员)渲染,
* 管理人员(admin 档)与普通账号同样只读(后端 requireJdProductWrite 同口径兜底);
* 分类写(建/改名/删除、归类)由 scope 的每组 editable 决定(2026-11-23 起 admin 对
* 全部可见 jd 分组 editable=true,不再要求组长身份),与本档无关。 */
const session = useAdminSessionStore()
const canWriteProducts = computed(
() => session.isSuperAdmin || isCompanySuperAdminRole(session.user?.role))
/** 分组筛选:超管档一直有;2026-11-23 起放开管理人员(admin 档)——分类可写已扩到
* 全部可见组,不给分组下拉的话多组管理人员只能操作默认分组,改动不完整。
* 普通账号仍不出现,其分类面固定落在作用域默认分组上。 */
const canFilterGroup = computed(() => isAdminRole(session.user?.role))
const loading = ref(false)
const loadFailure = ref('')
const rows = ref<JdProductItem[]>([])
const total = ref(0)
const page = ref(1)
/** 每页条数默认 50(v1.6 Phase 1 新契约),档位 50/100/200/500/1000(v1.7)收敛在 filter 层单一出处。 */
const pageSize = ref(JD_PRODUCT_DEFAULT_SIZE)
const filter = reactive(createJdProductFilterState())
/** 分类作用域(V203):可见分组 + 每组可写标记。失败静默回空作用域(分类面全空但商品照常)。 */
const categoryScope = ref<JdCategoryGroupOption[]>([])
const scopeDefaultGroupId = ref(0)
/** 操作者是否平台超管(scope 原样透传):京东侧还没有任何分组时,分类管理弹窗
* 靠它给平台超管开「直接新建数据权限分组」的口子(2026-11-17 用户口径「admin 权限最大」),
* 其余角色维持空态指路,不在分类管理里建组。 */
const scopeSuperAdmin = ref(false)
/** 当前分组(数据权限分组 id):分类列/分类筛选/分类管理的共同作用域。 */
const ownerGroupId = ref(0)
/** 当前分组的分类选项(按 ownerGroupId 取,切组即换;侧栏店铺分类 Tab 与归类对话框共用)。 */
const groupOptions = ref<JdProductGroup[]>([])
/** 类目计数行(京麦末级类目+商品数):侧栏商品目录树的徽标数据源;失败静默回空。 */
const categoryOptions = ref<JdProductCategoryOption[]>([])
const detailItem = ref<JdProductItem | null>(null)
const detailVisible = ref(false)
/** 归类对话框(2026-17 替代下线的店铺分类弹窗,工具栏「归入分类」打开)。 */
const batchEditVisible = ref(false)
/** 分类管理对话框(2026-11-17 第二刀:独立子菜单页并进来,建/改名/删+搜索)。 */
const manageVisible = ref(false)
/** 行级编辑抽屉(2026-11-15 用户口径「每一条数据后面 需要编辑或者删除」):入口随操作列
* 下线收进详情弹窗顶部动作区(Modal emit edit),删除走 removeOne 同一条链。 */
const editItem = ref<JdProductItem | null>(null)
const editVisible = ref(false)
/** 筛选弹层(v1.7 对齐原型):「筛选」按钮下方浮层,价格区间+同步起止+重置/应用。 */
const filterPopVisible = ref(false)
/** 当前分组的下拉项(带 editable)——分类写入口与侧栏新建分类行的可写档都读它。 */
const currentGroupOption = computed<JdCategoryGroupOption | null>(() =>
categoryScope.value.find((group) => group.id === ownerGroupId.value) || null)
const currentGroupName = computed(() => currentGroupOption.value?.name || '')
/** 分类可管理(建/改名/删除/归类):本组管理员、公司超管、平台超管(后端 scope 判)。 */
const canManageCategories = computed(() => currentGroupOption.value?.editable === true)
/** 勾选面(卡片勾选框+工具栏全选/批量动作)服务两类写:批量删除(超管级)+
* 归类/取消归属(可写分类的管理人员也要勾)。2026-11-17 弹窗下线后管理员失去
* 弹窗右栏的勾选面 ⇒ 勾选面对「本组可写」放开,否则管理人员没有归类入口。 */
const hasSelection = computed(() => canWriteProducts.value || canManageCategories.value)
/** 侧栏选中回显:类目(name+完整名称路径;树外名 path 空数组)与分类 id。 */
const selectedCategoryInfo = computed(() =>
filter.category ? { name: filter.category, path: filter.categoryPath ?? [] } : null)
const selectedGroupIdNum = computed(() => {
const id = Number.parseInt(filter.groupId, 10)
return Number.isFinite(id) && id > 0 ? id : null
})
/** 是否有任一激活筛选条件(回显条渲染判据;分组作用域不在判定里)。 */
const filterActive = computed(() => jdProductFilterActive(filter))
/** 回显条芯片(v1.7 对齐原型):类目/分类/店铺/价格区间/同步区间/归属档,各带人话前缀;
* 归属三档只回显偏离出厂默认(全部)的档位——默认档不是激活筛选。 */
const activeFilterChips = computed(() => {
const chips: { key: string; label: string }[] = []
const keyword = (filter.keyword || '').trim()
if (keyword) chips.push({ key: 'keyword', label: `搜索:${keyword}` })
if ((filter.category || '').trim()) chips.push({ key: 'category', label: `类目:${filter.category}` })
const shopName = (filter.shopName || '').trim()
if (shopName) chips.push({ key: 'shop', label: `店铺:${shopName}` })
if ((filter.groupId || '').trim()) {
const group = groupOptions.value.find((option) => String(option.id) === filter.groupId)
chips.push({ key: 'group', label: `分类:${group?.name || filter.groupId}` })
}
if (filter.classified === 'classified') chips.push({ key: 'classified', label: '已分类' })
else if (filter.classified === '') chips.push({ key: 'classified', label: '全部分类状态' })
const minPrice = finitePriceOrNull(filter.minPrice)
const maxPrice = finitePriceOrNull(filter.maxPrice)
if (minPrice != null || maxPrice != null) {
chips.push({ key: 'price', label: `价格:${minPrice ?? ''} ~ ${maxPrice ?? ''}` })
}
if (filter.syncedFrom || filter.syncedTo) {
chips.push({ key: 'synced', label: `同步:${filter.syncedFrom || '…'} ~ ${filter.syncedTo || '…'}` })
}
return chips
})
/** 侧栏选类目:树内节点带完整路径(序列化按子树展开 IN 下发)、树外兜底名 path 空=
* 单名等值旧形状;null=「全部商品」清选重查。选中即查(回第一页)。 */
function onSelectCategory(value: { name: string; path: string[] } | null): void {
filter.category = value?.name || ''
filter.categoryPath = value?.path ?? []
apply()
}
/** 侧栏选分类:id 或 null(清选,与旧「全部分类」下拉行同语义)。选中即查。 */
function onSelectGroup(id: number | null): void {
filter.groupId = id ? String(id) : ''
// 店铺筛选挂在分类下(v1.7 名单维度):分类清选时店铺筛选一起清,否则发一条
// 「分类不限 ∩ 店铺=X」的孤儿谓词(店铺全平台 DISTINCT,会命中别组分类下的同名店)。
filter.shopName = ''
apply()
}
// ---------------- 归属筛选查询(2026-11-22 用户口径「还要加个归属筛选查询」) ----------------
// 归属下拉是 filter 的视图:派生(显示档位)与回写(选档补丁)都在 jd-product-filter.ts
// 纯函数里(node --test 直测),本组件只接 change 与重查——真源永远只有 filter 一份。
/** 归属下拉显示档位:分类档(cat:<id>)优先于状态档(''/classified/ungrouped)。 */
const ownershipSelectValue = computed(() => jdOwnershipSelectValue(filter))
/** 「按分类查询」组选项(当前分组的分类;value 由 JD_OWNERSHIP_CATEGORY_PREFIX 拼出)。 */
const ownershipCategoryOptions = computed(() =>
groupOptions.value.map((cat) => ({
id: cat.id,
label: cat.name,
value: `${JD_OWNERSHIP_CATEGORY_PREFIX}${cat.id}`,
})),
)
/** 归属下拉选中即查(回第一页);分类档清店铺孤儿谓词的纪律在纯函数里(侧栏同形)。 */
function onOwnershipSelect(value: string): void {
applyJdOwnershipSelection(filter, value)
apply()
}
// ---------------- 店铺名单(v1.7 F6:侧栏展开/点店铺过滤/行内改名移除/添加店铺弹窗) ----------------
/** 添加店铺弹窗的目标分类(名单挂在分类下;null=弹窗关着)。 */
const addShopsTarget = ref<JdProductGroup | null>(null)
const addShopsVisible = ref(false)
/** 侧栏点店铺行:分类+店铺成对下发(精确 eq);null=清店铺筛选(分类选中保留)。 */
function onSelectShop(value: { groupId: number; shopName: string } | null): void {
if (!value) {
filter.shopName = ''
apply()
return
}
filter.groupId = String(value.groupId)
filter.shopName = value.shopName
apply()
}
/** 侧栏分类行内改名:成功后刷新分类(侧栏随 props 流回);失败弹后端中文文案(409 重名等)。 */
async function onRenameCategory(category: JdProductGroup, name: string): Promise<void> {
try {
await updateJdProductGroup(category.id, { name, sortOrder: category.sortOrder })
showAdminFeedback(`已把分类「${category.name}」改名为「${name}」`, 'success')
await loadGroupOptions()
} catch (error) {
const text = error instanceof Error && error.message ? error.message : '改名失败,请稍后重试'
showAdminFeedback(text, 'error')
}
}
/** 侧栏分类行内删除:二次确认(点名只解挂归属、商品保留)→ 删除 → 若当前筛选停在该
* 分类(含其店铺行)则清分类筛选,分类列表与行集都重取。 */
async function onDeleteCategory(category: JdProductGroup): Promise<void> {
const ok = await openAdminConfirm({ title: '删除分类', message: `确定删除分类「${category.name}」吗?只删除该分类的归属关系(${category.productCount} 个商品回到未分类),商品本身保留在公共库;分类下的店铺名单一并删除。此操作不可恢复。`, confirmText: '确认删除', danger: true })
if (!ok) return
try {
await deleteJdProductGroup(category.id)
showAdminFeedback(`已删除分类「${category.name}」`, 'success')
if (filter.groupId === String(category.id)) {
filter.groupId = ''
filter.shopName = ''
}
await loadGroupOptions()
await load()
} catch (error) {
const text = error instanceof Error && error.message ? error.message : '删除分类失败,请稍后重试'
showAdminFeedback(text, 'error')
}
}
/** 店铺行内改名:成功后刷新名单;若当前筛选正停在这个店(同分类)则把筛选词同步成新名。 */
async function onRenameShop(category: JdProductGroup, oldName: string, newName: string): Promise<void> {
try {
await renameJdGroupShop(category.id, oldName, newName)
showAdminFeedback(`已把店铺「${oldName}」改名为「${newName}」`, 'success')
if (filter.groupId === String(category.id) && filter.shopName === oldName) {
filter.shopName = newName
await load()
}
await loadGroupOptions()
} catch (error) {
const text = error instanceof Error && error.message ? error.message : '改名失败,请稍后重试'
showAdminFeedback(text, 'error')
}
}
/** 店铺行内移除(可重做——再加回名单即可,不设二次确认);筛选正停在它身上则清店铺筛选。 */
async function onRemoveShop(category: JdProductGroup, shopName: string): Promise<void> {
try {
await removeJdGroupShop(category.id, shopName)
showAdminFeedback(`已把「${shopName}」移出「${category.name}」的名单`, 'success')
if (filter.groupId === String(category.id) && filter.shopName === shopName) {
filter.shopName = ''
await load()
}
await loadGroupOptions()
} catch (error) {
const text = error instanceof Error && error.message ? error.message : '移除失败,请稍后重试'
showAdminFeedback(text, 'error')
}
}
/** 打开添加店铺弹窗(侧栏「+ 添加店铺」行;名单写闸 editable 在侧栏已分档,后端同闸兜底)。 */
function openAddShops(category: JdProductGroup): void {
addShopsTarget.value = category
addShopsVisible.value = true
}
/** 添加店铺成功:回执=实际新增行数(幂等跳过不计)。只刷新名单(侧栏店铺行随 props
* 流回)——名单是筛选器的候选面,不动商品行集,行集谓词 shop_name 无需重查。 */
async function onShopsAdded(added: number, categoryName: string): Promise<void> {
showAdminFeedback(
added > 0 ? `已把 ${added} 个店铺加进「${categoryName}」` : '所选店铺都已在名单里,没有新增',
'success',
)
await loadGroupOptions()
}
/** 侧栏新建分类(可写档):落当前分组,成功后刷新分类(侧栏徽标随 props 流回)。 */
async function onCreateCategory(name: string): Promise<void> {
try {
await createJdProductGroup({ name, sortOrder: 0, ownerGroupId: ownerGroupId.value })
showAdminFeedback(`已创建分类「${name}」`, 'success')
await loadGroupOptions()
} catch (error) {
const text = error instanceof Error && error.message ? error.message : '创建分类失败,请稍后重试'
showAdminFeedback(text, 'error')
}
}
/** min/max 挡不住手输,落值后把倒挂的起止交换回来。 */
function clampJdSyncedRange(): void {
const [s, e] = orderedPair(filter.syncedFrom, filter.syncedTo)
filter.syncedFrom = s
filter.syncedTo = e
}
function toggleFilterPopover(): void {
filterPopVisible.value = !filterPopVisible.value
}
/** 清除搜索词(× 即查):空词不重发(首屏没搜过就别白发一条)。 */
function clearKeyword(): void {
if (!filter.keyword) return
filter.keyword = ''
apply()
}
/** 弹层「应用」:价格倒挡先在门口挡住(后端同文案兜底),过了才关弹层查询。 */
function applyFromPopover(): void {
const minPrice = finitePriceOrNull(filter.minPrice)
const maxPrice = finitePriceOrNull(filter.maxPrice)
if (minPrice != null && maxPrice != null && minPrice > maxPrice) {
showAdminFeedback('价格区间最小值不能大于最大值', 'error')
return
}
filterPopVisible.value = false
apply()
}
/** 弹层「重置」:筛选面回出厂空态并查询(与回显条「清除筛选」同一条链,保留分组作用域)。 */
function resetFromPopover(): void {
filterPopVisible.value = false
reset()
}
const totalPages = computed(() => Math.max(1, Math.ceil(total.value / pageSize.value)))
/** 作用域 → 分组列表 → 该组分类:失败一律静默降级(分类只是筛选/归类面,不拦首屏行集)。 */
async function loadCategoryScope(): Promise<void> {
try {
const scope = await fetchJdCategoryScope()
categoryScope.value = scope.groups
scopeDefaultGroupId.value = scope.defaultGroupId ?? 0
scopeSuperAdmin.value = scope.platformSuperAdmin === true
} catch {
categoryScope.value = []
scopeDefaultGroupId.value = 0
scopeSuperAdmin.value = false
}
// 当前分组:保留用户已选(仍可见时),否则回默认(可见组第一项)。
const keep = categoryScope.value.some((group) => group.id === ownerGroupId.value)
if (!keep) ownerGroupId.value = scopeDefaultGroupId.value
await loadGroupOptions()
}
/** 当前分组的分类选项:按 ownerGroupId 取(跨组不可见);失败静默回空下拉。 */
async function loadGroupOptions(): Promise<void> {
try {
groupOptions.value = await fetchJdProductGroups(ownerGroupId.value)
} catch {
groupOptions.value = []
}
}
/** 类目计数行同分组口径:失败静默回空(侧栏只剩「全部商品」行),不拦首屏行集。 */
async function loadCategoryOptions(): Promise<void> {
try {
categoryOptions.value = await fetchJdProductCategories()
} catch {
categoryOptions.value = []
}
}
/** 切换「分组」下拉:分类筛选(属于上一组的分类)清空,分类选项换成本组,回第一页重取。
* 勾选集是按商品 id 记的(跨页累计),切组不清——归类的落点组随 ownerGroupId 走,
* 误跨组批量归类由归类对话框里的组名标注防住。 */
async function changeOwnerGroup(next: number): Promise<void> {
if (next === ownerGroupId.value) return
ownerGroupId.value = next
filter.groupId = ''
await loadGroupOptions()
page.value = 1
await load()
}
/** 打开归类对话框:先刷一遍当前组分类(商品数徽标要新鲜),再亮对话框。 */
async function openBatchEdit(): Promise<void> {
await loadGroupOptions()
batchEditVisible.value = true
}
/** 工具栏「取消归属」=批量解挂(groupId null,后端只解挂本组归属行):
* 与归类对话框的「移出分类」同一端点同语义;归类可重做(再归入即可)故不设二次确认。 */
async function detachSelected(): Promise<void> {
const ids = [...selectedIds.value]
if (!ids.length || assigning.value) return
assigning.value = true
try {
const updated = await batchAssignJdProductGroup(ids, null, ownerGroupId.value)
showAdminFeedback(`已把 ${updated} 个商品移出分类`, 'success')
selectedIds.value = new Set()
await load()
} catch (error) {
const text = error instanceof Error && error.message ? error.message : '移出分类失败,请稍后重试'
showAdminFeedback(text, 'error')
} finally {
assigning.value = false
}
}
/** 打开分类管理(对话框自取列表);入口对所有人渲染——同组普通号只读可看(V203),
* 写控件由对话框按 editable 分档。 */
function openManage(): void {
manageVisible.value = true
}
/** 分类管理里有写操作(建/改/删):刷一遍侧栏分类与商品数徽标。 */
async function onManaged(): Promise<void> {
await loadGroupOptions()
}
/**
* 分类管理里新建了第一个数据权限分组(平台超管空态直建,2026-11-17 用户口径「admin
* 权限最大」):整条作用域重取——新组会进可见集并把「当前分组」翻过去,侧栏分类、
* 弹窗内分类列表随之按新组取,操作者接着就能建分类,不用关弹窗再开一遍。
*/
async function onGroupCreated(): Promise<void> {
await loadCategoryScope()
}
/** 归类完成(对话框已关):清勾选+重取列表(行集分组变了)。
* 移出分类 targetName 为 null;updated 是后端实际写入/解挂的归属行数。 */
async function onBatchAssigned(updated: number, targetName: string | null): Promise<void> {
showAdminFeedback(
targetName ? `已把 ${updated} 个商品归入「${targetName}」` : `已把 ${updated} 个商品移出分类`,
'success',
)
selectedIds.value = new Set()
await load()
}
async function load(): Promise<void> {
loading.value = true
loadFailure.value = ''
try {
const result = await fetchJdProductList({
page: page.value,
pageSize: pageSize.value,
keyword: filter.keyword,
category: filter.category,
categoryPath: filter.categoryPath,
shopName: filter.shopName,
groupId: filter.groupId,
classified: filter.classified,
minPrice: filter.minPrice,
maxPrice: filter.maxPrice,
ownerGroupId: ownerGroupId.value,
syncedFrom: filter.syncedFrom,
syncedTo: filter.syncedTo,
})
rows.value = result.items
total.value = result.total
if (result.page >= 1) page.value = result.page
// 勾选跨页记忆(2026-11-17 用户口径「支持勾选翻页也要记住」):翻页/重取/改筛选都
// 不清勾选集——归类与批量删除都按累计 id 提交,操作前的确认弹窗点名数量;
// 「清除勾选」按钮同日用户口径删除,勾选只在批量操作成功后自动清。
} catch (error) {
loadFailure.value = loadFailureMessage(error, '京东商品列表加载失败')
showAdminFeedback(loadFailure.value, 'error')
} finally {
loading.value = false
}
}
function apply(): void {
page.value = 1
void load()
}
/** 重置:筛选面回出厂空态(搜索词/类目/店铺/价格/日期随 filter 回空=侧栏高亮自动清,
* 归属三档回默认全部档('')),回第一页重取;分组作用域(ownerGroupId)不在 filter 里,
* 天然保留(AC-4)。 */
function reset(): void {
Object.assign(filter, createJdProductFilterState())
page.value = 1
void load()
}
function changePage(next: number): void {
if (next < 1 || next > totalPages.value) return
page.value = next
void load()
}
/** 顶部分页(v1.7 对齐原型):首/末页按钮复用 changePage 的边界检查(越界页码静默忽略)。 */
function goFirstPage(): void {
changePage(1)
}
function goLastPage(): void {
changePage(totalPages.value)
}
function changeSize(size: number) {
pageSize.value = size
page.value = 1
void load()
}
function openDetail(item: JdProductItem): void {
detailItem.value = item
detailVisible.value = true
}
function closeDetail(): void {
detailVisible.value = false
detailItem.value = null
}
/** 商品编码一键复制(卡片 ⧉,@click.stop 防冒泡开详情):剪贴板不可用(非安全上下文)时弹失败文案。 */
async function copyProductCode(row: JdProductItem): Promise<void> {
const code = row.productCode
if (!code) return
try {
await navigator.clipboard.writeText(code)
showAdminFeedback(`已复制 ${code}`, 'success')
} catch {
showAdminFeedback('复制失败,请手动选择复制', 'error')
}
}
/** 详情弹窗顶部「编辑/删除」(Modal emit):先关弹窗再走既有链,与旧操作列同源。 */
function onDetailEdit(): void {
const item = detailItem.value
closeDetail()
if (item) openEdit(item)
}
function onDetailRemove(): void {
const item = detailItem.value
closeDetail()
if (item) void removeOne(item)
}
function openEdit(item: JdProductItem): void {
editItem.value = item
editVisible.value = true
}
function closeEdit(): void {
editVisible.value = false
editItem.value = null
}
/** 编辑保存成功:刷新列表(该行已置人工编辑锁,标题/价格等即时可见且不再被采集覆盖)。 */
async function onEditSaved(): Promise<void> {
await load()
}
// ---------------- 删除(先删桶、全成功才删库行,语义在后端 JdProductDeleteService) ----------------
/** 勾选集(按商品 id,跨页累计记忆,2026-11-17 用户口径)。翻页/重取不清;显式出口
* 曾是工具栏「清除勾选」,同日用户口径「清除勾选这个按钮删除」后只剩批量动作
* 成功后自动清。 */
const selectedIds = ref<Set<number>>(new Set())
const deleting = ref(false)
/** 归类/取消归属进行中(对话框提交与工具栏「取消归属」共用一颗互斥灯)。 */
const assigning = ref(false)
const selectedCount = computed(() => selectedIds.value.size)
function toggleSelect(row: JdProductItem, checked: boolean): void {
const next = new Set(selectedIds.value)
if (checked) next.add(row.id)
else next.delete(row.id)
selectedIds.value = next
}
/** 工具栏「全选本页/取消本页」:按当前页行集增删勾选(跨页累计集里其他页的行不动)。 */
function selectPage(): void {
const next = new Set(selectedIds.value)
for (const row of rows.value) next.add(row.id)
selectedIds.value = next
}
function unselectPage(): void {
const next = new Set(selectedIds.value)
for (const row of rows.value) next.delete(row.id)
selectedIds.value = next
}
/** 单行删除入口(详情弹窗顶部动作):确认弹窗点名商品编码与「桶内图片一并删除」。 */
async function removeOne(row: JdProductItem): Promise<void> {
await removeMany([row.id], `确定删除商品「${row.productCode || row.title}」吗?`)
}
/** 批量删除入口(工具栏按钮):按累计勾选 id 直删(跨页的行也在内),确认弹窗点名总数。 */
async function removeSelected(): Promise<void> {
await removeMany([...selectedIds.value], `确定批量删除选中的 ${selectedIds.value.size} 个商品吗?`)
}
/** 删除主流程:二次确认 → 先删桶、全成功才删库行(后端保证)→ 刷新列表并清勾选。 */
async function removeMany(ids: number[], message: string): Promise<void> {
if (!ids.length || deleting.value) return
const ok = await openAdminConfirm({ title: '删除京东商品', message: `${message}删除后该商品的 SKU、图片登记与 jd-products 桶内图片文件一并删除(被其他商品共用的图片会保留);此操作不可恢复。`, confirmText: '确认删除', danger: true })
if (!ok) return
deleting.value = true
try {
const receipt = await deleteJdProducts(ids)
showAdminFeedback(`已删除 ${receipt.deleted} 个商品,清理 ${receipt.objectsDeleted} 个图片文件` +
(receipt.objectsKept ? `(${receipt.objectsKept} 个共用图片保留)` : ''), 'success')
selectedIds.value = new Set()
if (ids.length >= rows.value.length && page.value > 1) page.value -= 1
await load()
} catch (error) {
const text = error instanceof Error && error.message ? error.message : '删除失败,请稍后重试'
showAdminFeedback(text, 'error')
} finally {
deleting.value = false
}
}
onMounted(async () => {
// 先解析作用域再取列表:首屏的 owner_group_id 必须与分类侧栏同一个组(铁律 30 同形——
// 不要先发一条不带 owner_group_id 的列表请求再补,两次请求会落在不同默认组上)。
await loadCategoryScope()
void loadCategoryOptions()
await load()
})
</script>
<template>
<div class="jd-database-view">
<!-- 顶部通栏面板(2026-11-21 红框整体上移+v1.7 对齐原型;2026-11-22 用户口径
「弄一排,重新排列筛选条件区域」):搜索框+筛选弹层+分组+归属+批量按钮条
全部并进同一行——筛选件住左、批量按钮 margin-left:auto 靠右;行内一律 34px
等高,分组/归属的说明改行内小标签(不再 label 悬在上方把行撑高)。主面板只剩
回显条+卡片网格+分页条+已选条。 -->
<section class="panel-box jd-top-panel">
<div class="jd-filter-row">
<!-- 合并搜索(v1.7 对齐原型):一个框同时搜商品编码与标题(OR 语义在后端 SQL,
前端不拆词);Enter 查询;有词时出 × 清除按钮(清除即查)。 -->
<div class="jd-search-wrap">
<input
v-model="filter.keyword"
class="jd-search-input"
type="text"
placeholder="搜索商品编码或标题"
aria-label="搜索商品"
@keyup.enter="apply"
/>
<button
v-if="filter.keyword"
class="jd-search-clear"
type="button"
aria-label="清除搜索"
@click="clearKeyword"
>×</button>
</div>
<!-- 筛选弹层(v1.7 对齐原型):低频条件(价格区间+同步起止)收进按钮下方浮层,
重置/应用在浮层底部;常驻的分组下拉与归属三档留在搜索行(v1.6 用户口径保留件)。 -->
<div class="jd-filter-anchor">
<button
class="btn btn-secondary"
type="button"
:class="{ 'jd-filter-open': filterPopVisible }"
@click="toggleFilterPopover"
>筛选</button>
<div v-if="filterPopVisible" class="jd-filter-popover">
<div class="jd-pop-row">
<label>价格区间</label>
<div class="jd-pop-fields">
<!-- 价格输入用 text+inputmode(不用 type=number):Vue 的 v-model 对
type=number 会 looseToNumber 把 state 写成数字,filter 契约(字符串
进、序列化层转 number)就断了;数字合法性由 finitePriceOrNull 在
序列化口收口,倒挡校验在「应用」门口。 -->
<input
v-model="filter.minPrice"
type="text"
inputmode="decimal"
placeholder="最低价"
aria-label="最低价格"
/>
<span class="jd-pop-sep">~</span>
<input
v-model="filter.maxPrice"
type="text"
inputmode="decimal"
placeholder="最高价"
aria-label="最高价格"
/>
</div>
</div>
<div class="jd-pop-row">
<label>同步日期</label>
<div class="jd-pop-fields">
<input
v-model="filter.syncedFrom"
type="date"
:max="filter.syncedTo"
aria-label="同步开始日期"
@change="clampJdSyncedRange"
/>
<span class="jd-pop-sep">~</span>
<input
v-model="filter.syncedTo"
type="date"
:min="filter.syncedFrom"
aria-label="同步结束日期"
@change="clampJdSyncedRange"
/>
</div>
</div>
<div class="jd-pop-actions">
<button class="btn btn-secondary" type="button" @click="resetFromPopover">重置</button>
<button class="btn" type="button" @click="applyFromPopover">应用</button>
</div>
</div>
</div>
<!-- 分组(V203):超管档(平台/公司超管)专属——2026-11-17 用户口径
「超管可以有分组筛选,管理员及以下则没有」,本控件对管理员及以下整块不渲染
(其分类面固定落在作用域默认分组)。恒渲染下拉——2026-11-23 用户口径
「就算一个分组也要下拉,后续分组管理加了新的就要有新的选项」,
旧「单组锁定芯片」形态退役(多组切换行为不变)。
分组是侧栏分类/分类筛选/分类管理的作用域,不是筛选条件 ⇒ 「重置」不清它
(挂载测试钉住)。行内小标签替代旧 label-above(2026-11-22 一排口径)。 -->
<div v-if="canFilterGroup && categoryScope.length" class="jd-field-group">
<span class="jd-field-label">分组</span>
<el-select
:model-value="ownerGroupId"
class="admin-filter"
filterable
aria-label="当前分组"
@change="changeOwnerGroup"
>
<el-option v-for="group in categoryScope" :key="group.id" :value="group.id" :label="group.label" />
</el-select>
</div>
<!-- 归属筛选查询(2026-11-22 用户口径「还要加个归属筛选查询」,把 11-17 三档快筛
升级成一颗能按分类查询的归属下拉):四类档位互斥单值——全部('',出厂默认)/
已分类/未分类(状态档,V203 归属表判当前分组下有无分类)+「按分类查询」组
(当前分组的分类项,cat:<id> → group_id 走归属表 EXISTS 下推,与侧栏点分类
同一条谓词);选中即查(回第一页),选分类清店铺孤儿谓词(侧栏同形);
「重置/清除筛选」回全部档,切分组清分类档(changeOwnerGroup 既有链)。
⚠ el-select 对 '' 值按空值处理(useSelect 的 hasModelValue 把 '' 算空)⇒
界面显示的是本 placeholder 而非「全部」选项的 label,placeholder 文案必须与
默认档同词「全部」;写回「未分类」=首屏灰字撒谎(逻辑已查全量、界面谎称卡
未分类)。下拉值是派生态(jdOwnershipSelectValue),不在 filter 里另立真源。 -->
<div class="jd-field-group">
<span class="jd-field-label">归属</span>
<el-select
:model-value="ownershipSelectValue"
class="admin-filter"
placeholder="全部"
filterable
aria-label="筛选归属"
@change="onOwnershipSelect"
>
<el-option label="全部" value="" />
<el-option label="已分类" value="classified" />
<el-option label="未分类" value="ungrouped" />
<el-option-group v-if="ownershipCategoryOptions.length" label="按分类查询">
<el-option
v-for="cat in ownershipCategoryOptions"
:key="cat.id"
:label="cat.label"
:value="cat.value"
/>
</el-option-group>
</el-select>
</div>
<!-- 批量按钮条(v1.6 重排,2026-11-21 按原型调序;2026-11-22 用户口径「弄一排」
并进筛选行,margin-left:auto 靠右收尾——筛选件住左、批量动作住右):
全选本页/取消本页|删除商品|归入分类/取消归属(分类写档)。勾选 0 时批量
动作置灰(与旧批量删除同款常显口径)。行级编辑/删除入口随操作列下线收进
详情弹窗顶部动作区;列表加载失败条随行集住主面板,不随筛选上移。 -->
<div v-if="hasSelection" class="jd-toolbar">
<button class="btn btn-secondary" type="button" :disabled="!rows.length" @click="selectPage">全选本页</button>
<button class="btn btn-secondary" type="button" :disabled="!selectedCount" @click="unselectPage">取消本页</button>
<button
v-if="canWriteProducts"
class="btn btn-danger jd-delete-btn"
type="button"
:disabled="deleting || !selectedCount"
@click="removeSelected"
>
{{ deleting ? '删除中...' : `删除商品(${selectedCount})` }}
</button>
<template v-if="canManageCategories">
<button
class="btn btn-secondary"
type="button"
:disabled="!selectedCount"
@click="openBatchEdit"
>归入分类</button>
<button
class="btn btn-secondary"
type="button"
:disabled="assigning || !selectedCount"
@click="detachSelected"
>取消归属</button>
</template>
</div>
</div>
<!-- 商品行写只对超管级(平台超管+公司超管)开放(2026-11-23 客户口径),管理人员与
普通账号看到只读提示;分类写由当前分组 editable 决定(2026-11-23 起 admin 对
全部可见 jd 分组可写),分类维护入口=侧栏「分类管理」(只读可看,写控件按
editable 分档)。 -->
<span v-if="!canWriteProducts" class="jd-readonly-tip">只读:编辑/删除需超级管理员或公司超级管理员;分类在左侧「分类管理」查看与维护</span>
</section>
<div class="jd-body">
<!-- 左侧栏(v1.6 → v1.7 名单维度):商品目录(京麦授权类目树+计数徽标+树外兜底)/
店铺分类(分类平铺+店铺名单展开/点选过滤/行内改名移除+添加店铺入口+新建+
分类管理入口)。取数在页面,侧栏纯展示。 -->
<JdCategorySidebar
:category-options="categoryOptions"
:categories="groupOptions"
:editable="canManageCategories"
:selected-category="selectedCategoryInfo"
:selected-group-id="selectedGroupIdNum"
:selected-shop-name="filter.shopName"
@select-category="onSelectCategory"
@select-group="onSelectGroup"
@select-shop="onSelectShop"
@create-category="onCreateCategory"
@rename-category="onRenameCategory"
@delete-category="onDeleteCategory"
@rename-shop="onRenameShop"
@remove-shop="onRemoveShop"
@add-shops="openAddShops"
@manage-categories="openManage"
/>
<section class="panel-box jd-main-panel">
<LoadFailureBar v-if="loadFailure" :message="loadFailure" @retry="load" />
<!-- 筛选回显条(v1.7 F5 对齐原型):有激活筛选才渲染;芯片按当前筛选态逐条回显,
「共 N 条」给行集规模,「清除筛选」回出厂空态(保留分组作用域,AC-4)。 -->
<div v-if="filterActive" class="jd-filter-chips">
<span v-for="chip in activeFilterChips" :key="chip.key" class="jd-chip">{{ chip.label }}</span>
<span class="jd-chip-total">共 {{ total }} 条</span>
<button class="jd-chip-clear" type="button" @click="reset">清除筛选</button>
</div>
<!-- 卡片网格(v1.6):商城款式;卡片整体点击=开详情弹窗,勾选/复制 @click.stop 防冒泡。 -->
<div class="jd-card-grid">
<template v-if="rows.length">
<JdProductCard
v-for="row in rows"
:key="row.id"
:item="row"
:selected="selectedIds.has(row.id)"
:selectable="hasSelection"
@toggle-select="(checked: boolean) => toggleSelect(row, checked)"
@open="openDetail(row)"
@copy="copyProductCode(row)"
/>
</template>
<div v-else-if="loading" class="jd-empty-tip">加载中...</div>
<div v-else class="jd-empty-tip">暂无京东商品数据</div>
</div>
<!-- 顶部分页条(v1.7 F4 对齐原型):「第 X/Y 页 · 共 N 条」+首页/上一页/刷新/
下一页/末页+每页条数档位(50/100/200/500/1000,与后端上限逐字同步)。
本页底部 OldPagination 随 v1.7 撤下(组件本体不动,他页在用)。 -->
<div class="jd-page-bar">
<span class="jd-page-info">第 {{ page }} / {{ totalPages }} 页 · 共 {{ total }} 条</span>
<div class="jd-page-btns">
<button class="jd-page-btn" type="button" :disabled="page <= 1" @click="goFirstPage">首页</button>
<button class="jd-page-btn" type="button" :disabled="page <= 1" @click="changePage(page - 1)">上一页</button>
<button class="jd-page-btn" type="button" aria-label="刷新列表" @click="load">刷新</button>
<button class="jd-page-btn" type="button" :disabled="page >= totalPages" @click="changePage(page + 1)">下一页</button>
<button class="jd-page-btn" type="button" :disabled="page >= totalPages" @click="goLastPage">末页</button>
</div>
<el-select
class="page-size-select"
:model-value="pageSize"
aria-label="每页条数"
@change="changeSize"
>
<el-option
v-for="size in JD_PRODUCT_PAGE_SIZE_OPTIONS"
:key="size"
:value="size"
:label="`${size}条/页`"
/>
</el-select>
</div>
<!-- 底部已选条(v1.7 F7 骨架):常驻悬浮(不随勾选数显隐);只对有勾选面的档渲染
(普通员工没有勾选框,已选恒 0 无意义)。 -->
<div v-if="hasSelection" class="jd-select-bar">已选 {{ selectedCount }} 件</div>
</section>
</div>
<JdBatchAssignDialog
:visible="batchEditVisible"
:owner-group-id="ownerGroupId"
:owner-group-name="currentGroupName"
:categories="groupOptions"
:selected-ids="[...selectedIds]"
@close="batchEditVisible = false"
@done="onBatchAssigned"
/>
<!-- 添加店铺弹窗(v1.7 F6):侧栏「+ 添加店铺」行打开,弹窗自取候选(available)
与批量提交,done 回执带新增行数给页面刷名单。 -->
<JdShopPickerDialog
:visible="addShopsVisible"
:category-id="addShopsTarget?.id ?? 0"
:category-name="addShopsTarget?.name ?? ''"
@close="addShopsVisible = false"
@done="onShopsAdded"
/>
<JdCategoryManageDialog
:visible="manageVisible"
:owner-group-id="ownerGroupId"
:owner-group-name="currentGroupName"
:editable="canManageCategories"
:platform-super-admin="scopeSuperAdmin"
@close="manageVisible = false"
@changed="onManaged"
@group-created="onGroupCreated"
/>
<JdProductDetailModal
:item="detailItem"
:visible="detailVisible"
:can-write="canWriteProducts"
@close="closeDetail"
@edit="onDetailEdit"
@remove="onDetailRemove"
/>
<JdProductEditDrawer
:item="editItem"
:visible="editVisible"
@close="closeEdit"
@saved="onEditSaved"
/>
</div>
</template>
<style scoped src="./jd-database-page.css"></style>
<style>
/* 一屏装配前置(2026-11-22 用户口径「左侧菜单超出变滚动条、右侧一屏后也要滚动」):
admin-content 平时是块级滚动容器——整页一起滚,侧栏/主面板的内部滚动区永远触发不了。
本页要把「可视高度」传给 .jd-body(左右两栏各自内部滚动),就必须让它转纵向 flex:
面包屑/操作指南等兄弟件保持自然高度,.jd-database-view 吃剩余高度(页面 CSS 里
flex: 1 1 auto + min-height: 0 接住)。必须是非 scoped 块——scoped 会给选择器挂
data-v 属性,admin-content 上没有这个属性就永远匹配不上。用 :has() 只在本页挂载时
生效,路由切走 :has 不再命中、自动还原成块级,其他页面零感知、不用动 AdminLayout。 */
.admin-content:has(> .jd-database-view) {
display: flex;
flex-direction: column;
}
</style>