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

@@ -49,6 +49,7 @@ type AlipayConfig struct {
}
type WxpayConfig struct {
AppID string
AppSecret string
MchID string
MchCertificateSerialNumber string
MchApiv3Key string

View File

@@ -0,0 +1,29 @@
package product
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"qnc-server/app/user/cmd/api/internal/logic/product"
"qnc-server/app/user/cmd/api/internal/svc"
"qnc-server/app/user/cmd/api/internal/types"
"qnc-server/common/result"
"qnc-server/pkg/lzkit/validator"
)
func GetProductRenderListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.GetProductRenderListRequest
if err := httpx.Parse(r, &req); err != nil {
result.ParamErrorResult(r, w, err)
return
}
if err := validator.Validate(req); err != nil {
result.ParamValidateErrorResult(r, w, err)
return
}
l := product.NewGetProductRenderListLogic(r.Context(), svcCtx)
resp, err := l.GetProductRenderList(&req)
result.HttpResult(r, w, resp, err)
}
}

View File

@@ -98,6 +98,20 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
rest.WithPrefix("/api/v1/product"),
)
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.SourceInterceptor},
[]rest.Route{
{
Method: http.MethodGet,
Path: "/render_list/:module",
Handler: product.GetProductRenderListHandler(serverCtx),
},
}...,
),
rest.WithPrefix("/api/v1/product"),
)
server.AddRoutes(
[]rest.Route{
{

View File

@@ -68,7 +68,7 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp,
if aesEncryptErr != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 加密参数失败: %+v", aesEncryptErr)
}
var prepayID string
var prepayData interface{}
var outTradeNo string
var amount float64
user, err := l.svcCtx.UserModel.FindOne(l.ctx, userID)
@@ -81,16 +81,17 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp,
} else {
amount = product.SellPrice
}
var createOrderErr error
if req.PayMethod == "wechatpay" {
if req.PayMethod == "wechat" {
outTradeNo = l.svcCtx.WechatPayService.GenerateOutTradeNo()
prepayID, createOrderErr = l.svcCtx.WechatPayService.CreateWechatOrder(l.ctx, amount, product.ProductName, outTradeNo)
prepayData, createOrderErr = l.svcCtx.WechatPayService.CreateWechatOrder(l.ctx, amount, product.ProductName, outTradeNo)
} else if req.PayMethod == "alipay" {
outTradeNo = l.svcCtx.AlipayService.GenerateOutTradeNo()
prepayID, createOrderErr = l.svcCtx.AlipayService.CreateAlipayOrder(l.ctx, amount, product.ProductName, outTradeNo, brand)
prepayData, createOrderErr = l.svcCtx.AlipayService.CreateAlipayOrder(l.ctx, amount, product.ProductName, outTradeNo, brand)
} else if req.PayMethod == "appleiap" {
outTradeNo = l.svcCtx.ApplePayService.GenerateOutTradeNo()
prepayID = l.svcCtx.ApplePayService.GetIappayAppID(product.ProductEn)
prepayData = l.svcCtx.ApplePayService.GetIappayAppID(product.ProductEn)
}
if createOrderErr != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 创建支付订单失败: %+v", createOrderErr)
@@ -132,5 +133,5 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp,
return nil, transErr
}
return &types.PaymentResp{PrepayID: prepayID, OrderID: orderID}, nil
return &types.PaymentResp{PrepayData: prepayData, OrderID: orderID}, nil
}

View File

