chart.go 1.8 KB

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