report_outer.go 3.1 KB

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