添加获取所有分类以及分类按层级显示的逻辑

This commit is contained in:
Siyi Guo committed 2023-02-03 13:35:53 +08:00
1 parent fc259ba274
commit e42a4549e9
4 files changed
+60 -6

No files matched your search

+36
View File
@@ -2,6 +2,7 @@ package apis
import (
"fmt"
"strconv"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
@@ -55,6 +56,41 @@ func (e Category) GetPage(c *gin.Context) {
e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}
func (e Category) GetPageAll(c *gin.Context) {
s := service.Category{}
err := e.MakeContext(c).
MakeOrm().
MakeService(&s.Service).
Errors
if err != nil {
e.Logger.Error(err)
e.Error(500, err, err.Error())
return
}
list := make([]models.Category, 0)
err = s.GetPageAll(&list)
if err != nil {
e.Error(500, err, fmt.Sprintf("获取所有分类失败,\r\n失败信息 %s", err.Error()))
return
}
treeList := getTree(list, "0")
e.OK(treeList, "成功")
}
func getTree(list []models.Category, parent string) []models.Category {
res := make([]models.Category, 0)
for _, v := range list {
if v.Parent == parent {
v.Children = getTree(list, strconv.FormatInt(int64(v.Id), 10))
res = append(res, v)
}
}
return res
}
// Get 获取分类
// @Summary 获取分类
// @Description 获取分类
+7 -6
View File
@@ -7,12 +7,13 @@ import (
type Category struct {
models.Model
Title string `json:"title" gorm:"type:varchar(127);comment:标题"`
Slug string `json:"slug" gorm:"type:varchar(127);comment:id"`
Desc string `json:"desc" gorm:"type:varchar(127);comment:描述"`
Parent string `json:"parent" gorm:"type:bigint;comment:父"`
Order string `json:"order" gorm:"type:bigint;comment:排序"`
Status string `json:"status" gorm:"type:int;comment:状态"`
Title string `json:"title" gorm:"type:varchar(127);comment:标题"`
Slug string `json:"slug" gorm:"type:varchar(127);comment:id"`
Desc string `json:"desc" gorm:"type:varchar(127);comment:描述"`
Parent string `json:"-" gorm:"type:bigint;comment:父"`
Order string `json:"order" gorm:"type:bigint;comment:排序"`
Status string `json:"status" gorm:"type:int;comment:状态"`
Children []Category `json:"children" gorm:"-"`
models.ModelTime
models.ControlBy
}
+4
View File
@@ -24,4 +24,8 @@ func registerCategoryRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddl
r.PUT("/:id", actions.PermissionAction(), api.Update)
r.DELETE("", api.Delete)
}
f := v1.Group("/fe/category")
{
f.GET("", api.GetPageAll)
}
}
+13
View File
@@ -36,6 +36,19 @@ func (e *Category) GetPage(c *dto.CategoryGetPageReq, p *actions.DataPermission,
return nil
}
// GetPageAll 获取Category所有
func (e *Category) GetPageAll(list *[]models.Category) error {
var err error
var data models.Category
err = e.Orm.Model(&data).Find(list).Limit(-1).Offset(-1).Error
if err != nil {
e.Log.Errorf("CategoryService GetPageAll error:%s \r\n", err)
return err
}
return nil
}
// Get 获取Category对象
func (e *Category) Get(d *dto.CategoryGetReq, p *actions.DataPermission, model *models.Category) error {
var data models.Category