From 27be45b5eaa26d07de86a5f83150bf81dbf07d7c Mon Sep 17 00:00:00 2001 From: renzhiyuan <465386466@qq.com> Date: Mon, 16 Jun 2025 16:18:26 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B4=A2=E5=8A=A1=E7=B3=BB=E7=BB=9F=E6=94=AF?= =?UTF-8?q?=E8=A1=8C=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.go | 35 ++++++++++++++++++++++++ const.go | 6 +++++ go.mod | 7 +++-- msg.go | 26 ++++++++++++++++++ paramset.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ test/msg_test.go | 12 +++++++++ 6 files changed, 154 insertions(+), 2 deletions(-) diff --git a/config.go b/config.go index a451c4f..d97cd01 100644 --- a/config.go +++ b/config.go @@ -141,4 +141,39 @@ type ( FileName string `json:"fileName,optional"` //文件名称。 FileType string `json:"fileType,optional"` //文件类型。 } + + FinanceBank struct { + Keyword string `json:"keyword"` + BankName string `json:"bankName"` // 模糊查询 (bankName,branchName, branchCode, code) + Province string `json:"province"` + City string `json:"city"` + Code string `json:"code"` + IsDesc bool `json:"isDesc"` // 是否倒序查询(id) + } + + financeBankRequest struct { + Keyword string `json:"keyword"` + BankName string `json:"bankName"` // 模糊查询 (bankName,branchName, branchCode, code) + Province string `json:"province"` + City string `json:"city"` + Code string `json:"code"` + Page uint32 `json:"page"` + PageSize uint32 `json:"pageSize"` + IsDesc bool `json:"isDesc"` // 是否倒序查询(id) + } + + FinanceBankResp struct { + Banks []BankItem `json:"banks"` + Total uint32 `json:"total"` // 模糊查询 (bankName,branchName, branchCode, code) + + } + + BankItem struct { + BranchCode string `json:"branchCode"` + Province string `json:"province"` + City string `json:"city"` + BranchName string `json:"branchName"` + Code string `json:"code"` + BankName string `json:"bankName"` + } ) diff --git a/const.go b/const.go index 3134d08..cda16c1 100644 --- a/const.go +++ b/const.go @@ -53,3 +53,9 @@ const ( Http RequestWay = iota + 1 Rpc ) + +const financeUrl = "https://financeouter.22233.cn" + +const ( + financeBanks = "/outer/v1/dingtalk/payment/banks" +) diff --git a/go.mod b/go.mod index 017f9b8..6325314 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,14 @@ module gitea.cdlsxd.cn/self-tools/l_msg_api -go 1.22.2 +go 1.22.10 + +toolchain go1.23.6 require ( + gitea.cdlsxd.cn/rzy_tools/request v1.0.0 github.com/valyala/fasthttp v1.59.0 google.golang.org/grpc v1.71.1 + google.golang.org/protobuf v1.36.4 ) require ( @@ -12,7 +16,6 @@ require ( golang.org/x/sys v0.30.0 // indirect golang.org/x/text v0.22.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f // indirect - google.golang.org/protobuf v1.36.4 // indirect ) require ( diff --git a/msg.go b/msg.go index beee123..3edb670 100644 --- a/msg.go +++ b/msg.go @@ -4,6 +4,8 @@ import ( "context" "encoding/json" "errors" + "fmt" + "gitea.cdlsxd.cn/rzy_tools/request" ) type MessageCenter struct { @@ -126,3 +128,27 @@ func (m *MessageCenter) OAComment(ctx context.Context, outTradeNo, text, comment } return } + +// OAComment OA评论,CommentUserId为空则默认审核发起人评论 +func (m *MessageCenter) FinanceBankList(ctx context.Context, financeBank *FinanceBank, page uint32, pageSize uint32) (data FinanceBankResp, err error) { + + res, err := m.OtherSend(ctx, &request.Request{ + Method: "GET", + Url: fmt.Sprintf("%s%s", financeUrl, financeBanks), + Params: mapInterfaceToMapString(structToMap(financeBankRequest{ + Keyword: financeBank.Keyword, + BankName: financeBank.BankName, + Province: financeBank.Province, + City: financeBank.City, + Code: financeBank.Code, + Page: page, + PageSize: pageSize, + IsDesc: financeBank.IsDesc, + })), + }) + if err != nil { + return + } + err = json.Unmarshal(res.Content, &data) + return +} diff --git a/paramset.go b/paramset.go index 201ac58..34b1881 100644 --- a/paramset.go +++ b/paramset.go @@ -4,8 +4,10 @@ import ( "context" "encoding/json" "fmt" + "gitea.cdlsxd.cn/rzy_tools/request" "gitea.cdlsxd.cn/self-tools/l_msg_api/cache" "gitea.cdlsxd.cn/self-tools/l_msg_api/httpclient" + "reflect" "strings" "time" ) @@ -98,6 +100,11 @@ func (m *MessageCenter) post(ctx context.Context, path string, data []byte, resR return } +func (m *MessageCenter) OtherSend(ctx context.Context, request *request.Request) (res request.Response, err error) { + res, err = request.Send() + return +} + func (m *MessageCenter) accessPost(path requestPathIndex, data []byte, resReflect interface{}) (err error) { pathStr := requestPath[path][Http] var body responseBody @@ -132,3 +139,66 @@ func (m *MessageCenter) setHeader() (err error) { m.header = map[string]string{"Authorization": token, "content-type": "application/json; charset=utf-8"} return } + +// StructToMap 将一个struct转换为map[string]interface{} +func structToMap(obj interface{}) map[string]interface{} { + // 获取obj的类型 + val := reflect.ValueOf(obj) + if val.Kind() == reflect.Ptr { + val = val.Elem() + } + + // 确保obj是一个struct + if val.Kind() != reflect.Struct { + return nil + } + + // 创建一个map来保存结果 + data := make(map[string]interface{}) + + // 遍历struct的字段 + for i := 0; i < val.NumField(); i++ { + // 获取字段的类型和值 + valueField := val.Field(i) + typeField := val.Type().Field(i) + jsonTag := typeField.Tag.Get("json") + if idx := strings.Index(jsonTag, ","); idx != -1 { + // 如果有逗号,则取逗号之前的部分 + jsonTag = jsonTag[:idx] + } + // 忽略未导出的字段(字段名首字母小写) + if !typeField.IsExported() { + continue + } + + // 将字段名和值添加到map中 + data[jsonTag] = valueField.Interface() + } + + return data +} + +// MapInterfaceToMapString map[string]interface{} to map[string]string +func mapInterfaceToMapString(inputMap map[string]interface{}) map[string]string { + var mapString = make(map[string]string) + for key, value := range inputMap { + mapString[key] = fmt.Sprintf("%v", value) + } + return mapString +} + +// ChunkSlice 将一个切片分割成多个子切片,每个子切片最多包含 maxSize 个元素 +func ChunkSlice[T any](slice []T, maxSize int) [][]T { + var chunks [][]T + length := len(slice) + + for i := 0; i < length; i += maxSize { + end := i + maxSize + if end > length { + end = length + } + chunks = append(chunks, slice[i:end]) + } + + return chunks +} diff --git a/test/msg_test.go b/test/msg_test.go index 8204c08..2a89355 100644 --- a/test/msg_test.go +++ b/test/msg_test.go @@ -82,3 +82,15 @@ func TestContext(*testing.T) { c := context.Background() c.Done() } + +func TestFinanceBankList(t *testing.T) { + c := context.Background() + msg, err := l_msg_api.NewMessageCenter(serverHost, ClientKey, ClientSecret, "sw_oa", "sw_oa_purchase") + if err != nil { + panic(err) + } + res, err := msg.FinanceBankList(c, &l_msg_api.FinanceBank{ + Keyword: "招商", + }, 1, 50) + t.Log(res, err) +}