From a5e48469f1be312ef3fa99af3af31305ca9e1dfb Mon Sep 17 00:00:00 2001 From: ziming Date: Tue, 29 Apr 2025 13:42:27 +0800 Subject: [PATCH] order query --- cmd/server/main.go | 19 ++++- cmd/server/wire.go | 3 +- deploy.sh | 1 + internal/server/http.go | 3 + internal/service/cmb.go | 163 +++++++++++++++++----------------------- templates/index.tmpl | 11 +++ 6 files changed, 104 insertions(+), 96 deletions(-) create mode 100644 templates/index.tmpl diff --git a/cmd/server/main.go b/cmd/server/main.go index 362c44b..88559b8 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -12,6 +12,7 @@ import ( "github.com/nacos-group/nacos-sdk-go/common/constant" _ "go.uber.org/automaxprocs" "gopkg.in/yaml.v2" + "html/template" "os" "voucher/internal/conf" log2 "voucher/internal/pkg/log" @@ -125,7 +126,7 @@ func main() { businessLogger := log2.NewBusinessLogger(bc.Logs.Business, Name, Name, Version) accessLogger := log2.NewAccessLogger(bc.Logs.Access, id, Name, Version) - app, cleanup, err := wireApp(bc, businessLogger, accessLogger) + app, cleanup, err := wireApp(bc, temp(), businessLogger, accessLogger) if err != nil { panic(err) } @@ -136,3 +137,19 @@ func main() { panic(err) } } + +//var templateFS embed.FS + +func temp() *template.Template { + wd, err := os.Getwd() + if err != nil { + panic(fmt.Sprintf("获取当前工作目录失败:", err)) + } + + templatePath := fmt.Sprintf("%s/templates/index.tmpl", wd) + tmpl, err := template.ParseFiles(templatePath) + if err != nil { + panic(err) + } + return tmpl +} diff --git a/cmd/server/wire.go b/cmd/server/wire.go index f03001e..ac08eb0 100644 --- a/cmd/server/wire.go +++ b/cmd/server/wire.go @@ -10,6 +10,7 @@ import ( "github.com/go-kratos/kratos/v2/log" "github.com/google/wire" "github.com/robfig/cron" + "html/template" "voucher/internal/biz" "voucher/internal/biz/cmb" "voucher/internal/conf" @@ -23,7 +24,7 @@ import ( ) // wireApp init kratos application. -func wireApp(*conf.Bootstrap, log.Logger, *log2.AccessLogger) (*kratos.App, func(), error) { +func wireApp(*conf.Bootstrap, *template.Template, log.Logger, *log2.AccessLogger) (*kratos.App, func(), error) { panic(wire.Build( server.ProviderSetServer, service.ProviderSetService, diff --git a/deploy.sh b/deploy.sh index 49ca6a4..7689bb8 100644 --- a/deploy.sh +++ b/deploy.sh @@ -11,6 +11,7 @@ docker stop $server && docker rm $server docker run --network=merketing-network -itd --name $server --restart=always \ -p $http_port:13000 \ -v /var/www/marketing/cert:/app/cert \ + -v /var/www/marketing/templates:/app/templates \ registry.cn-chengdu.aliyuncs.com/lsxdjr/$image:"${CI_COMMIT_ID}" \ ./server -nacosIp "47.110.74.203" -nacosPort 8848 -nacosSpace "voucher" -nacosUsername "" -nacosPassword "" docker image prune -f diff --git a/internal/server/http.go b/internal/server/http.go index cba90e4..40c12b1 100644 --- a/internal/server/http.go +++ b/internal/server/http.go @@ -40,6 +40,9 @@ func NewHTTPServer( 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) + v1.RegisterCmbHTTPServer(srv, cmb) return srv diff --git a/internal/service/cmb.go b/internal/service/cmb.go index 060acb2..d8589ca 100644 --- a/internal/service/cmb.go +++ b/internal/service/cmb.go @@ -6,6 +6,7 @@ import ( "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" @@ -25,6 +26,7 @@ type CmbService struct { VoucherBiz *biz.VoucherBiz CmbMixRepo mixrepos.CmbMixRepo WechatCpnRepo wechatrepo.WechatCpnRepo + tmpl *template.Template } func NewCmbService( @@ -33,6 +35,7 @@ func NewCmbService( VoucherBiz *biz.VoucherBiz, CmbMixRepo mixrepos.CmbMixRepo, WechatCpnRepo wechatrepo.WechatCpnRepo, + tmpl *template.Template, ) *CmbService { return &CmbService{ bc: bc, @@ -40,6 +43,7 @@ func NewCmbService( VoucherBiz: VoucherBiz, CmbMixRepo: CmbMixRepo, WechatCpnRepo: WechatCpnRepo, + tmpl: tmpl, } } @@ -91,6 +95,55 @@ func (this *CmbService) QueryOrder(ctx http.Context) error { }) } +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") @@ -106,11 +159,24 @@ func (this *CmbService) QueryByOrderNo(ctx http.Context) error { return fmt.Errorf("orderNo is empty") } - order, err := this.VoucherBiz.OrderQuery(ctx.Request().Context(), orderNo) + 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") @@ -120,98 +186,7 @@ func (this *CmbService) QueryByOrderNo(ctx http.Context) error { lastUseTimeStr = order.LastUseTime.Format("2006-01-02 15:04:05") } - html := ` - - - - - - 订单查询结果 - - - -

订单查询

-

查询结果 [%s] 悉知

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
订单号%s
招行订单号%s
订单状态%s
openid%s
微信券 ID%s
商品编号%s
批次号%s
商户号%s
appId%s
订单创建时间%s
领取成功时间(成功即有该值)%s
最后一次核销时间(核销即有该值)%s
订单备注说明(失败说明)%s
- - -` - - str := fmt.Sprintf(html, - orderNo, + orderMsg := fmt.Sprintf(s, orderNo, order.OutBizNo, order.Status.GetText(), @@ -227,5 +202,5 @@ func (this *CmbService) QueryByOrderNo(ctx http.Context) error { order.Remark, ) - return ctx.String(http2.StatusOK, str) + return orderMsg, nil } diff --git a/templates/index.tmpl b/templates/index.tmpl new file mode 100644 index 0000000..1a8d870 --- /dev/null +++ b/templates/index.tmpl @@ -0,0 +1,11 @@ + + + + + {{.Title}} + + +

{{.Title}}

+

{{.Message}}

+ + \ No newline at end of file