153 lines
4.0 KiB
Go
153 lines
4.0 KiB
Go
package l_msg_api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"gitea.cdlsxd.cn/self-tools/l_msg_api/cache"
|
|
"gitea.cdlsxd.cn/self-tools/l_msg_api/httpclient"
|
|
"strings"
|
|
)
|
|
|
|
type MessageCenter struct {
|
|
Host string // 消息中心地址
|
|
ClientKey string // 客户端id,获取授权token需要
|
|
ClientSecret string
|
|
header map[string]string
|
|
Base
|
|
}
|
|
|
|
func NewMessageCenter(clientKey, clientSecret, serverIndex, tempIndex string) (*MessageCenter, error) {
|
|
msg := &MessageCenter{
|
|
Host: ServerHost,
|
|
ClientKey: clientKey,
|
|
ClientSecret: clientSecret,
|
|
Base: Base{
|
|
ServerIndex: serverIndex,
|
|
TempIndex: tempIndex,
|
|
},
|
|
}
|
|
accessToken, err := msg.getAccessToken()
|
|
msg.header = map[string]string{"Authorization": accessToken, "content-type": "application/json; charset=utf-8"}
|
|
return msg, err
|
|
}
|
|
|
|
/*
|
|
获取授权token
|
|
AccessExpire 有效期内可缓存起来使用
|
|
*/
|
|
func (m *MessageCenter) getAccessToken() (string, error) {
|
|
if tokenInterface, exist := cache.InstanceCacheMap().Get(m.ClientKey); exist {
|
|
return tokenInterface.(string), nil
|
|
}
|
|
var data accessTokenResponse
|
|
url := fmt.Sprintf("%s%s", m.Host, PathAccessToken)
|
|
var authParam, _ = json.Marshal(map[string]string{"client_key": m.ClientKey, "client_secret": m.ClientSecret})
|
|
res, err := httpclient.FastHttpPost(url, map[string]string{"content-type": "application/json; charset=utf-8"}, authParam, TimeOut)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
err = json.Unmarshal(res, &data)
|
|
if data.Code != 0 {
|
|
return "", errors.New(data.Msg)
|
|
}
|
|
cache.InstanceCacheMap().Put(m.ClientKey, data.Data.AccessToken)
|
|
return data.Data.AccessToken, err
|
|
}
|
|
|
|
// 采购单OA审批
|
|
func (m *MessageCenter) OACreate(dTalkUserId, treadNo string, checkForm []formComponentValues) (data OACreatePurchaseResponse, err error) {
|
|
res, err := httpclient.FastHttpPost(
|
|
fmt.Sprintf("%s%s", m.Host, PathOaCreat),
|
|
m.header,
|
|
m.parseOACreateParam(dTalkUserId, treadNo, checkForm),
|
|
TimeOut)
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = json.Unmarshal(res, &data)
|
|
return
|
|
}
|
|
|
|
func (m *MessageCenter) parseOACreateParam(dTalkUserId, treadNo string, checkForm []formComponentValues) (out []byte) {
|
|
req := formDataRequest{
|
|
Base: m.Base,
|
|
}
|
|
req.FormBase = FormBase{
|
|
OutTradeNo: treadNo,
|
|
OriginatorUserId: dTalkUserId,
|
|
}
|
|
req.FormComponentValues = checkForm
|
|
out, _ = json.Marshal(req)
|
|
return
|
|
}
|
|
|
|
func (m *MessageCenter) OAGetDetail(outTradeNo string) (data OAGetDetailData, err error) {
|
|
param, _ := json.Marshal(oAGetDetailRequest{
|
|
Base: m.Base,
|
|
OutTradeNo: outTradeNo,
|
|
})
|
|
res, err := httpclient.FastHttpPost(fmt.Sprintf("%s%s", m.Host, PathOaGet), m.header, param, TimeOut)
|
|
if err != nil {
|
|
return
|
|
}
|
|
// 解析响应参数
|
|
body := OAGetDetailResponse{}
|
|
err = json.Unmarshal(res, &body)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if body.Code != 0 {
|
|
err = errors.New(body.Msg)
|
|
return
|
|
}
|
|
return body.Data, nil
|
|
}
|
|
|
|
// 采购单OA审批
|
|
func (m *MessageCenter) SendSms(tels []string, jsonParam string) (data SmsSendRes, err error) {
|
|
param := m.parseSmsSendParam(tels, jsonParam)
|
|
res, err := httpclient.FastHttpPost(
|
|
fmt.Sprintf("%s%s", m.Host, SendSms),
|
|
m.header,
|
|
param,
|
|
TimeOut)
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = json.Unmarshal(res, &data)
|
|
return
|
|
}
|
|
|
|
func (m *MessageCenter) parseSmsSendParam(tels []string, jsonParam string) (out []byte) {
|
|
out, _ = json.Marshal(SmsRequest{
|
|
Base: m.Base,
|
|
Tels: strings.Join(tels, ","),
|
|
Param: jsonParam,
|
|
})
|
|
return
|
|
}
|
|
|
|
func (m *MessageCenter) SendBlackBoard(title, content string, receiver BlackboardReceiverView) (data DefaultRes, err error) {
|
|
res, err := httpclient.FastHttpPost(
|
|
fmt.Sprintf("%s%s", m.Host, SendSms),
|
|
m.header,
|
|
m.parseSendBlackBoardParam(title, content, receiver),
|
|
TimeOut)
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = json.Unmarshal(res, &data)
|
|
return
|
|
}
|
|
|
|
func (m *MessageCenter) parseSendBlackBoardParam(title, content string, receiver BlackboardReceiverView) (out []byte) {
|
|
out, _ = json.Marshal(DingTalkBlackBoardSendReq{
|
|
Base: m.Base,
|
|
Content: content,
|
|
Title: title,
|
|
BlackboardReceiver: receiver,
|
|
})
|
|
return
|
|
}
|