Files
crawler-plugin/admin-frontend-vue/src/pages/jd/JdDatabasePage.vue
T

540 lines
25 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">
/** 京东公共商品库:支持分类和京麦类目组合筛选。
* V203(2026-11-16 用户口径):分类按「数据权限分组」隔离——不同组看不到彼此的分类,
* 能看多组的人用筛选行「分组」下拉切换当前组(只有一组时锁定显示)。
* 2026-11-17 用户口径:店铺分类弹窗下线,本页工具栏改「批量编辑分类」——勾选商品
* (翻页累计记忆)后选目标分类归入或移出;同日第二刀「京东分类管理并到京东数据库分类
* 编辑里」:V204 独立子菜单页(JdCategoryManagerPage)下线,建/改名/删分类收进工具栏
* 「分类管理」对话框(JdCategoryManageDialog,带名称搜索)。 */
import { computed, onMounted, reactive, ref } from 'vue'
import {
deleteJdProducts,
fetchJdCategoryScope,
fetchJdProductCategories,
fetchJdProductGroups,
fetchJdProductList,
type JdCategoryGroupOption,
type JdProductCategoryOption,
type JdProductGroup,
} from './jd-product-api.ts'
import type { JdProductItem } from './jd-product-model.ts'
import { jdPriceRange } from './jd-product-model.ts'
import { createJdProductFilterState } from './jd-product-filter.ts'
import JdProductDetailDrawer from './JdProductDetailDrawer.vue'
import JdProductEditDrawer from './JdProductEditDrawer.vue'
import JdCategoryCascader from './JdCategoryCascader.vue'
import JdBatchAssignDialog from './JdBatchAssignDialog.vue'
import JdCategoryManageDialog from './JdCategoryManageDialog.vue'
import OldPagination from '@/components/OldPagination.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 { formatDateTime, orderedPair } from '@/utils/datetime'
/** 写权限口径(2026-11-16 用户口径):商品行写(编辑/删除/勾选批量删)只对平台超级管理员渲染;
* 分类写(建/改名/删除、归类)由 scope 的每组 editable 决定(本组管理员/公司超管/平台超管)。 */
const session = useAdminSessionStore()
const isSuperAdmin = computed(() => session.isSuperAdmin)
const loading = ref(false)
const loadFailure = ref('')
const rows = ref<JdProductItem[]>([])
const total = ref(0)
const page = ref(1)
const pageSize = ref(20)
const filter = reactive(createJdProductFilterState())
/** 分类作用域(V203):可见分组 + 每组可写标记。失败静默回空作用域(分类列全空但商品照常)。 */
const categoryScope = ref<JdCategoryGroupOption[]>([])
const scopeDefaultGroupId = ref(0)
/** 当前分组(数据权限分组 id):分类列/分类筛选/分类管理的共同作用域。 */
const ownerGroupId = ref(0)
/** 当前分组的分类下拉选项(按 ownerGroupId 取,切组即换)。 */
const groupOptions = ref<JdProductGroup[]>([])
/** 类目下拉选项(京麦末级类目):与当前行集同源(公共库全量);失败静默回空。 */
const categoryOptions = ref<JdProductCategoryOption[]>([])
const detailItem = ref<JdProductItem | null>(null)
const detailVisible = ref(false)
/** 批量编辑分类对话框(2026-11-17 替代下线的店铺分类弹窗)。 */
const batchEditVisible = ref(false)
/** 分类管理对话框(2026-11-17 第二刀:独立子菜单页并进来,建/改名/删+搜索)。 */
const manageVisible = ref(false)
/** 行级编辑抽屉(2026-11-15 用户口径「每一条数据后面 需要编辑或者删除;编辑的话
* 这里面都是可以编辑的」):删除走下面的 removeOne(同一条删除链),编辑走独立抽屉。 */
const editItem = ref<JdProductItem | null>(null)
const editVisible = 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 hasSelectionColumn = computed(() => isSuperAdmin.value || canManageCategories.value)
/** 空态行 colspan:勾选列有无决定 5/6/7 列三种形状(超管含操作列)。 */
const emptyColspan = computed(() => (isSuperAdmin.value ? 7 : hasSelectionColumn.value ? 6 : 5))
/** 分组下拉只在能看多组时才渲染;只有一组(含无可见组)时按锁定态展示。 */
const canSwitchGroup = computed(() => categoryScope.value.length > 1)
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()
}
/** min/max 挡不住手输,落值后把倒挂的起止交换回来。 */
function clampJdSyncedRange(): void {
const [s, e] = orderedPair(filter.syncedFrom, filter.syncedTo)
filter.syncedFrom = s
filter.syncedTo = e
}
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
} catch {
categoryScope.value = []
scopeDefaultGroupId.value = 0
}
// 当前分组:保留用户已选(仍可见时),否则回默认(可见组第一项)。
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
}
/** 打开分类管理(对话框自取列表);入口对所有人渲染——同组普通号只读可看(V203),
* 写控件由对话框按 editable 分档。 */
function openManage(): void {
manageVisible.value = true
}
/** 分类管理里有写操作(建/改/删):刷一遍筛选下拉与商品数徽标。 */
async function onManaged(): Promise<void> {
await loadGroupOptions()
}
/** 批量归类完成(对话框已关):清勾选+重取列表(行集 groupName 变了)。
* 移出分类 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,
productCode: filter.productCode,
titleKeyword: filter.titleKeyword,
category: filter.category,
groupId: filter.groupId,
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()
}
/** 类目级联面板点末级即查:选完立刻按该类目过滤刷新(回第一页),同分组下拉口径;
* 点「全部类目」/触发框 × = 清选重查。 */
function onCategoryPicked(name: string): void {
filter.category = name
apply()
}
/** 重置:筛选面回出厂空态,回第一页重取。 */
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()
}
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 copyText(text: string): Promise<void> {
if (!text) return
try {
await navigator.clipboard.writeText(text)
showAdminFeedback(`已复制 ${text}`, 'success')
} catch {
showAdminFeedback('复制失败,请手动选择复制', 'error')
}
}
// ---------------- 删除(先删桶、全成功才删库行,语义在后端 JdProductDeleteService) ----------------
/** 勾选集(按商品 id,跨页累计记忆,2026-11-17 用户口径)。翻页/重取不清;显式出口
* 曾是工具栏「清除勾选」,同日用户口径「清除勾选这个按钮删除」后只剩批量删除/
* 批量归类成功后自动清。 */
const selectedIds = ref<Set<number>>(new Set())
const deleting = 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
}
const allSelected = computed(() => rows.value.length > 0 && rows.value.every((row) => selectedIds.value.has(row.id)))
function toggleSelectAll(checked: boolean): void {
const next = new Set(selectedIds.value)
for (const row of rows.value) {
if (checked) next.add(row.id)
else 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">
<section class="panel-box">
<div class="jd-panel-head">
<h3>京东数据库</h3>
</div>
<div class="jd-filter-row">
<div class="form-group">
<label>商品编码</label>
<input v-model="filter.productCode" type="text" placeholder="输入商品编码" @keyup.enter="apply" />
</div>
<div class="form-group">
<label>标题关键字</label>
<input v-model="filter.titleKeyword" type="text" placeholder="输入标题关键字" @keyup.enter="apply" />
</div>
<div class="form-group">
<label>类目</label>
<!-- 类目(京麦末级类目)京麦同款级联面板(2026-11-16 客户口径):一/二级为静态导航
(采集链无层级数据),第三栏已落库末级+搜索,点行选中即查(回第一页)。 -->
<JdCategoryCascader
:options="categoryOptions"
:model-value="filter.category"
tree-only
@change="onCategoryPicked"
/>
</div>
<div v-if="categoryScope.length" class="form-group">
<label>分组</label>
<!-- 当前分组(V203):能看多组的人(平台超管/公司超管)下拉切换;只有一组时锁定显示组名。
2026-11-16 用户口径「做成分组」:从工具栏裸下拉上移进筛选行,做成带标签的表单控件
(与其他筛选同壳同圆角)。2026-11-17 用户口径「和亚马逊不统一」:原生 <select> 的
弹层是浏览器直角原生物,换 el-select(同亚马逊面 admin-filter 同壳),组多时可搜索。
分组是分类列/分类筛选/分类管理的作用域,不是筛选条件 ⇒ 放在筛选行也只是占位,
「重置」不清它(挂载测试钉住)。 -->
<el-select
v-if="canSwitchGroup"
: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>
<span v-else class="jd-group-locked" title="你的数据权限内只有这一个分组">{{ currentGroupName }}</span>
</div>
<div class="form-group">
<label>同步开始日期</label>
<input v-model="filter.syncedFrom" type="date" :max="filter.syncedTo" @change="clampJdSyncedRange" />
</div>
<div class="form-group">
<label>同步结束日期</label>
<input v-model="filter.syncedTo" type="date" :min="filter.syncedFrom" @change="clampJdSyncedRange" />
</div>
<div class="jd-filter-actions">
<button class="btn" type="button" @click="apply">查询</button>
<button class="btn btn-secondary" type="button" @click="reset">重置</button>
</div>
</div>
<LoadFailureBar v-if="loadFailure" :message="loadFailure" @retry="load" />
<div class="jd-toolbar">
<span class="jd-summary">共 {{ total }} 条京东商品</span>
<!-- 批量编辑分类(2026-11-17 用户口径「店铺分类弹窗删了,加个批量编辑分类」):
只对当前分组可写档(本组管理员/公司超管/平台超管)渲染;勾选 0 时置灰(与
删除勾选同款常显口径),打开后按累计勾选(跨页)提交归入/移出。 -->
<button
v-if="canManageCategories"
class="btn btn-secondary"
type="button"
:disabled="!selectedCount"
@click="openBatchEdit"
>批量编辑分类</button>
<!-- 商品行写只对平台超管;分类写由当前分组 editable 决定(本组管理员/公司超管/平台超管)。
非超管看到只读提示,分类维护入口=工具栏「分类管理」(只读可看,写控件按 editable 分档)。 -->
<span v-if="!isSuperAdmin" class="jd-readonly-tip">只读:编辑/删除仅超级管理员可用;分类在本页「分类管理」查看与维护</span>
<!-- 分类筛选(2026-11-17 与分组同刀换 el-select,弹层圆角与亚马逊统一):保留显式
「全部分类」行(value='',与旧原生下拉同语义——列表里永远有回全量的出口),
选中它/重置回 '' 时闭壳显示同文案占位。 -->
<el-select
v-model="filter.groupId"
class="admin-filter"
placeholder="全部分类"
aria-label="筛选店铺分类"
@change="apply"
>
<el-option label="全部分类" value="" />
<el-option v-for="group in groupOptions" :key="group.id" :label="group.name" :value="String(group.id)" />
</el-select>
<!-- 分类管理(2026-11-17 用户口径「京东分类管理并到京东数据库分类编辑里」):V204 独立
子菜单页整体并入本对话框(搜索+新建+改名+删除)。入口对所有人渲染(同组普通号
只读可看,V203),写控件由对话框按当前分组 editable 分档。 -->
<button
class="btn btn-secondary jd-manage-btn"
type="button"
@click="openManage"
>分类管理</button>
<!-- 批量删除 2026-11-16 客户口径「不要勾选后才显示」:常显,勾选 0 时置灰。
同日写权限收窄到超管:非超管整颗按钮不渲染(勾选列 2026-11-17 起对本组可写
分类管理员开放,服务批量归类)。 -->
<button
v-if="isSuperAdmin"
class="btn btn-danger jd-delete-btn"
type="button"
:disabled="deleting || !selectedCount"
@click="removeSelected"
>
{{ deleting ? '删除中...' : `删除勾选(${selectedCount})` }}
</button>
</div>
<div class="table-scroll jd-table-scroll">
<table>
<thead>
<tr>
<th v-if="hasSelectionColumn" style="width: 42px"><input type="checkbox" :checked="allSelected" @change="toggleSelectAll(($event.target as HTMLInputElement).checked)" /></th>
<th style="width: 92px">分类</th>
<th style="width: 520px">商品信息</th>
<th style="width: 76px">总库存</th>
<th style="width: 120px">价格</th>
<!-- 同步时间收尾(创建/修改两列 2026-11-16 客户口径「不要」下线,仍收详情抽屉);
操作列 2026-11-16「操作放最后一列」口径置末;同日写权限收窄到超管 ⇒ 非超管不渲染。 -->
<th style="width: 170px">同步时间</th>
<th v-if="isSuperAdmin" style="width: 138px">操作</th>
</tr>
</thead>
<tbody>
<template v-if="rows.length">
<tr v-for="row in rows" :key="row.id" class="jd-row" @click="openDetail(row)">
<td v-if="hasSelectionColumn" @click.stop><input type="checkbox" :checked="selectedIds.has(row.id)" @change="toggleSelect(row, ($event.target as HTMLInputElement).checked)" /></td>
<td :title="row.groupName || ''">{{ row.groupName || '未分类' }}</td>
<td class="jd-info-cell">
<div class="jd-info-wrap">
<div class="jd-info-figure">
<img v-if="row.mainImageUrl" class="jd-thumb" :src="row.mainImageUrl" alt="" loading="lazy" />
<span v-else class="jd-thumb jd-thumb-empty">无图</span>
<span v-if="row.healthScore !== null" class="jd-health">信息分 {{ row.healthScore }}</span>
</div>
<div class="jd-info-body">
<div class="jd-info-title" :title="row.title">{{ row.title || '-' }}</div>
<div class="jd-info-meta">
商品编码:<span class="jd-info-code">{{ row.productCode || '-' }}</span>
<button v-if="row.productCode" class="jd-copy-btn" type="button" title="复制商品编码" @click.stop="copyText(row.productCode)">⧉</button>
</div>
<div class="jd-info-meta">货号:<span class="jd-info-code">{{ row.itemNum || '-' }}</span></div>
<!-- 品牌·类目行:京麦列表同款(末级类目名紧随品牌),字段与顺序 2026-11-15 对齐京麦。 -->
<div v-if="row.brand || row.categoryPath" class="jd-info-meta jd-brand-cat">
<span class="jd-brand-name">{{ row.brand || '' }}</span>&nbsp;{{ row.categoryPath || '' }}
</div>
<div v-if="row.skuCount" class="jd-info-tags"><span class="jd-tag">{{ row.skuCount }}个SKU</span></div>
</div>
</div>
</td>
<td>{{ row.stockTotal ?? '-' }}</td>
<td>{{ jdPriceRange(row) || '-' }}</td>
<td>{{ formatDateTime(row.syncedAt || '') }}</td>
<!-- 操作列(2026-11-15 用户口径):编辑/删除;@click.stop 防冒泡开详情抽屉。
2026-11-16「操作放最后一列」口径置末;「上架时间 / 商品状态」列同日整列下线;
同日写权限收窄到超管 ⇒ 非超管不渲染该列。 -->
<td v-if="isSuperAdmin" class="jd-ops-cell" @click.stop>
<button class="btn btn-sm" type="button" @click="openEdit(row)">编辑</button>
<button class="btn btn-sm btn-danger" type="button" :disabled="deleting" @click="removeOne(row)">删除</button>
</td>
</tr>
</template>
<tr v-else-if="loading">
<td :colspan="emptyColspan" class="empty-tip">加载中...</td>
</tr>
<tr v-else>
<td :colspan="emptyColspan" class="empty-tip">暂无京东商品数据</td>
</tr>
</tbody>
</table>
</div>
<OldPagination :total="total" :page="page" :page-size="pageSize" @change="changePage" @size-change="changeSize" />
</section>
<JdBatchAssignDialog
:visible="batchEditVisible"
:owner-group-id="ownerGroupId"
:owner-group-name="currentGroupName"
:categories="groupOptions"
:selected-ids="[...selectedIds]"
@close="batchEditVisible = false"
@done="onBatchAssigned"
/>
<JdCategoryManageDialog
:visible="manageVisible"
:owner-group-id="ownerGroupId"
:owner-group-name="currentGroupName"
:editable="canManageCategories"
@close="manageVisible = false"
@changed="onManaged"
/>
<JdProductDetailDrawer
:item="detailItem"
:visible="detailVisible"
@close="closeDetail"
/>
<JdProductEditDrawer
:item="editItem"
:visible="editVisible"
@close="closeEdit"
@saved="onEditSaved"
/>
</div>
</template>
<style scoped src="./jd-database-page.css"></style>