report_outer.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/utils"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "strings"
  10. )
  11. // OuterReportCallBackApiUrl 外部报告回调接口
  12. const OuterReportCallBackApiUrl = "/subject/report/writingCallback"
  13. // OuterReportCallBackRequest 外部报告回调请求
  14. type OuterReportCallBackRequest struct {
  15. Name string `json:"name"`
  16. ReportId int `json:"reportId"`
  17. Abstract string `json:"abstracts"`
  18. Url string `json:"url"`
  19. FileType string `json:"fileType"`
  20. FileSize int `json:"fileSize"`
  21. }
  22. // OuterReportCallBackResp 外部报告回调响应
  23. type OuterReportCallBackResp struct {
  24. Code int `json:"code"`
  25. Msg string `json:"msg"`
  26. Data bool `json:"data"`
  27. }
  28. // OuterReportCallBack 外部报告回调(提交审批)
  29. func OuterReportCallBack(outReportId int, title, abstract, fileUrl, fileType string) (err error) {
  30. var requestUrl, params, respBody string
  31. defer func() {
  32. if err != nil {
  33. tips := fmt.Sprintf("OuterReportCallBack-外部报告回调失败, Url: %s, Request: %s, Resp: %s, ErrMsg: %v", requestUrl, params, respBody, err)
  34. fmt.Println(tips)
  35. utils.FileLog.Info(tips)
  36. return
  37. }
  38. utils.FileLog.Info(fmt.Sprintf("OuterReportCallBack-回调成功, Url: %s, Request: %s, Resp: %s", requestUrl, params, respBody))
  39. }()
  40. // 入参
  41. var req OuterReportCallBackRequest
  42. req.Name = fmt.Sprintf("%s%s", title, fileType)
  43. req.ReportId = outReportId
  44. req.Abstract = abstract
  45. req.Url = fileUrl
  46. req.FileType = fileType
  47. b, e := json.Marshal(req)
  48. if e != nil {
  49. err = fmt.Errorf("请求参数JSON格式化失败, %v", e)
  50. return
  51. }
  52. params = string(b)
  53. // 获取地址
  54. conf, e := models.GetBusinessConfByKey(models.BusinessConfOuterReportApiUrl)
  55. if e != nil {
  56. if utils.IsErrNoRow(e) {
  57. err = fmt.Errorf("外部报告API地址未配置")
  58. }
  59. err = fmt.Errorf("获取外部报告API地址配置失败, %v", e)
  60. return
  61. }
  62. if conf.ConfVal == "" {
  63. err = fmt.Errorf("外部报告API地址为空")
  64. return
  65. }
  66. requestUrl = conf.ConfVal + OuterReportCallBackApiUrl
  67. // 请求接口
  68. resByte, e := OuterReportCallBackPut(requestUrl, params, "application/json;charset=utf-8")
  69. if e != nil {
  70. err = fmt.Errorf("接口请求失败, %v", e)
  71. return
  72. }
  73. respBody = string(resByte)
  74. var resp OuterReportCallBackResp
  75. if e := json.Unmarshal(resByte, &resp); e != nil {
  76. err = fmt.Errorf("响应JSON解析失败, %v", e)
  77. return
  78. }
  79. if resp.Code != 200 {
  80. err = fmt.Errorf("回调失败, Code: %d, Msg: %s", resp.Code, resp.Msg)
  81. return
  82. }
  83. return
  84. }
  85. // OuterReportCallBackPut 外部报告回调POST
  86. func OuterReportCallBackPut(url, postData string, params ...string) ([]byte, error) {
  87. body := ioutil.NopCloser(strings.NewReader(postData))
  88. client := &http.Client{}
  89. req, e := http.NewRequest("PUT", url, body)
  90. if e != nil {
  91. return nil, fmt.Errorf("http request err: %v", e)
  92. }
  93. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  94. if len(params) > 0 && params[0] != "" {
  95. contentType = params[0]
  96. }
  97. req.Header.Set("Content-Type", contentType)
  98. resp, e := client.Do(req)
  99. if e != nil {
  100. return nil, fmt.Errorf("client do err: %v", e)
  101. }
  102. defer resp.Body.Close()
  103. b, e := ioutil.ReadAll(resp.Body)
  104. if e != nil {
  105. return nil, fmt.Errorf("read body err: %v", e)
  106. }
  107. return b, e
  108. }