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" "voucher/internal/biz/mixrepos" "voucher/internal/biz/vo" "voucher/internal/biz/wechatrepo" "voucher/internal/conf" ) var _ v1.CmbHTTPServer = (*CmbService)(nil) type CmbService struct { bc *conf.Bootstrap cron *cron.Cron VoucherBiz *biz.VoucherBiz CmbMixRepo mixrepos.CmbMixRepo WechatCpnRepo wechatrepo.WechatCpnRepo tmpl *template.Template } func NewCmbService( bc *conf.Bootstrap, cron *cron.Cron, VoucherBiz *biz.VoucherBiz, CmbMixRepo mixrepos.CmbMixRepo, WechatCpnRepo wechatrepo.WechatCpnRepo, tmpl *template.Template, ) *CmbService { return &CmbService{ bc: bc, cron: cron, VoucherBiz: VoucherBiz, CmbMixRepo: CmbMixRepo, WechatCpnRepo: WechatCpnRepo, tmpl: tmpl, } } func (c *CmbService) GetResponse(ctx context.Context, replyBizContent []byte) (*v1.CmbReply, error) { req := &bo.CmbResponseBo{ RespCode: vo.CmbResponseStatusSuccess.GetValue(), RespMsg: "成功", BizContent: string(replyBizContent), } reply, err := c.CmbMixRepo.GetResponse(ctx, req) if err != nil { log.Errorf("build cmb response fail: %v", err) return nil, err } 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 }