report2img.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. package services
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "eta_gn/eta_report/models"
  6. "eta_gn/eta_report/services/alarm_msg"
  7. "eta_gn/eta_report/utils"
  8. "fmt"
  9. "io/ioutil"
  10. "net/http"
  11. "sync"
  12. "time"
  13. )
  14. const (
  15. PythonReportHtml2ImgApi = "/api/report/html2img" // 图片生成接口
  16. PythonReportHtml2ImgClearLocalApi = "/api/report/clear_local_file" // 清除本地图片文件接口
  17. )
  18. func CreateReportImgAndPdf(req Report2ImgQueueReq) {
  19. if req.ReportType != 2 {
  20. return
  21. }
  22. if req.ReportCode == "" {
  23. return
  24. }
  25. var err error
  26. defer func() {
  27. if err != nil {
  28. tips := fmt.Sprintf("报告详情转长图, ErrMsg: %s", err.Error())
  29. fmt.Println(tips)
  30. utils.FileLog.Info(tips)
  31. go alarm_msg.SendAlarmMsg(tips, 3)
  32. }
  33. }()
  34. conf, e := models.GetBusinessConf()
  35. if e != nil {
  36. err = fmt.Errorf("获取商家配置失败, Err: %s", e.Error())
  37. return
  38. }
  39. reportViewHost := conf[models.BusinessConfReport2ImgUrl]
  40. if reportViewHost == "" {
  41. reportViewHost = conf[models.BusinessConfReportViewUrl]
  42. }
  43. if reportViewHost == "" {
  44. err = fmt.Errorf("报告分享域名未配置")
  45. return
  46. }
  47. reportOb := new(models.SmartReport)
  48. cond := ` AND report_code = ?`
  49. pars := make([]interface{}, 0)
  50. pars = append(pars, req.ReportCode)
  51. item, e := reportOb.GetItemByCondition(cond, pars)
  52. if e != nil {
  53. if e.Error() == utils.ErrNoRow() {
  54. return
  55. }
  56. err = fmt.Errorf("获取报告失败, Err: %s", e.Error())
  57. return
  58. }
  59. apiDone := make(chan bool, 1)
  60. apiError := make(chan error, 1)
  61. fileName := utils.GetRandStringNoSpecialChar(28)
  62. apiResult := make([]string, 0)
  63. go func() {
  64. var apiReq ReportHtml2ImgApiReq
  65. apiReq.ReportUrl = fmt.Sprintf("%s/%s?code=%s", reportViewHost, "smart_report_getImg", req.ReportCode)
  66. apiReq.FileName = fileName
  67. res, e := CurlReportHtml2ImgApi(apiReq)
  68. if e != nil {
  69. apiError <- fmt.Errorf("CurlReportHtml2ImgApi err: %s", e.Error())
  70. return
  71. }
  72. apiResult = res
  73. apiDone <- true
  74. }()
  75. select {
  76. case <-time.After(6 * time.Minute):
  77. err = fmt.Errorf("报告生成长图超时")
  78. return
  79. case e = <-apiError:
  80. err = e
  81. return
  82. case <-apiDone:
  83. fmt.Println("api done")
  84. }
  85. if len(apiResult) != 2 {
  86. err = fmt.Errorf("文件生成有误")
  87. return
  88. }
  89. var imgUrl, pdfUrl string
  90. var errImg, errPdf error
  91. wg := sync.WaitGroup{}
  92. wg.Add(2)
  93. go func() {
  94. defer func() {
  95. wg.Done()
  96. }()
  97. imgFileName := fileName + ".png"
  98. ossClient := NewOssClient()
  99. if ossClient == nil {
  100. errImg = fmt.Errorf("初始化OSS服务失败")
  101. return
  102. }
  103. fmt.Println("start UploadFile")
  104. si, e := ossClient.UploadFile(imgFileName, apiResult[0], "")
  105. if e != nil {
  106. fmt.Println("UploadFile,Err:" + e.Error())
  107. errImg = fmt.Errorf("文件上传失败, Err: %s", e.Error())
  108. return
  109. }
  110. fmt.Println("end UploadFile:" + imgFileName)
  111. imgUrl = si
  112. var clearReq ReportHtml2ImgApiClearLocalReq
  113. clearReq.FileName = imgFileName
  114. e = CurlReportHtml2ImgApiClearLocal(clearReq)
  115. if e != nil {
  116. errImg = e
  117. return
  118. }
  119. }()
  120. go func() {
  121. defer func() {
  122. wg.Done()
  123. }()
  124. pdfFileName := fileName + ".pdf"
  125. ossClient := NewOssClient()
  126. if ossClient == nil {
  127. errImg = fmt.Errorf("初始化OSS服务失败")
  128. return
  129. }
  130. sp, e := ossClient.UploadFile(pdfFileName, apiResult[1], "")
  131. if e != nil {
  132. errImg = fmt.Errorf("文件上传失败, Err: %s", e.Error())
  133. return
  134. }
  135. pdfUrl = sp
  136. var clearReq ReportHtml2ImgApiClearLocalReq
  137. clearReq.FileName = pdfFileName
  138. e = CurlReportHtml2ImgApiClearLocal(clearReq)
  139. if e != nil {
  140. errImg = e
  141. return
  142. }
  143. }()
  144. wg.Wait()
  145. if errImg != nil {
  146. err = fmt.Errorf("upload img err: %s", e.Error())
  147. return
  148. }
  149. if errPdf != nil {
  150. err = fmt.Errorf("upload pdf err: %s", e.Error())
  151. return
  152. }
  153. fmt.Println("imgUrl:" + imgUrl + ";pdfUrl:" + pdfUrl)
  154. item.DetailImgUrl = imgUrl
  155. item.DetailPdfUrl = pdfUrl
  156. item.ModifyTime = time.Now().Local()
  157. updateCols := []string{"DetailImgUrl", "DetailPdfUrl", "ModifyTime"}
  158. if e = item.Update(updateCols); e != nil {
  159. err = fmt.Errorf("更新报告链接失败, Err: %s", e.Error())
  160. return
  161. }
  162. return
  163. }
  164. type ReportHtml2ImgApiReq struct {
  165. ReportUrl string `json:"report_url" description:"报告详情分享地址"`
  166. FileName string `json:"file_name" description:"生成的文件名"`
  167. OutputType string `json:"output_type" description:"生成类型: img/pdf, 为空则两种均生成"`
  168. }
  169. type ReportHtml2ImgApiResp struct {
  170. Code int `json:"code" description:"状态码"`
  171. Msg string `json:"error" description:"提示信息"`
  172. Data []string `json:"data" description:"返回数据"`
  173. }
  174. func CurlReportHtml2ImgApi(params ReportHtml2ImgApiReq) (apiResp []string, err error) {
  175. if utils.Report2ImgServerUrl == "" {
  176. err = fmt.Errorf("服务地址为空")
  177. return
  178. }
  179. url := fmt.Sprint(utils.Report2ImgServerUrl, PythonReportHtml2ImgApi)
  180. jsonData, e := json.Marshal(params)
  181. if e != nil {
  182. err = fmt.Errorf("data json marshal err: %s", e.Error())
  183. return
  184. }
  185. resp, e := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
  186. if e != nil {
  187. err = fmt.Errorf("http post err: %s", e.Error())
  188. return
  189. }
  190. defer resp.Body.Close()
  191. b, e := ioutil.ReadAll(resp.Body)
  192. if e != nil {
  193. err = fmt.Errorf("resp body read err: %s", e.Error())
  194. return
  195. }
  196. if len(b) == 0 {
  197. err = fmt.Errorf("resp body is empty")
  198. return
  199. }
  200. result := new(ReportHtml2ImgApiResp)
  201. if e = json.Unmarshal(b, &result); e != nil {
  202. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  203. return
  204. }
  205. if result.Code != 200 {
  206. err = fmt.Errorf("result: %s", string(b))
  207. return
  208. }
  209. apiResp = result.Data
  210. return
  211. }
  212. type ReportHtml2ImgApiClearLocalReq struct {
  213. FileName string `json:"file_name" description:"生成的文件名"`
  214. }
  215. type ReportHtml2ImgApiClearLocalResp struct {
  216. Code int `json:"code" description:"状态码"`
  217. Msg string `json:"error" description:"提示信息"`
  218. Data string `json:"data" description:"返回数据"`
  219. }
  220. func CurlReportHtml2ImgApiClearLocal(params ReportHtml2ImgApiClearLocalReq) (err error) {
  221. if utils.Report2ImgServerUrl == "" {
  222. err = fmt.Errorf("服务地址为空")
  223. return
  224. }
  225. url := fmt.Sprint(utils.Report2ImgServerUrl, PythonReportHtml2ImgClearLocalApi)
  226. jsonData, e := json.Marshal(params)
  227. if e != nil {
  228. err = fmt.Errorf("data json marshal err: %s", e.Error())
  229. return
  230. }
  231. resp, e := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
  232. if e != nil {
  233. err = fmt.Errorf("http post err: %s", e.Error())
  234. return
  235. }
  236. defer resp.Body.Close()
  237. b, e := ioutil.ReadAll(resp.Body)
  238. if e != nil {
  239. err = fmt.Errorf("resp body read err: %s", e.Error())
  240. return
  241. }
  242. if len(b) == 0 {
  243. err = fmt.Errorf("resp body is empty")
  244. return
  245. }
  246. result := new(ReportHtml2ImgApiClearLocalResp)
  247. if e = json.Unmarshal(b, &result); e != nil {
  248. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  249. return
  250. }
  251. if result.Code != 200 {
  252. err = fmt.Errorf("result: %s", string(b))
  253. return
  254. }
  255. return
  256. }