kongdeqiang
2026-10-12 285bf234bd50ce54fcae7bb777f8fa579b91266e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<template>
  <div class="category-picker">
    <n-select
      v-model:value="selected"
      size="small"
      clearable
      :options="options"
      :placeholder="placeholder"
      :disabled="disabled"
      @update:value="handleChange"
    />
    <!-- 允许现场新建:上传前才发现没有合适分类是很常见的情况,
         强迫用户先去别处建好再回来会打断操作流 -->
    <n-button size="small" quaternary :disabled="disabled" @click="showCreate = true">
      + 新建分类
    </n-button>
 
    <n-modal
      v-model:show="showCreate"
      preset="card"
      title="新建分类"
      class="create-modal"
      :bordered="false"
    >
      <n-form label-placement="top">
        <n-form-item label="分类名称">
          <n-input
            v-model:value="newName"
            placeholder="例如:产品文档、售后政策、技术规范"
            maxlength="64"
            show-count
            @keyup.enter="handleCreate"
          />
        </n-form-item>
        <n-form-item label="说明(可选)">
          <n-input
            v-model:value="newDescription"
            placeholder="便于他人理解这个分类装什么"
            maxlength="255"
          />
        </n-form-item>
        <n-form-item v-if="allowGlobal" label="可见范围">
          <n-radio-group v-model:value="newGlobal">
            <n-radio :value="false">仅自己</n-radio>
            <n-radio :value="true">所有用户(全局分类)</n-radio>
          </n-radio-group>
        </n-form-item>
      </n-form>
      <template #footer>
        <div class="modal-footer">
          <n-button size="small" @click="showCreate = false">取消</n-button>
          <n-button size="small" type="primary" :loading="creating" @click="handleCreate">
            创建
          </n-button>
        </div>
      </template>
    </n-modal>
  </div>
</template>
 
<script setup>
import { computed, onMounted, ref } from 'vue'
import {
  NButton,
  NForm,
  NFormItem,
  NInput,
  NModal,
  NRadio,
  NRadioGroup,
  NSelect,
  useMessage
} from 'naive-ui'
import { createCategory, listCategories } from '@/api/categoryApi'
 
const props = defineProps({
  /** 当前选中的分类 id(支持 v-model:value) */
  value: { type: Number, default: null },
  /** 是否允许创建全局分类(仅管理员页面传 true) */
  allowGlobal: { type: Boolean, default: false },
  disabled: { type: Boolean, default: false },
  placeholder: { type: String, default: '选择分类(可不选)' }
})
 
const emit = defineEmits(['update:value', 'created'])
 
const message = useMessage()
const categories = ref([])
const selected = ref(props.value)
const showCreate = ref(false)
const creating = ref(false)
const newName = ref('')
const newDescription = ref('')
const newGlobal = ref(false)
 
const options = computed(() =>
  categories.value.map((c) => ({
    label: c.isGlobal ? `${c.name}(全局)` : c.name,
    value: c.id
  }))
)
 
// 父组件异步设置初值时同步过来
import { watch } from 'vue'
watch(
  () => props.value,
  (v) => {
    selected.value = v
  }
)
 
onMounted(load)
 
async function load() {
  try {
    categories.value = (await listCategories()) || []
  } catch (error) {
    message.error(error.message || '加载分类失败')
  }
}
 
function handleChange(value) {
  emit('update:value', value ?? null)
}
 
async function handleCreate() {
  const name = newName.value.trim()
  if (!name) {
    message.warning('请输入分类名称')
    return
  }
  creating.value = true
  try {
    const created = await createCategory(name, newDescription.value || null, newGlobal.value)
    categories.value = [...categories.value, created]
    // 新建后直接选中,省掉一次手动点击
    selected.value = created.id
    emit('update:value', created.id)
    emit('created', created)
    message.success(`分类「${created.name}」已创建`)
 
    showCreate.value = false
    newName.value = ''
    newDescription.value = ''
    newGlobal.value = false
  } catch (error) {
    message.error(error.message || '创建失败')
  } finally {
    creating.value = false
  }
}
</script>
 
<style scoped>
.category-picker {
  display: flex;
  align-items: center;
  gap: 6px;
}
 
.category-picker :deep(.n-select) {
  flex: 1;
  min-width: 0;
}
 
.create-modal {
  width: 440px;
}
 
.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}
</style>