chart.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_report/models"
  5. "eta_gn/eta_report/services"
  6. "eta_gn/eta_report/utils"
  7. "fmt"
  8. "io/ioutil"
  9. "net/http"
  10. )
  11. type ChartController struct {
  12. BaseAuthController
  13. }
  14. // @router /detail [post]
  15. func (this *ChartController) ChartDetail() {
  16. br := new(models.BaseResponse).Init()
  17. defer func() {
  18. if br.ErrMsg == "" {
  19. br.IsSendEmail = false
  20. }
  21. this.Data["json"] = br
  22. this.ServeJSONNoEncryption()
  23. }()
  24. var req services.ChartDetailReq
  25. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  26. if err != nil {
  27. br.Msg = "参数解析异常!"
  28. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  29. return
  30. }
  31. if req.UniqueCode == "" {
  32. br.Msg = "参数不能为空!"
  33. br.ErrMsg = "参数不能为空!"
  34. return
  35. }
  36. url := utils.ChartLibUrl + `/chart/common/detail?UniqueCode=%s`
  37. url = fmt.Sprintf(url, req.UniqueCode)
  38. resp, e := http.Get(url)
  39. if e != nil {
  40. err = fmt.Errorf("http Get err: %s", e.Error())
  41. br.ErrMsg = err.Error()
  42. return
  43. }
  44. defer resp.Body.Close()
  45. b, e := ioutil.ReadAll(resp.Body)
  46. if e != nil {
  47. err = fmt.Errorf("resp body read err: %s", e.Error())
  48. br.ErrMsg = err.Error()
  49. return
  50. }
  51. if len(b) == 0 {
  52. err = fmt.Errorf("resp body is empty")
  53. br.ErrMsg = err.Error()
  54. return
  55. }
  56. result := new(models.BaseResponse)
  57. if e = json.Unmarshal(b, &result); e != nil {
  58. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  59. br.ErrMsg = err.Error()
  60. return
  61. }
  62. if result.Ret != 200 {
  63. err = fmt.Errorf("result: %s", string(b))
  64. br.ErrMsg = err.Error()
  65. return
  66. }
  67. br.Ret = 200
  68. br.Success = true
  69. br.Msg = "获取成功"
  70. br.Data = result.Data
  71. return
  72. }