154 lines
5.7 KiB
Go
154 lines
5.7 KiB
Go
package query
|
|
|
|
import (
|
|
"context"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/pkg/errors"
|
|
"github.com/zeromicro/go-zero/core/stores/redis"
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
|
"qnc-server/app/user/cmd/api/internal/service"
|
|
"qnc-server/app/user/model"
|
|
"qnc-server/common/ctxdata"
|
|
"qnc-server/common/xerr"
|
|
"qnc-server/pkg/lzkit/crypto"
|
|
"qnc-server/pkg/lzkit/validator"
|
|
|
|
"qnc-server/app/user/cmd/api/internal/svc"
|
|
"qnc-server/app/user/cmd/api/internal/types"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type HomeServiceLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewHomeServiceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HomeServiceLogic {
|
|
return &HomeServiceLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *HomeServiceLogic) HomeService(req *types.QueryReq) (resp *types.QueryResp, err error) {
|
|
userID, getUidErr := ctxdata.GetUidFromCtx(l.ctx)
|
|
if getUidErr != nil {
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "家政服务, 获取用户信息失败, %+v", getUidErr)
|
|
}
|
|
// 1、AES解密
|
|
secretKey := l.svcCtx.Config.Encrypt.SecretKey
|
|
key, decodeErr := hex.DecodeString(secretKey)
|
|
if decodeErr != nil {
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "家政服务, 密钥获取失败: %+v", decodeErr)
|
|
}
|
|
decryptData, aesDecryptErr := crypto.AesDecrypt(req.Data, key)
|
|
if aesDecryptErr != nil || len(decryptData) == 0 {
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "家政服务, 解密失败: %+v", decodeErr)
|
|
}
|
|
|
|
// 2、校验
|
|
var data types.HomeServiceReq
|
|
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)
|
|
}
|
|
|
|
// 校验验证码
|
|
codeRedisKey := fmt.Sprintf("%s:%s", "query", data.Mobile)
|
|
cacheCode, err := l.svcCtx.Redis.Get(codeRedisKey)
|
|
if err != nil {
|
|
if errors.Is(err, redis.Nil) {
|
|
return nil, errors.Wrapf(xerr.NewErrMsg("验证码已过期"), "家政服务, 验证码过期: %s", data.Mobile)
|
|
}
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "家政服务, 读取验证码redis缓存失败, mobile: %s, err: %+v", data.Mobile, err)
|
|
}
|
|
if cacheCode != data.Code {
|
|
return nil, errors.Wrapf(xerr.NewErrMsg("验证码不正确"), "家政服务, 验证码不正确: %s", data.Mobile)
|
|
}
|
|
|
|
// 3、二要素三要素核验
|
|
twoVerification := service.TwoFactorVerificationRequest{
|
|
Name: data.Name,
|
|
IDCard: data.IDCard,
|
|
}
|
|
verification, err := l.svcCtx.VerificationService.TwoFactorVerification(twoVerification)
|
|
if err != nil {
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "家政服务, 二要素验证失败: %+v", err)
|
|
}
|
|
if !verification.Passed {
|
|
return nil, errors.Wrapf(xerr.NewErrCodeMsg(xerr.SERVER_COMMON_ERROR, verification.Err.Error()), "家政服务, 二要素验证不通过: %+v", err)
|
|
}
|
|
product, findProductErr := l.svcCtx.ProductModel.FindOneByProductEn(l.ctx, "homeservice")
|
|
if findProductErr != nil {
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "家政服务, 查找婚姻产品错误: %+v", findProductErr)
|
|
}
|
|
jsonData, marshalErr := json.Marshal(data)
|
|
if marshalErr != nil {
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "家政服务, 序列化参数失败: %+v", marshalErr)
|
|
}
|
|
|
|
outTradeNo := l.svcCtx.WechatPayService.GenerateOutTradeNo()
|
|
redisKey := fmt.Sprintf("%s:%d:%s", "homeservice", userID, outTradeNo)
|
|
cacheErr := l.svcCtx.Redis.SetexCtx(l.ctx, redisKey, string(jsonData), 1800)
|
|
if cacheErr != nil {
|
|
return nil, cacheErr
|
|
}
|
|
|
|
var prepayID string
|
|
var createOrderErr error
|
|
if data.PayMethod == "wechatpay" {
|
|
prepayID, createOrderErr = l.svcCtx.WechatPayService.CreateWechatAppOrder(l.ctx, product.SellPrice, product.Description, outTradeNo)
|
|
} else {
|
|
prepayID, createOrderErr = l.svcCtx.AlipayService.CreateAlipayAppOrder(product.SellPrice, product.Description, outTradeNo)
|
|
}
|
|
if createOrderErr != nil {
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "家政服务, 创建支付订单失败: %+v", createOrderErr)
|
|
}
|
|
var orderID int64
|
|
transErr := l.svcCtx.OrderModel.Trans(l.ctx, func(ctx context.Context, session sqlx.Session) error {
|
|
order := model.Order{
|
|
OrderNo: outTradeNo,
|
|
UserId: userID,
|
|
ProductId: product.Id,
|
|
PaymentPlatform: data.PayMethod,
|
|
PaymentScene: "app",
|
|
Amount: product.SellPrice,
|
|
Status: "pending",
|
|
}
|
|
orderInsertResult, insertOrderErr := l.svcCtx.OrderModel.Insert(ctx, session, &order)
|
|
if insertOrderErr != nil {
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "家政服务, 保存订单失败: %+v", insertOrderErr)
|
|
}
|
|
insertedOrderID, lastInsertIdErr := orderInsertResult.LastInsertId()
|
|
if lastInsertIdErr != nil {
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "家政服务, 获取保存订单ID失败: %+v", lastInsertIdErr)
|
|
}
|
|
orderID = insertedOrderID
|
|
query := model.Query{
|
|
OrderId: orderID,
|
|
UserId: userID,
|
|
ProductId: product.Id,
|
|
QueryParams: req.Data,
|
|
QueryState: "pending",
|
|
}
|
|
_, insertQueryErr := l.svcCtx.QueryModel.Insert(l.ctx, session, &query)
|
|
if insertQueryErr != nil {
|
|
return insertQueryErr
|
|
}
|
|
return nil
|
|
})
|
|
if transErr != nil {
|
|
return nil, transErr
|
|
}
|
|
|
|
return &types.QueryResp{PrepayID: prepayID, OrderID: orderID}, nil
|
|
}
|