From d01900b519585879e7a5666d6c5bc75ddef0d41e Mon Sep 17 00:00:00 2001 From: ziming Date: Thu, 11 Dec 2025 18:13:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E7=AC=94=E7=AB=8B=E5=87=8F=E9=87=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- configs/config_test.yaml | 4 +++ internal/biz/bo/qixing.go | 19 +++++++++-- internal/pkg/helper/utils_test.go | 34 +++++++++++++++++-- internal/service/qixing.go | 14 +++++++- test/bank_multi_activity_test.go | 56 ++++++++++++++++++++++++++++++- 5 files changed, 120 insertions(+), 7 deletions(-) diff --git a/configs/config_test.yaml b/configs/config_test.yaml index 1a61af0..dbc15ab 100644 --- a/configs/config_test.yaml +++ b/configs/config_test.yaml @@ -118,6 +118,10 @@ aliYunSms: signName: 蓝色兄弟 templateWarning: "SMS_489660721" +tripartite: + qiXing: + appKey: "DrY1zLkOH01q0sN66vrmkdpbWsyb41ho" + #配置日志 logs: business: business.log #业务日志路径:如果不写日志,则不配置或配置为空 diff --git a/internal/biz/bo/qixing.go b/internal/biz/bo/qixing.go index 2d0dd61..6eaf2da 100644 --- a/internal/biz/bo/qixing.go +++ b/internal/biz/bo/qixing.go @@ -1,12 +1,25 @@ package bo +import "github.com/go-playground/validator/v10" + type QiXingRequestBo struct { - Content string `json:"content"` - Timestamp int64 `json:"timestamp"` - Ciphertext string `json:"ciphertext"` + Content string `json:"content" validate:"required"` + Timestamp int64 `json:"timestamp" validate:"required"` + Ciphertext string `json:"ciphertext" validate:"required"` } // QiXingResponse 响应结构体 {"msg":"SUCCESS"} / {"msg":"操作成功"} type QiXingResponse struct { Msg string `json:"msg"` } + +func (c *QiXingRequestBo) Validate() error { + + if err := validator.New().Struct(c); err != nil { + for _, err = range err.(validator.ValidationErrors) { + return err + } + } + + return nil +} diff --git a/internal/pkg/helper/utils_test.go b/internal/pkg/helper/utils_test.go index 04142a2..647fe9f 100644 --- a/internal/pkg/helper/utils_test.go +++ b/internal/pkg/helper/utils_test.go @@ -45,8 +45,38 @@ func queryUsed(useNum *int) { } func TestMd5(t *testing.T) { - s := Md5(`{"product_no":"","start_time":"2025-04-20 09:00:00","end_time":"2025-05-01 00:00:00"}`) - t.Log(s) + + jsonStr := `{ + "id": "4ab2699d-e91d-5460-9810-25fd6d4c69a5", + "create_time": "2025-12-08T17:54:24+08:00", + "resource_type": "encrypt-resource", + "event_type": "COUPON.USE", + "summary": "代金券核销通知", + "original_type": "coupon", + "associated_data": "coupon", + "plain_text": { + "stock_creator_mchid": "1652465541", + "stock_id": "21386484", + "coupon_id": "142388354994", + "coupon_name": "银行卡多笔立减", + "description": "", + "status": "SENDED", + "create_time": "2025-12-08T17:50:48+08:00", + "coupon_type": "NORMAL", + "no_cash": false, + "singleitem": false, + "business_type": "", + "consume_information": { + "consume_time": "2025-12-08T17:54:24+08:00", + "consume_mchid": "1274938601", + "transaction_id": "4200002996202512083063051834", + "consume_amount": 16 + } + } +}` + + ciphertext := Md5(jsonStr) + t.Log(ciphertext) } func TestLength(t *testing.T) { diff --git a/internal/service/qixing.go b/internal/service/qixing.go index b1d14a2..4518be1 100644 --- a/internal/service/qixing.go +++ b/internal/service/qixing.go @@ -1,6 +1,7 @@ package service import ( + "encoding/base64" "encoding/json" "fmt" "github.com/go-kratos/kratos/v2/log" @@ -40,6 +41,11 @@ func (srv *TripartiteService) QiXingNotify(ctx http.Context) error { return srv.ResponseErr(ctx, fmt.Sprintf("json unmarshal bodyBytes error: %v", err)) } + if err = req.Validate(); err != nil { + log.Errorf("qixing notify req validate err ip:%s,error:%v,body:%s", ip, err, string(bodyBytes)) + return srv.ResponseErr(ctx, fmt.Sprintf("validate req error: %v", err)) + } + // 加密校验串,(生成规则:MD5(content+公钥)) if srv.bc.Tripartite.QiXing.AppKey == "" { return srv.ResponseErr(ctx, "qixing appKey is empty") @@ -51,8 +57,14 @@ func (srv *TripartiteService) QiXingNotify(ctx http.Context) error { return srv.ResponseErr(ctx, "sign error") } + wxNotifyDataStr, err := base64.StdEncoding.DecodeString(req.Content) + if err != nil { + log.Errorf("qixing notify wxNotifyDataStr err ip:%s,error:%v,body:%s", ip, err, string(bodyBytes)) + return srv.ResponseErr(ctx, fmt.Sprintf("base64 decode req content error: %v", err)) + } + var wxNotifyData *bo.WechatVoucherNotifyBo - if err = json.Unmarshal([]byte(req.Content), &wxNotifyData); err != nil { + if err = json.Unmarshal(wxNotifyDataStr, &wxNotifyData); err != nil { log.Errorf("qixing notify wxNotifyData err ip:%s,error:%v,body:%s", ip, err, string(bodyBytes)) return srv.ResponseErr(ctx, fmt.Sprintf("json unmarshal wxNotifyData error: %v", err)) } diff --git a/test/bank_multi_activity_test.go b/test/bank_multi_activity_test.go index e6963fc..2e3f446 100644 --- a/test/bank_multi_activity_test.go +++ b/test/bank_multi_activity_test.go @@ -1,6 +1,13 @@ package test -import "testing" +import ( + "encoding/base64" + "encoding/json" + "testing" + "time" + "voucher/internal/biz/bo" + "voucher/internal/pkg/helper" +) func Test_MarketingSend(t *testing.T) { tests := []struct { @@ -47,3 +54,50 @@ func Test_MarketingQuery(t *testing.T) { //openid不是自己的appid下的喔,这也能查询到吗” //不行的,需要是在自己appid下的才能查到 } + +func Test_QixingNotifyData(t *testing.T) { + + wxBody := `{ + "id": "4ab2699d-e91d-5460-9810-25fd6d4c69a5", + "create_time": "2025-12-08T17:54:24+08:00", + "resource_type": "encrypt-resource", + "event_type": "COUPON.USE", + "summary": "代金券核销通知", + "original_type": "coupon", + "associated_data": "coupon", + "plain_text": { + "stock_creator_mchid": "1652465541", + "stock_id": "21386484", + "coupon_id": "142388354994", + "coupon_name": "银行卡多笔立减", + "description": "", + "status": "SENDED", + "create_time": "2025-12-08T17:50:48+08:00", + "coupon_type": "NORMAL", + "no_cash": false, + "singleitem": false, + "business_type": "", + "consume_information": { + "consume_time": "2025-12-08T17:54:24+08:00", + "consume_mchid": "1274938601", + "transaction_id": "4200002996202512083063051834", + "consume_amount": 16 + } + } +}` + content := base64.StdEncoding.EncodeToString([]byte(wxBody)) + + ciphertext := helper.Md5(content + "DrY1zLkOH01q0sN66vrmkdpbWsyb41ho") + + req := bo.QiXingRequestBo{ + Content: content, + Timestamp: time.Now().UnixMilli(), + Ciphertext: ciphertext, + } + + b, _ := json.Marshal(req) + + t.Log(string(b)) + // {"content":"base64(微信通知json对象数据)","timestamp":1765447477945,"ciphertext":"md5(base64(微信通知json对象数据)+key)"} + //t.Logf(`{"content":"%s","timestamp":122345677890,"ciphertext":"%s"}`, content, ciphertext) +}