@@ -38,7 +38,7 @@ func (l *WechatPayCallbackLogic) WechatPayCallback(w http.ResponseWriter, r *htt
}
amount := lzUtils.ToWechatAmount(order.Amount)
if &amount != notification.Amount.Total {
if amount != *notification.Amount.Total {
logx.Errorf("微信支付回调,金额不一致")
return nil
}

View File

@@ -0,0 +1,80 @@
package product
import (
"context"
"github.com/Masterminds/squirrel"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/mr"
"qnc-server/app/user/cmd/api/internal/svc"
"qnc-server/app/user/cmd/api/internal/types"
"qnc-server/app/user/model"
"qnc-server/common/xerr"
"github.com/zeromicro/go-zero/core/logx"
)
type GetProductRenderListLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetProductRenderListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetProductRenderListLogic {
return &GetProductRenderListLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetProductRenderListLogic) GetProductRenderList(req *types.GetProductRenderListRequest) (resp *types.GetProductRenderListResponse, err error) {
platform, platformOk := l.ctx.Value("platform").(string)
if !platformOk || platform == "" {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取渲染列表失败,没有相关平台信息")
}
// 从上下文中获取品牌信息
brand, brandOk := l.ctx.Value("brand").(string)
if !brandOk || brand == "" {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取渲染列表失败,没有相关品牌信息")
}
var platformKey = brand + "_" + platform
builder := l.svcCtx.ProductRenderModel.SelectBuilder().Where(squirrel.Eq{
"platform": platformKey,
"module": req.Module,
})
productRenderModelList, err := l.svcCtx.ProductRenderModel.FindAll(l.ctx, builder, "")
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "获取渲染列表失败,获取渲染列表失败%s", err.Error())
}
var productList []types.Product
mr.MapReduceVoid(func(source chan<- interface{}) {
for _, productRender := range productRenderModelList {
if productRender.IsRendered == 1 {
source <- productRender.ProductId
}
}
}, func(item interface{}, writer mr.Writer[*model.Product], cancel func(error)) {
id := item.(int64)
product, findProductErr := l.svcCtx.ProductModel.FindOne(l.ctx, id)
if findProductErr != nil {
logx.WithContext(l.ctx).Errorf("获取渲染列表失败, 查找关联product错误: %d, err:%v", id, findProductErr)
return
}
if product != nil && product.Id > 0 {
product.Description = ""
product.SellPrice = 0
writer.Write(product)
}
}, func(pipe <-chan *model.Product, cancel func(error)) {
for item := range pipe {
var product types.Product
_ = copier.Copy(&product, item)
productList = append(productList, product)
}
})
return &types.GetProductRenderListResponse{
Product: productList,
}, nil
}

View File

@@ -119,7 +119,6 @@ func (l *QueryDetailByOrderNoLogic) QueryDetailByOrderNo(req *types.QueryDetailB
if processErr != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告查询, 报告结果处理失败: %v", processErr)
}
// 复制报告数据
err = copier.Copy(&query, queryModel)
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "报告查询, 报告结构体复制失败, %+v", err)

View File

@@ -38,7 +38,7 @@ func (l *QueryProvisionalOrderLogic) QueryProvisionalOrder(req *types.QueryProvi
if cacheErr != nil {
return nil, cacheErr
}
var data types.QueryCache
var data types.QueryCacheLoad
err = json.Unmarshal([]byte(cache), &data)
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取临时订单, 解析缓存内容失败, %+v", err)
@@ -54,9 +54,7 @@ func (l *QueryProvisionalOrderLogic) QueryProvisionalOrder(req *types.QueryProvi
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取临时订单, 用户信息结构体复制失败: %+v", err)
}
return &types.QueryProvisionalOrderResp{
Name: data.Name,
IdCard: data.Name,
Mobile: data.Mobile,
Product: product,
QueryParams: data.Params,
Product: product,
}, nil
}

View File

