diff --git a/constant/request_code.go b/constant/request_code.go index b9920a4..0267b0d 100644 --- a/constant/request_code.go +++ b/constant/request_code.go @@ -5,3 +5,9 @@ type RequestCode string const ( GetAccountBalance RequestCode = "SKBALQRY" ) + +type ResponseCode string + +const ( + Success ResponseCode = "AAAAAAA" +) diff --git a/entity/account_center.go b/entity/account_center.go index fefce81..f3c0f6b 100644 --- a/entity/account_center.go +++ b/entity/account_center.go @@ -2,14 +2,14 @@ package entity type ( GetAccountBalanceResp []struct { - AccountNo string `json:"accountNo" comment:"账号" xml:"accountNo"` - AccountName string `json:"accountName" comment:"账户名称" xml:"accountName"` - UsableBalance float64 `json:"usableBalance" comment:"可用账户余额" xml:"usableBalance"` //可操作的账户余额 - Balance float64 `json:"Balance" comment:"账号余额" xml:"Balance"` //该账户中全部余额,包含冻结金额、可操作余额等 - FraAmt float64 `json:"fraAmt" comment:"冻结余额" xml:"FraAmt"` - LastUdtTms string `json:"lastUdtTms" comment:"更新时间" xml:"LastUdtTms"` - DataSrc string `json:"dataSrc" comment:"数据来源" xml:"DataSrc"` //直联、非直联-人工等 - CurrencyID string `json:"currencyID" comment:"币种" xml:"CurrencyID"` //CNY:人民币 USD:美元 - Date string `json:"date" comment:"日期" xml:"Date"` + AccountNo string `json:"accountNo" comment:"账号" ` + AccountName string `json:"accountName" comment:"账户名称"` + UsableBalance float64 `json:"usableBalance" comment:"可用账户余额"` //可操作的账户余额 + Balance float64 `json:"Balance" comment:"账号余额"` //该账户中全部余额,包含冻结金额、可操作余额等 + FraAmt float64 `json:"fraAmt" comment:"冻结余额"` + LastUdtTms string `json:"lastUdtTms" comment:"更新时间"` + DataSrc string `json:"dataSrc" comment:"数据来源"` //直联、非直联-人工等 + CurrencyID string `json:"currencyID" comment:"币种"` //CNY:人民币 USD:美元 + Date string `json:"date" comment:"日期"` } ) diff --git a/go.mod b/go.mod index c5fe939..032999b 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module tysk +module gitea.cdlsxd.cn/self-tools/tysk go 1.23.0 @@ -34,6 +34,7 @@ require ( require ( 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/buger/jsonparser v1.1.1 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect diff --git a/pkg/func.go b/pkg/func.go index 63575b2..af94aec 100644 --- a/pkg/func.go +++ b/pkg/func.go @@ -2,12 +2,16 @@ package pkg import ( "bytes" + "encoding/json" + "encoding/xml" "fmt" + "gitea.cdlsxd.cn/self-tools/tysk/constant" + "strings" ) // XmlRequest 定义请求结构 type XmlRequest struct { - Action string `xml:"action"` + Action constant.RequestCode `xml:"action"` UserName string `xml:"userName"` ExtraFields map[string]interface{} `xml:"-"` // 忽略此字段,手动处理 } @@ -42,9 +46,39 @@ func (req XmlRequest) MarshalToXML() ([]byte, error) { } buf.WriteString("\n") // 可以根据需要添加其他类型的处理 + case []string: + // 处理列表数据 + buf.WriteString(fmt.Sprintf("\n", key)) + buf.WriteString("\n") + for _, row := range v { + + } + buf.WriteString("\n") + buf.WriteString("\n") + // 可以根据需要添加其他类型的处理 } } buf.WriteString("") 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 +} diff --git a/pkg/req_test.go b/pkg/func_test.go similarity index 100% rename from pkg/req_test.go rename to pkg/func_test.go diff --git a/pkg/request.go b/pkg/request.go deleted file mode 100644 index 202b47b..0000000 --- a/pkg/request.go +++ /dev/null @@ -1,6 +0,0 @@ -package pkg - -func HandleRequest(reqData XmlRequest, result interface{}) { - gbkXML, err := reqData.MarshalToXML() - -} diff --git a/request.go b/request.go new file mode 100644 index 0000000..fbb197e --- /dev/null +++ b/request.go @@ -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 "" + } + +} diff --git a/tysk.go b/tysk.go index 1d196a2..16b2a32 100644 --- a/tysk.go +++ b/tysk.go @@ -1,18 +1,32 @@ package tysk import ( - "tysk/entity" + "gitea.cdlsxd.cn/self-tools/tysk/constant" + "gitea.cdlsxd.cn/self-tools/tysk/entity" ) type Tysk struct { - UserName string + UserName string // 用户名 + Env string // 环境,测试:dev,不传默认正式 } -func NewTysk(UserName string) *Tysk { - return &Tysk{UserName: UserName} +func NewTysk(userName string, opts ...Option) TyskFacecade { + 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 } diff --git a/tyskFacecade.go b/tyskFacecade.go new file mode 100644 index 0000000..d7292b4 --- /dev/null +++ b/tyskFacecade.go @@ -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) +} diff --git a/tysk_test.go b/tysk_test.go new file mode 100644 index 0000000..aeae607 --- /dev/null +++ b/tysk_test.go @@ -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) +}