Files
crawler-plugin/admin-frontend-vue/src/pages/jd/JdCategoryManageDialog.vue
T
huangzd1997 283921df0c style(admin): 分类管理弹窗数量徽标贴住名称,按钮仍靠右
2026-11-17 用户反馈「这个 0 和名称离的有点远」:行内 .option-name 是 flex:1,
把整行剩余空白全吃满,商品数徽标与「改名/删除」一起被顶到行尾,名称和号码
之间隔着一大片空白。

改法:名称改 flex:0 1 auto(只包自己的文字,过长仍按 min-width:0 省略号截断),
剩余空间只由 .row-actions 的 margin-left:auto 吸收 ⇒ 徽标紧贴名称、按钮照旧靠右;
只读档没有按钮时整行自然左对齐。

验证:jd-category-manage-dialog 挂载 12/12、test:mount 164/164、npm test
1923/1924(唯一红=date-range-order 在册环境红)、vue-tsc 0 错。
2026-11-17 14:55:19 +08:00

459 lines
14 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">
/** 分类管理对话框(2026-11-17 用户口径「京东分类管理并到京东数据库分类编辑里,
* 做一个搜索和添加 删除 编辑 分类名称即可」):原 V204 独立子菜单页(JdCategoryManagerPage)
* 整体并入本对话框,从京东数据库页工具栏「分类管理」打开。
* 职责不变:当前数据权限分组的分类建/改名/删除+新增的名称搜索;商品归类不在这里
* (去「批量编辑分类」)。列表状态自管理(写操作后自刷新并 emit changed 让父页刷筛选下拉)。
* 权限口径(V203)不变:本组管理员/公司超管/平台超管可写(editable);同组普通号
* 只读可看(无新建行,改名/删除按钮逐行不渲染),后端同口径 403 兜底。 */
import { computed, ref, watch } from 'vue'
import {
createJdProductGroup,
deleteJdProductGroup,
fetchJdProductGroups,
updateJdProductGroup,
type JdProductGroup,
} from './jd-product-api.ts'
import { openAdminConfirm } from '@/components/admin-confirm'
import { showAdminFeedback } from '@/components/admin-feedback-ui'
import { loadFailureMessage } from '@/components/load-failure-model'
import LoadFailureBar from '@/components/LoadFailureBar.vue'
import { requestErrorMessage } from '../../api/envelope.ts'
const props = defineProps<{
visible: boolean
/** 当前数据权限分组(分类的作用域;<=0=不属于任何分组,只留空态)。 */
ownerGroupId: number
ownerGroupName: string
/** 当前分组可写(scope 的 editable):建/改名/删除控件全挂它。 */
editable: boolean
}>()
const emit = defineEmits<{ (event: 'close'): void; (event: 'changed'): void }>()
const loading = ref(false)
/** 首屏取数失败(可见红条+重试);写操作失败走 errorText 红条(无重试语义)。 */
const loadFailure = ref('')
const errorText = ref('')
const groups = ref<JdProductGroup[]>([])
/** 搜索:按分类名称包含过滤(本地过滤,不另发请求)。 */
const keyword = ref('')
/** 新建:只收名称(排序输入 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 canCreate = computed(() => props.editable && createName.value.trim().length > 0 && !creating.value && !saving.value)
/** 搜索命中(名称包含,大小写不敏感;空关键字=全量)。 */
const filteredGroups = computed(() => {
const kw = keyword.value.trim().toLowerCase()
if (!kw) return groups.value
return groups.value.filter((group) => group.name.toLowerCase().includes(kw))
})
/** 空表文案分四种:不属于任何组 / 本组没分类(可管理)/ 本组没分类(只读)/ 搜索无命中。 */
const emptyListText = computed(() => {
if (props.ownerGroupId <= 0) return '你还不属于任何数据权限分组'
if (!groups.value.length) {
return props.editable ? '本组暂无分类,先新建一个' : '本组暂无分类'
}
return '没有匹配的分类'
})
async function loadGroups(): Promise<void> {
if (props.ownerGroupId <= 0) {
groups.value = []
loading.value = false
loadFailure.value = ''
errorText.value = ''
return
}
loading.value = true
loadFailure.value = ''
try {
groups.value = await fetchJdProductGroups(props.ownerGroupId)
} catch (error) {
loadFailure.value = loadFailureMessage(error, '分类列表加载失败')
groups.value = []
} finally {
loading.value = false
}
}
async function submitCreate(): Promise<void> {
const name = createName.value.trim()
if (!name || creating.value || !props.editable) return
creating.value = true
errorText.value = ''
try {
await createJdProductGroup({ name, sortOrder: 0, ownerGroupId: props.ownerGroupId })
createName.value = ''
keyword.value = ''
showAdminFeedback(`已新建分类「${name}」`, 'success')
emit('changed')
await loadGroups()
} 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 })
groups.value = groups.value.map((item) => (item.id === group.id ? { ...item, name } : item))
cancelRename()
showAdminFeedback(`已把分类改名为「${name}」`, 'success')
emit('changed')
} catch (error) {
errorText.value = requestErrorMessage(error) || '改名失败'
} finally {
saving.value = false
}
}
/** 删除分类不删商品:只解挂本组归属行(商品回到未分类),后端 JdProductGroupService.delete。 */
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)
showAdminFeedback(`已删除分类「${group.name}」`, 'success')
emit('changed')
await loadGroups()
} catch (error) {
errorText.value = requestErrorMessage(error) || '删除分类失败'
} finally {
saving.value = false
}
}
watch(
() => props.visible,
(visible) => {
if (visible) {
keyword.value = ''
errorText.value = ''
loadFailure.value = ''
cancelRename()
createName.value = ''
void loadGroups()
}
},
// immediate:对话框可能以 visible=true 直接挂载(测试/复用场景),这时也要首取。
{ immediate: true },
)
</script>
<template>
<div v-if="visible" class="modal-mask" @click.self="emit('close')">
<div class="manage-modal">
<div class="modal-head">
<h3>分类管理<span v-if="ownerGroupName" class="modal-sub">· {{ ownerGroupName }}</span></h3>
<button class="modal-close" type="button" aria-label="关闭" @click="emit('close')">×</button>
</div>
<p class="modal-tip">
分类按「数据权限分组」隔离,当前组{{ editable ? '只组长/超管可维护' : '由组长/超管维护' }};
给商品归类:关掉本框,勾选商品后点「批量编辑分类」。
</p>
<div class="manage-bar">
<input v-model="keyword" class="search-input" type="text" placeholder="搜索分类名称" aria-label="搜索分类名称" />
<div v-if="editable" class="group-create">
<input v-model="createName" type="text" placeholder="输入新分类名称" aria-label="新分类名称" @keyup.enter="submitCreate" />
<button class="btn" type="button" :disabled="!canCreate" @click="submitCreate">新建</button>
</div>
</div>
<LoadFailureBar v-if="loadFailure" :message="loadFailure" @retry="loadGroups" />
<p v-if="errorText" class="group-error">{{ errorText }}</p>
<ul class="option-list">
<template v-if="filteredGroups.length">
<li v-for="group in filteredGroups" :key="group.id" class="option-row">
<template v-if="editingId === group.id">
<input v-model="editingName" class="rename-input" type="text" aria-label="分类新名称" @keyup.enter="submitRename(group)" />
<span class="row-actions">
<button class="btn btn-sm" type="button" :disabled="saving" @click="submitRename(group)">保存</button>
<button class="btn btn-sm btn-secondary" type="button" @click="cancelRename">取消</button>
</span>
</template>
<template v-else>
<span class="option-name" :title="group.name">{{ group.name }}</span>
<span class="option-meta" title="该分类下的商品数">{{ group.productCount }}</span>
<span v-if="editable" class="row-actions">
<button class="btn btn-sm btn-secondary" type="button" :disabled="saving" @click="startRename(group)">改名</button>
<button class="btn btn-sm btn-danger" type="button" :disabled="saving" @click="removeGroup(group)">删除</button>
</span>
</template>
</li>
</template>
<li v-else-if="loading" class="manage-empty">加载中...</li>
<li v-else class="manage-empty">{{ emptyListText }}</li>
</ul>
<div class="modal-foot">
<button class="btn btn-secondary" type="button" @click="emit('close')">关闭</button>
</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);
}
.manage-modal {
display: flex;
flex-direction: column;
width: min(480px, 100%);
max-height: 80vh;
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-sub {
margin-left: 6px;
color: #5b6f83;
font-size: 13px;
font-weight: 500;
}
.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;
}
.manage-bar {
display: flex;
flex-wrap: wrap;
gap: 8px;
align-items: center;
margin-bottom: 12px;
}
.search-input,
.group-create input {
min-height: 36px;
padding: 6px 12px;
border: 1px solid #cbd9e6;
border-radius: 9px;
background: #f8fbfd;
color: #24384d;
font-size: 13px;
font-family: inherit;
outline: none;
transition: border-color 160ms ease, box-shadow 160ms ease, background 160ms ease;
}
.search-input:hover,
.group-create input:hover {
border-color: #9fb7cd;
}
.search-input:focus,
.group-create input:focus {
background: #ffffff;
border-color: #5f85ad;
box-shadow: 0 0 0 3px rgba(95, 133, 173, 0.16);
}
.search-input {
flex: 1;
min-width: 140px;
}
.group-create {
display: flex;
flex: 1;
gap: 8px;
min-width: 220px;
}
.group-create input {
flex: 1;
min-width: 0;
}
.group-error {
margin: 0 0 8px;
padding: 10px 12px;
border: 1px solid #f0c9c2;
border-radius: 10px;
background: #fdf1ef;
color: #a04432;
font-size: 13px;
}
.option-list {
flex: 1;
min-height: 0;
overflow-y: auto;
margin: 0;
padding: 0;
list-style: none;
border-top: 1px solid #e7eef5;
}
.option-row {
display: flex;
align-items: center;
gap: 10px;
padding: 9px 8px;
border-bottom: 1px solid #e7eef5;
font-size: 13.5px;
}
.option-row:hover {
background: #f1f7fb;
}
/* 名称不占剩余空间:它只包住自己的文字,数量徽标紧贴其后。
剩余空间全部让给 row-actions 的 margin-left:auto(有操作时按钮仍靠右)。
写回 flex:1 会让名称把空白吃满、把号码顶到行尾(2026-11-17 用户反馈「0 和名称离的有点远」)。 */
.option-name {
flex: 0 1 auto;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
color: #24384d;
}
.option-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;
}
.row-actions {
flex: none;
margin-left: auto;
white-space: nowrap;
}
.manage-empty {
padding: 30px 12px;
text-align: center;
color: #8293a5;
font-size: 13px;
list-style: none;
}
.rename-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;
outline: none;
}
.rename-input:focus {
background: #ffffff;
border-color: #5f85ad;
}
.modal-foot {
display: flex;
justify-content: flex-end;
gap: 10px;
margin-top: 14px;
}
.btn {
display: inline-flex;
align-items: center;
justify-content: center;
min-height: 36px;
padding: 6px 14px;
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.55;
}
.btn-sm {
min-height: 28px;
padding: 3px 10px;
font-size: 12.5px;
margin-right: 6px;
}
.btn-sm:last-child {
margin-right: 0;
}
.btn-secondary {
background: #ffffff;
color: #5b6f83;
border-color: #c7d7e5;
}
.btn-secondary:hover:not(:disabled) {
color: #2f5d8b;
border-color: #95b1cb;
background: #edf5fb;
}
/* 危险动作与全站 .btn-danger 同形(实心红渐变+白字)。 */
.btn-danger {
background: linear-gradient(135deg, #c06d77, #b35f6a);
border-color: #b35f6a;
color: #ffffff;
}
.btn-danger:hover:not(:disabled) {
background: linear-gradient(135deg, #cb7c84, #b96570);
}
</style>