From b20fa8ad357f664be092d318aa4196eb4b97db1b Mon Sep 17 00:00:00 2001 From: ziming Date: Tue, 29 Apr 2025 15:54:29 +0800 Subject: [PATCH] order query --- internal/server/http.go | 4 +- internal/service/cmb.go | 145 ----------------------------------- internal/service/cmb_mock.go | 102 +++++++++++++++++++++++- templates/index.tmpl | 55 ++++++++++++- 4 files changed, 155 insertions(+), 151 deletions(-) diff --git a/internal/server/http.go b/internal/server/http.go index 40c12b1..6aed79e 100644 --- a/internal/server/http.go +++ b/internal/server/http.go @@ -38,10 +38,8 @@ func NewHTTPServer( srv.Route("/voucher/").GET("notifyRetry/{id}", cmb.NotifyRetry) srv.Route("/voucher/").GET("queryOrder/{order_no}", cmb.QueryOrder) - srv.Route("/voucher/").GET("queryByOrderNo/{order_no}", cmb.QueryByOrderNo) - // 注册处理函数 - srv.HandleFunc("/voucher/queryByOrderNo2/{order_no}", cmb.QueryByOrderNo2func) + srv.HandleFunc("/voucher/queryByOrderNo/{order_no}", cmb.QueryByOrderNo) v1.RegisterCmbHTTPServer(srv, cmb) diff --git a/internal/service/cmb.go b/internal/service/cmb.go index d8589ca..e037566 100644 --- a/internal/service/cmb.go +++ b/internal/service/cmb.go @@ -2,13 +2,9 @@ package service import ( "context" - "fmt" "github.com/go-kratos/kratos/v2/log" - "github.com/go-kratos/kratos/v2/transport/http" "github.com/robfig/cron" "html/template" - http2 "net/http" - "strconv" v1 "voucher/api/v1" "voucher/internal/biz" "voucher/internal/biz/bo" @@ -63,144 +59,3 @@ func (c *CmbService) GetResponse(ctx context.Context, replyBizContent []byte) (* return reply, nil } - -func (this *CmbService) NotifyRetry(ctx http.Context) error { - id := ctx.Vars().Get("id") - if id == "" { - return fmt.Errorf("id is empty") - } - - orderNotifyId, err := strconv.ParseUint(id, 10, 64) - if err != nil { - return err - } - - return this.VoucherBiz.PushNotifyRetryDelayMQ(ctx, 1, orderNotifyId) -} - -func (this *CmbService) QueryOrder(ctx http.Context) error { - - orderNo := ctx.Vars().Get("order_no") - if orderNo == "" { - return fmt.Errorf("orderNo is empty") - } - - str, err := this.VoucherBiz.QueryOrder(ctx, orderNo) - if err != nil { - return err - } - - return ctx.JSON(http2.StatusOK, map[string]interface{}{ - "data": str, - }) -} - -func (this *CmbService) QueryByOrderNo2func(w http.ResponseWriter, r *http.Request) { - - // 定义要传递给模板的数据 - data := struct { - Title string - Message string - }{ - Title: "欢迎使用", - Message: "", - } - - orderNo, err := this.QueryByOrderNoFunc(r) - if err != nil { - data.Message = err.Error() - } else { - str, err2 := this.queryOrder(r.Context(), orderNo) - if err2 != nil { - data.Message = err2.Error() - } else { - data.Message = str - } - } - - // 执行模板并将结果写入响应 - err = this.tmpl.Execute(w, data) - if err != nil { - http2.Error(w, err.Error(), http2.StatusInternalServerError) - return - } -} - -func (this *CmbService) QueryByOrderNoFunc(r *http.Request) (string, error) { - - ip := r.Header.Get("X-Forwarded-For") - if len(ip) == 0 { - ip = r.RemoteAddr - } - if ip != "117.175.169.61" && ip != "127.0.0.1" { - return "", fmt.Errorf("ip check fail,IP:%s", ip) - } - - orderNo := r.URL.Query().Get("order_no") - if orderNo == "" { - return "", fmt.Errorf("order is nil") - } - - return orderNo, nil -} - -func (this *CmbService) QueryByOrderNo(ctx http.Context) error { - // 获取访问 ip - ip := ctx.Request().Header.Get("X-Forwarded-For") - if len(ip) == 0 { - ip = ctx.Request().RemoteAddr - } - if ip != "117.175.169.61" && ip != "127.0.0.1" { - return fmt.Errorf("ip check fail,IP:%s", ip) - } - - orderNo := ctx.Vars().Get("order_no") - if orderNo == "" { - return fmt.Errorf("orderNo is empty") - } - - str, err := this.queryOrder(ctx, orderNo) - if err != nil { - return err - } - - return ctx.String(http2.StatusOK, str) -} - -func (this *CmbService) queryOrder(ctx context.Context, orderNo string) (string, error) { - - order, err := this.VoucherBiz.OrderQuery(ctx, orderNo) - if err != nil { - return "", err - } - - s := "\n订单号:%s\n招行订单号:%s\n订单状态:%s\nopenid:%s\n微信券ID:%s\n商品编号:%s\n批次号:%s\n商户号:%s\nappId:%s\n订单创建时间:%s\n" + - "领取成功时间(成功即有该值):%s\n最后一次核销时间(核销即有该值):%s\n订单备注说明(失败说明):%s\n" - - receiveSuccessTimeStr := "" - if order.ReceiveSuccessTime != nil { - receiveSuccessTimeStr = order.ReceiveSuccessTime.Format("2006-01-02 15:04:05") - } - lastUseTimeStr := "" - if order.LastUseTime != nil { - lastUseTimeStr = order.LastUseTime.Format("2006-01-02 15:04:05") - } - - orderMsg := fmt.Sprintf(s, - orderNo, - order.OutBizNo, - order.Status.GetText(), - order.Account, - order.VoucherNo, - order.ProductNo, - order.BatchNo, - order.MerchantNo, - order.AppID, - order.CreateTime.Format("2006-01-02 15:04:05"), - receiveSuccessTimeStr, - lastUseTimeStr, - order.Remark, - ) - - return orderMsg, nil -} diff --git a/internal/service/cmb_mock.go b/internal/service/cmb_mock.go index 549e4e0..cb45686 100644 --- a/internal/service/cmb_mock.go +++ b/internal/service/cmb_mock.go @@ -3,7 +3,13 @@ package service import ( "context" "encoding/json" + "fmt" + "github.com/go-kratos/kratos/v2/transport/http" + http2 "net/http" + "strconv" + "strings" v1 "voucher/api/v1" + "voucher/internal/biz/bo" ) func (s *CmbService) OrderMock(ctx context.Context, request *v1.CmbOrderRequest) (*v1.CmbRequest, error) { @@ -63,7 +69,7 @@ func (s *CmbService) DecryptBody(ctx context.Context, request *v1.EncryptBodyReq }, nil } -func (s *CmbService) Test(ctx context.Context, request *v1.Empty) (*v1.Empty, error) { +func (s *CmbService) Test(ctx context.Context, _ *v1.Empty) (*v1.Empty, error) { if err := s.VoucherBiz.ExecuteNotice(ctx); err != nil { return nil, err @@ -71,3 +77,97 @@ func (s *CmbService) Test(ctx context.Context, request *v1.Empty) (*v1.Empty, er return nil, nil } + +func (this *CmbService) NotifyRetry(ctx http.Context) error { + + id := ctx.Vars().Get("id") + if id == "" { + return fmt.Errorf("id is empty") + } + + orderNotifyId, err := strconv.ParseUint(id, 10, 64) + if err != nil { + return err + } + + return this.VoucherBiz.PushNotifyRetryDelayMQ(ctx, 1, orderNotifyId) +} + +func (this *CmbService) QueryOrder(ctx http.Context) error { + + orderNo := ctx.Vars().Get("order_no") + if orderNo == "" { + return fmt.Errorf("orderNo is empty") + } + + str, err := this.VoucherBiz.QueryOrder(ctx, orderNo) + if err != nil { + return err + } + + return ctx.JSON(http2.StatusOK, map[string]string{"data": str}) +} + +func (this *CmbService) QueryByOrderNo(w http.ResponseWriter, r *http.Request) { + + data := struct { + Title string + ErrMsg string + Order *bo.OrderBo + }{ + Title: "招行立减金订单查询", + ErrMsg: "暂无数据", + Order: nil, + } + + order, err := this.queryOrder(r) + if err != nil { + data.ErrMsg = err.Error() + } else { + data.Order = order + } + + if err = this.tmpl.Execute(w, data); err != nil { + http2.Error(w, err.Error(), http2.StatusInternalServerError) + } + return +} + +func (this *CmbService) queryOrder(r *http.Request) (*bo.OrderBo, error) { + + orderNo, err := this.queryByOrderNo(r) + if err != nil { + return nil, err + } + + order, err := this.VoucherBiz.OrderQuery(r.Context(), orderNo) + if err != nil { + return nil, err + } + + return order, nil +} + +func (this *CmbService) queryByOrderNo(r *http.Request) (string, error) { + + //ip := r.Header.Get("X-Forwarded-For") + //if len(ip) == 0 { + // ip = r.RemoteAddr + //} + //if ip != "117.175.169.61" && ip != "127.0.0.1" { + // return "", fmt.Errorf("ip check fail,IP:%s", ip) + //} + + path := r.URL.Path + parts := strings.Split(path, "/") + if len(parts) == 0 { + return "", fmt.Errorf("发起请求格式有误:%s", path) + } + + orderNo := parts[len(parts)-1] + if len(orderNo) == 0 { + return "", fmt.Errorf("order is nil") + } + + return orderNo, nil +} diff --git a/templates/index.tmpl b/templates/index.tmpl index 1a8d870..f9adbf2 100644 --- a/templates/index.tmpl +++ b/templates/index.tmpl @@ -3,9 +3,60 @@ {{.Title}} + -

{{.Title}}

-

{{.Message}}

+ +
+        {{if.Order}}
+            订单号: {{.Order.OrderNo}}
+            招行订单号: {{.Order.OutBizNo}}
+            订单状态: {{.Order.Status.GetText}}
+            openid: {{.Order.Account}}
+            微信券ID: {{.Order.VoucherNo}}
+            商品编号: {{.Order.ProductNo}}
+            批次号: {{.Order.BatchNo}}
+            商户号: {{.Order.MerchantNo}}
+            appId: {{.Order.AppID}}
+            订单创建时间: {{.Order.CreateTime}}
+            领取成功时间(成功即有该值): {{.Order.ReceiveSuccessTime}}
+            最后一次核销时间(核销即有该值): {{.Order.LastUseTime}}
+            订单备注说明(失败说明): {{.Order.Remark}}
+        {{else}}
+            

{{.ErrMsg}}

+ {{end}} +
+ \ No newline at end of file