Refactor and optimize MessageCenter, add paramset.go

This commit is contained in:
renzhiyuan 2025-03-26 17:59:15 +08:00
parent 89c9eeb4c4
commit 5c982fb266
5 changed files with 30 additions and 23 deletions

View File

@ -31,7 +31,7 @@ type (
} }
FormsData struct { FormsData struct {
formBase FormBase
FormComponentValues []FormComponentValues `json:"form_component_values"` // 审批表单数据 FormComponentValues []FormComponentValues `json:"form_component_values"` // 审批表单数据
Finance *Finance `json:"finance"` // 财务模块调用 Finance *Finance `json:"finance"` // 财务模块调用
} }
@ -64,7 +64,7 @@ type (
} }
// 创建OA基础数据 // 创建OA基础数据
formBase struct { FormBase struct {
OutTradeNo string `json:"out_trade_no"` // 流水号 OutTradeNo string `json:"out_trade_no"` // 流水号
OriginatorUserId string `json:"originator_user_id"` // 钉钉申请人id OriginatorUserId string `json:"originator_user_id"` // 钉钉申请人id
} }

View File

@ -1,8 +1,8 @@
package l_msg_api package l_msg_api
const ( const (
ServerHost = "http://127.0.0.1:8001" serverHost = "http://127.0.0.1:8001"
TimeOut = 80000000 timeOut = 60
) )
const ( const (
@ -14,9 +14,9 @@ const (
) )
const ( const (
AccessToken = "/oauth/v1/accesstoken" accessToken = "/oauth/v1/accesstoken"
OaCreat = "/msg/v1/dingtalk/oa/create" oaCreat = "/msg/v1/dingtalk/oa/create"
OaGet = "/msg/v1/dingtalk/oa/get" oaGet = "/msg/v1/dingtalk/oa/get"
OaComment = "/msg/v1/dingtalk/oa/comment" oaComment = "/msg/v1/dingtalk/oa/comment"
SendSms = "/msg/v1/sms/send" sendSms = "/msg/v1/sms/send"
) )

14
msg.go
View File

@ -31,9 +31,9 @@ func NewMessageCenter(host, clientKey, clientSecret, serverIndex, tempIndex stri
// OACreate 发起OA审批 // OACreate 发起OA审批
func (m *MessageCenter) OACreate(dTalkUserId, treadNo string, formModel *FormsData) (data OAResponse, err error) { func (m *MessageCenter) OACreate(dTalkUserId, treadNo string, formModel *FormsData) (data OAResponse, err error) {
formModel.formBase.OutTradeNo = treadNo formModel.FormBase.OutTradeNo = treadNo
formModel.formBase.OriginatorUserId = dTalkUserId formModel.FormBase.OriginatorUserId = dTalkUserId
err = m.post(OaCreat, m.parseOACreateParam(formModel), &data) err = m.post(oaCreat, m.parseOACreateParam(formModel), &data)
if err != nil { if err != nil {
return return
} }
@ -46,7 +46,7 @@ func (m *MessageCenter) OAGetDetail(outTradeNo string) (data OAGetDetailData, er
Base: m.base, Base: m.base,
OutTradeNo: outTradeNo, OutTradeNo: outTradeNo,
}) })
err = m.post(OaGet, param, &data) err = m.post(oaGet, param, &data)
if err != nil { if err != nil {
return return
} }
@ -56,7 +56,7 @@ func (m *MessageCenter) OAGetDetail(outTradeNo string) (data OAGetDetailData, er
// SendSms 短信 // SendSms 短信
func (m *MessageCenter) SendSms(tels []string, jsonParam string) (data SmsSend, err error) { func (m *MessageCenter) SendSms(tels []string, jsonParam string) (data SmsSend, err error) {
param := m.parseSmsSendParam(tels, jsonParam) param := m.parseSmsSendParam(tels, jsonParam)
err = m.post(SendSms, param, &data) err = m.post(sendSms, param, &data)
if err != nil { if err != nil {
return return
} }
@ -73,7 +73,7 @@ func (m *MessageCenter) SendBlackBoard(title, content string, deptidList []int,
receiver := blackboardReceiverView{ receiver := blackboardReceiverView{
deptidList, useridList, deptidList, useridList,
} }
err = m.post(SendSms, m.parseSendBlackBoardParam(title, content, receiver), &data) err = m.post(sendSms, m.parseSendBlackBoardParam(title, content, receiver), &data)
if err != nil { if err != nil {
return return
} }
@ -93,7 +93,7 @@ func (m *MessageCenter) OAComment(outTradeNo, text, commentUserId string, file *
} }
param, _ := json.Marshal(req) param, _ := json.Marshal(req)
err = m.post(OaComment, param, &data) err = m.post(oaComment, param, &data)
if err != nil { if err != nil {
return return
} }

View File

@ -14,7 +14,7 @@ func (m *MessageCenter) parseOACreateParam(formModel *FormsData) (out []byte) {
} }
req.FormComponentValues = formModel.FormComponentValues req.FormComponentValues = formModel.FormComponentValues
req.Finance = formModel.Finance req.Finance = formModel.Finance
req.formBase = formModel.formBase req.FormBase = formModel.FormBase
out, _ = json.Marshal(req) out, _ = json.Marshal(req)
return return
} }
@ -45,7 +45,7 @@ func (m *MessageCenter) getAccessToken() (string, error) {
var data accessTokenResponse var data accessTokenResponse
var authParam, _ = json.Marshal(map[string]string{"client_key": m.ClientKey, "client_secret": m.ClientSecret}) var authParam, _ = json.Marshal(map[string]string{"client_key": m.ClientKey, "client_secret": m.ClientSecret})
err := m.accessPost(AccessToken, authParam, &data) err := m.accessPost(accessToken, authParam, &data)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -55,7 +55,7 @@ func (m *MessageCenter) getAccessToken() (string, error) {
func (m *MessageCenter) post(path string, data []byte, resReflect interface{}) (err error) { func (m *MessageCenter) post(path string, data []byte, resReflect interface{}) (err error) {
var body responseBody var body responseBody
res, err := httpclient.FastHttpPost(fmt.Sprintf("%s%s", m.Host, path), m.header, data, TimeOut) res, err := httpclient.FastHttpPost(fmt.Sprintf("%s%s", m.Host, path), m.header, data, timeOut)
if err != nil { if err != nil {
return return
} }
@ -79,7 +79,7 @@ func (m *MessageCenter) post(path string, data []byte, resReflect interface{}) (
func (m *MessageCenter) accessPost(path string, data []byte, resReflect interface{}) (err error) { func (m *MessageCenter) accessPost(path string, data []byte, resReflect interface{}) (err error) {
var body responseBody var body responseBody
res, err := httpclient.FastHttpPost(fmt.Sprintf("%s%s", m.Host, path), m.header, data, TimeOut) res, err := httpclient.FastHttpPost(fmt.Sprintf("%s%s", m.Host, path), m.header, data, timeOut)
if err != nil { if err != nil {
return return
} }

View File

@ -1,6 +1,7 @@
package test package test
import ( import (
"context"
"fmt" "fmt"
"gitea.cdlsxd.cn/self-tools/l_msg_api" "gitea.cdlsxd.cn/self-tools/l_msg_api"
"testing" "testing"
@ -9,10 +10,11 @@ import (
const ( const (
ClientKey = "ymt" ClientKey = "ymt"
ClientSecret = "ymt" ClientSecret = "ymt"
serverHost = "http://127.0.0.1:8001"
) )
func TestSms(t *testing.T) { func TestSms(t *testing.T) {
msg, err := l_msg_api.NewMessageCenter(ClientKey, ClientSecret, "smssass", "sasssms") msg, err := l_msg_api.NewMessageCenter(serverHost, ClientKey, ClientSecret, "smssass", "sasssms")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -23,7 +25,7 @@ func TestSms(t *testing.T) {
} }
func TestOaCreate(t *testing.T) { func TestOaCreate(t *testing.T) {
msg, err := l_msg_api.NewMessageCenter(ClientKey, ClientSecret, "sw_oa", "sw_oa_purchase") msg, err := l_msg_api.NewMessageCenter(serverHost, ClientKey, ClientSecret, "sw_oa", "sw_oa_purchase")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -59,10 +61,15 @@ func TestOaCreate(t *testing.T) {
} }
func TestOaComment(t *testing.T) { func TestOaComment(t *testing.T) {
msg, err := l_msg_api.NewMessageCenter(ClientKey, ClientSecret, "sw_oa", "sw_oa_purchase") msg, err := l_msg_api.NewMessageCenter(serverHost, ClientKey, ClientSecret, "sw_oa", "sw_oa_purchase")
if err != nil { if err != nil {
panic(err) panic(err)
} }
res, err := msg.OAComment("P24110515152043136-20250318181554", "sdk测试", "", nil) res, err := msg.OAComment("P24110515152043136-20250318181554", "sdk测试", "", nil)
fmt.Println(res, err) fmt.Println(res, err)
} }
func TestContext(*testing.T) {
c := context.Background()
c.Done()
}