oa.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package gn
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta_gn/eta_bridge/global"
  6. "eta_gn/eta_bridge/services/alarm_msg"
  7. "fmt"
  8. "io"
  9. "net/http"
  10. "strings"
  11. )
  12. // OABaseDataResp
  13. // @Description: 基础返回
  14. type OABaseDataResp struct {
  15. Code int `json:"code"`
  16. Msg string `json:"msg"`
  17. }
  18. // OASendDbResp
  19. // @Description: 指标列表请求返回结构体
  20. type OASendDbResp struct {
  21. OABaseDataResp
  22. Data struct {
  23. Result bool `json:"result"`
  24. TaskId string `json:"taskId"`
  25. } `json:"data"`
  26. }
  27. // OASendDbReq
  28. // @Description: oa审批请求参数
  29. type OASendDbReq struct {
  30. AppTaskId string `json:"appTaskId"`
  31. AppPersonId string `json:"appPersonId"`
  32. AppPersonName string `json:"appPersonName"`
  33. AppTaskUrl string `json:"appTaskUrl"`
  34. TaskName string `json:"taskName"`
  35. StatusName string `json:"statusName"`
  36. Status int `json:"status"`
  37. }
  38. // OASendDb
  39. // @Description: 发送待办信息
  40. // @param param
  41. // @return resp
  42. // @return err
  43. func OASendDb(param OASendDbReq) (resp OASendDbResp, err error) {
  44. defer func() {
  45. if err != nil {
  46. go alarm_msg.SendAlarmMsg("发送待办信息失败;ERR:"+err.Error(), 3)
  47. }
  48. }()
  49. urlStr := global.CONFIG.Gn.OAHost + "/api/oa/sendDB"
  50. // 请求
  51. result, err := HttpPostOA(urlStr, param)
  52. if err != nil {
  53. return
  54. }
  55. // 解析响应结果
  56. err = json.Unmarshal(result, &resp)
  57. if err != nil {
  58. return
  59. }
  60. return
  61. }
  62. // HttpPostOA
  63. // @Description: post请求
  64. // @param urlPath
  65. // @param token
  66. // @param postDataParams
  67. // @return []byte
  68. // @return error
  69. func HttpPostOA(urlPath string, postDataParams interface{}) ([]byte, error) {
  70. if global.CONFIG.Gn.OAHost == `` {
  71. return nil, errors.New("OA平台地址为空")
  72. }
  73. // 请求地址
  74. postUrl := urlPath
  75. var err error
  76. postDataByte, e := json.Marshal(postDataParams)
  77. if e != nil {
  78. err = fmt.Errorf("data json marshal err: %s", e.Error())
  79. return []byte{}, err
  80. }
  81. postData := string(postDataByte)
  82. body := io.NopCloser(strings.NewReader(postData))
  83. client := &http.Client{}
  84. req, err := http.NewRequest("POST", postUrl, body)
  85. if err != nil {
  86. return nil, err
  87. }
  88. req.Header.Set("content-Type", "application/json; charset=utf-8")
  89. req.Header.Set("Accept-Encoding", "application/json; charset=utf-8")
  90. req.Header.Set("Accept", "application/json; charset=utf-8")
  91. resp, err := client.Do(req)
  92. if err != nil {
  93. return nil, err
  94. }
  95. defer func() {
  96. _ = resp.Body.Close()
  97. }()
  98. result, err := io.ReadAll(resp.Body)
  99. if err != nil {
  100. return nil, err
  101. }
  102. // 日志记录
  103. global.FILE_LOG.Debug("OA审批:地址:" + postUrl + ";\n请求参数:" + postData + ";\n返回参数:" + string(result))
  104. // 解析返回参数,判断是否是json
  105. if !json.Valid(result) {
  106. err = errors.New("返回参数不是json格式")
  107. return []byte{}, err
  108. }
  109. var baseResp OASendDbResp
  110. // 解析响应结果
  111. err = json.Unmarshal(result, &baseResp)
  112. if err != nil {
  113. return []byte{}, err
  114. }
  115. if baseResp.Code != 200 {
  116. err = errors.New(fmt.Sprintf("响应代码:%d,错误信息:%s", baseResp.Code, baseResp.Msg))
  117. global.LOG.Info(fmt.Sprint("post data err:", baseResp.Msg, ";url:", postUrl, ";response:", postData))
  118. return []byte{}, err
  119. }
  120. return result, err
  121. }