order query

This commit is contained in:
ziming 2025-04-29 13:42:27 +08:00
parent 465192e6fd
commit a5e48469f1
6 changed files with 104 additions and 96 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -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 := `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>订单查询结果</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
}
h1 {
color: green;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<h1>订单查询</h1>
<p>查询结果 [<span style="color: red;">%s</span>] 悉知</p>
<table>
<tr>
<th>订单号</th>
<td>%s</td>
</tr>
<tr>
<th>招行订单号</th>
<td>%s</td>
</tr>
<tr>
<th>订单状态</th>
<td>%s</td>
</tr>
<tr>
<th>openid</th>
<td>%s</td>
</tr>
<tr>
<th>微信券 ID</th>
<td>%s</td>
</tr>
<tr>
<th>商品编号</th>
<td>%s</td>
</tr>
<tr>
<th>批次号</th>
<td>%s</td>
</tr>
<tr>
<th>商户号</th>
<td>%s</td>
</tr>
<tr>
<th>appId</th>
<td>%s</td>
</tr>
<tr>
<th>订单创建时间</th>
<td>%s</td>
</tr>
<tr>
<th>领取成功时间(成功即有该值)</th>
<td>%s</td>
</tr>
<tr>
<th>最后一次核销时间(核销即有该值)</th>
<td>%s</td>
</tr>
<tr>
<th>订单备注说明(失败说明)</th>
<td>%s</td>
</tr>
</table>
</body>
</html>
`
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
}

11
templates/index.tmpl Normal file
View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{.Title}}</title>
</head>
<body>
<h1>{{.Title}}</h1>
<p>{{.Message}}</p>
</body>
</html>