From f0292afa9a694fd6cd472d641bb81e50f9bd6575 Mon Sep 17 00:00:00 2001 From: "qiyunfanbo126.com" <815699> Date: Thu, 29 Aug 2024 22:21:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=AF=B9=E8=B4=A6=E5=8D=95?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/console/command.go | 11 ++++++-- app/http/entities/front/Youchu.go | 4 +-- app/third/youchu/youchu_api.go | 45 ++++++++++++++++++------------- app/utils/util.go | 37 +++++++++++++++++++++++-- config/config.go | 1 + 5 files changed, 73 insertions(+), 25 deletions(-) diff --git a/app/console/command.go b/app/console/command.go index 2fa1e0c..e05243f 100644 --- a/app/console/command.go +++ b/app/console/command.go @@ -1,6 +1,7 @@ package console import ( + "fmt" "github.com/qit-team/snow-core/command" "qteam/app/constants/errorcode" "qteam/app/models/ordersmodel" @@ -46,8 +47,14 @@ func ClearUnpayOrder() { func DownLoadAccountBill() { client := youchu.NewYouChuClient(config.GetConf().YouChu) code, rs := client.AccountBillQuery() + fmt.Println(rs) if code == errorcode.Success { - fileId := rs.FileId - client.AccountBillDownload(fileId) + if len(rs.Files) > 0 { + for k, v := range rs.Files { + fileId := v["fileId"].(string) + client.AccountBillDownload(fileId, k) + } + } + } } diff --git a/app/http/entities/front/Youchu.go b/app/http/entities/front/Youchu.go index 6303cb3..2ca2816 100644 --- a/app/http/entities/front/Youchu.go +++ b/app/http/entities/front/Youchu.go @@ -126,8 +126,8 @@ type AccountBillBody struct { } type AccountBillResponse struct { - RespCode string `json:"respCode"` - FileId string `json:"fileId"` + RespCode string `json:"respCode"` + Files []map[string]interface{} `json:"files"` } type RefundRequestData struct { diff --git a/app/third/youchu/youchu_api.go b/app/third/youchu/youchu_api.go index 61197e9..8a51f17 100644 --- a/app/third/youchu/youchu_api.go +++ b/app/third/youchu/youchu_api.go @@ -4,13 +4,13 @@ import ( "encoding/base64" "encoding/json" "fmt" - "io/ioutil" "qteam/app/constants/errorcode" "qteam/app/http/entities/front" "qteam/app/models/ordersmodel" "qteam/app/utils" "qteam/app/utils/postbank" "qteam/config" + "strings" "time" ) @@ -176,13 +176,14 @@ func (this *YouChuClient) AccountBillQuery() (code int, response front.AccountBi AppID: config.GetConf().YouChu.AppID, }, Body: front.AccountBillBody{ - now, - config.GetConf().YouChu.MerchantId, + "20240529", + "100610100018971", }, } - url := this.cfg.OrderHost + config.GetConf().YouChu.MerchantId + ".htm?partnerTxSriNo=" + request.Head.PartnerTxSriNo + url := this.cfg.FileHost + config.GetConf().YouChu.MerchantId + ".htm?partnerTxSriNo=" + request.Head.PartnerTxSriNo requestData := EncryptRequest(request) bytes, err := json.Marshal(requestData) + fmt.Println(string(bytes)) if err != nil { return errorcode.SystemError, response } @@ -202,7 +203,7 @@ func (this *YouChuClient) AccountBillQuery() (code int, response front.AccountBi } // 下载对账单 -func (this *YouChuClient) AccountBillDownload(fileId string) (code int, response front.AccountBillDownloadResponse) { +func (this *YouChuClient) AccountBillDownload(fileId string, key int) (code int, response front.AccountBillDownloadResponse) { var now = time.Now().Format("20060102150405") var BusiMainId = now + utils.RandomNumber(10) request := front.AccountBillDownloadRequest{ @@ -211,44 +212,50 @@ func (this *YouChuClient) AccountBillDownload(fileId string) (code int, response AccessType: "API", ReqTime: now, Reserve: "", - Method: "ufile.query.commonQuery", + Method: "ufile.download.commonDownload", Version: "1", MerchantId: config.GetConf().YouChu.MerchantId, AppID: config.GetConf().YouChu.AppID, }, Body: front.AccountBillDownloadBody{ - config.GetConf().YouChu.MerchantId, + "100610100018971", config.GetConf().SmFourKey, fileId, }, } - url := this.cfg.OrderHost + config.GetConf().YouChu.MerchantId + ".htm?partnerTxSriNo=" + request.Head.PartnerTxSriNo + url := this.cfg.FileHost + config.GetConf().YouChu.MerchantId + ".htm?partnerTxSriNo=" + request.Head.PartnerTxSriNo requestData := EncryptRequest(request) bytes, err := json.Marshal(requestData) if err != nil { return errorcode.SystemError, response } - post, err := this.doPost(url, request.Head.PartnerTxSriNo, "ufile.query.commonQuery", bytes) + post, err := this.doPost(url, request.Head.PartnerTxSriNo, "ufile.download.commonDownload", bytes) utils.Log(nil, "ufile.query", string(post)) if err != nil { fmt.Println(err, "****************") return errorcode.YouChuOrderRefundFail, response } - responseData := DecryptResponse(string(post), false) - utils.Log(nil, "OrderQuery", responseData) - err = json.Unmarshal([]byte(responseData), &response) + //fmt.Println(string(post)) + //responseData := DecryptResponse(string(post), false) + //utils.Log(nil, "OrderQuery", responseData) + //err = json.Unmarshal([]byte(responseData), &response) + //fmt.Println(responseData) if err != nil { return errorcode.SystemError, front.AccountBillDownloadResponse{} } else { //存入文件 - bt, err := base64.StdEncoding.DecodeString(response.FileContent) + //var rs []byte + res, err := base64.StdEncoding.DecodeString(string(post)) + //fmt.Println(string(res)) if err == nil { - rs, err := utils.Sm4Decrpt(bt) + rs, err := utils.Sm4Decrpt(res) + //fmt.Println(string(rs), "dd") if err == nil { - err = ioutil.WriteFile("./bills/"+time.Now().Format(time.DateOnly)+"output.csv", rs, 0644) - if err != nil { - - } + var ori = string(rs) + var content = ori[0 : strings.Index(ori, "=")+1] + rds, _ := base64.StdEncoding.DecodeString(content) + //fmt.Println(string(rds)) + utils.DownloadBill(string(rds), key) } } else { @@ -279,7 +286,7 @@ func DecryptResponse(response string, isRequest bool) (Rsponse string) { var MerchantId, PrivateKey, PublicKey string if isRequest { MerchantId = config.GetConf().YouChuCallBack.MerchantId - PrivateKey = config.GetConf().YouChuCallBack.PrivateKey + PrivateKey = config.GetConf().Sm2.PrivateKey PublicKey = config.GetConf().YouChuCallBack.SopPublicKey } else { MerchantId = config.GetConf().YouChu.MerchantId diff --git a/app/utils/util.go b/app/utils/util.go index 622047e..0ccdc7e 100644 --- a/app/utils/util.go +++ b/app/utils/util.go @@ -6,6 +6,7 @@ import ( "crypto/rand" "crypto/sha256" "crypto/sha512" + "encoding/csv" "encoding/hex" "fmt" "github.com/ZZMarquis/gm/sm4" @@ -48,8 +49,7 @@ func GetHostIp() string { } func Log(c *gin.Context, name string, msg ...interface{}) { _, file, line, _ := runtime.Caller(1) - timeLayout := "2006-01-01 03:04:05" //转化所需模板 - var datetime = time.Unix(time.Now().Unix(), 0).Format(timeLayout) + var datetime = time.Now().Format(time.DateTime) fmt.Println(name, msg, file, line, datetime) } @@ -475,3 +475,36 @@ func Sm4Decrpt(data []byte) ([]byte, error) { data, err := sm4.CBCDecrypt([]byte(config.GetConf().SmFourKey), []byte(iv), data) return data, err } +func DownloadBill(xmlString string, key int) { + f, _ := os.Create(fmt.Sprintf("./bills/对账单%s-%d.csv", time.Now().Format(time.DateOnly), key)) + defer f.Close() + w := csv.NewWriter(f) + w.Write([]string{"交易缩写", "清算日期", "商户日期时间 ", "订单号", "平台流水号", "商户号", "终端号", "订单金额", + "手续费", "响应码", "商户保留 1", "商户保留 2", "交易渠道", "费率类型", "交易通道", "成本手续费", "账户卡类型", "商户保留 3", "交易状态"}) + + var string = xmlString[strings.Index(xmlString, "CSER"):] + data := string[0:strings.Index(string, "<")] + rows := strings.Split(data, "\n") + for _, v := range rows { + var tp = "支付" + if strings.Index(v, "SPR") > 0 { + tp = "退款" + } + v = strings.Replace(v, "CSER", "", 1) + v = strings.Replace(v, "CSPR", "", 1) + row := strings.Split(v, "|+|") + if len(row) == 19 { + row[0] = tp + row[18] = strings.Replace(row[18], "|-|", "", 1) + row[2] = "'" + row[2] + row[3] = "'" + row[3] + row[4] = "'" + row[4] + row[5] = "'" + row[5] + fmt.Println(w.Write(row)) + } else { + fmt.Println(v) + } + + } + w.Flush() +} diff --git a/config/config.go b/config/config.go index bd004fb..86d01fd 100644 --- a/config/config.go +++ b/config/config.go @@ -52,6 +52,7 @@ type YouChuConfig struct { AppID string //appid SopPublicKey string //服开公钥 SopPrivateKey string //服开私钥 + FileHost string } type YouChuCallBack struct {