457 lines
13 KiB
Vue
457 lines
13 KiB
Vue
<script setup lang="ts">
|
||
/** 京东商品「店铺分类」弹窗(2026-11-15 由「分组管理」小弹窗重做):左右分栏——左栏分类管理
|
||
* (新建=输入行尾「+」图标,排序数字输入 2026-11-15 客户口径下线、sortOrder 恒 0;
|
||
* 改名/删除;分组全平台一份、措辞「分类」),右栏商品人工归类(独立件
|
||
* JdProductAssignPane:范围切换+搜索+勾选 →「归入所选分类」/「移出分类」,批量端点
|
||
* batch-assign-group 与单行编辑同口径置 manual_locked=1——归类即人工干预,采集不再覆盖/
|
||
* 清理该行)。左栏点亮的分类=右栏归类目标;目标被删时右栏自动拉回全量页签。
|
||
* 重名等业务失败把后端返回的中文文案原样展示在弹窗内(envelope code 非 0 → errorTextOf);
|
||
* 删除分类前 openAdminConfirm 二次确认;变更成功后 emit('changed'),由页面刷新分组下拉与
|
||
* 列表。弹窗壳 v-if 整树重建 ⇒ 每次打开即两个面板都重新拉数据,无需手动重置。 */
|
||
import { computed, ref, watch } from 'vue'
|
||
import {
|
||
createJdProductGroup,
|
||
deleteJdProductGroup,
|
||
fetchJdProductGroups,
|
||
updateJdProductGroup,
|
||
type JdProductGroup,
|
||
} from './jd-product-api.ts'
|
||
import JdProductAssignPane from './JdProductAssignPane.vue'
|
||
import { openAdminConfirm } from '@/components/admin-confirm'
|
||
import { requestErrorMessage } from '../../api/envelope.ts'
|
||
|
||
const props = defineProps<{
|
||
visible: boolean
|
||
}>()
|
||
|
||
const emit = defineEmits<{ (event: 'close'): void; (event: 'changed'): void }>()
|
||
|
||
// ---------------- 左栏:分类管理(分组全平台一份,界面措辞「分类」) ----------------
|
||
|
||
const loading = ref(false)
|
||
const errorText = ref('')
|
||
const groups = ref<JdProductGroup[]>([])
|
||
/** 新建:只收名称(排序输入 2026-11-15 客户口径下线,sortOrder 恒 0,后端契约不变)。 */
|
||
const createName = ref('')
|
||
const creating = ref(false)
|
||
/** 改名:正在编辑的分类 id 与草稿名。 */
|
||
const editingId = ref(0)
|
||
const editingName = ref('')
|
||
const saving = ref(false)
|
||
/** 左侧点亮的分类=右栏「归入所选分类」的归类目标;删除后自动清掉。 */
|
||
const selectedGroupId = ref(0)
|
||
|
||
const canCreate = computed(() => createName.value.trim().length > 0 && !creating.value && !saving.value)
|
||
const selectedGroup = computed(() => groups.value.find((group) => group.id === selectedGroupId.value) || null)
|
||
|
||
async function loadGroups(): Promise<void> {
|
||
loading.value = true
|
||
errorText.value = ''
|
||
try {
|
||
groups.value = await fetchJdProductGroups()
|
||
} catch (error) {
|
||
errorText.value = requestErrorMessage(error) || '分类列表加载失败'
|
||
groups.value = []
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
function sortGroups(list: JdProductGroup[]): JdProductGroup[] {
|
||
return [...list].sort((a, b) => a.sortOrder - b.sortOrder || a.id - b.id)
|
||
}
|
||
|
||
/** 变更后统一收口:刷新左栏商品数徽标+上抛页面(分组下拉与行集都依赖它)。 */
|
||
async function refreshAfterChange(): Promise<void> {
|
||
groups.value = sortGroups(await fetchJdProductGroups())
|
||
emit('changed')
|
||
}
|
||
|
||
async function submitCreate(): Promise<void> {
|
||
const name = createName.value.trim()
|
||
if (!name || creating.value) return
|
||
creating.value = true
|
||
errorText.value = ''
|
||
try {
|
||
await createJdProductGroup({ name, sortOrder: 0 })
|
||
createName.value = ''
|
||
await refreshAfterChange()
|
||
} catch (error) {
|
||
errorText.value = requestErrorMessage(error) || '新建分类失败'
|
||
} finally {
|
||
creating.value = false
|
||
}
|
||
}
|
||
|
||
function startRename(group: JdProductGroup): void {
|
||
editingId.value = group.id
|
||
editingName.value = group.name
|
||
errorText.value = ''
|
||
}
|
||
|
||
function cancelRename(): void {
|
||
editingId.value = 0
|
||
editingName.value = ''
|
||
}
|
||
|
||
async function submitRename(group: JdProductGroup): Promise<void> {
|
||
const name = editingName.value.trim()
|
||
if (!name || saving.value) return
|
||
saving.value = true
|
||
errorText.value = ''
|
||
try {
|
||
await updateJdProductGroup(group.id, { name, sortOrder: group.sortOrder })
|
||
cancelRename()
|
||
await refreshAfterChange()
|
||
} catch (error) {
|
||
errorText.value = requestErrorMessage(error) || '改名失败'
|
||
} finally {
|
||
saving.value = false
|
||
}
|
||
}
|
||
|
||
async function removeGroup(group: JdProductGroup): Promise<void> {
|
||
const ok = await openAdminConfirm({ title: '删除分类', message: `确定删除分类「${group.name}」吗?删除后该分类下 ${group.productCount} 个商品回到未分类。`, confirmText: '确认删除', danger: true })
|
||
if (!ok || saving.value) return
|
||
saving.value = true
|
||
errorText.value = ''
|
||
try {
|
||
await deleteJdProductGroup(group.id)
|
||
// 删掉的是当前归类目标:清点亮,右栏自己会拉回全量页签(watch selectedGroup)。
|
||
if (selectedGroupId.value === group.id) {
|
||
selectedGroupId.value = 0
|
||
}
|
||
await refreshAfterChange()
|
||
} catch (error) {
|
||
errorText.value = requestErrorMessage(error) || '删除分类失败'
|
||
} finally {
|
||
saving.value = false
|
||
}
|
||
}
|
||
|
||
/** 左侧点亮分类=归类目标(右栏「当前分类」页签随点亮对象换行集)。 */
|
||
function pickGroup(group: JdProductGroup): void {
|
||
if (saving.value) return
|
||
selectedGroupId.value = group.id
|
||
}
|
||
|
||
// 打开即拉一次分类列表;右栏行集由 JdProductAssignPane 挂载时自取。
|
||
watch(
|
||
() => props.visible,
|
||
(visible) => {
|
||
if (visible) {
|
||
cancelRename()
|
||
createName.value = ''
|
||
errorText.value = ''
|
||
selectedGroupId.value = 0
|
||
void loadGroups()
|
||
}
|
||
},
|
||
)
|
||
|
||
/** 右栏归类成功:分类商品数变化+页面行集的 groupName 都要刷。 */
|
||
function onPaneChanged(): void {
|
||
void refreshAfterChange()
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<div v-if="visible" class="modal-mask" @click.self="emit('close')">
|
||
<div class="group-modal">
|
||
<div class="modal-head">
|
||
<h3>店铺分类</h3>
|
||
<button class="modal-close" type="button" aria-label="关闭" @click="emit('close')">×</button>
|
||
</div>
|
||
<p class="modal-tip">
|
||
左侧管理分类并点选目标分类;右侧勾选商品后「归入所选分类」或「移出分类」。
|
||
人工归类过的商品不再被采集自动覆盖(价格/库存等字段停更)。
|
||
</p>
|
||
<p v-if="errorText" class="group-error">{{ errorText }}</p>
|
||
|
||
<div class="split-body">
|
||
<!-- 左栏:分类管理 -->
|
||
<aside class="left-pane">
|
||
<div class="group-create">
|
||
<input
|
||
v-model="createName"
|
||
type="text"
|
||
placeholder="输入新分类名称"
|
||
@keyup.enter="submitCreate"
|
||
/>
|
||
<!-- 新建=输入行尾「+」图标(2026-11-15 客户口径:原第二行文字按钮收成图标上移,
|
||
排序数字输入下线——sortOrder 恒 0,后端契约不变)。 -->
|
||
<button
|
||
class="create-icon-btn"
|
||
type="button"
|
||
title="新建分类"
|
||
aria-label="新建分类"
|
||
:disabled="!canCreate"
|
||
@click="submitCreate"
|
||
>+</button>
|
||
</div>
|
||
<p v-if="loading" class="pane-tip">加载中...</p>
|
||
<ul class="group-list">
|
||
<li
|
||
v-for="group in groups"
|
||
:key="group.id"
|
||
class="group-row"
|
||
:class="{ active: selectedGroupId === group.id }"
|
||
@click="pickGroup(group)"
|
||
>
|
||
<template v-if="editingId === group.id">
|
||
<input v-model="editingName" type="text" @keyup.enter="submitRename(group)" />
|
||
<span class="group-actions" @click.stop>
|
||
<button class="btn btn-mini" type="button" :disabled="saving" @click="submitRename(group)">保存</button>
|
||
<button class="btn btn-secondary btn-mini" type="button" @click="cancelRename">取消</button>
|
||
</span>
|
||
</template>
|
||
<template v-else>
|
||
<span class="group-name" :title="group.name">{{ group.name }}</span>
|
||
<span class="group-meta">{{ group.productCount }}</span>
|
||
<span class="group-actions" @click.stop>
|
||
<button class="btn btn-secondary btn-mini" type="button" @click="startRename(group)">改名</button>
|
||
<button class="btn btn-danger btn-mini" type="button" :disabled="saving" @click="removeGroup(group)">删除</button>
|
||
</span>
|
||
</template>
|
||
</li>
|
||
<li v-if="!loading && !groups.length && !errorText" class="pane-tip">暂无分类,先新建一个</li>
|
||
</ul>
|
||
</aside>
|
||
|
||
<!-- 右栏:商品人工归类(独立件,行集/勾选/批量归类都在那里) -->
|
||
<JdProductAssignPane :selected-group="selectedGroup" @changed="onPaneChanged" />
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<style scoped>
|
||
.modal-mask {
|
||
position: fixed;
|
||
inset: 0;
|
||
z-index: 2800;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
padding: 40px 20px;
|
||
background: rgba(45, 66, 86, 0.36);
|
||
backdrop-filter: blur(6px);
|
||
}
|
||
.group-modal {
|
||
display: flex;
|
||
flex-direction: column;
|
||
width: min(1080px, 100%);
|
||
max-height: 86vh;
|
||
padding: 18px 20px 20px;
|
||
border: 1px solid #c7d7e5;
|
||
border-radius: 14px;
|
||
background: #ffffff;
|
||
box-shadow: 0 22px 60px rgba(39, 67, 94, 0.2);
|
||
}
|
||
.modal-head {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
}
|
||
.modal-head h3 {
|
||
margin: 0;
|
||
font-size: 15px;
|
||
font-weight: 650;
|
||
color: #24384d;
|
||
}
|
||
.modal-close {
|
||
width: 30px;
|
||
height: 30px;
|
||
border: 1px solid transparent;
|
||
border-radius: 9px;
|
||
background: none;
|
||
color: #5b6f83;
|
||
font-size: 20px;
|
||
line-height: 1;
|
||
cursor: pointer;
|
||
}
|
||
.modal-close:hover {
|
||
border-color: #cbd9e6;
|
||
background: #eef4fa;
|
||
color: #24384d;
|
||
}
|
||
.modal-tip {
|
||
margin: 8px 0 10px;
|
||
color: #8293a5;
|
||
font-size: 12.5px;
|
||
line-height: 1.5;
|
||
}
|
||
.group-error {
|
||
margin: 0 0 8px;
|
||
padding: 10px 12px;
|
||
border: 1px solid #f0c9c2;
|
||
border-radius: 10px;
|
||
background: #fdf1ef;
|
||
color: #a04432;
|
||
font-size: 13px;
|
||
}
|
||
.split-body {
|
||
display: flex;
|
||
gap: 0;
|
||
flex: 1;
|
||
min-height: 0;
|
||
}
|
||
/* 左栏(分类管理):固定宽,独立滚动。 */
|
||
.left-pane {
|
||
flex: 0 0 248px;
|
||
min-width: 0;
|
||
display: flex;
|
||
flex-direction: column;
|
||
border-right: 1px solid #e7eef5;
|
||
padding-right: 14px;
|
||
}
|
||
.group-create {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 8px;
|
||
margin-bottom: 10px;
|
||
}
|
||
.group-create input {
|
||
flex: 1;
|
||
min-width: 0;
|
||
min-height: 36px;
|
||
padding: 6px 12px;
|
||
border: 1px solid #cbd9e6;
|
||
border-radius: 9px;
|
||
background: #f8fbfd;
|
||
color: #24384d;
|
||
font-size: 13.5px;
|
||
font-family: inherit;
|
||
outline: none;
|
||
}
|
||
.group-create input:focus {
|
||
background: #ffffff;
|
||
border-color: #5f85ad;
|
||
}
|
||
/* 新建「+」图标按钮:与输入同行(原第二行文字按钮上移收成图标)。 */
|
||
.create-icon-btn {
|
||
flex: none;
|
||
width: 36px;
|
||
min-height: 36px;
|
||
padding: 0;
|
||
border: 1px solid #4f78a5;
|
||
border-radius: 9px;
|
||
background: linear-gradient(135deg, #5f85ad, #4f78a5);
|
||
color: #ffffff;
|
||
font-size: 18px;
|
||
line-height: 1;
|
||
cursor: pointer;
|
||
}
|
||
.create-icon-btn:disabled {
|
||
cursor: not-allowed;
|
||
opacity: 0.6;
|
||
}
|
||
.group-list {
|
||
flex: 1;
|
||
min-height: 0;
|
||
overflow-y: auto;
|
||
margin: 0;
|
||
padding: 0;
|
||
list-style: none;
|
||
border-top: 1px solid #e7eef5;
|
||
}
|
||
.group-row {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 8px;
|
||
padding: 8px 6px;
|
||
border-bottom: 1px solid #e7eef5;
|
||
font-size: 13.5px;
|
||
cursor: pointer;
|
||
}
|
||
.group-row:hover {
|
||
background: #f1f7fb;
|
||
}
|
||
/* 左侧点亮=归类目标:右侧「归入所选分类」就落在这一行。 */
|
||
.group-row.active {
|
||
background: #e8f1fa;
|
||
box-shadow: inset 3px 0 0 #4f78a5;
|
||
}
|
||
.group-row input {
|
||
flex: 1;
|
||
min-width: 0;
|
||
min-height: 32px;
|
||
padding: 4px 10px;
|
||
border: 1px solid #cbd9e6;
|
||
border-radius: 8px;
|
||
font-size: 13.5px;
|
||
font-family: inherit;
|
||
}
|
||
.group-name {
|
||
flex: 1;
|
||
min-width: 0;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
color: #24384d;
|
||
}
|
||
.group-meta {
|
||
flex: none;
|
||
min-width: 22px;
|
||
padding: 1px 7px;
|
||
border-radius: 9px;
|
||
background: #eef4fa;
|
||
color: #4e6479;
|
||
font-size: 12px;
|
||
text-align: center;
|
||
white-space: nowrap;
|
||
}
|
||
.group-actions {
|
||
flex: none;
|
||
display: flex;
|
||
gap: 6px;
|
||
}
|
||
.pane-tip {
|
||
margin: 10px 2px;
|
||
color: #8293a5;
|
||
font-size: 13px;
|
||
}
|
||
.btn {
|
||
display: inline-flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
min-height: 38px;
|
||
padding: 7px 16px;
|
||
border: 1px solid #4f78a5;
|
||
border-radius: 9px;
|
||
background: linear-gradient(135deg, #5f85ad, #4f78a5);
|
||
color: #ffffff;
|
||
font-family: inherit;
|
||
font-size: 13px;
|
||
cursor: pointer;
|
||
}
|
||
.btn:disabled {
|
||
cursor: not-allowed;
|
||
opacity: 0.6;
|
||
}
|
||
.btn-mini {
|
||
min-height: 28px;
|
||
padding: 3px 9px;
|
||
font-size: 12px;
|
||
}
|
||
.btn-secondary {
|
||
background: #ffffff;
|
||
color: #5b6f83;
|
||
border-color: #c7d7e5;
|
||
}
|
||
.btn-secondary:hover:not(:disabled) {
|
||
color: #2f5d8b;
|
||
border-color: #95b1cb;
|
||
background: #edf5fb;
|
||
}
|
||
.btn-danger {
|
||
background: #ffffff;
|
||
color: #a04432;
|
||
border-color: #e4b3a8;
|
||
}
|
||
.btn-danger:hover:not(:disabled) {
|
||
background: #fdf1ef;
|
||
border-color: #d08a7b;
|
||
}
|
||
</style>
|