zhongji_index.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package index_data
  2. import (
  3. "encoding/json"
  4. "eta/eta_bridge/controller/resp"
  5. "eta/eta_bridge/global"
  6. indexDataReq "eta/eta_bridge/models/request/index_data"
  7. "eta/eta_bridge/models/response"
  8. indexDataService "eta/eta_bridge/services/index_data"
  9. "fmt"
  10. "github.com/gin-gonic/gin"
  11. "github.com/go-playground/validator/v10"
  12. )
  13. // ZhongjiIndexController 中基宁波
  14. type ZhongjiIndexController struct{}
  15. // GetIndexList
  16. // @Description 获取smm指标列表信息
  17. // @Success 200 {string} string "获取成功"
  18. // @Router /smm/list [post]
  19. func (j *ZhongjiIndexController) GetIndexList(c *gin.Context) {
  20. var req indexDataReq.ZhongjiSmmIndexListReq
  21. if e := c.Bind(&req); e != nil {
  22. err, ok := e.(validator.ValidationErrors)
  23. if !ok {
  24. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  25. return
  26. }
  27. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  28. return
  29. }
  30. if req.Page < 1 {
  31. resp.FailMsg("请输入页码", "请输入页码", c)
  32. return
  33. }
  34. if req.PageSize <= 0 || req.PageSize > 500 {
  35. req.PageSize = 500
  36. }
  37. token, err := indexDataService.GetSmmAccessToken(false)
  38. if err!= nil {
  39. resp.FailData("获取token失败", err.Error(), c)
  40. return
  41. }
  42. body, err := indexDataService.GetEdbDataListFromSmmHttp(req.Page, req.PageSize, token)
  43. if err!= nil {
  44. resp.FailData("获取token失败", err.Error(), c)
  45. return
  46. }
  47. result := new(response.SmmIndexListResponse)
  48. if err = json.Unmarshal(body, &result); err != nil {
  49. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", err.Error(), string(body))
  50. return
  51. }
  52. resp.OkData("获取成功", result, c)
  53. return
  54. }
  55. // GetIndex
  56. // @Description 获取smm指标信息
  57. // @Success 200 {string} string "获取成功"
  58. // @Router /smm/data [post]
  59. func (j *ZhongjiIndexController) GetIndexData(c *gin.Context) {
  60. var req indexDataReq.ZhongjiSmmIndexDataReq
  61. if e := c.Bind(&req); e != nil {
  62. err, ok := e.(validator.ValidationErrors)
  63. if !ok {
  64. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  65. return
  66. }
  67. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  68. return
  69. }
  70. if req.EdbCode == "" {
  71. resp.FailMsg("参数异常", "请输入指标Code", c)
  72. return
  73. }
  74. token, err := indexDataService.GetSmmAccessToken(false)
  75. if err!= nil {
  76. resp.FailData("获取token失败", err.Error(), c)
  77. return
  78. }
  79. body, err := indexDataService.GetEdbDataFromSmmHttp(req.EdbCode, token)
  80. if err!= nil {
  81. resp.FailData("获取token失败", err.Error(), c)
  82. return
  83. }
  84. result := new(response.SmmResponse)
  85. if err = json.Unmarshal(body, &result); err != nil {
  86. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", err.Error(), string(body))
  87. return
  88. }
  89. resp.OkData("获取成功", result, c)
  90. return
  91. }
  92. // GetIndexLatest
  93. // @Description 获取smm指标最新信息
  94. // @Success 200 {string} string "获取成功"
  95. // @Router /smm/latest [post]
  96. func (j *ZhongjiIndexController) GetIndexDataLatest(c *gin.Context) {
  97. var req indexDataReq.ZhongjiSmmIndexLatestDataReq
  98. if e := c.Bind(&req); e != nil {
  99. err, ok := e.(validator.ValidationErrors)
  100. if !ok {
  101. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  102. return
  103. }
  104. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  105. return
  106. }
  107. if req.EdbCode == "" {
  108. resp.FailMsg("参数异常", "请输入指标Code", c)
  109. return
  110. }
  111. if req.LastTime < 0 {
  112. resp.FailMsg("参数异常", "请输入日期", c)
  113. return
  114. }
  115. token, err := indexDataService.GetSmmAccessToken(false)
  116. if err!= nil {
  117. resp.FailData("获取token失败", err.Error(), c)
  118. return
  119. }
  120. body, err := indexDataService.GetEdbLatestDataFromSmmHttp(req.EdbCode, token, req.LastTime)
  121. if err!= nil {
  122. resp.FailData("获取token失败", err.Error(), c)
  123. return
  124. }
  125. result := new(response.SmmLatestDataResponse)
  126. if err = json.Unmarshal(body, &result); err != nil {
  127. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", err.Error(), string(body))
  128. return
  129. }
  130. resp.OkData("获取成功", result, c)
  131. return
  132. }