@@ -56,13 +56,19 @@ var productProcessors = map[string]func(*QueryServiceLogic, *types.QueryServiceR
"toc_IDCardTwoElements": (*QueryServiceLogic).ProcessTocIDCardTwoElementsLogic,
"toc_NaturalLifeStatus": (*QueryServiceLogic).ProcessTocNaturalLifeStatusLogic,
"toc_PersonVehicleVerification": (*QueryServiceLogic).ProcessTocPersonVehicleVerificationLogic,
"toc_BankCardBlacklist": (*QueryServiceLogic).ProcessTocBankCardBlacklistLogic,
"toc_VehiclesUnderName": (*QueryServiceLogic).ProcessTocVehiclesUnderNameLogic,
"toc_DualMarriage": (*QueryServiceLogic).ProcessTocDualMarriageLogic,
"toc_PhoneNumberRisk": (*QueryServiceLogic).ProcessTocPhoneNumberRiskLogic,
"toc_NetworkDuration": (*QueryServiceLogic).ProcessTocNetworkDurationLogic,
"toc_PhoneSecondaryCard": (*QueryServiceLogic).ProcessTocPhoneSecondaryCardLogic,
"toc_BankCardFourElements": (*QueryServiceLogic).ProcessTocBankCardFourElementsLogic,
// 车辆部分
"toc_BankCardBlacklist": (*QueryServiceLogic).ProcessTocBankCardBlacklistLogic,
"toc_VehiclesUnderName": (*QueryServiceLogic).ProcessTocVehiclesUnderNameLogic,
"toc_vehicleInsuranceSummary": (*QueryServiceLogic).ProcessTocVehicleInsuranceSummaryLogic,
"toc_vehicleMaintenanceRecord": (*QueryServiceLogic).ProcessTocVehicleMaintenanceRecordLogic,
"toc_vehicleValuation": (*QueryServiceLogic).ProcessTocVehicleValuationLogic,
"toc_chassisNumberCheck": (*QueryServiceLogic).ProcessTocChassisNumberCheckLogic,
"toc_vehicleTransferCount": (*QueryServiceLogic).ProcessTocVehicleTransferCountLogic,
}
func (l *QueryServiceLogic) PreprocessLogic(req *types.QueryServiceReq, product string) (*types.QueryServiceResp, error) {
@@ -852,6 +858,178 @@ func (l *QueryServiceLogic) ProcessTocPersonVehicleVerificationLogic(req *types.
return &types.QueryServiceResp{Id: cacheNo}, nil
}
// ProcessTocVehicleInsuranceSummaryLogic 车辆出险信息
func (l *QueryServiceLogic) ProcessTocVehicleInsuranceSummaryLogic(req *types.QueryServiceReq) (*types.QueryServiceResp, error) {
userID, getUidErr := ctxdata.GetUidFromCtx(l.ctx)
if getUidErr != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "查询服务, 获取用户信息失败, %+v", getUidErr)
}
// AES解密
decryptData, DecryptDataErr := l.DecryptData(req.Data)
if DecryptDataErr != nil {
return nil, DecryptDataErr
}
// 校验参数
var data types.TocCarVin
if unmarshalErr := json.Unmarshal(decryptData, &data); unmarshalErr != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "查询服务, 解密后的数据格式不正确: %+v", unmarshalErr)
}
if validatorErr := validator.Validate(data); validatorErr != nil {
return nil, errors.Wrapf(xerr.NewErrCodeMsg(xerr.PARAM_VERIFICATION_ERROR, validatorErr.Error()), "查询服务, 参数不正确: %+v", validatorErr)
}
params := map[string]interface{}{
"vin_code": data.VinCode,
}
cacheNo, cacheDataErr := l.CacheData(params, "toc_vehicleInsuranceSummary", userID)
if cacheDataErr != nil {
return nil, cacheDataErr
}
return &types.QueryServiceResp{Id: cacheNo}, nil
}
// ProcessTocVehicleMaintenanceRecordLogic 车辆维保记录
func (l *QueryServiceLogic) ProcessTocVehicleMaintenanceRecordLogic(req *types.QueryServiceReq) (*types.QueryServiceResp, error) {
userID, getUidErr := ctxdata.GetUidFromCtx(l.ctx)
if getUidErr != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "查询服务, 获取用户信息失败, %+v", getUidErr)
}
// AES解密
decryptData, DecryptDataErr := l.DecryptData(req.Data)
if DecryptDataErr != nil {
return nil, DecryptDataErr
}
// 校验参数
var data types.TocCarVinDrivingPermit
if unmarshalErr := json.Unmarshal(decryptData, &data); unmarshalErr != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "查询服务, 解密后的数据格式不正确: %+v", unmarshalErr)
}
if validatorErr := validator.Validate(data); validatorErr != nil {
return nil, errors.Wrapf(xerr.NewErrCodeMsg(xerr.PARAM_VERIFICATION_ERROR, validatorErr.Error()), "查询服务, 参数不正确: %+v", validatorErr)
}
params := map[string]interface{}{
"vin_code": data.VinCode,
"car_driving_permit": data.CarDrivingPermit,
}
cacheNo, cacheDataErr := l.CacheData(params, "toc_vehicleMaintenanceRecord", userID)
if cacheDataErr != nil {
return nil, cacheDataErr
}
return &types.QueryServiceResp{Id: cacheNo}, nil
}
// ProcessTocVehicleValuationLogic 车辆估值
func (l *QueryServiceLogic) ProcessTocVehicleValuationLogic(req *types.QueryServiceReq) (*types.QueryServiceResp, error) {
userID, getUidErr := ctxdata.GetUidFromCtx(l.ctx)
if getUidErr != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "查询服务, 获取用户信息失败, %+v", getUidErr)
}
// AES解密
decryptData, DecryptDataErr := l.DecryptData(req.Data)
if DecryptDataErr != nil {
return nil, DecryptDataErr
}
// 校验参数
var data types.TocCarVinLicense
if unmarshalErr := json.Unmarshal(decryptData, &data); unmarshalErr != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "查询服务, 解密后的数据格式不正确: %+v", unmarshalErr)
}
if validatorErr := validator.Validate(data); validatorErr != nil {
return nil, errors.Wrapf(xerr.NewErrCodeMsg(xerr.PARAM_VERIFICATION_ERROR, validatorErr.Error()), "查询服务, 参数不正确: %+v", validatorErr)
}
params := map[string]interface{}{
"vin_code": data.VinCode,
"car_license": data.CarLicense,
}
cacheNo, cacheDataErr := l.CacheData(params, "toc_vehicleValuation", userID)
if cacheDataErr != nil {
return nil, cacheDataErr
}
return &types.QueryServiceResp{Id: cacheNo}, nil
}
// ProcessTocChassisNumberCheckLogic 车架号查车
func (l *QueryServiceLogic) ProcessTocChassisNumberCheckLogic(req *types.QueryServiceReq) (*types.QueryServiceResp, error) {
userID, getUidErr := ctxdata.GetUidFromCtx(l.ctx)
if getUidErr != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "查询服务, 获取用户信息失败, %+v", getUidErr)
}
// AES解密
decryptData, DecryptDataErr := l.DecryptData(req.Data)
if DecryptDataErr != nil {
return nil, DecryptDataErr
}
// 校验参数
var data types.TocCarVin
if unmarshalErr := json.Unmarshal(decryptData, &data); unmarshalErr != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "查询服务, 解密后的数据格式不正确: %+v", unmarshalErr)
}
if validatorErr := validator.Validate(data); validatorErr != nil {
return nil, errors.Wrapf(xerr.NewErrCodeMsg(xerr.PARAM_VERIFICATION_ERROR, validatorErr.Error()), "查询服务, 参数不正确: %+v", validatorErr)
}
params := map[string]interface{}{
"vin_code": data.VinCode,
}
cacheNo, cacheDataErr := l.CacheData(params, "toc_chassisNumberCheck", userID)
if cacheDataErr != nil {
return nil, cacheDataErr
}
return &types.QueryServiceResp{Id: cacheNo}, nil
}
// ProcessTocVehicleTransferCountLogic 车辆过户次数查询
func (l *QueryServiceLogic) ProcessTocVehicleTransferCountLogic(req *types.QueryServiceReq) (*types.QueryServiceResp, error) {
userID, getUidErr := ctxdata.GetUidFromCtx(l.ctx)
if getUidErr != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "查询服务, 获取用户信息失败, %+v", getUidErr)
}
// AES解密
decryptData, DecryptDataErr := l.DecryptData(req.Data)
if DecryptDataErr != nil {
return nil, DecryptDataErr
}
// 校验参数
var data types.TocCarVin
if unmarshalErr := json.Unmarshal(decryptData, &data); unmarshalErr != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "查询服务, 解密后的数据格式不正确: %+v", unmarshalErr)
}
if validatorErr := validator.Validate(data); validatorErr != nil {
return nil, errors.Wrapf(xerr.NewErrCodeMsg(xerr.PARAM_VERIFICATION_ERROR, validatorErr.Error()), "查询服务, 参数不正确: %+v", validatorErr)
}
params := map[string]interface{}{
"vin_code": data.VinCode,
}
cacheNo, cacheDataErr := l.CacheData(params, "toc_vehicleTransferCount", userID)
if cacheDataErr != nil {
return nil, cacheDataErr
}
return &types.QueryServiceResp{Id: cacheNo}, nil
}
// ProcessTocBankCardBlacklistLogic 银行卡黑名单
func (l *QueryServiceLogic) ProcessTocBankCardBlacklistLogic(req *types.QueryServiceReq) (*types.QueryServiceResp, error) {
userID, getUidErr := ctxdata.GetUidFromCtx(l.ctx)

View File

@@ -2,9 +2,19 @@ package user
import (
"context"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"io"
"net/http"
"net/url"
"qnc-server/app/user/cmd/api/internal/svc"
"qnc-server/app/user/cmd/api/internal/types"
"qnc-server/app/user/model"
jwtx "qnc-server/common/jwt"
"qnc-server/common/xerr"
"time"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -24,7 +34,117 @@ func NewWxMiniAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WxMini
}
func (l *WxMiniAuthLogic) WxMiniAuth(req *types.WXMiniAuthReq) (resp *types.WXMiniAuthResp, err error) {
// todo: add your logic here and delete this line
// 1. 使用微信提供的 code 换取 session_key 和 openid
weChatResponse, err := l.exchangeCodeForSession(req.Code)
if err != nil {
return nil, errors.Wrapf(xerr.NewErrMsg("微信登录失败"), "微信登录, code 换取 session 失败: %s, err: %+v", req.Code, err)
}
// 2. 根据 openid 查找用户
userAuth, findErr := l.svcCtx.UserAuthModel.FindOneByAuthTypeAuthKey(l.ctx, model.UserAuthTypeWxMini, weChatResponse.OpenId)
if findErr != nil && findErr != model.ErrNotFound {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "微信登录, 读取用户认证信息失败, openid: %s, err: %+v", weChatResponse.OpenId, findErr)
}
var user *model.User
if findErr == model.ErrNotFound {
// 用户不存在,创建新用户
user = &model.User{}
if transErr := l.svcCtx.UserModel.Trans(l.ctx, func(ctx context.Context, session sqlx.Session) error {
// 插入新用户
insertResult, userInsertErr := l.svcCtx.UserModel.Insert(ctx, session, user)
if userInsertErr != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "微信登录, 数据库插入新用户失败, openid: %s, err: %+v", weChatResponse.OpenId, userInsertErr)
}
// 获取新用户的 ID
lastId, lastInsertIdErr := insertResult.LastInsertId()
if lastInsertIdErr != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "微信登录, 获取新用户ID失败, err:%+v, user:%+v", lastInsertIdErr, user)
}
user.Id = lastId
// 创建用户认证信息
newUserAuth := &model.UserAuth{
UserId: lastId,
AuthKey: weChatResponse.OpenId,
AuthType: model.UserAuthTypeWxMini,
}
if _, userAuthInsertErr := l.svcCtx.UserAuthModel.Insert(ctx, session, newUserAuth); userAuthInsertErr != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "微信登录, 数据库插入用户认证信息失败, err:%+v", userAuthInsertErr)
}
return nil
}); transErr != nil {
return nil, transErr
}
} else {
// 获取用户信息
user, err = l.svcCtx.UserModel.FindOne(l.ctx, userAuth.UserId)
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "微信登录, 读取用户信息失败, userId: %d, err: %+v", userAuth.UserId, err)
}
}
// 3. 生成 JWT 令牌
token, generateErr := jwtx.GenerateJwtToken(user.Id, l.svcCtx.Config.JwtAuth.AccessSecret, l.svcCtx.Config.JwtAuth.AccessExpire)
if generateErr != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "微信登录, 生成token失败 : %d", user.Id)
}
// 4. 获取当前时间戳
now := time.Now().Unix()
return &types.WXMiniAuthResp{
AccessToken: token,
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
}, nil
}
type WxLoginResp struct {
OpenId string `json:"openid"`
SessionKey string `json:"session_key"`
Unionid string `json:"unionid"`
ErrCode int `json:"errcode"`
ErrMsg string `json:"errmsg"`
}
func (l *WxMiniAuthLogic) exchangeCodeForSession(code string) (response WxLoginResp, err error) {
// 向微信发出登录请求
baseURL := "https://api.weixin.qq.com/sns/jscode2session"
// 创建查询参数
params := url.Values{}
params.Add("appid", l.svcCtx.Config.Wxpay.AppID)
params.Add("secret", l.svcCtx.Config.Wxpay.AppSecret)
params.Add("js_code", code)
params.Add("grant_type", "authorization_code")
// 构建完整的请求 URL
requestURL := fmt.Sprintf("%s?%s", baseURL, params.Encode())
// 发送 GET 请求
resp, err := http.Get(requestURL)
if err != nil {
return
}
defer resp.Body.Close()
// 读取响应体
body, err := io.ReadAll(resp.Body)
if err != nil {
return
}
// 将响应体解析为结构体
err = json.Unmarshal(body, &response)
if err != nil {
return
}
if response.ErrCode != 0 {
err = errors.New(response.ErrMsg)
return
}
return
}

