修复知识库的标签管理和分类管理

This commit is contained in:
2025-11-02 18:34:04 +08:00
parent 494e0160b7
commit 7972f0a6d9
14 changed files with 1675 additions and 331 deletions
+36 -9
View File
@@ -290,12 +290,13 @@ func (c *KnowledgeController) GetTags() {
var result []map[string]interface{}
for _, tag := range tags {
result = append(result, map[string]interface{}{
"tagId": tag.TagId,
"tagName": tag.TagName,
"tagColor": tag.TagColor,
"usageCount": tag.UsageCount,
"createTime": tag.CreateTime,
"updateTime": tag.UpdateTime,
"tagId": tag.TagId,
"tagName": tag.TagName,
"tagColor": tag.TagColor,
"tagBackground": tag.TagBackground,
"usageCount": tag.UsageCount,
"createTime": tag.CreateTime,
"updateTime": tag.UpdateTime,
})
}
@@ -322,7 +323,8 @@ func (c *KnowledgeController) AddCategory() {
return
}
id, err := models.AddCategory(&category)
// 不处理id,直接添加,表自动递增
_, err = models.AddCategory(&category)
if err != nil {
c.Data["json"] = map[string]interface{}{
"code": 1,
@@ -336,12 +338,12 @@ func (c *KnowledgeController) AddCategory() {
c.Data["json"] = map[string]interface{}{
"code": 0,
"message": "添加成功",
"data": map[string]interface{}{"categoryId": id},
"data": nil,
}
c.ServeJSON()
}
// AddTag 添加标签
// AddTag 添加或更新标签
// @router /api/knowledge/tag/add [post]
func (c *KnowledgeController) AddTag() {
var tag models.KnowledgeTag
@@ -356,6 +358,31 @@ func (c *KnowledgeController) AddTag() {
return
}
// 判断是添加还是更新
if tag.TagId > 0 {
// 更新操作
err = models.UpdateTag(&tag)
if err != nil {
c.Data["json"] = map[string]interface{}{
"code": 1,
"message": "更新标签失败: " + err.Error(),
"data": nil,
}
c.ServeJSON()
return
}
c.Data["json"] = map[string]interface{}{
"code": 0,
"message": "更新成功",
"data": map[string]interface{}{"tagId": tag.TagId},
}
c.ServeJSON()
return
}
// 添加操作:确保 TagId 为 0,让数据库自动生成
tag.TagId = 0
id, err := models.AddTag(&tag)
if err != nil {
c.Data["json"] = map[string]interface{}{
+65
View File
@@ -0,0 +1,65 @@
-- 添加知识库管理相关的菜单项
-- 注意:需要先查询知识库菜单的ID(parent_id),假设为 11
-- 如果知识库菜单ID为11,添加分类管理和标签管理菜单
-- 请根据实际数据库中的知识库菜单ID修改下面的 parent_id 值
-- 查询知识库菜单ID(如果需要)
-- SELECT id FROM yz_menus WHERE path = '/apps/knowledge';
-- 添加分类管理菜单(如果不存在)
INSERT INTO yz_menus (
name,
path,
parent_id,
icon,
`order`,
status,
component_path,
menu_type,
description
)
SELECT
'分类管理',
'/apps/knowledge/category',
id,
'fa-solid fa-folder',
2,
1,
'@/views/apps/knowledge/category/index.vue',
1,
'知识库分类管理'
FROM yz_menus
WHERE path = '/apps/knowledge'
AND NOT EXISTS (
SELECT 1 FROM yz_menus WHERE path = '/apps/knowledge/category'
);
-- 添加标签管理菜单(如果不存在)
INSERT INTO yz_menus (
name,
path,
parent_id,
icon,
`order`,
status,
component_path,
menu_type,
description
)
SELECT
'标签管理',
'/apps/knowledge/tag',
id,
'fa-solid fa-tags',
3,
1,
'@/views/apps/knowledge/tag/index.vue',
1,
'知识库标签管理'
FROM yz_menus
WHERE path = '/apps/knowledge'
AND NOT EXISTS (
SELECT 1 FROM yz_menus WHERE path = '/apps/knowledge/tag'
);
+21 -6
View File
@@ -54,12 +54,13 @@ func (kc *KnowledgeCategory) TableName() string {
// KnowledgeTag 知识库标签模型
type KnowledgeTag struct {
TagId int `orm:"column(tag_id);pk;auto" json:"tagId"`
TagName string `orm:"column(tag_name);size(50);unique" json:"tagName"`
TagColor string `orm:"column(tag_color);size(20);null" json:"tagColor"`
UsageCount int `orm:"column(usage_count);default(0)" json:"usageCount"`
CreateTime time.Time `orm:"column(create_time);auto_now_add;type(datetime)" json:"createTime"`
UpdateTime time.Time `orm:"column(update_time);auto_now;type(datetime)" json:"updateTime"`
TagId int `orm:"column(tag_id);pk;auto" json:"tagId"`
TagName string `orm:"column(tag_name);size(50);unique" json:"tagName"`
TagColor string `orm:"column(tag_color);size(20);null" json:"tagColor"`
TagBackground string `orm:"column(tag_background);size(20);null" json:"tagBackground"`
UsageCount int `orm:"column(usage_count);default(0)" json:"usageCount"`
CreateTime time.Time `orm:"column(create_time);auto_now_add;type(datetime)" json:"createTime"`
UpdateTime time.Time `orm:"column(update_time);auto_now;type(datetime)" json:"updateTime"`
}
// TableName 设置表名
@@ -286,9 +287,23 @@ func AddCategory(category *KnowledgeCategory) (int64, error) {
return id, err
}
// UpdateCategory 更新分类
func UpdateCategory(category *KnowledgeCategory) error {
o := orm.NewOrm()
_, err := o.Update(category, "CategoryName", "CategoryDesc", "ParentId", "SortOrder")
return err
}
// AddTag 添加标签
func AddTag(tag *KnowledgeTag) (int64, error) {
o := orm.NewOrm()
id, err := o.Insert(tag)
return id, err
}
// UpdateTag 更新标签
func UpdateTag(tag *KnowledgeTag) error {
o := orm.NewOrm()
_, err := o.Update(tag, "TagName", "TagColor", "TagBackground")
return err
}