voucher/internal/service/cmb_mock.go

174 lines
3.5 KiB
Go

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) {
bizJsonBytes, err := json.Marshal(request)
if err != nil {
return nil, err
}
reply, err := s.CmbMixRepo.GetMockRequest(ctx, string(bizJsonBytes))
if err != nil {
return nil, err
}
return reply, nil
}
func (s *CmbService) QueryMock(ctx context.Context, request *v1.CmbQueryRequest) (*v1.CmbRequest, error) {
bizJsonBytes, err := json.Marshal(request)
if err != nil {
return nil, err
}
reply, err := s.CmbMixRepo.GetMockRequest(ctx, string(bizJsonBytes))
if err != nil {
return nil, err
}
return reply, nil
}
func (s *CmbService) QueryProductMock(ctx context.Context, request *v1.CmbQueryProductRequest) (*v1.CmbRequest, error) {
bizJsonBytes, err := json.Marshal(request)
if err != nil {
return nil, err
}
reply, err := s.CmbMixRepo.GetMockRequest(ctx, string(bizJsonBytes))
if err != nil {
return nil, err
}
return reply, nil
}
func (s *CmbService) DecryptBody(ctx context.Context, request *v1.EncryptBodyRequest) (*v1.DecryptBodyReply, error) {
decryptBody, err := s.CmbMixRepo.Decrypt(ctx, request.EncryptBody)
if err != nil {
return nil, err
}
return &v1.DecryptBodyReply{
DecryptBody: decryptBody,
}, nil
}
func (s *CmbService) Test(ctx context.Context, _ *v1.Empty) (*v1.Empty, error) {
if err := s.VoucherBiz.ExecuteNotice(ctx); err != nil {
return nil, err
}
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
}