classify.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package controllers
  2. import "eta/eta_mini_crm_ht/models"
  3. type ClassifyController struct {
  4. BaseAuthController
  5. }
  6. // List
  7. // @Title 系统类别列表
  8. // @Description 系统类别列表
  9. // @Success 200 {object} models.LoginResp
  10. // @router /list [get]
  11. func (this *ClassifyController) List() {
  12. br := new(models.BaseResponse).Init()
  13. defer func() {
  14. this.Data["json"] = br
  15. this.ServeJSON()
  16. }()
  17. classifyList, err := models.GetClassifyList()
  18. if err != nil {
  19. br.Msg = "分类列表获取失败"
  20. br.ErrMsg = "分类列表获取失败,系统错误,Err:" + err.Error()
  21. return
  22. }
  23. classifyList = getClassifyTree(classifyList, 0)
  24. br.Data = classifyList
  25. br.Ret = 200
  26. br.Msg = "列表获取成功"
  27. br.Success = true
  28. }
  29. // getClassifyTree 递归获取分类树
  30. func getClassifyTree(classifyList []*models.ClassifyView, parentId int) []*models.ClassifyView {
  31. res := make([]*models.ClassifyView, 0)
  32. for _, v := range classifyList {
  33. if v.ParentId == parentId {
  34. t := new(models.ClassifyView)
  35. t.Id = v.Id
  36. t.ClassifyName = v.ClassifyName
  37. t.ParentId = v.ParentId
  38. t.CreateTime = v.CreateTime
  39. t.ModifyTime = v.ModifyTime
  40. t.Enabled = v.Enabled
  41. t.ClassifyLabel = v.ClassifyLabel
  42. t.Child = getClassifyTree(classifyList, v.Id)
  43. res = append(res, t)
  44. }
  45. }
  46. return res
  47. }