token.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package http
  2. import (
  3. "strconv"
  4. "strings"
  5. "go-common/app/admin/main/aegis/model/common"
  6. "go-common/app/admin/main/aegis/model/net"
  7. "go-common/library/ecode"
  8. bm "go-common/library/net/http/blademaster"
  9. )
  10. func listToken(c *bm.Context) {
  11. pm := new(net.ListTokenParam)
  12. if err := c.Bind(pm); err != nil {
  13. c.JSON(nil, ecode.RequestErr)
  14. return
  15. }
  16. if pm.Sort != "desc" && pm.Sort != "asc" {
  17. pm.Sort = "desc"
  18. }
  19. c.JSON(srv.GetTokenList(c, pm))
  20. }
  21. func tokenGroupByType(c *bm.Context) {
  22. pm := c.Request.Form.Get("net_id")
  23. netID, err := strconv.ParseInt(pm, 10, 64)
  24. if err != nil {
  25. c.JSON(nil, ecode.RequestErr)
  26. return
  27. }
  28. c.JSON(srv.TokenGroupByType(c, netID))
  29. }
  30. func tokenByName(c *bm.Context) {
  31. name := strings.TrimSpace(c.Request.Form.Get("name"))
  32. busIDStr := c.Request.Form.Get("business_id")
  33. busID, err := strconv.ParseInt(busIDStr, 10, 64)
  34. if err != nil || busID <= 0 || name == "" {
  35. c.JSON(nil, ecode.RequestErr)
  36. return
  37. }
  38. c.JSON(srv.TokenByName(c, busID, name))
  39. }
  40. func configToken(c *bm.Context) {
  41. data := map[string]interface{}{
  42. "compare": net.TokenCompareDesc,
  43. "value_types": net.TokenValueTypeDesc,
  44. }
  45. c.JSONMap(data, nil)
  46. }
  47. func showToken(c *bm.Context) {
  48. pm := c.Request.Form.Get("id")
  49. id, err := strconv.ParseInt(pm, 10, 64)
  50. if err != nil {
  51. c.JSON(nil, ecode.RequestErr)
  52. return
  53. }
  54. c.JSON(srv.ShowToken(c, id))
  55. }
  56. func preToken(pm *net.Token) (invalid bool) {
  57. compare := net.GetTokenCompare(pm.Compare)
  58. tp := net.GetTokenValueType(pm.Type)
  59. pm.Value = strings.TrimSpace(pm.Value)
  60. pm.ChName = common.FilterChname(pm.ChName)
  61. pm.Name = common.FilterName(pm.Name)
  62. if pm.ChName == "" || pm.Name == "" || compare == "" || tp == "" || pm.Value == "" {
  63. invalid = true
  64. }
  65. return
  66. }
  67. func addToken(c *bm.Context) {
  68. pm := new(net.Token)
  69. if err := c.Bind(pm); err != nil || pm.NetID <= 0 {
  70. c.JSON(nil, ecode.RequestErr)
  71. return
  72. }
  73. if preToken(pm) {
  74. c.JSON(nil, ecode.RequestErr)
  75. return
  76. }
  77. pm.UID = uid(c)
  78. id, err, msg := srv.AddToken(c, pm)
  79. c.JSONMap(map[string]interface{}{
  80. "id": id,
  81. "message": msg,
  82. }, err)
  83. }