feat(jd): 整合自定义分类并保护商品数据

This commit is contained in:
2026-11-16 16:12:04 +08:00
parent 79eb7a7f16
commit 8c81546aa7
23 changed files with 708 additions and 1643 deletions
@@ -101,22 +101,10 @@ test('类目筛选参数序列化:空串不下发、非空下发 category(
assert.equal(jdProductFilterActive(filtered), true, '选中类目算激活筛选')
})
/** 分类状态筛选(2026-11-15 客户口径「查询已分类的跟未分类,分类好的可以不显示」):
* 全部不下发、已分类下发 grouped、未分类下发 ungrouped(与店铺分类弹窗同一后端谓词)。 */
test('分类状态筛选参数序列化:全部不下发、已分类 grouped、未分类 ungrouped', async () => {
const { createJdProductFilterState, toJdProductListParams, jdProductFilterActive } =
await import('../src/pages/jd/jd-product-filter.ts')
const empty = createJdProductFilterState()
assert.equal('grouped' in toJdProductListParams(empty, 1, 20), false, '全部不下发 grouped')
assert.equal('ungrouped' in toJdProductListParams(empty, 1, 20), false, '全部不下发 ungrouped')
const grouped = { ...empty, groupScope: 'grouped' as const }
assert.equal(toJdProductListParams(grouped, 1, 20).grouped, true, '已分类档下发 grouped=true')
assert.equal('ungrouped' in toJdProductListParams(grouped, 1, 20), false, '已分类档不下发 ungrouped')
assert.equal(jdProductFilterActive(grouped), true, '选中已分类算激活筛选')
const ungrouped = { ...empty, groupScope: 'ungrouped' as const }
assert.equal(toJdProductListParams(ungrouped, 1, 20).ungrouped, true, '未分类档下发 ungrouped=true')
assert.equal('grouped' in toJdProductListParams(ungrouped, 1, 20), false, '未分类档不下发 grouped')
assert.equal(jdProductFilterActive(ungrouped), true, '选中未分类算激活筛选')
test('自定义分类使用 group_id,不发送分类状态参数', async () => {
const { createJdProductFilterState, toJdProductListParams } = await import('../src/pages/jd/jd-product-filter.ts')
const query = toJdProductListParams({ ...createJdProductFilterState(), groupId: '7' }, 1, 20)
assert.equal(query.group_id, 7)
assert.equal('grouped' in query, false)
assert.equal('ungrouped' in query, false)
})
@@ -24,8 +24,8 @@ import {
toJdProductListParams,
} from '../src/pages/jd/jd-product-filter.ts'
import {
jdCategoryNavChildren,
jdCategoryNavRoots,
jdCategoryChildren,
jdCategoryRoots,
} from '../src/pages/jd/jd-category-tree.ts'
import {
jdPriceRange,
@@ -36,15 +36,14 @@ import {
test('类目级联静态导航树——服饰内衣二级与京麦面板样本逐字同源,一级集非空且无重名', () => {
// 京麦面板真机样本(2026-11-16 客户截图):服饰内衣下就是这五个二级,改名即红。
assert.deepEqual(jdCategoryNavChildren('服饰内衣'), ['服饰配件', '内衣', '女装', '男装', '定制服装/工装'])
assert.deepEqual(jdCategoryNavChildren('不存在的类目'), [], '未知一级回落空子级(面板不出第二栏)')
assert.deepEqual(jdCategoryChildren(['服饰内衣']).map(node => node.name), ['服饰配件', '内衣', '女装', '男装', '定制服装/工装'])
assert.deepEqual(jdCategoryChildren(['不存在的类目']), [], '未知一级回落空子级(面板不出第二栏)')
const roots = jdCategoryNavRoots()
assert.ok(roots.length >= 20, `一级导航 ${roots.length} 个,常用集不应被清空`)
const roots = jdCategoryRoots().map(node => node.name)
assert.deepEqual(roots, ['服饰内衣', '母婴', '运动户外'], '一级导航与京麦授权类目一致')
assert.equal(new Set(roots).size, roots.length, '一级类目名不得重复')
assert.equal(roots[0], '服饰内衣', '服饰内衣置首(客户商品全在服饰类目)')
for (const root of roots) {
const children = jdCategoryNavChildren(root)
const children = jdCategoryChildren([root]).map(node => node.name)
assert.equal(new Set(children).size, children.length, `${root} 的二级不得重复`)
}
})
@@ -16,7 +16,7 @@ import { flushPromises, mount, type VueWrapper } from '@vue/test-utils'
import { createPinia, setActivePinia, type Pinia } from 'pinia'
import JdDatabasePage from '@/pages/jd/JdDatabasePage.vue'
import JdProductGroupDialog from '@/pages/jd/JdProductGroupDialog.vue'
import { useAdminSessionStore } from '@/stores/admin-session'
const jdApi = vi.hoisted(() => ({
@@ -227,7 +227,8 @@ describe('京东数据库页(v1.4 Phase 5)', () => {
expect(textInputs).toHaveLength(2, '商品编码+标题关键字两个文本筛选框(店铺/商品状态已下线)')
await textInputs[0].setValue('JD-1001')
await textInputs[1].setValue('保温杯')
await wrapper.find('select').setValue('7')
await wrapper.find('.cat-trigger').trigger('click')
await wrapper.find('.cat-group-pick').trigger('click')
const dateInputs = wrapper.findAll('input[type="date"]')
await dateInputs[0].setValue('2026-11-01')
await dateInputs[1].setValue('2026-11-09')
@@ -257,13 +258,10 @@ describe('京东数据库页(v1.4 Phase 5)', () => {
const wrapper = await mountWith({ items: [], total: 0, page: 1, pageSize: 20 })
expect(jdApi.fetchJdProductGroups).toHaveBeenCalledWith()
const groupSelect = wrapper.find('select')
const optionTexts = groupSelect.findAll('option').map((o) => o.text())
expect(optionTexts).toContain('全部分类')
expect(optionTexts).toContain('甲组')
await wrapper.find('.cat-trigger').trigger('click')
expect(wrapper.find('.cat-custom').text()).toContain('甲组')
const callsBeforeSelect = jdApi.fetchJdProductList.mock.calls.length
await groupSelect.setValue('7')
await wrapper.find('.cat-group-pick').trigger('click')
await flushPromises()
expect(jdApi.fetchJdProductList.mock.calls.length).toBe(callsBeforeSelect + 1, '选中分组必须立刻重查')
expect(jdApi.fetchJdProductList.mock.calls.at(-1)![0]).toMatchObject({ groupId: '7', page: 1 })
@@ -358,22 +356,11 @@ describe('京东数据库页(v1.4 Phase 5)', () => {
expect(jdApi.fetchJdProductList.mock.calls.at(-1)![0].category).toBe('')
})
it('分类状态下拉:全部/已分类/未分类三档,选中即查回第一页并下发对应参数', async () => {
it('不再显示独立分类与分类状态筛选', async () => {
const wrapper = await mountWith({ items: [], total: 0, page: 1, pageSize: 20 })
const selects = wrapper.findAll('.jd-filter-row select')
const scopeSelect = selects[1]
const optionTexts = scopeSelect.findAll('option').map((o) => o.text())
expect(optionTexts).toEqual(['全部', '已分类', '未分类'])
// 选「已分类」= 只看 group_id 非空的行(分类好的行想单独看时用这一档)
await scopeSelect.setValue('grouped')
await flushPromises()
expect(jdApi.fetchJdProductList.mock.calls.at(-1)![0]).toMatchObject({ groupScope: 'grouped', page: 1 })
// 选「未分类」= 只看 group_id 为空的行(分类好的不显示)
await scopeSelect.setValue('ungrouped')
await flushPromises()
expect(jdApi.fetchJdProductList.mock.calls.at(-1)![0]).toMatchObject({ groupScope: 'ungrouped', page: 1 })
expect(wrapper.find('.jd-filter-row').text()).not.toContain('分类状态')
expect(wrapper.find('.jd-filter-row select').exists()).toBe(false)
expect(wrapper.findAll('button').some(button => button.text() === '店铺分类')).toBe(false)
})
it('重置把筛选面回空并回第一页重取', async () => {
@@ -565,6 +552,8 @@ describe('京东数据库页(v1.4 Phase 5)', () => {
})
it('移除分类(2026-11-16 页工具栏):计数只算勾选中已分类的行(未分类行不参与=不白吃人工锁)→ 确认点名数量与人工锁后果 → (ids, null) 调批量归类,成功后刷新并清勾选', async () => {
jdApi.fetchJdProductGroups.mockResolvedValueOnce([{ id: 7, name: '甲组', sortOrder: 0, productCount: 3 }])
.mockResolvedValueOnce([{ id: 7, name: '甲组', sortOrder: 0, productCount: 1 }])
const wrapper = await mountWith({
items: [item(1), item(2, { groupId: null, groupName: '' }), item(3)],
total: 3, page: 1, pageSize: 20,
@@ -604,6 +593,9 @@ describe('京东数据库页(v1.4 Phase 5)', () => {
// 成功后重取列表并清勾选:按钮回到置灰。
expect(jdApi.fetchJdProductList.mock.calls.length).toBeGreaterThan(1)
expect(removeBtn().attributes('disabled')).toBeDefined()
await wrapper.find('.cat-trigger').trigger('click')
expect(jdApi.fetchJdProductGroups).toHaveBeenCalledTimes(2)
expect(wrapper.find('.cat-custom').text()).toContain('甲组(1)')
})
it('移除分类:确认弹窗点取消就不发请求(防误操作的第一道闸)', async () => {
@@ -628,90 +620,62 @@ describe('京东数据库页(v1.4 Phase 5)', () => {
expect(document.body.textContent).toContain('分类不存在,请刷新分类后重试')
})
it('店铺分类弹窗(原「管理分组」):打开即拉分类列表与右栏商品行集,变更后刷新分类下拉与列表', async () => {
const wrapper = await mountWith({ items: [item(1)], total: 1, page: 1, pageSize: 20 })
const callsBefore = jdApi.fetchJdProductList.mock.calls.length
await wrapper.findAll('button').find((b) => b.text() === '店铺分类')!.trigger('click')
await flushPromises()
await flushPromises()
expect(wrapper.find('.group-modal').exists()).toBe(true)
expect(wrapper.find('.group-modal').text()).toContain('店铺分类', '弹窗标题改「店铺分类」')
// 打开弹窗 = 拉一次分类列表(第 2 次:首屏已拉过一次;分类全平台一份、不带公司维度)
expect(jdApi.fetchJdProductGroups).toHaveBeenCalledTimes(2)
expect(jdApi.fetchJdProductGroups).toHaveBeenLastCalledWith()
// 右栏取数复用 fetchJdProductList:不带公司维度(公共库),默认「全部」范围不带 ungrouped=true。
const dialogCall = jdApi.fetchJdProductList.mock.calls.at(-1)![0]
expect(dialogCall).not.toHaveProperty('companyId')
expect(dialogCall.ungrouped).toBe(false)
// 模拟分类管理里删了一个分类:emit changed → 页面刷新分类下拉与列表(回第一页)
wrapper.findComponent(JdProductGroupDialog).vm.$emit('changed')
await flushPromises()
await flushPromises()
expect(jdApi.fetchJdProductList.mock.calls.length).toBeGreaterThan(callsBefore)
expect(jdApi.fetchJdProductList.mock.calls.at(-1)![0]).toMatchObject({ page: 1 })
})
it('店铺分类弹窗:左侧点亮分类→勾选商品→「归入所选分类」按(ids, 目标分类 id)调批量归类', async () => {
jdApi.fetchJdProductGroups.mockResolvedValue([
{ id: 7, name: '甲组', sortOrder: 0, productCount: 3 },
{ id: 8, name: '乙组', sortOrder: 1, productCount: 0 },
])
jdApi.fetchJdProductList.mockImplementation(() =>
Promise.resolve({ items: [item(1), item(2)], total: 2, page: 1, pageSize: 20 }))
const wrapper = mount(JdProductGroupDialog, { props: { visible: false }, global: { plugins: [pinia] } })
await wrapper.setProps({ visible: true })
await flushPromises()
await flushPromises()
// 左侧点亮「甲组」=归类目标;只点亮还没勾选时按钮仍置灰(归入=目标+勾选两者齐备)。
const assignBtn = () => wrapper.findAll('button').find((b) => b.text().includes('归入'))
expect(assignBtn()!.attributes('disabled')).toBeDefined()
await wrapper.findAll('.group-row')[0].trigger('click')
await flushPromises()
expect(assignBtn()!.attributes('disabled')).toBeDefined()
expect(assignBtn()!.text()).toContain('甲组', '按钮点名归类目标')
// 勾选两行 → 按钮点亮 → 归入「甲组」。
const boxes = wrapper.findAll('tbody input[type="checkbox"]')
await boxes[0].setValue(true)
await boxes[1].setValue(true)
expect(assignBtn()!.attributes('disabled')).toBeUndefined()
jdApi.batchAssignJdProductGroup.mockResolvedValue(2)
await assignBtn()!.trigger('click')
await flushPromises()
await flushPromises()
expect(jdApi.batchAssignJdProductGroup).toHaveBeenCalledTimes(1)
expect(jdApi.batchAssignJdProductGroup).toHaveBeenCalledWith([1, 2], 7)
// 成功后刷新左栏分类数(商品数)与右栏行集,勾选清空。
expect(jdApi.fetchJdProductGroups).toHaveBeenCalled()
expect(wrapper.findAll('tbody input[type="checkbox"]').every((b) => !(b.element as HTMLInputElement).checked).valueOf()).toBe(true)
})
it('店铺分类弹窗:「未分类」页签走 ungrouped=true 取行集;「移出分类」按(ids, null)调批量归类', async () => {
it('归类先提示人工锁,取消不发送请求', async () => {
jdApi.fetchJdProductGroups.mockResolvedValue([{ id: 7, name: '甲组', sortOrder: 0, productCount: 3 }])
jdApi.fetchJdProductList.mockImplementation(() =>
Promise.resolve({ items: [item(1, { groupId: null, groupName: '' })], total: 1, page: 1, pageSize: 20 }))
const wrapper = mount(JdProductGroupDialog, { props: { visible: false }, global: { plugins: [pinia] } })
await wrapper.setProps({ visible: true })
await flushPromises()
confirmMock.mockResolvedValue(false)
const wrapper = await mountWith({ items: [item(1)], total: 1, page: 1, pageSize: 20 })
await wrapper.find('tbody input[type="checkbox"]').setValue(true)
await wrapper.find('[aria-label="选择归入的分类"]').setValue('7')
await wrapper.findAll('button').find(b => b.text().includes('归入分类'))!.trigger('click')
await flushPromises()
expect(confirmMock).toHaveBeenCalledTimes(1)
expect(confirmMock.mock.calls[0][0].message).toContain('人工编辑锁')
expect(confirmMock.mock.calls[0][0].message).toContain('不再被采集自动覆盖')
expect(jdApi.batchAssignJdProductGroup).not.toHaveBeenCalled()
})
const listCallsBefore = jdApi.fetchJdProductList.mock.calls.length
await wrapper.findAll('.scope-tabs button').find((b) => b.text() === '未分类')!.trigger('click')
await flushPromises()
expect(jdApi.fetchJdProductList.mock.calls.length).toBe(listCallsBefore + 1, '切「未分类」必须立刻重取')
expect(jdApi.fetchJdProductList.mock.calls.at(-1)![0]).toMatchObject({ ungrouped: true })
await wrapper.findAll('tbody input[type="checkbox"]')[0].setValue(true)
it('分类增删、筛选切换和勾选归类', async () => {
jdApi.fetchJdProductGroups.mockResolvedValue([{ id: 7, name: '甲组', sortOrder: 0, productCount: 3 }])
jdApi.createJdProductGroup.mockRejectedValueOnce(new Error('分类已存在')).mockResolvedValueOnce({})
jdApi.deleteJdProductGroup.mockResolvedValue(undefined)
jdApi.batchAssignJdProductGroup.mockResolvedValue(1)
await wrapper.findAll('button').find((b) => b.text() === '移出分类')!.trigger('click')
const wrapper = await mountWith({ items: [item(1)], total: 1, page: 1, pageSize: 20 })
await wrapper.find('.cat-trigger').trigger('click')
await wrapper.find('[aria-label="新建分类名称"]').setValue('新分类')
await wrapper.find('.cat-create').trigger('submit')
await flushPromises()
expect(document.body.textContent).toContain('分类已存在')
expect((wrapper.find('[aria-label="新建分类名称"]').element as HTMLInputElement).value).toBe('新分类')
await wrapper.find('.cat-create').trigger('submit')
await flushPromises()
expect(jdApi.batchAssignJdProductGroup).toHaveBeenCalledWith([1], null)
expect(jdApi.createJdProductGroup).toHaveBeenCalledWith({ name: '新分类', sortOrder: 0 })
confirmMock.mockResolvedValueOnce(false)
await wrapper.find('[aria-label="删除分类 甲组"]').trigger('click')
await flushPromises()
expect(jdApi.deleteJdProductGroup).not.toHaveBeenCalled()
await wrapper.find('.cat-group-pick').trigger('click')
await flushPromises()
expect(jdApi.fetchJdProductList.mock.calls.at(-1)![0]).toMatchObject({ groupId: '7', category: '' })
await wrapper.find('.cat-trigger').trigger('click')
await wrapper.findAll('.cat-col')[0].find('.cat-search').setValue('艺考')
await wrapper.find('.cat-col-search .cat-row').trigger('click')
await flushPromises()
expect(jdApi.fetchJdProductList.mock.calls.at(-1)![0]).toMatchObject({ groupId: '', category: '艺考形体服' })
await wrapper.find('.cat-trigger').trigger('click')
await wrapper.find('.cat-group-pick').trigger('click')
await flushPromises()
expect(jdApi.fetchJdProductList.mock.calls.at(-1)![0]).toMatchObject({ groupId: '7', category: '' })
await wrapper.find('tbody input[type="checkbox"]').setValue(true)
await wrapper.find('[aria-label="选择归入的分类"]').setValue('7')
await wrapper.findAll('button').find(b => b.text().includes('归入分类'))!.trigger('click')
await flushPromises()
expect(jdApi.batchAssignJdProductGroup).toHaveBeenCalledWith([1], 7)
await wrapper.find('.cat-trigger').trigger('click')
await wrapper.find('[aria-label="删除分类 甲组"]').trigger('click')
await flushPromises()
expect(confirmMock.mock.calls.at(-1)![0].message).toContain('商品将保留')
expect(jdApi.deleteJdProductGroup).toHaveBeenCalledWith(7)
expect(jdApi.fetchJdProductList.mock.calls.at(-1)![0].groupId).toBe('')
})
it('空态与加载失败条(失败给重试,不把整页打死)', async () => {