auth.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package gn
  2. import (
  3. "eta_gn/eta_bridge/controller/resp"
  4. "eta_gn/eta_bridge/global"
  5. "eta_gn/eta_bridge/logic/gn"
  6. gnRequest "eta_gn/eta_bridge/models/request/gn"
  7. "fmt"
  8. "github.com/gin-gonic/gin"
  9. "github.com/go-playground/validator/v10"
  10. )
  11. // GuoNengController 国能
  12. type GuoNengController struct{}
  13. func (gc *GuoNengController) GetToken(c *gin.Context) {
  14. var req gnRequest.GetTokenReq
  15. if e := c.Bind(&req); e != nil {
  16. err, ok := e.(validator.ValidationErrors)
  17. if !ok {
  18. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  19. return
  20. }
  21. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  22. return
  23. }
  24. if req.Code == "" {
  25. resp.FailMsg("请传入code码", "请输入指标code码", c)
  26. return
  27. }
  28. result, err, errMsg := gn.LoginEta(req.Code)
  29. if err != nil {
  30. fmt.Printf("获取失败, Err: %v, ErrMsg: %s", err, errMsg)
  31. resp.FailData(errMsg, err.Error(), c)
  32. return
  33. }
  34. resp.OkData("获取成功", result, c)
  35. }
  36. // GetUserInfo
  37. // @Description: 根据sci的token获取用户信息
  38. // @author: Roc
  39. // @receiver gc
  40. // @datetime 2024-10-30 10:11:51
  41. // @param c *gin.Context
  42. func (gc *GuoNengController) GetUserInfo(c *gin.Context) {
  43. var req gnRequest.GetUserInfoReq
  44. if e := c.Bind(&req); e != nil {
  45. err, ok := e.(validator.ValidationErrors)
  46. if !ok {
  47. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  48. return
  49. }
  50. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  51. return
  52. }
  53. if req.Token == "" {
  54. resp.FailMsg("请传入token", "请传入token", c)
  55. return
  56. }
  57. // 根据token在卓创这边获取用户信息
  58. result, err, errMsg := gn.GetUserInfoByToken(req.Token)
  59. if err != nil {
  60. fmt.Printf("获取失败, Err: %v, ErrMsg: %s", err, errMsg)
  61. resp.FailData(errMsg, err.Error(), c)
  62. return
  63. }
  64. resp.OkData("获取成功", result, c)
  65. }