129 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Go
		
	
	
	
			
		
		
	
	
			129 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Go
		
	
	
	
| package l_msg_api
 | ||
| 
 | ||
| import (
 | ||
| 	"context"
 | ||
| 	"encoding/json"
 | ||
| 	"errors"
 | ||
| )
 | ||
| 
 | ||
| type MessageCenter struct {
 | ||
| 	Host         string // 消息中心地址
 | ||
| 	ClientKey    string // 客户端id,获取授权token需要
 | ||
| 	ClientSecret string
 | ||
| 	header       map[string]string
 | ||
| 	option       *mesOptionData
 | ||
| 	base
 | ||
| }
 | ||
| 
 | ||
| func NewMessageCenter(host, clientKey, clientSecret, serverIndex, tempIndex string, args ...MesOption) (*MessageCenter, error) {
 | ||
| 	msg := &MessageCenter{
 | ||
| 		Host:         host,
 | ||
| 		ClientKey:    clientKey,
 | ||
| 		ClientSecret: clientSecret,
 | ||
| 		base: base{
 | ||
| 			ServerIndex: serverIndex,
 | ||
| 			TempIndex:   tempIndex,
 | ||
| 		},
 | ||
| 		option: new(mesOptionData),
 | ||
| 	}
 | ||
| 	err := msg.setHeader()
 | ||
| 	if err != nil {
 | ||
| 		return nil, err
 | ||
| 	}
 | ||
| 	for _, arg := range args {
 | ||
| 		arg(msg.option)
 | ||
| 	}
 | ||
| 	return msg, err
 | ||
| }
 | ||
| 
 | ||
| // OACreate 发起OA审批
 | ||
| func (m *MessageCenter) OACreate(ctx context.Context, dTalkUserId, treadNo string, formModel *FormsData) (data OAResponse, err error) {
 | ||
| 	formModel.formBase = formBase{
 | ||
| 		OutTradeNo:       treadNo,
 | ||
| 		OriginatorUserId: dTalkUserId,
 | ||
| 	}
 | ||
| 	err = m.send(ctx, oaCreat, m.parseOACreateParam(formModel), &data)
 | ||
| 	if err != nil {
 | ||
| 		return
 | ||
| 	}
 | ||
| 	return
 | ||
| }
 | ||
| 
 | ||
| // OAGetDetail OA详情
 | ||
| func (m *MessageCenter) OAGetDetail(ctx context.Context, outTradeNo string) (data OAGetDetailData, err error) {
 | ||
| 	param, _ := json.Marshal(oAGetDetailRequest{
 | ||
| 		Base:       m.base,
 | ||
| 		OutTradeNo: outTradeNo,
 | ||
| 	})
 | ||
| 	err = m.send(ctx, oaGet, param, &data)
 | ||
| 	if err != nil {
 | ||
| 		return
 | ||
| 	}
 | ||
| 	return
 | ||
| }
 | ||
| 
 | ||
| // SendSms 短信
 | ||
| // business  SmsBusiness
 | ||
| func (m *MessageCenter) SendSms(ctx context.Context, tels []string, jsonParam string, args ...SmsOption) (data SmsSend, err error) {
 | ||
| 	var (
 | ||
| 		e = new(smsOptionData)
 | ||
| 	)
 | ||
| 	if len(tels) == 0 {
 | ||
| 		err = errors.New("手机号不能为空")
 | ||
| 		return
 | ||
| 	}
 | ||
| 	for _, arg := range args {
 | ||
| 		arg(e)
 | ||
| 	}
 | ||
| 	if e.Business != "" {
 | ||
| 		if _, ex := smsBusinessWithRequestPath[e.Business]; !ex {
 | ||
| 			err = errors.New("business参数错误")
 | ||
| 			return
 | ||
| 		}
 | ||
| 	} else {
 | ||
| 		e.Business = SmsBusinessDefault
 | ||
| 	}
 | ||
| 	path := smsBusinessWithRequestPath[e.Business]
 | ||
| 
 | ||
| 	param := m.parseSmsSendParam(tels, jsonParam)
 | ||
| 	err = m.send(ctx, path, param, &data)
 | ||
| 	if err != nil {
 | ||
| 		return
 | ||
| 	}
 | ||
| 	return
 | ||
| }
 | ||
| 
 | ||
| // SendBlackBoard 钉钉公告
 | ||
| // deptidList  //接收部门ID列表,最大的列表长度为20。
 | ||
| // UseridList  //接收部用户ID列表,最大的列表长度为20。
 | ||
| func (m *MessageCenter) SendBlackBoard(ctx context.Context, title, content string, deptidList []int, useridList []string) (data Default, err error) {
 | ||
| 	receiver := blackboardReceiverView{
 | ||
| 		deptidList, useridList,
 | ||
| 	}
 | ||
| 	err = m.send(ctx, sendSms, m.parseSendBlackBoardParam(title, content, receiver), &data)
 | ||
| 	if err != nil {
 | ||
| 		return
 | ||
| 	}
 | ||
| 	return
 | ||
| }
 | ||
| 
 | ||
| // OAComment OA评论,CommentUserId为空则默认审核发起人评论
 | ||
| func (m *MessageCenter) OAComment(ctx context.Context, outTradeNo, text, commentUserId string, file *DingOACommentReqFile) (data OAResponse, err error) {
 | ||
| 	req := &oACommentRequest{
 | ||
| 		Base:          m.base,
 | ||
| 		OutTradeNo:    outTradeNo,
 | ||
| 		Text:          text,
 | ||
| 		CommentUserId: commentUserId,
 | ||
| 	}
 | ||
| 	if file != nil {
 | ||
| 		req.File = *file
 | ||
| 	}
 | ||
| 	param, _ := json.Marshal(req)
 | ||
| 
 | ||
| 	err = m.send(ctx, oaComment, param, &data)
 | ||
| 	if err != nil {
 | ||
| 		return
 | ||
| 	}
 | ||
| 	return
 | ||
| }
 |