View File

@@ -7,7 +7,7 @@ import (
"qnc-server/app/user/cmd/api/internal/svc"
)
const TASKTIME = "32 * * * *"
const TASKTIME = "0 2 * * *"
type CleanQueryDataHandler struct {
svcCtx *svc.ServiceContext
@@ -20,6 +20,30 @@ func NewCleanQueryDataHandler(svcCtx *svc.ServiceContext) *CleanQueryDataHandler
}
func (l *CleanQueryDataHandler) ProcessTask(ctx context.Context, t *asynq.Task) error {
fmt.Println("企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅企鹅-ProcessTask")
fmt.Println(TASKTIME)
//// 计算 30 天前的时间
//threshold := time.Now().AddDate(0, 0, -30)
//
//// 1. 构造查询条件,查找创建时间小于 threshold 的记录
//// 假设表中“创建时间”字段为 create_time且字段类型对应数据库里实际的列名
//rowBuilder := l.svcCtx.QueryModel.SelectBuilder().
// Where(squirrel.Lt{"create_time": threshold})
//
//// 2. 调用 FindAll 获取所有符合条件的记录
//// orderBy 这里可传空字符串或你想要的排序字段
//records, err := l.svcCtx.QueryModel.FindAll(ctx, rowBuilder, "")
//if err != nil {
// return err
//}
//
//// 3. 遍历记录,逐条删除
//// 假设你的 Delete 方法签名是Delete(ctx context.Context, id int64) error
//// 并且 records[i] 中有一个字段 ID 用于表示主键
//for _, record := range records {
// if err := l.svcCtx.QueryModel.Delete(ctx, record.Id); err != nil {
// return err
// }
//}
return nil
}

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 处理微信支付回调

