chart_permission.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_crm_ht/models"
  5. "eta/eta_mini_crm_ht/models/request"
  6. "eta/eta_mini_crm_ht/models/response"
  7. "regexp"
  8. )
  9. type ChartPermissionController struct {
  10. BaseAuthController
  11. }
  12. // List
  13. // @Title 系统品种列表
  14. // @Description 系统品种列表
  15. // @Param UserId query int true "角色ID"
  16. // @Success 200 {object} models.LoginResp
  17. // @router /list [get]
  18. func (this *ChartPermissionController) List() {
  19. br := new(models.BaseResponse).Init()
  20. defer func() {
  21. this.Data["json"] = br
  22. this.ServeJSON()
  23. }()
  24. items, err := models.GetPermissionList()
  25. if err != nil {
  26. br.Msg = "权限列表获取失败"
  27. br.ErrMsg = "权限列表获取失败,系统错误,Err:" + err.Error()
  28. return
  29. }
  30. root := &PermissionNode{
  31. ID: 0,
  32. ParentID: 0,
  33. }
  34. assemblePermissionNode(items, root, 0, 2)
  35. resp := new(response.ChartPermissionListResp)
  36. resp.List = root.Children
  37. br.Ret = 200
  38. br.Data = resp
  39. br.Msg = "列表获取成功"
  40. br.Success = true
  41. }
  42. func checkRiskLevel(riskLevel string) bool {
  43. pattern := `^R\d+$`
  44. matched, _ := regexp.MatchString(pattern, riskLevel)
  45. return matched
  46. }
  47. // SetRiskLevel
  48. // @Title 系统品种列表
  49. // @Description 系统品种列表
  50. // @Param UserId query int true "角色ID"
  51. // @Success 200 {object} models.LoginResp
  52. // @router /setRiskLevel [post]
  53. func (this *ChartPermissionController) SetRiskLevel() {
  54. br := new(models.BaseResponse).Init()
  55. defer func() {
  56. this.Data["json"] = br
  57. this.ServeJSON()
  58. }()
  59. var req request.PermissionRiskReq
  60. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  61. if err != nil {
  62. br.Msg = "请求参数错误"
  63. br.ErrMsg = "解析请求参数异常,Err:" + err.Error()
  64. return
  65. }
  66. if req.Id <= 0 {
  67. br.Msg = "品种ID非法"
  68. br.ErrMsg = "品种ID不能为负数"
  69. return
  70. }
  71. if req.RiskLevel == "" {
  72. br.Msg = "风控等级为空"
  73. br.ErrMsg = "风控等级为空"
  74. return
  75. }
  76. if !checkRiskLevel(req.RiskLevel) {
  77. br.Msg = "风控等级包含非法字符"
  78. br.ErrMsg = "风控等级非法:" + req.RiskLevel
  79. return
  80. }
  81. item, err := models.GetPermissionByPermissionId(req.Id)
  82. if err != nil {
  83. br.Msg = "获取品种信息失败"
  84. br.ErrMsg = "获取品种信息失败,系统错误,Err:" + err.Error()
  85. return
  86. }
  87. if item.ParentId == 0 {
  88. br.Msg = "当前品种为一级品种,不能设置风险等级"
  89. br.ErrMsg = "当前品种为一级品种,不能设置风险等级"
  90. return
  91. }
  92. item.RiskLevel = req.RiskLevel
  93. err = models.InsertProductRiskLevel(req.RiskLevel)
  94. if err != nil {
  95. br.Msg = "保存自定义风险等级失败"
  96. br.ErrMsg = "保存自定义风险等级失败,Err:" + err.Error()
  97. return
  98. }
  99. err = item.SetPermissionRiskLevel()
  100. if err != nil {
  101. br.Msg = "设置风险等级失败"
  102. br.ErrMsg = "设置风险等级失败,Err:" + err.Error()
  103. return
  104. }
  105. br.Ret = 200
  106. br.Msg = "设置风险等级成功"
  107. br.Success = true
  108. }
  109. type PermissionNode struct {
  110. ID int `json:"id"`
  111. Name string `json:"name"`
  112. ParentID int `json:"parentId"`
  113. RiskLevel string `json:"riskLevel"`
  114. Children []*PermissionNode `json:"children"`
  115. }
  116. func assemblePermissionNode(list []*models.Permission, node *PermissionNode, current int, level int) {
  117. if node != nil && current < level {
  118. for _, permission := range list {
  119. if permission.ParentId == node.ID {
  120. childNode := &PermissionNode{
  121. ID: permission.PermissionId,
  122. Name: permission.Name,
  123. RiskLevel: permission.RiskLevel,
  124. ParentID: permission.ParentId,
  125. }
  126. node.Children = append(node.Children, childNode)
  127. assemblePermissionNode(list, childNode, current+1, level)
  128. }
  129. }
  130. }
  131. }