chart_permission.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package controllers
  2. import (
  3. "eta/eta_mini_crm_ht/models"
  4. "eta/eta_mini_crm_ht/models/response"
  5. )
  6. type ChartPermissionController struct {
  7. BaseAuthController
  8. }
  9. // List
  10. // @Title 系统品种列表
  11. // @Description 系统品种列表
  12. // @Param UserId query int true "角色ID"
  13. // @Success 200 {object} models.LoginResp
  14. // @router /list [get]
  15. func (this *ChartPermissionController) List() {
  16. br := new(models.BaseResponse).Init()
  17. defer func() {
  18. this.Data["json"] = br
  19. this.ServeJSON()
  20. }()
  21. userId, _ := this.GetInt("UserId")
  22. items, err := models.GetChartPermissionList()
  23. if err != nil {
  24. br.Msg = "权限列表获取失败"
  25. br.ErrMsg = "权限列表获取失败,系统错误,Err:" + err.Error()
  26. return
  27. }
  28. root := &PermissionNode{
  29. ID: 0,
  30. ParentID: 0,
  31. }
  32. assemblePermissionNode(items, root, 0, 2)
  33. resp := new(response.ChartPermissionListresp)
  34. if userId > 0 {
  35. ids, err := models.GetChartPermissionIdByUserId(userId)
  36. if err != nil {
  37. br.Msg = "权限列表获取失败"
  38. br.ErrMsg = "权限列表获取失败,系统错误,Err:" + err.Error()
  39. }
  40. resp.SelectedList = ids
  41. }
  42. resp.List = root.Children
  43. br.Ret = 200
  44. br.Data = resp
  45. br.Msg = "列表获取成功"
  46. br.Success = true
  47. }
  48. type PermissionNode struct {
  49. ID int `json:"id"`
  50. Name string `json:"name"`
  51. ParentID int `json:"parentId"`
  52. Children []*PermissionNode `json:"children,omitempty"`
  53. }
  54. func assemblePermissionNode(list []*models.ChartPermission, node *PermissionNode, current int, level int) {
  55. if node != nil && current < level {
  56. for _, permission := range list {
  57. if permission.ParentID == node.ID {
  58. childNode := &PermissionNode{
  59. ID: permission.ChartPermissionID,
  60. Name: permission.PermissionName,
  61. ParentID: permission.ParentID,
  62. }
  63. node.Children = append(node.Children, childNode)
  64. assemblePermissionNode(list, childNode, current+1, level)
  65. }
  66. }
  67. }
  68. }