oa.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. type OABaseDataResp struct {
  13. Code int `json:"code"`
  14. Msg string `json:"msg"`
  15. }
  16. type OASendDbResp struct {
  17. OABaseDataResp
  18. Data struct {
  19. Result bool `json:"result"`
  20. TaskId string `json:"taskId"`
  21. } `json:"data"`
  22. }
  23. type OASendDbReq struct {
  24. AppTaskId string `json:"appTaskId"`
  25. AppPersonId string `json:"appPersonId"`
  26. AppPersonName string `json:"appPersonName"`
  27. AppTaskUrl string `json:"appTaskUrl"`
  28. TaskName string `json:"taskName"`
  29. StatusName string `json:"statusName"`
  30. Status int `json:"status"`
  31. }
  32. func OASendDb(param OASendDbReq) (resp OASendDbResp, err error) {
  33. defer func() {
  34. if err != nil {
  35. go alarm_msg.SendAlarmMsg("发送待办信息失败;ERR:"+err.Error(), 3)
  36. }
  37. }()
  38. urlStr := global.CONFIG.Gn.OAHost + "/api/oa/sendDB"
  39. result, err := HttpPostOA(urlStr, param)
  40. if err != nil {
  41. return
  42. }
  43. err = json.Unmarshal(result, &resp)
  44. if err != nil {
  45. return
  46. }
  47. return
  48. }
  49. func HttpPostOA(urlPath string, postDataParams interface{}) ([]byte, error) {
  50. if global.CONFIG.Gn.OAHost == `` {
  51. return nil, errors.New("OA平台地址为空")
  52. }
  53. postUrl := urlPath
  54. var err error
  55. postDataByte, e := json.Marshal(postDataParams)
  56. if e != nil {
  57. err = fmt.Errorf("data json marshal err: %s", e.Error())
  58. return []byte{}, err
  59. }
  60. postData := string(postDataByte)
  61. body := io.NopCloser(strings.NewReader(postData))
  62. client := &http.Client{}
  63. req, err := http.NewRequest("POST", postUrl, body)
  64. if err != nil {
  65. return nil, err
  66. }
  67. req.Header.Set("content-Type", "application/json; charset=utf-8")
  68. req.Header.Set("Accept-Encoding", "application/json; charset=utf-8")
  69. req.Header.Set("Accept", "application/json; charset=utf-8")
  70. resp, err := client.Do(req)
  71. if err != nil {
  72. return nil, err
  73. }
  74. defer func() {
  75. _ = resp.Body.Close()
  76. }()
  77. result, err := io.ReadAll(resp.Body)
  78. if err != nil {
  79. return nil, err
  80. }
  81. global.FILE_LOG.Debug("OA审批:地址:" + postUrl + ";\n请求参数:" + postData + ";\n返回参数:" + string(result))
  82. if !json.Valid(result) {
  83. err = errors.New("返回参数不是json格式")
  84. return []byte{}, err
  85. }
  86. var baseResp OASendDbResp
  87. err = json.Unmarshal(result, &baseResp)
  88. if err != nil {
  89. return []byte{}, err
  90. }
  91. if baseResp.Code != 200 {
  92. err = errors.New(fmt.Sprintf("响应代码:%d,错误信息:%s", baseResp.Code, baseResp.Msg))
  93. global.LOG.Info(fmt.Sprint("post data err:", baseResp.Msg, ";url:", postUrl, ";response:", postData))
  94. return []byte{}, err
  95. }
  96. return result, err
  97. }