SkillManagement.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. <script setup>
  2. import { ref, onMounted, onUnmounted } from 'vue'
  3. import { useRouter } from 'vue-router'
  4. import { NButton, NIcon, NSpin, NEmpty, useDialog, useMessage } from 'naive-ui'
  5. import { AddOutline, ReloadOutline, SearchOutline } from '@vicons/ionicons5'
  6. import { useSkillStore } from '../stores/skill'
  7. import { getCategoryTree, createCategory, updateCategory, deleteCategory as deleteCategoryApi } from '../api/category'
  8. import { setSkillCategory } from '../api/skill'
  9. import SkillCard from '../components/skill/SkillCard.vue'
  10. import SkillUpload from '../components/skill/SkillUpload.vue'
  11. import TagSelector from '../components/tag/TagSelector.vue'
  12. import CategoryTree from '../components/skill/CategoryTree.vue'
  13. const router = useRouter()
  14. const skillStore = useSkillStore()
  15. const dialog = useDialog()
  16. const message = useMessage()
  17. const showUpload = ref(false)
  18. const showTagSelector = ref(false)
  19. const taggingSkill = ref(null)
  20. // 分类树相关
  21. const categories = ref([])
  22. const expandedIds = ref(new Set())
  23. // 分类树内联编辑状态
  24. const editingId = ref(null)
  25. const editingName = ref('')
  26. const addingParentId = ref(undefined) // undefined = 未激活,null = 添加根分类,数字 = 添加子分类
  27. const addingName = ref('')
  28. onMounted(() => {
  29. skillStore.init()
  30. loadCategories()
  31. })
  32. onUnmounted(() => {
  33. skillStore.destroy()
  34. })
  35. // ==================== 分类树操作 ====================
  36. async function loadCategories() {
  37. try {
  38. const res = await getCategoryTree()
  39. categories.value = res.data || []
  40. } catch (err) {
  41. console.error('加载分类失败', err)
  42. }
  43. }
  44. function toggleExpand(id) {
  45. if (expandedIds.value.has(id)) {
  46. expandedIds.value.delete(id)
  47. } else {
  48. expandedIds.value.add(id)
  49. }
  50. }
  51. function selectCategory(categoryId) {
  52. if (skillStore.selectedCategoryId === categoryId) {
  53. skillStore.setCategory(null)
  54. } else {
  55. skillStore.setCategory(categoryId)
  56. }
  57. }
  58. function startAdd(parentId) {
  59. addingParentId.value = parentId
  60. addingName.value = ''
  61. }
  62. function cancelAdd() {
  63. addingParentId.value = undefined
  64. addingName.value = ''
  65. }
  66. async function confirmAdd() {
  67. if (!addingName.value.trim()) return
  68. try {
  69. await createCategory({ parentId: addingParentId.value, name: addingName.value.trim() })
  70. await loadCategories()
  71. if (addingParentId.value != null) {
  72. expandedIds.value.add(addingParentId.value)
  73. }
  74. cancelAdd()
  75. } catch (err) {
  76. message.error(err.message || '创建失败')
  77. }
  78. }
  79. function startEdit({ id, name }) {
  80. editingId.value = id
  81. editingName.value = name
  82. }
  83. function cancelEdit() {
  84. editingId.value = null
  85. editingName.value = ''
  86. }
  87. async function confirmEdit() {
  88. if (!editingName.value.trim()) return
  89. try {
  90. await updateCategory(editingId.value, { name: editingName.value.trim() })
  91. await loadCategories()
  92. cancelEdit()
  93. } catch (err) {
  94. message.error(err.message || '更新失败')
  95. }
  96. }
  97. function handleDeleteCategory({ id, name }) {
  98. dialog.warning({
  99. title: '确认删除',
  100. content: `确定要删除分类「${name}」吗?该分类下的技能将变为未分类。`,
  101. positiveText: '确认删除',
  102. negativeText: '取消',
  103. onPositiveClick: async () => {
  104. try {
  105. await deleteCategoryApi(id)
  106. await loadCategories()
  107. if (skillStore.selectedCategoryId === id) {
  108. skillStore.setCategory(null)
  109. }
  110. message.success('删除成功')
  111. } catch (err) {
  112. message.error(err.message || '删除失败')
  113. }
  114. }
  115. })
  116. }
  117. // ==================== 技能操作 ====================
  118. function handleRefresh() {
  119. skillStore.fetchSkills()
  120. loadCategories()
  121. }
  122. async function handleUpload(file) {
  123. try {
  124. await skillStore.createSkill(file)
  125. showUpload.value = false
  126. loadCategories()
  127. message.success('技能上传成功')
  128. } catch (err) {
  129. message.error(err.message || '上传失败')
  130. }
  131. }
  132. function handleEdit(skill) {
  133. router.push(`/skill/edit/${skill.folderName}`)
  134. }
  135. function handleDelete(skill) {
  136. dialog.warning({
  137. title: '确认删除',
  138. content: `确定要删除技能「${skill.translatedName || skill.name}」吗?此操作不可恢复。`,
  139. positiveText: '确认删除',
  140. negativeText: '取消',
  141. onPositiveClick: async () => {
  142. try {
  143. await skillStore.removeSkill(skill.folderName)
  144. loadCategories()
  145. message.success('删除成功')
  146. } catch (err) {
  147. message.error(err.message || '删除失败')
  148. }
  149. }
  150. })
  151. }
  152. function handleTag(skill) {
  153. taggingSkill.value = skill
  154. showTagSelector.value = true
  155. }
  156. async function onTagsSaved() {
  157. await skillStore.fetchSkills()
  158. message.success('标签已更新')
  159. }
  160. // 搜索防抖
  161. let searchTimer = null
  162. function onSearchInput(value) {
  163. clearTimeout(searchTimer)
  164. searchTimer = setTimeout(() => {
  165. skillStore.setSearch(value)
  166. }, 300)
  167. }
  168. </script>
  169. <template>
  170. <div class="skill-management">
  171. <div class="main-layout">
  172. <!-- 左侧分类树 -->
  173. <div class="sidebar">
  174. <div class="sidebar-header">
  175. <span class="sidebar-title">分类</span>
  176. <button class="icon-btn" @click="startAdd(null)" title="新建根分类">
  177. <n-icon size="16"><AddOutline /></n-icon>
  178. </button>
  179. </div>
  180. <div class="category-list">
  181. <!-- 全部 -->
  182. <div
  183. class="all-item"
  184. :class="{ active: !skillStore.selectedCategoryId }"
  185. @click="selectCategory(null)"
  186. >
  187. <span class="cat-name">全部技能</span>
  188. <span class="cat-count">{{ skillStore.total }}</span>
  189. </div>
  190. <!-- 递归分类树 -->
  191. <CategoryTree
  192. :nodes="categories"
  193. :level="0"
  194. :selected-id="skillStore.selectedCategoryId"
  195. :expanded-ids="expandedIds"
  196. :editing-id="editingId"
  197. v-model:editing-name="editingName"
  198. :adding-parent-id="addingParentId"
  199. v-model:adding-name="addingName"
  200. @select="selectCategory"
  201. @toggle="toggleExpand"
  202. @add="startAdd"
  203. @delete="handleDeleteCategory"
  204. @edit-start="startEdit"
  205. @edit-confirm="confirmEdit"
  206. @edit-cancel="cancelEdit"
  207. @add-confirm="confirmAdd"
  208. @add-cancel="cancelAdd"
  209. />
  210. <!-- 根分类添加输入框 -->
  211. <div v-if="addingParentId === null" class="root-adding">
  212. <input
  213. class="inline-input"
  214. v-model="addingName"
  215. placeholder="输入分类名称"
  216. @keyup.enter="confirmAdd"
  217. @keyup.escape="cancelAdd"
  218. @blur="cancelAdd"
  219. autofocus
  220. />
  221. </div>
  222. </div>
  223. </div>
  224. <!-- 右侧内容 -->
  225. <div class="content">
  226. <!-- 工具栏 -->
  227. <div class="toolbar">
  228. <div class="toolbar-left">
  229. <div class="search-box">
  230. <n-icon size="16" class="search-icon"><SearchOutline /></n-icon>
  231. <input
  232. class="search-input"
  233. :value="skillStore.searchKeyword"
  234. @input="onSearchInput($event.target.value)"
  235. placeholder="搜索技能名称或描述..."
  236. />
  237. </div>
  238. <span class="skill-count">
  239. 共 <span class="count-num">{{ skillStore.total }}</span> 个技能
  240. </span>
  241. </div>
  242. <div class="toolbar-right">
  243. <n-button quaternary @click="handleRefresh" :loading="skillStore.loading">
  244. <template #icon>
  245. <n-icon><ReloadOutline /></n-icon>
  246. </template>
  247. 刷新
  248. </n-button>
  249. <n-button type="primary" @click="showUpload = true">
  250. <template #icon>
  251. <n-icon><AddOutline /></n-icon>
  252. </template>
  253. 新增技能
  254. </n-button>
  255. </div>
  256. </div>
  257. <!-- 加载状态 -->
  258. <div v-if="skillStore.loading && skillStore.skills.length === 0" class="loading-area">
  259. <n-spin size="large" />
  260. <p class="loading-text">正在加载技能列表...</p>
  261. </div>
  262. <!-- 空状态 -->
  263. <div v-else-if="skillStore.skills.length === 0" class="empty-area">
  264. <div class="empty-illustration">
  265. <svg width="120" height="120" viewBox="0 0 120 120" fill="none" xmlns="http://www.w3.org/2000/svg">
  266. <rect x="20" y="30" width="80" height="60" rx="8" stroke="rgba(56, 189, 248, 0.15)" stroke-width="2" fill="none"/>
  267. <path d="M45 55H75" stroke="rgba(56, 189, 248, 0.2)" stroke-width="2" stroke-linecap="round"/>
  268. <path d="M45 65H65" stroke="rgba(56, 189, 248, 0.15)" stroke-width="2" stroke-linecap="round"/>
  269. <circle cx="60" cy="60" r="30" stroke="rgba(37, 99, 235, 0.1)" stroke-width="1" fill="none"/>
  270. </svg>
  271. </div>
  272. <p class="empty-title">暂无技能</p>
  273. <p class="empty-desc">点击「新增技能」上传您的第一个 Skill</p>
  274. <n-button type="primary" @click="showUpload = true" style="margin-top: 16px">
  275. <template #icon>
  276. <n-icon><AddOutline /></n-icon>
  277. </template>
  278. 新增技能
  279. </n-button>
  280. </div>
  281. <!-- 卡片网格 -->
  282. <div v-else class="skill-grid">
  283. <SkillCard
  284. v-for="(skill, index) in skillStore.skills"
  285. :key="skill.folderName"
  286. :skill="skill"
  287. :style="{ animationDelay: `${index * 60}ms` }"
  288. class="fade-in"
  289. @edit="handleEdit"
  290. @delete="handleDelete"
  291. @tag="handleTag"
  292. />
  293. </div>
  294. <!-- 分页 -->
  295. <div v-if="skillStore.totalPages > 1" class="pagination">
  296. <button
  297. class="page-btn"
  298. :disabled="skillStore.currentPage <= 1"
  299. @click="skillStore.setPage(skillStore.currentPage - 1)"
  300. >上一页</button>
  301. <template v-for="p in skillStore.totalPages" :key="p">
  302. <button
  303. v-if="p === 1 || p === skillStore.totalPages || Math.abs(p - skillStore.currentPage) <= 1"
  304. class="page-btn"
  305. :class="{ active: p === skillStore.currentPage }"
  306. @click="skillStore.setPage(p)"
  307. >{{ p }}</button>
  308. <span v-else-if="Math.abs(p - skillStore.currentPage) === 2" class="page-ellipsis">...</span>
  309. </template>
  310. <button
  311. class="page-btn"
  312. :disabled="skillStore.currentPage >= skillStore.totalPages"
  313. @click="skillStore.setPage(skillStore.currentPage + 1)"
  314. >下一页</button>
  315. </div>
  316. </div>
  317. </div>
  318. <!-- 上传弹窗 -->
  319. <SkillUpload
  320. v-model:show="showUpload"
  321. :uploading="skillStore.uploading"
  322. :progress="skillStore.uploadProgress"
  323. @upload="handleUpload"
  324. />
  325. <!-- 标签选择器 -->
  326. <TagSelector
  327. v-model:show="showTagSelector"
  328. entity-type="skill"
  329. :entity-id="taggingSkill?.folderName || ''"
  330. :selected-tag-ids="taggingSkill?.tags?.map(t => t.tagId) || []"
  331. @saved="onTagsSaved"
  332. />
  333. </div>
  334. </template>
  335. <style scoped>
  336. .skill-management {
  337. min-height: 100%;
  338. }
  339. .main-layout {
  340. display: flex;
  341. gap: 24px;
  342. height: 100%;
  343. }
  344. /* ==================== 左侧分类树 ==================== */
  345. .sidebar {
  346. width: 240px;
  347. min-width: 240px;
  348. background: var(--bg-card);
  349. border: 1px solid var(--border-color);
  350. border-radius: var(--radius-lg);
  351. padding: 16px 0;
  352. display: flex;
  353. flex-direction: column;
  354. overflow: hidden;
  355. }
  356. .sidebar-header {
  357. display: flex;
  358. align-items: center;
  359. justify-content: space-between;
  360. padding: 0 16px 12px;
  361. border-bottom: 1px solid rgba(56, 189, 248, 0.06);
  362. }
  363. .sidebar-title {
  364. font-size: 14px;
  365. font-weight: 600;
  366. color: var(--text-primary);
  367. }
  368. .category-list {
  369. flex: 1;
  370. overflow-y: auto;
  371. padding: 8px 0;
  372. }
  373. .all-item {
  374. display: flex;
  375. align-items: center;
  376. justify-content: space-between;
  377. padding: 6px 16px;
  378. cursor: pointer;
  379. transition: all 0.15s;
  380. min-height: 32px;
  381. }
  382. .all-item:hover {
  383. background: rgba(37, 99, 235, 0.06);
  384. }
  385. .all-item.active {
  386. background: rgba(37, 99, 235, 0.12);
  387. }
  388. .all-item.active .cat-name {
  389. color: var(--color-accent);
  390. font-weight: 500;
  391. }
  392. .cat-name {
  393. font-size: 13px;
  394. color: var(--text-secondary);
  395. white-space: nowrap;
  396. overflow: hidden;
  397. text-overflow: ellipsis;
  398. }
  399. .cat-count {
  400. font-size: 11px;
  401. color: var(--text-muted);
  402. background: rgba(100, 116, 139, 0.1);
  403. padding: 1px 6px;
  404. border-radius: 8px;
  405. min-width: 20px;
  406. text-align: center;
  407. }
  408. .icon-btn {
  409. background: none;
  410. border: none;
  411. color: var(--text-tertiary);
  412. cursor: pointer;
  413. padding: 4px;
  414. border-radius: 4px;
  415. display: flex;
  416. align-items: center;
  417. transition: all 0.15s;
  418. }
  419. .icon-btn:hover {
  420. color: var(--color-accent);
  421. background: rgba(37, 99, 235, 0.1);
  422. }
  423. .root-adding {
  424. padding: 6px 16px;
  425. }
  426. .inline-input {
  427. background: rgba(37, 99, 235, 0.08);
  428. border: 1px solid rgba(37, 99, 235, 0.2);
  429. border-radius: 4px;
  430. padding: 2px 8px;
  431. font-size: 13px;
  432. color: var(--text-primary);
  433. outline: none;
  434. width: 100%;
  435. font-family: inherit;
  436. }
  437. .inline-input:focus {
  438. border-color: var(--color-accent);
  439. box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.15);
  440. }
  441. /* ==================== 右侧内容 ==================== */
  442. .content {
  443. flex: 1;
  444. min-width: 0;
  445. }
  446. .toolbar {
  447. display: flex;
  448. align-items: center;
  449. justify-content: space-between;
  450. margin-bottom: 20px;
  451. }
  452. .toolbar-left {
  453. display: flex;
  454. align-items: center;
  455. gap: 16px;
  456. }
  457. .search-box {
  458. position: relative;
  459. width: 280px;
  460. }
  461. .search-icon {
  462. position: absolute;
  463. left: 10px;
  464. top: 50%;
  465. transform: translateY(-50%);
  466. color: var(--text-muted);
  467. }
  468. .search-input {
  469. width: 100%;
  470. background: var(--bg-card);
  471. border: 1px solid var(--border-color);
  472. border-radius: var(--radius-sm);
  473. padding: 8px 12px 8px 32px;
  474. font-size: 13px;
  475. color: var(--text-primary);
  476. outline: none;
  477. transition: border-color 0.2s;
  478. font-family: inherit;
  479. }
  480. .search-input:focus {
  481. border-color: var(--color-accent);
  482. box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.15);
  483. }
  484. .search-input::placeholder {
  485. color: var(--text-muted);
  486. }
  487. .skill-count {
  488. font-size: 14px;
  489. color: var(--text-secondary);
  490. }
  491. .count-num {
  492. color: var(--color-accent);
  493. font-weight: 600;
  494. }
  495. .toolbar-right {
  496. display: flex;
  497. gap: 8px;
  498. }
  499. /* 加载状态 */
  500. .loading-area {
  501. display: flex;
  502. flex-direction: column;
  503. align-items: center;
  504. justify-content: center;
  505. padding: 80px 0;
  506. gap: 16px;
  507. }
  508. .loading-text {
  509. font-size: 14px;
  510. color: var(--text-tertiary);
  511. }
  512. /* 空状态 */
  513. .empty-area {
  514. display: flex;
  515. flex-direction: column;
  516. align-items: center;
  517. justify-content: center;
  518. padding: 80px 0;
  519. }
  520. .empty-illustration {
  521. margin-bottom: 20px;
  522. opacity: 0.6;
  523. }
  524. .empty-title {
  525. font-size: 16px;
  526. font-weight: 500;
  527. color: var(--text-secondary);
  528. margin-bottom: 8px;
  529. }
  530. .empty-desc {
  531. font-size: 13px;
  532. color: var(--text-muted);
  533. }
  534. /* 卡片网格 */
  535. .skill-grid {
  536. display: grid;
  537. grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  538. gap: 20px;
  539. }
  540. /* 分页 */
  541. .pagination {
  542. display: flex;
  543. align-items: center;
  544. justify-content: center;
  545. gap: 6px;
  546. margin-top: 32px;
  547. padding-top: 20px;
  548. border-top: 1px solid rgba(56, 189, 248, 0.06);
  549. }
  550. .page-btn {
  551. background: var(--bg-card);
  552. border: 1px solid var(--border-color);
  553. border-radius: var(--radius-sm);
  554. padding: 6px 14px;
  555. font-size: 13px;
  556. color: var(--text-secondary);
  557. cursor: pointer;
  558. transition: all 0.15s;
  559. font-family: inherit;
  560. }
  561. .page-btn:hover:not(:disabled) {
  562. border-color: var(--color-accent);
  563. color: var(--color-accent);
  564. }
  565. .page-btn.active {
  566. background: var(--color-accent);
  567. border-color: var(--color-accent);
  568. color: #fff;
  569. }
  570. .page-btn:disabled {
  571. opacity: 0.4;
  572. cursor: not-allowed;
  573. }
  574. .page-ellipsis {
  575. color: var(--text-muted);
  576. padding: 0 4px;
  577. }
  578. </style>