123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- package gn
- import (
- "encoding/json"
- "errors"
- "eta_gn/eta_bridge/global"
- "eta_gn/eta_bridge/services/alarm_msg"
- "fmt"
- "io"
- "net/http"
- "strings"
- )
- // OABaseDataResp
- // @Description: 基础返回
- type OABaseDataResp struct {
- Code int `json:"code"`
- Msg string `json:"msg"`
- }
- // OASendDbResp
- // @Description: 指标列表请求返回结构体
- type OASendDbResp struct {
- OABaseDataResp
- Data struct {
- Result bool `json:"result"`
- TaskId string `json:"taskId"`
- } `json:"data"`
- }
- // OASendDbReq
- // @Description: oa审批请求参数
- type OASendDbReq struct {
- AppTaskId string `json:"appTaskId"`
- AppPersonId string `json:"appPersonId"`
- AppPersonName string `json:"appPersonName"`
- AppTaskUrl string `json:"appTaskUrl"`
- TaskName string `json:"taskName"`
- StatusName string `json:"statusName"`
- Status int `json:"status"`
- }
- // OASendDb
- // @Description: 发送待办信息
- // @param param
- // @return resp
- // @return err
- func OASendDb(param OASendDbReq) (resp OASendDbResp, err error) {
- defer func() {
- if err != nil {
- go alarm_msg.SendAlarmMsg("发送待办信息失败;ERR:"+err.Error(), 3)
- }
- }()
- urlStr := global.CONFIG.Gn.OAHost + "/api/oa/sendDB"
- // 请求
- result, err := HttpPostOA(urlStr, param)
- if err != nil {
- return
- }
- // 解析响应结果
- err = json.Unmarshal(result, &resp)
- if err != nil {
- return
- }
- return
- }
- // HttpPostOA
- // @Description: post请求
- // @param urlPath
- // @param token
- // @param postDataParams
- // @return []byte
- // @return error
- func HttpPostOA(urlPath string, postDataParams interface{}) ([]byte, error) {
- if global.CONFIG.Gn.OAHost == `` {
- return nil, errors.New("OA平台地址为空")
- }
- // 请求地址
- postUrl := urlPath
- var err error
- postDataByte, e := json.Marshal(postDataParams)
- if e != nil {
- err = fmt.Errorf("data json marshal err: %s", e.Error())
- return []byte{}, err
- }
- postData := string(postDataByte)
- body := io.NopCloser(strings.NewReader(postData))
- client := &http.Client{}
- req, err := http.NewRequest("POST", postUrl, body)
- if err != nil {
- return nil, err
- }
- req.Header.Set("content-Type", "application/json; charset=utf-8")
- req.Header.Set("Accept-Encoding", "application/json; charset=utf-8")
- req.Header.Set("Accept", "application/json; charset=utf-8")
- resp, err := client.Do(req)
- if err != nil {
- return nil, err
- }
- defer func() {
- _ = resp.Body.Close()
- }()
- result, err := io.ReadAll(resp.Body)
- if err != nil {
- return nil, err
- }
- // 日志记录
- global.FILE_LOG.Debug("OA审批:地址:" + postUrl + ";\n请求参数:" + postData + ";\n返回参数:" + string(result))
- // 解析返回参数,判断是否是json
- if !json.Valid(result) {
- err = errors.New("返回参数不是json格式")
- return []byte{}, err
- }
- var baseResp OASendDbResp
- // 解析响应结果
- err = json.Unmarshal(result, &baseResp)
- if err != nil {
- return []byte{}, err
- }
- if baseResp.Code != 200 {
- err = errors.New(fmt.Sprintf("响应代码:%d,错误信息:%s", baseResp.Code, baseResp.Msg))
- global.LOG.Info(fmt.Sprint("post data err:", baseResp.Msg, ";url:", postUrl, ";response:", postData))
- return []byte{}, err
- }
- return result, err
- }
|