auth.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package xiangyu
  2. import (
  3. "eta/eta_bridge/controller/resp"
  4. "eta/eta_bridge/global"
  5. "eta/eta_bridge/logic/xiangyu"
  6. xyReq "eta/eta_bridge/models/request/xiangyu"
  7. "github.com/gin-gonic/gin"
  8. "github.com/go-playground/validator/v10"
  9. )
  10. // GetToken
  11. // @Description: 获取token
  12. // @author: Roc
  13. // @receiver xc
  14. // @datetime 2024-01-23 17:06:34
  15. // @param c *gin.Context
  16. func (xc *XiangyuController) GetToken(c *gin.Context) {
  17. var req xyReq.GetTokenReq
  18. if e := c.Bind(&req); e != nil {
  19. err, ok := e.(validator.ValidationErrors)
  20. if !ok {
  21. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  22. return
  23. }
  24. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  25. return
  26. }
  27. if req.Code == "" {
  28. resp.FailMsg("请传入code码", "请输入指标code码", c)
  29. return
  30. }
  31. result, err, errMsg := xiangyu.LoginEta(req.Code)
  32. if err != nil {
  33. resp.FailData(errMsg, err.Error(), c)
  34. return
  35. }
  36. resp.OkData("获取成功", result, c)
  37. return
  38. }
  39. // RefreshToken
  40. // @Description: 刷新token
  41. // @author: Roc
  42. // @receiver xc
  43. // @datetime 2024-01-23 17:06:25
  44. // @param c *gin.Context
  45. func (xc *XiangyuController) RefreshToken(c *gin.Context) {
  46. var req xyReq.RefreshTokenReq
  47. if e := c.Bind(&req); e != nil {
  48. err, ok := e.(validator.ValidationErrors)
  49. if !ok {
  50. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  51. return
  52. }
  53. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  54. return
  55. }
  56. if req.RefreshToken == "" {
  57. resp.FailMsg("请传入refresh_token", "请传入refresh_token", c)
  58. return
  59. }
  60. err := xiangyu.RefreshToken(req.RefreshToken)
  61. if err != nil {
  62. resp.FailData("参数解析失败", err.Error(), c)
  63. return
  64. }
  65. resp.Ok("刷新成功", c)
  66. return
  67. }
  68. // RevokeToken
  69. // @Description: 销毁token
  70. // @author: Roc
  71. // @receiver xc
  72. // @datetime 2024-01-23 17:06:12
  73. // @param c *gin.Context
  74. func (xc *XiangyuController) RevokeToken(c *gin.Context) {
  75. var req xyReq.RevokeTokenReq
  76. if e := c.Bind(&req); e != nil {
  77. err, ok := e.(validator.ValidationErrors)
  78. if !ok {
  79. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  80. return
  81. }
  82. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  83. return
  84. }
  85. if req.Token == "" {
  86. resp.FailMsg("请传入token", "请传入token", c)
  87. return
  88. }
  89. err := xiangyu.RevokeToken(req.Token)
  90. if err != nil {
  91. resp.FailData("参数解析失败", err.Error(), c)
  92. return
  93. }
  94. resp.Ok("销毁成功", c)
  95. return
  96. }