From e42a4549e9dab07ca577facd7af64b57b5b167bd Mon Sep 17 00:00:00 2001 From: Siyi Guo Date: Fri, 3 Feb 2023 13:35:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=8E=B7=E5=8F=96=E6=89=80?= =?UTF-8?q?=E6=9C=89=E5=88=86=E7=B1=BB=E4=BB=A5=E5=8F=8A=E5=88=86=E7=B1=BB?= =?UTF-8?q?=E6=8C=89=E5=B1=82=E7=BA=A7=E6=98=BE=E7=A4=BA=E7=9A=84=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/jbxh/apis/category.go | 36 ++++++++++++++++++++++++++++++++++++ app/jbxh/models/category.go | 13 +++++++------ app/jbxh/router/category.go | 4 ++++ app/jbxh/service/category.go | 13 +++++++++++++ 4 files changed, 60 insertions(+), 6 deletions(-) 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