feat(shop): add product taxonomy write REST for editor

Expose POST /shop/products/taxonomy to create category/tag terms,
enrich GET term payload with slug/parent/count, and let
ProductEditorDrawer create categories and ensure tags without WP
native UI.
This commit is contained in:
2026-07-11 18:22:10 +08:00
parent e0b60fb3ea
commit 3d021d08f6
3 changed files with 198 additions and 33 deletions
@@ -45,14 +45,26 @@
<n-divider title-placement="left">分类与标签</n-divider>
<n-form-item label="分类">
<n-select
v-model:value="form.categories"
:options="categoryOptions"
multiple
filterable
placeholder="选择分类(可多选)"
:loading="loadingTaxonomy"
/>
<n-space vertical style="width:100%;">
<n-select
v-model:value="form.categories"
:options="categoryOptions"
multiple
filterable
placeholder="选择分类(可多选)"
:loading="loadingTaxonomy"
style="width:100%;"
/>
<n-space>
<n-input
v-model:value="newCategoryName"
placeholder="新建分类名称"
style="min-width:180px;"
@keyup.enter="createCategory"
/>
<n-button :loading="creatingCategory" @click="createCategory">新建分类</n-button>
</n-space>
</n-space>
</n-form-item>
<n-form-item label="标签">
@@ -62,8 +74,9 @@
multiple
filterable
tag
placeholder="输入标签后回车"
placeholder="选择或输入新标签后回车"
:loading="loadingTaxonomy"
@update:value="onTagsUpdate"
/>
</n-form-item>
@@ -164,6 +177,9 @@ const shippingModeOptions = [
const categoryOptions = ref([])
const tagOptions = ref([])
const newCategoryName = ref('')
const creatingCategory = ref(false)
const creatingTagNames = new Set()
// Load taxonomy options
async function loadTaxonomy() {
@@ -175,12 +191,75 @@ async function loadTaxonomy() {
tagOptions.value = (res.data.tags || []).map(t => ({ label: t.name, value: t.name }))
}
} catch {
// Taxonomy endpoint may not exist yet — silent fallback
message.warning('分类标签加载失败,可稍后重试')
} finally {
loadingTaxonomy.value = false
}
}
async function createCategory() {
const name = (newCategoryName.value || '').trim()
if (!name) {
message.warning('请输入分类名称')
return
}
creatingCategory.value = true
try {
const res = await api.request('/shop/products/taxonomy', {
method: 'POST',
data: { taxonomy: 'category', name },
})
const term = res?.data
if (term?.id) {
if (!categoryOptions.value.some(o => o.value === term.id)) {
categoryOptions.value = [...categoryOptions.value, { label: term.name, value: term.id }]
}
if (!form.categories.includes(term.id)) {
form.categories = [...form.categories, term.id]
}
newCategoryName.value = ''
message.success(term.existing ? '分类已存在,已选中' : '分类已创建')
}
} catch (err) {
message.error(err?.message || '创建分类失败')
} finally {
creatingCategory.value = false
}
}
async function ensureTagTerm(name) {
const label = String(name || '').trim()
if (!label || creatingTagNames.has(label)) return
const exists = tagOptions.value.some(o => o.value === label || o.label === label)
if (exists) return
creatingTagNames.add(label)
try {
const res = await api.request('/shop/products/taxonomy', {
method: 'POST',
data: { taxonomy: 'tag', name: label },
})
const term = res?.data
if (term?.name && !tagOptions.value.some(o => o.value === term.name)) {
tagOptions.value = [...tagOptions.value, { label: term.name, value: term.name }]
}
} catch {
// Keep typed tag in form; backend product save still accepts string tags.
} finally {
creatingTagNames.delete(label)
}
}
function onTagsUpdate(values) {
const list = Array.isArray(values) ? values : []
form.tags = list
list.forEach((name) => {
if (!tagOptions.value.some(o => o.value === name)) {
ensureTagTerm(name)
}
})
}
// Load product data when editing
watch(() => props.show, async (val) => {
if (val) {