View File

@@ -21,6 +21,7 @@ type ServiceContext struct {
ProductModel model.ProductModel
FeatureModel model.FeatureModel
ProductFeatureModel model.ProductFeatureModel
ProductRenderModel model.ProductRenderModel
OrderModel model.OrderModel
QueryModel model.QueryModel
GlobalNotificationsModel model.GlobalNotificationsModel
@@ -56,12 +57,13 @@ func NewServiceContext(c config.Config) *ServiceContext {
yushanService := service.NewYushanService(c)
productFeatureModel := model.NewProductFeatureModel(db, c.CacheRedis)
featureModel := model.NewFeatureModel(db, c.CacheRedis)
userAuthModel := model.NewUserAuthModel(db, c.CacheRedis)
return &ServiceContext{
Config: c,
Redis: redis.MustNewRedis(redisConf),
SourceInterceptor: middleware.NewSourceInterceptorMiddleware().Handle,
AlipayService: service.NewAliPayService(c),
WechatPayService: service.NewWechatPayService(c),
WechatPayService: service.NewWechatPayService(c, userAuthModel),
ApplePayService: service.NewApplePayService(c),
WestDexService: westDexService,
YushanService: yushanService,
@@ -70,8 +72,9 @@ func NewServiceContext(c config.Config) *ServiceContext {
ApiRequestService: service.NewApiRequestService(c, westDexService, yushanService, featureModel, productFeatureModel),
AsynqService: service.NewAsynqService(c),
UserModel: model.NewUserModel(db, c.CacheRedis),
UserAuthModel: model.NewUserAuthModel(db, c.CacheRedis),
UserAuthModel: userAuthModel,
ProductModel: model.NewProductModel(db, c.CacheRedis),
ProductRenderModel: model.NewProductRenderModel(db, c.CacheRedis),
OrderModel: model.NewOrderModel(db, c.CacheRedis),
QueryModel: model.NewQueryModel(db, c.CacheRedis),
GlobalNotificationsModel: model.NewGlobalNotificationsModel(db, c.CacheRedis),

View File

@@ -82,6 +82,17 @@ type TocPersonVehicleVerification struct {
CarType string `json:"car_type" validate:"required"`
CarLicense string `json:"car_license" validate:"required"`
}
type TocCarVin struct {
VinCode string `json:"vin_code" validate:"required"`
}
type TocCarVinDrivingPermit struct {
VinCode string `json:"vin_code" validate:"required"`
CarDrivingPermit string `json:"car_drivingPermit" validate:"required"`
}
type TocCarVinLicense struct {
VinCode string `json:"vin_code" validate:"required"`
CarLicense string `json:"car_license" validate:"required"`
}
// 银行卡黑名单
type TocBankCardBlacklist struct {

View File

@@ -20,6 +20,14 @@ type GetProductByIDRequest struct {
Id int64 `path:"id"`
}
type GetProductRenderListRequest struct {
Module string `path:"module"`
}
type GetProductRenderListResponse struct {
Product []Product
}
type IapCallbackReq struct {
OrderID int64 `json:"order_id" validate:"required"`
TransactionReceipt string `json:"transaction_receipt" validate:"required"`
@@ -63,8 +71,8 @@ type PaymentReq struct {
}
type PaymentResp struct {
PrepayID string `json:"prepay_id"`
OrderID int64 `json:"order_id"`
PrepayData interface{} `json:"prepay_data"`
OrderID int64 `json:"order_id"`
}
type Product struct {
@@ -139,10 +147,8 @@ type QueryProvisionalOrderReq struct {
}
type QueryProvisionalOrderResp struct {
Name string `json:"name"`
IdCard string `json:"id_card"`
Mobile string `json:"mobile"`
Product Product `json:"product"`
QueryParams map[string]interface{} `json:"query_params"`
Product Product `json:"product"`
}
type QueryReq struct {
@@ -202,9 +208,7 @@ type WXH5AuthResp struct {
}
type WXMiniAuthReq struct {
Code string `json:"code"`
IV string `json:"iv"`
EncryptedData string `json:"encryptedData"`
Code string `json:"code"`
}
type WXMiniAuthResp struct {