123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- package services
- import (
- "encoding/json"
- "eta_gn/eta_api/models"
- "eta_gn/eta_api/utils"
- "fmt"
- "io/ioutil"
- "net/http"
- "strings"
- )
- // OuterReportCallBackApiUrl 外部报告回调接口
- const OuterReportCallBackApiUrl = "/subject/report/writingCallback"
- // OuterReportCallBackRequest 外部报告回调请求
- type OuterReportCallBackRequest struct {
- Name string `json:"name"`
- ReportId int `json:"reportId"`
- Url string `json:"url"`
- FileType string `json:"fileType"`
- FileSize int `json:"fileSize"`
- }
- // OuterReportCallBackResp 外部报告回调响应
- type OuterReportCallBackResp struct {
- Code int `json:"code"`
- Msg string `json:"msg"`
- Data bool `json:"data"`
- }
- // OuterReportCallBack 外部报告回调(提交审批)
- func OuterReportCallBack(outReportId int, title, fileUrl, fileType string) (err error) {
- var requestUrl, params, respBody string
- defer func() {
- if err != nil {
- tips := fmt.Sprintf("OuterReportCallBack-外部报告回调失败, Url: %s, Request: %s, Resp: %s, ErrMsg: %v", requestUrl, params, respBody, err)
- fmt.Println(tips)
- utils.FileLog.Info(tips)
- return
- }
- utils.FileLog.Info(fmt.Sprintf("OuterReportCallBack-回调成功, Url: %s, Request: %s, Resp: %s", requestUrl, params, respBody))
- }()
- // 入参
- var req OuterReportCallBackRequest
- req.Name = fmt.Sprintf("%s%s", title, fileType)
- req.ReportId = outReportId
- req.Url = fileUrl
- req.FileType = fileType
- b, e := json.Marshal(req)
- if e != nil {
- err = fmt.Errorf("请求参数JSON格式化失败, %v", e)
- return
- }
- params = string(b)
- // 获取地址
- conf, e := models.GetBusinessConfByKey(models.BusinessConfOuterReportApiUrl)
- if e != nil {
- if utils.IsErrNoRow(e) {
- err = fmt.Errorf("外部报告API地址未配置")
- }
- err = fmt.Errorf("获取外部报告API地址配置失败, %v", e)
- return
- }
- if conf.ConfVal == "" {
- err = fmt.Errorf("外部报告API地址为空")
- return
- }
- requestUrl = conf.ConfVal + OuterReportCallBackApiUrl
- // 请求接口
- resByte, e := OuterReportCallBackPut(requestUrl, params, "application/json;charset=utf-8")
- if e != nil {
- err = fmt.Errorf("接口请求失败, %v", e)
- return
- }
- respBody = string(resByte)
- var resp OuterReportCallBackResp
- if e := json.Unmarshal(resByte, &resp); e != nil {
- err = fmt.Errorf("响应JSON解析失败, %v", e)
- return
- }
- if resp.Code != 200 {
- err = fmt.Errorf("回调失败, Code: %d, Msg: %s", resp.Code, resp.Msg)
- return
- }
- return
- }
- // OuterReportCallBackPut 外部报告回调POST
- func OuterReportCallBackPut(url, postData string, params ...string) ([]byte, error) {
- body := ioutil.NopCloser(strings.NewReader(postData))
- client := &http.Client{}
- req, e := http.NewRequest("PUT", url, body)
- if e != nil {
- return nil, fmt.Errorf("http request err: %v", e)
- }
- contentType := "application/x-www-form-urlencoded;charset=utf-8"
- if len(params) > 0 && params[0] != "" {
- contentType = params[0]
- }
- req.Header.Set("Content-Type", contentType)
- resp, e := client.Do(req)
- if e != nil {
- return nil, fmt.Errorf("client do err: %v", e)
- }
- defer resp.Body.Close()
- b, e := ioutil.ReadAll(resp.Body)
- if e != nil {
- return nil, fmt.Errorf("read body err: %v", e)
- }
- return b, e
- }
|