http.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package block
  2. // ParamValidator .
  3. type ParamValidator interface {
  4. Validate() bool
  5. }
  6. // ParamSearch .
  7. type ParamSearch struct {
  8. MIDs []int64 `form:"mids,split"`
  9. }
  10. // Validate .
  11. func (p *ParamSearch) Validate() bool {
  12. p.MIDs = intsSet(p.MIDs)
  13. if len(p.MIDs) == 0 || len(p.MIDs) > 200 {
  14. return false
  15. }
  16. return true
  17. }
  18. // ParamHistory .
  19. type ParamHistory struct {
  20. MID int64 `form:"mid"`
  21. Desc bool `form:"desc"`
  22. PS int `form:"ps"`
  23. PN int `form:"pn"`
  24. }
  25. // Validate .
  26. func (p *ParamHistory) Validate() bool {
  27. if p.MID <= 0 {
  28. return false
  29. }
  30. if p.PS <= 0 || p.PS > 100 {
  31. return false
  32. }
  33. if p.PN <= 0 {
  34. return false
  35. }
  36. return true
  37. }
  38. // ParamBatchBlock .
  39. type ParamBatchBlock struct {
  40. MIDs []int64 `form:"mids,split"`
  41. AdminID int64 `form:"admin_id"`
  42. AdminName string `form:"admin_name"`
  43. Source BlockMgrSource `form:"source"`
  44. Area BlockArea `form:"area"`
  45. Reason string `form:"reason"`
  46. Comment string `form:"comment"`
  47. Action BlockAction `form:"action"`
  48. Duration int64 `form:"duration"` // 单位:天
  49. Notify bool `form:"notify"`
  50. }
  51. // Validate .
  52. func (p *ParamBatchBlock) Validate() bool {
  53. p.MIDs = intsSet(p.MIDs)
  54. if len(p.MIDs) == 0 || len(p.MIDs) > 200 {
  55. return false
  56. }
  57. if p.AdminID <= 0 {
  58. return false
  59. }
  60. if p.AdminName == "" {
  61. return false
  62. }
  63. if p.Source != BlockMgrSourceSys && p.Source != BlockMgrSourceCredit {
  64. return false
  65. }
  66. if !p.Area.Contain() {
  67. return false
  68. }
  69. if p.Comment == "" {
  70. return false
  71. }
  72. if p.Action != BlockActionForever && p.Action != BlockActionLimit {
  73. return false
  74. }
  75. if p.Action == BlockActionLimit {
  76. if p.Duration <= 0 {
  77. return false
  78. }
  79. }
  80. return true
  81. }
  82. // ParamBatchRemove .
  83. type ParamBatchRemove struct {
  84. MIDs []int64 `form:"mids,split"`
  85. AdminID int64 `form:"admin_id"`
  86. AdminName string `form:"admin_name"`
  87. Comment string `form:"comment"`
  88. Notify bool `form:"notify"`
  89. }
  90. // Validate .
  91. func (p *ParamBatchRemove) Validate() bool {
  92. p.MIDs = intsSet(p.MIDs)
  93. if len(p.MIDs) == 0 || len(p.MIDs) > 200 {
  94. return false
  95. }
  96. if p.AdminID <= 0 {
  97. return false
  98. }
  99. if p.AdminName == "" {
  100. return false
  101. }
  102. if p.Comment == "" {
  103. return false
  104. }
  105. return true
  106. }
  107. func intsSet(ints []int64) (intSet []int64) {
  108. if len(ints) == 0 {
  109. return
  110. }
  111. OUTER:
  112. for i := range ints {
  113. for ni := range intSet {
  114. if ints[i] == intSet[ni] {
  115. continue OUTER
  116. }
  117. }
  118. intSet = append(intSet, ints[i])
  119. }
  120. return
  121. }