order query

This commit is contained in:
ziming 2025-04-29 15:54:29 +08:00
parent a5e48469f1
commit b20fa8ad35
4 changed files with 155 additions and 151 deletions

View File

@ -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)

View File

@ -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
}

View File

@ -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
}

View File

@ -3,9 +3,60 @@
<head>
<meta charset="UTF-8">
<title>{{.Title}}</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
h1 {
color: #FF6600; /* 设置为橙色可替换为其他颜色代码 */
text-align: center;
margin-bottom: 30px;
}
pre {
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
white-space: pre-line;
line-height: 1.6;
font-size: 16px;
}
pre strong {
color: #007BFF;
}
</style>
</head>
<body>
<h1>{{.Title}}</h1>
<p>{{.Message}}</p>
<pre>
{{if.Order}}
<strong>订单号:</strong> {{.Order.OrderNo}}
<strong>招行订单号:</strong> {{.Order.OutBizNo}}
<strong>订单状态:</strong> {{.Order.Status.GetText}}
<strong>openid:</strong> {{.Order.Account}}
<strong>微信券ID:</strong> {{.Order.VoucherNo}}
<strong>商品编号:</strong> {{.Order.ProductNo}}
<strong>批次号:</strong> {{.Order.BatchNo}}
<strong>商户号:</strong> {{.Order.MerchantNo}}
<strong>appId:</strong> {{.Order.AppID}}
<strong>订单创建时间:</strong> {{.Order.CreateTime}}
<strong>领取成功时间(成功即有该值):</strong> {{.Order.ReceiveSuccessTime}}
<strong>最后一次核销时间(核销即有该值):</strong> {{.Order.LastUseTime}}
<strong>订单备注说明(失败说明):</strong> {{.Order.Remark}}
{{else}}
<h1 style="color: #FF6600">{{.ErrMsg}}</h1>
{{end}}
</pre>
</body>
</html>