diff --git a/app/jbxh/apis/category.go b/app/jbxh/apis/category.go index eb2dec8..f799059 100644 --- a/app/jbxh/apis/category.go +++ b/app/jbxh/apis/category.go @@ -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 获取分类 diff --git a/app/jbxh/models/category.go b/app/jbxh/models/category.go index bf8b3bb..1a3b07b 100644 --- a/app/jbxh/models/category.go +++ b/app/jbxh/models/category.go @@ -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 } diff --git a/app/jbxh/router/category.go b/app/jbxh/router/category.go index c85b66a..d3ef6d6 100644 --- a/app/jbxh/router/category.go +++ b/app/jbxh/router/category.go @@ -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) + } } diff --git a/app/jbxh/service/category.go b/app/jbxh/service/category.go index fb280c0..1114dd2 100644 --- a/app/jbxh/service/category.go +++ b/app/jbxh/service/category.go @@ -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