This commit is contained in:
parent
c7f95cb80b
commit
9e7ce5ff72
|
@ -5,3 +5,9 @@ type RequestCode string
|
||||||
const (
|
const (
|
||||||
GetAccountBalance RequestCode = "SKBALQRY"
|
GetAccountBalance RequestCode = "SKBALQRY"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ResponseCode string
|
||||||
|
|
||||||
|
const (
|
||||||
|
Success ResponseCode = "AAAAAAA"
|
||||||
|
)
|
||||||
|
|
|
@ -2,14 +2,14 @@ package entity
|
||||||
|
|
||||||
type (
|
type (
|
||||||
GetAccountBalanceResp []struct {
|
GetAccountBalanceResp []struct {
|
||||||
AccountNo string `json:"accountNo" comment:"账号" xml:"accountNo"`
|
AccountNo string `json:"accountNo" comment:"账号" `
|
||||||
AccountName string `json:"accountName" comment:"账户名称" xml:"accountName"`
|
AccountName string `json:"accountName" comment:"账户名称"`
|
||||||
UsableBalance float64 `json:"usableBalance" comment:"可用账户余额" xml:"usableBalance"` //可操作的账户余额
|
UsableBalance float64 `json:"usableBalance" comment:"可用账户余额"` //可操作的账户余额
|
||||||
Balance float64 `json:"Balance" comment:"账号余额" xml:"Balance"` //该账户中全部余额,包含冻结金额、可操作余额等
|
Balance float64 `json:"Balance" comment:"账号余额"` //该账户中全部余额,包含冻结金额、可操作余额等
|
||||||
FraAmt float64 `json:"fraAmt" comment:"冻结余额" xml:"FraAmt"`
|
FraAmt float64 `json:"fraAmt" comment:"冻结余额"`
|
||||||
LastUdtTms string `json:"lastUdtTms" comment:"更新时间" xml:"LastUdtTms"`
|
LastUdtTms string `json:"lastUdtTms" comment:"更新时间"`
|
||||||
DataSrc string `json:"dataSrc" comment:"数据来源" xml:"DataSrc"` //直联、非直联-人工等
|
DataSrc string `json:"dataSrc" comment:"数据来源"` //直联、非直联-人工等
|
||||||
CurrencyID string `json:"currencyID" comment:"币种" xml:"CurrencyID"` //CNY:人民币 USD:美元
|
CurrencyID string `json:"currencyID" comment:"币种"` //CNY:人民币 USD:美元
|
||||||
Date string `json:"date" comment:"日期" xml:"Date"`
|
Date string `json:"date" comment:"日期"`
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
3
go.mod
3
go.mod
|
@ -1,4 +1,4 @@
|
||||||
module tysk
|
module gitea.cdlsxd.cn/self-tools/tysk
|
||||||
|
|
||||||
go 1.23.0
|
go 1.23.0
|
||||||
|
|
||||||
|
@ -34,6 +34,7 @@ require (
|
||||||
|
|
||||||
require (
|
require (
|
||||||
dario.cat/mergo v1.0.0 // indirect
|
dario.cat/mergo v1.0.0 // indirect
|
||||||
|
gitea.cdlsxd.cn/self-tools/l_request v1.0.7 // indirect
|
||||||
github.com/aliyun/alibaba-cloud-sdk-go v1.61.18 // indirect
|
github.com/aliyun/alibaba-cloud-sdk-go v1.61.18 // indirect
|
||||||
github.com/buger/jsonparser v1.1.1 // indirect
|
github.com/buger/jsonparser v1.1.1 // indirect
|
||||||
github.com/fsnotify/fsnotify v1.6.0 // indirect
|
github.com/fsnotify/fsnotify v1.6.0 // indirect
|
||||||
|
|
36
pkg/func.go
36
pkg/func.go
|
@ -2,12 +2,16 @@ package pkg
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"encoding/xml"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"gitea.cdlsxd.cn/self-tools/tysk/constant"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// XmlRequest 定义请求结构
|
// XmlRequest 定义请求结构
|
||||||
type XmlRequest struct {
|
type XmlRequest struct {
|
||||||
Action string `xml:"action"`
|
Action constant.RequestCode `xml:"action"`
|
||||||
UserName string `xml:"userName"`
|
UserName string `xml:"userName"`
|
||||||
ExtraFields map[string]interface{} `xml:"-"` // 忽略此字段,手动处理
|
ExtraFields map[string]interface{} `xml:"-"` // 忽略此字段,手动处理
|
||||||
}
|
}
|
||||||
|
@ -42,9 +46,39 @@ func (req XmlRequest) MarshalToXML() ([]byte, error) {
|
||||||
}
|
}
|
||||||
buf.WriteString("</list>\n")
|
buf.WriteString("</list>\n")
|
||||||
// 可以根据需要添加其他类型的处理
|
// 可以根据需要添加其他类型的处理
|
||||||
|
case []string:
|
||||||
|
// 处理列表数据
|
||||||
|
buf.WriteString(fmt.Sprintf("<list name=\"%s\">\n", key))
|
||||||
|
buf.WriteString("<row>\n")
|
||||||
|
for _, row := range v {
|
||||||
|
|
||||||
|
}
|
||||||
|
buf.WriteString("</row>\n")
|
||||||
|
buf.WriteString("</list>\n")
|
||||||
|
// 可以根据需要添加其他类型的处理
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
buf.WriteString("</stream>")
|
buf.WriteString("</stream>")
|
||||||
return buf.Bytes(), nil
|
return buf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func XMLToByte(xmlData string) ([]byte, error) {
|
||||||
|
// 1. 定义一个通用接口来存储解析后的XML
|
||||||
|
var data interface{}
|
||||||
|
|
||||||
|
// 2. 使用xml.Unmarshal解析XML
|
||||||
|
decoder := xml.NewDecoder(strings.NewReader(xmlData))
|
||||||
|
err := decoder.Decode(&data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("xml decode error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 将解析后的数据转换为JSON
|
||||||
|
jsonData, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("json encode error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return jsonData, nil
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +0,0 @@
|
||||||
package pkg
|
|
||||||
|
|
||||||
func HandleRequest(reqData XmlRequest, result interface{}) {
|
|
||||||
gbkXML, err := reqData.MarshalToXML()
|
|
||||||
|
|
||||||
}
|
|
|
@ -0,0 +1,55 @@
|
||||||
|
package tysk
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"gitea.cdlsxd.cn/self-tools/l_request"
|
||||||
|
"gitea.cdlsxd.cn/self-tools/tysk/constant"
|
||||||
|
"gitea.cdlsxd.cn/self-tools/tysk/entity"
|
||||||
|
"gitea.cdlsxd.cn/self-tools/tysk/pkg"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (g *Tysk) handleRequest(Action constant.RequestCode, requestData map[string]interface{}, result interface{}) (err error) {
|
||||||
|
reqData := pkg.XmlRequest{
|
||||||
|
Action: Action,
|
||||||
|
UserName: g.UserName,
|
||||||
|
ExtraFields: requestData,
|
||||||
|
}
|
||||||
|
gbkXML, err := reqData.MarshalToXML()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
req := l_request.Request{
|
||||||
|
Method: "POST",
|
||||||
|
Url: g.getRequestUrl(),
|
||||||
|
Xml: gbkXML,
|
||||||
|
}
|
||||||
|
response, err := req.Send()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
resByte, err := pkg.XMLToByte(response.Text)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var commonResponse entity.RespCommon
|
||||||
|
err = json.Unmarshal(resByte, &commonResponse)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if commonResponse.Status != string(constant.Success) {
|
||||||
|
return fmt.Errorf("请求失败,错误码:%s,错误信息:%s", commonResponse.Status, commonResponse.FailReason)
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.Unmarshal(resByte, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Tysk) getRequestUrl() string {
|
||||||
|
switch g.Env {
|
||||||
|
case "dev":
|
||||||
|
return "http://127.0.0.1:6789"
|
||||||
|
default:
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
24
tysk.go
24
tysk.go
|
@ -1,18 +1,32 @@
|
||||||
package tysk
|
package tysk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"tysk/entity"
|
"gitea.cdlsxd.cn/self-tools/tysk/constant"
|
||||||
|
"gitea.cdlsxd.cn/self-tools/tysk/entity"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Tysk struct {
|
type Tysk struct {
|
||||||
UserName string
|
UserName string // 用户名
|
||||||
|
Env string // 环境,测试:dev,不传默认正式
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTysk(UserName string) *Tysk {
|
func NewTysk(userName string, opts ...Option) TyskFacecade {
|
||||||
return &Tysk{UserName: UserName}
|
tysk := &Tysk{UserName: userName}
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(tysk) // 应用选项
|
||||||
|
}
|
||||||
|
return tysk
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAccountBalance(accountList []string) (res entity.GetAccountBalanceResp, err error) {
|
type Option func(*Tysk)
|
||||||
|
|
||||||
|
func WithEnvTest() Option {
|
||||||
|
return func(tysk *Tysk) {
|
||||||
|
tysk.Env = "dev"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Tysk) GetAccountBalance(accountList []string) (res entity.GetAccountBalanceResp, err error) {
|
||||||
|
err = g.handleRequest(constant.GetAccountBalance, map[string]interface{}{"accountNo": accountList}, &res)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
package tysk
|
||||||
|
|
||||||
|
import "gitea.cdlsxd.cn/self-tools/tysk/entity"
|
||||||
|
|
||||||
|
type TyskFacecade interface {
|
||||||
|
// GetAccountBalance 账户余额查询
|
||||||
|
GetAccountBalance(accountList []string) (res entity.GetAccountBalanceResp, err error)
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
package tysk
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
const UserName = "LSXDWL003_ZL"
|
||||||
|
|
||||||
|
var g = NewTysk(UserName, WithEnvTest())
|
||||||
|
|
||||||
|
func Test_GetAccountBalance(t *testing.T) {
|
||||||
|
res, err := g.GetAccountBalance([]string{"8110701013301269598", "8110701012401269599", "8110701013801269600"})
|
||||||
|
t.Log(res, err)
|
||||||
|
}
|
Loading…
Reference in New Issue