chart.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. "strings"
  11. )
  12. // ChartController 图表详情
  13. type ChartController struct {
  14. BaseAuthController
  15. }
  16. // ChartDetail
  17. // @Title 表格详情
  18. // @Description 表格详情
  19. // @Success 200 {object} models.EnglishReportShareDetailResp
  20. // @router /detail [post]
  21. func (this *ChartController) ChartDetail() {
  22. br := new(models.BaseResponse).Init()
  23. defer func() {
  24. if br.ErrMsg == "" {
  25. br.IsSendEmail = false
  26. }
  27. this.Data["json"] = br
  28. this.ServeJSONNoEncryption()
  29. }()
  30. var req services.ChartDetailReq
  31. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  32. if err != nil {
  33. br.Msg = "参数解析异常!"
  34. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  35. return
  36. }
  37. if req.UniqueCode == "" {
  38. br.Msg = "参数不能为空!"
  39. br.ErrMsg = "参数不能为空!"
  40. return
  41. }
  42. url := utils.ChartLibUrl + `/chart/common/detail?UniqueCode=%s`
  43. url = fmt.Sprintf(url, req.UniqueCode)
  44. resp, e := http.Get(url)
  45. if e != nil {
  46. err = fmt.Errorf("http Get err: %s", e.Error())
  47. br.ErrMsg = err.Error()
  48. return
  49. }
  50. defer resp.Body.Close()
  51. b, e := ioutil.ReadAll(resp.Body)
  52. if e != nil {
  53. err = fmt.Errorf("resp body read err: %s", e.Error())
  54. br.ErrMsg = err.Error()
  55. return
  56. }
  57. if len(b) == 0 {
  58. err = fmt.Errorf("resp body is empty")
  59. br.ErrMsg = err.Error()
  60. return
  61. }
  62. result := new(models.BaseResponse)
  63. if e = json.Unmarshal(b, &result); e != nil {
  64. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  65. br.ErrMsg = err.Error()
  66. return
  67. }
  68. if result.Ret != 200 {
  69. err = fmt.Errorf("result: %s", string(b))
  70. br.ErrMsg = err.Error()
  71. return
  72. }
  73. // (为了兼容内网客户)需要判断是否替换资源地址
  74. var urlReplace models.Report2ImgReplace
  75. if req.IsReplace == 1 {
  76. conf, e := models.GetBusinessConf()
  77. if e != nil {
  78. br.Msg = "获取失败"
  79. br.ErrMsg = "获取免责声明失败, Err: " + e.Error()
  80. return
  81. }
  82. if conf[models.BusinessConfReport2ImgReplace] != "" {
  83. if e := json.Unmarshal([]byte(conf[models.BusinessConfReport2ImgReplace]), &urlReplace); e != nil {
  84. br.Msg = "获取失败"
  85. br.ErrMsg = fmt.Sprintf("获取报告替换配置失败, %v", e)
  86. return
  87. }
  88. }
  89. if urlReplace.IsReplace && urlReplace.OssUrlOrigin != "" {
  90. // 替换水印图片地址
  91. replaceFunc := func(data interface{}, originUrl, newUrl string) {
  92. if dataMap, ok := data.(map[string]interface{}); ok {
  93. if waterMark, ok := dataMap["WaterMark"].(string); ok {
  94. newWaterMark := strings.ReplaceAll(waterMark, originUrl, newUrl)
  95. dataMap["WaterMark"] = newWaterMark
  96. }
  97. }
  98. }
  99. replaceFunc(result.Data, urlReplace.OssUrlOrigin, urlReplace.OssUrlNew)
  100. }
  101. }
  102. br.Ret = 200
  103. br.Success = true
  104. br.Msg = "获取成功"
  105. br.Data = result.Data
  106. }