transition.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package http
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "go-common/app/admin/main/aegis/model/common"
  7. "go-common/app/admin/main/aegis/model/net"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/http/blademaster/binding"
  12. )
  13. func listTransition(c *bm.Context) {
  14. pm := new(net.ListNetElementParam)
  15. if err := c.Bind(pm); err != nil {
  16. c.JSON(nil, ecode.RequestErr)
  17. return
  18. }
  19. if pm.Sort != "desc" && pm.Sort != "asc" {
  20. pm.Sort = "desc"
  21. }
  22. c.JSON(srv.GetTransitionList(c, pm))
  23. }
  24. func getTranByNet(c *bm.Context) {
  25. pm := c.Request.Form.Get("net_id")
  26. id, err := strconv.ParseInt(pm, 10, 64)
  27. if err != nil {
  28. c.JSON(nil, ecode.RequestErr)
  29. return
  30. }
  31. c.JSON(srv.GetTranByNet(c, id))
  32. }
  33. func showTransition(c *bm.Context) {
  34. pm := c.Request.Form.Get("id")
  35. id, err := strconv.ParseInt(pm, 10, 64)
  36. if err != nil {
  37. c.JSON(nil, ecode.RequestErr)
  38. return
  39. }
  40. c.JSON(srv.ShowTransition(c, id))
  41. }
  42. func preTran(pm *net.TransitionEditParam) (invalid bool) {
  43. invalid = true
  44. pm.Trigger = net.TriggerManual
  45. pm.ChName = common.FilterChname(pm.ChName)
  46. pm.Name = common.FilterName(pm.Name)
  47. if pm.ChName == "" || pm.Name == "" || (pm.Trigger == net.TriggerManual && pm.Limit > 50) {
  48. return
  49. }
  50. existBindMap := map[string]int{}
  51. for _, item := range pm.TokenList {
  52. item.TokenID = strings.TrimSpace(item.TokenID)
  53. item.ChName = strings.TrimSpace(item.ChName)
  54. if item.ID < 0 || item.TokenID == "" || item.Type == net.BindTypeFlow {
  55. return
  56. }
  57. kk := fmt.Sprintf("%s_%d", item.TokenID, item.Type)
  58. if existBindMap[kk] > 0 {
  59. log.Error("preTran bind(%+v) duplicated in tokenid+type", item)
  60. return
  61. }
  62. existBindMap[kk] = 1
  63. }
  64. invalid = false
  65. return
  66. }
  67. func addTransition(c *bm.Context) {
  68. pm := new(net.TransitionEditParam)
  69. if err := c.BindWith(pm, binding.JSON); err != nil || pm.NetID <= 0 {
  70. log.Error("addTransition bind params error(%v) body(%+v)", err, c.Request.Body)
  71. c.JSON(nil, ecode.RequestErr)
  72. return
  73. }
  74. if preTran(pm) {
  75. c.JSON(nil, ecode.RequestErr)
  76. return
  77. }
  78. admin := uid(c)
  79. id, err, msg := srv.AddTransition(c, admin, pm)
  80. c.JSONMap(map[string]interface{}{
  81. "id": id,
  82. "message": msg,
  83. }, err)
  84. }
  85. func updateTransition(c *bm.Context) {
  86. pm := new(net.TransitionEditParam)
  87. if err := c.BindWith(pm, binding.JSON); err != nil || pm.ID <= 0 {
  88. log.Error("updateTransition bind params error(%v) body(%+v)", err, c.Request.Body)
  89. c.JSON(nil, ecode.RequestErr)
  90. return
  91. }
  92. if preTran(pm) {
  93. c.JSON(nil, ecode.RequestErr)
  94. return
  95. }
  96. admin := uid(c)
  97. err, msg := srv.UpdateTransition(c, admin, pm)
  98. c.JSONMap(map[string]interface{}{
  99. "message": msg,
  100. }, err)
  101. }
  102. func switchTransition(c *bm.Context) {
  103. pm := new(net.SwitchParam)
  104. if err := c.Bind(pm); err != nil {
  105. c.JSON(nil, ecode.RequestErr)
  106. return
  107. }
  108. c.JSON(nil, srv.SwitchTransition(c, pm.ID, pm.Disable))
  109. }