添加获取所有分类以及分类按层级显示的逻辑
This commit is contained in:
1 parent
fc259ba274
commit
e42a4549e9
4 files changed
+60
-6
No files matched your search
@@ -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 获取分类
|
||||
|
||||
Reference in new issue
Block a user