feat(main): add mp-weixin

This commit is contained in:
2025-01-18 22:34:27 +08:00
parent 9a31ec15a4
commit 9459d51501
28 changed files with 1580 additions and 71 deletions

View File

@@ -42,6 +42,7 @@ func NewApiRequestService(c config.Config, westDexService *WestDexService, yusha
type APIResponseData struct {
ApiID string `json:"apiID"`
Data json.RawMessage `json:"data"` // 这里用 RawMessage 来存储原始的 data
Sort int64 `json:"sort"`
Success bool `json:"success"`
Timestamp string `json:"timestamp"`
Error string `json:"error,omitempty"`
@@ -59,8 +60,12 @@ func (a *ApiRequestService) ProcessRequests(params []byte, productID int64) ([]b
return nil, findProductFeatureErr
}
var featureIDs []int64
isImportantMap := make(map[int64]int64, len(productFeatureList))
sortMap := make(map[int64]int64, len(productFeatureList)) // 新增
for _, pf := range productFeatureList {
featureIDs = append(featureIDs, pf.FeatureId)
isImportantMap[pf.FeatureId] = pf.IsImportant
sortMap[pf.FeatureId] = pf.Sort // 新增
}
if len(featureIDs) == 0 {
return nil, errors.New("featureIDs 是空的")
@@ -78,7 +83,8 @@ func (a *ApiRequestService) ProcessRequests(params []byte, productID int64) ([]b
resultsCh = make(chan APIResponseData, len(featureList))
errorsCh = make(chan error, len(featureList))
errorCount int32
errorLimit = 1
errorLimit = len(featureList)
retryNum = 5
)
for i, feature := range featureList {
@@ -94,10 +100,28 @@ func (a *ApiRequestService) ProcessRequests(params []byte, productID int64) ([]b
result := APIResponseData{
ApiID: feature.ApiId,
Success: false,
Sort: sortMap[feature.Id],
}
// 请求参数预处理
resp, preprocessErr := a.PreprocessRequestApi(params, feature.ApiId)
timestamp := time.Now().Format("2006-01-02 15:04:05")
var (
resp json.RawMessage
preprocessErr error
)
// 若 isImportantMap[feature.ID] == 1则表示需要在出错时重试
isImportant := isImportantMap[feature.Id] == 1
tryCount := 0
for {
tryCount++
resp, preprocessErr = a.PreprocessRequestApi(params, feature.ApiId)
if preprocessErr == nil {
break
}
if isImportant && tryCount < retryNum {
continue
} else {
break
}
}
if preprocessErr != nil {
result.Timestamp = timestamp
result.Error = preprocessErr.Error()
@@ -166,6 +190,11 @@ var requestProcessors = map[string]func(*ApiRequestService, []byte) ([]byte, err
"G02BJ02": (*ApiRequestService).ProcessG02BJ02Request,
"G19BJ02": (*ApiRequestService).ProcessG19BJ02Request,
"G20GZ01": (*ApiRequestService).ProcessG20GZ01Request,
"CAR074": (*ApiRequestService).ProcessCAR074Request,
"CAR058": (*ApiRequestService).ProcessCAR058Request,
"CAR079": (*ApiRequestService).ProcessCAR079Request,
"CAR066": (*ApiRequestService).ProcessCAR066Request,
"CAR100": (*ApiRequestService).ProcessCAR100Request,
}
// PreprocessRequestApi 调用指定的请求处理函数
@@ -710,6 +739,97 @@ func (a *ApiRequestService) ProcessP_C_B332Request(params []byte) ([]byte, error
return resp, nil
}
// 车辆出险信息
func (a *ApiRequestService) ProcessCAR074Request(params []byte) ([]byte, error) {
vinCode := gjson.GetBytes(params, "vin_code")
if !vinCode.Exists() {
return nil, errors.New("api请求, CAR074, 获取相关参数失败")
}
request := map[string]interface{}{
"vin": vinCode.String(),
}
resp, err := a.yushanService.request("CAR074", request)
if err != nil {
return nil, fmt.Errorf("人车核验查询失败: %+v", err)
}
return resp, nil
}
// 车辆维保记录
func (a *ApiRequestService) ProcessCAR058Request(params []byte) ([]byte, error) {
vinCode := gjson.GetBytes(params, "vin_code")
carType := gjson.GetBytes(params, "car_driving_permit")
if !vinCode.Exists() || !carType.Exists() {
return nil, errors.New("api请求, CAR058, 获取相关参数失败")
}
request := map[string]interface{}{
"vin": vinCode,
"image": carType,
"notifyUrl": "",
}
resp, err := a.yushanService.request("CAR058", request)
if err != nil {
return nil, fmt.Errorf("人车核验查询失败: %+v", err)
}
return resp, nil
}
// 车架号查车
func (a *ApiRequestService) ProcessCAR079Request(params []byte) ([]byte, error) {
vinCode := gjson.GetBytes(params, "vin_code")
if !vinCode.Exists() {
return nil, errors.New("api请求, CAR079, 获取相关参数失败")
}
request := map[string]interface{}{
"vin": vinCode.String(),
}
resp, err := a.yushanService.request("CAR079", request)
if err != nil {
return nil, fmt.Errorf("车架号查车查询失败: %+v", err)
}
return resp, nil
}
// 车辆过户次数
func (a *ApiRequestService) ProcessCAR066Request(params []byte) ([]byte, error) {
vinCode := gjson.GetBytes(params, "vin_code")
if !vinCode.Exists() {
return nil, errors.New("api请求, CAR066, 获取相关参数失败")
}
request := map[string]interface{}{
"vin": vinCode.String(),
}
resp, err := a.yushanService.request("CAR066", request)
if err != nil {
return nil, fmt.Errorf("车辆过户次数查询失败: %+v", err)
}
return resp, nil
}
// 车辆估值
func (a *ApiRequestService) ProcessCAR100Request(params []byte) ([]byte, error) {
vinCode := gjson.GetBytes(params, "vin_code")
carLicense := gjson.GetBytes(params, "car_license")
if !vinCode.Exists() || !carLicense.Exists() {
return nil, errors.New("api请求, CAR100, 获取相关参数失败")
}
request := map[string]interface{}{
"vin": vinCode.String(),
"carNumber": carLicense.String(),
"cardNo": "",
}
resp, err := a.yushanService.request("CAR100", request)
if err != nil {
return nil, fmt.Errorf("车辆估值查询失败: %+v", err)
}
return resp, nil
}
// 银行卡黑名单
func (a *ApiRequestService) ProcessFIN019Request(params []byte) ([]byte, error) {
name := gjson.GetBytes(params, "name")

View File

@@ -16,6 +16,8 @@ import (
"github.com/zeromicro/go-zero/core/logx"
"net/http"
"qnc-server/app/user/cmd/api/internal/config"
"qnc-server/app/user/model"
"qnc-server/common/ctxdata"
"qnc-server/pkg/lzkit/lzUtils"
"strconv"
"time"
@@ -35,10 +37,11 @@ type WechatPayService struct {
config config.WxpayConfig
wechatClient *core.Client
notifyHandler *notify.Handler
userAuthModel model.UserAuthModel
}
// NewWechatPayService 初始化微信支付服务
func NewWechatPayService(c config.Config) *WechatPayService {
func NewWechatPayService(c config.Config, userAuthModel model.UserAuthModel) *WechatPayService {
// 从配置中加载商户信息
mchID := c.Wxpay.MchID
mchCertificateSerialNumber := c.Wxpay.MchCertificateSerialNumber
@@ -71,6 +74,7 @@ func NewWechatPayService(c config.Config) *WechatPayService {
config: c.Wxpay,
wechatClient: client,
notifyHandler: notifyHandler,
userAuthModel: userAuthModel,
}
}
@@ -104,7 +108,7 @@ func (w *WechatPayService) CreateWechatAppOrder(ctx context.Context, amount floa
}
// CreateWechatMiniProgramOrder 创建微信小程序支付订单
func (w *WechatPayService) CreateWechatMiniProgramOrder(ctx context.Context, amount float64, description string, outTradeNo string, openid string) (string, error) {
func (w *WechatPayService) CreateWechatMiniProgramOrder(ctx context.Context, amount float64, description string, outTradeNo string, openid string) (interface{}, error) {
totalAmount := lzUtils.ToWechatAmount(amount)
// 构建支付请求参数
@@ -131,24 +135,32 @@ func (w *WechatPayService) CreateWechatMiniProgramOrder(ctx context.Context, amo
}
// 返回预支付交易会话标识
return *resp.PrepayId, nil
return resp, nil
}
// CreateWechatOrder 创建微信支付订单(集成 APP、H5、小程序
func (w *WechatPayService) CreateWechatOrder(ctx context.Context, amount float64, description string, outTradeNo string) (string, error) {
func (w *WechatPayService) CreateWechatOrder(ctx context.Context, amount float64, description string, outTradeNo string) (interface{}, error) {
// 根据 ctx 中的 platform 判断平台
platform := ctx.Value("platform").(string)
var prepayId string
var prepayData interface{}
var err error
switch platform {
case "mp-weixin":
userID, getUidErr := ctxdata.GetUidFromCtx(ctx)
if getUidErr != nil {
return "", fmt.Errorf("获取用户信息失败: %s", getUidErr)
}
userAuthModel, findUserAuthErr := w.userAuthModel.FindOneByUserIdAuthType(ctx, userID, "wx_mini")
if findUserAuthErr != nil {
return "", fmt.Errorf("获取用户认证信息失败: %s", findUserAuthErr)
}
// 如果是小程序平台,调用小程序支付订单创建
prepayId, err = w.CreateWechatMiniProgramOrder(ctx, amount, description, outTradeNo, "asdasd")
prepayData, err = w.CreateWechatMiniProgramOrder(ctx, amount, description, outTradeNo, userAuthModel.AuthKey)
case "app":
// 如果是 APP 平台,调用 APP 支付订单创建
prepayId, err = w.CreateWechatAppOrder(ctx, amount, description, outTradeNo)
prepayData, err = w.CreateWechatAppOrder(ctx, amount, description, outTradeNo)
default:
return "", fmt.Errorf("不支持的支付平台: %s", platform)
}
@@ -159,7 +171,7 @@ func (w *WechatPayService) CreateWechatOrder(ctx context.Context, amount float64
}
// 返回预支付ID
return prepayId, nil
return prepayData, nil
}
// HandleWechatPayNotification 处理微信支付回调