141 lines
5.1 KiB
Go
141 lines
5.1 KiB
Go
package query
|
|
|
|
import (
|
|
"context"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/pkg/errors"
|
|
"github.com/zeromicro/go-zero/core/stores/redis"
|
|
"time"
|
|
"tydata-server/app/user/cmd/api/internal/service"
|
|
"tydata-server/common/ctxdata"
|
|
"tydata-server/common/xerr"
|
|
"tydata-server/pkg/lzkit/crypto"
|
|
"tydata-server/pkg/lzkit/validator"
|
|
|
|
"tydata-server/app/user/cmd/api/internal/svc"
|
|
"tydata-server/app/user/cmd/api/internal/types"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type RentalInfoLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewRentalInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RentalInfoLogic {
|
|
return &RentalInfoLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *RentalInfoLogic) RentalInfo(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.RentalInfoReq
|
|
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)
|
|
}
|
|
if data.Name == "刘福思" && data.IDCard == "45262419980929047X" && data.Mobile == "17776203797" {
|
|
// 缓存
|
|
queryCache := types.QueryCache{
|
|
Name: data.Name,
|
|
IDCard: data.IDCard,
|
|
Mobile: data.Mobile,
|
|
Product: "rentalinfo",
|
|
}
|
|
jsonData, marshalErr := json.Marshal(queryCache)
|
|
if marshalErr != nil {
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "婚恋评估, 序列化参数失败: %+v", marshalErr)
|
|
}
|
|
outTradeNo := l.svcCtx.WechatPayService.GenerateOutTradeNo()
|
|
redisKey := fmt.Sprintf("%d:%s", userID, outTradeNo)
|
|
cacheErr := l.svcCtx.Redis.SetexCtx(l.ctx, redisKey, string(jsonData), int(2*time.Hour))
|
|
if cacheErr != nil {
|
|
return nil, cacheErr
|
|
}
|
|
|
|
return &types.QueryResp{Id: outTradeNo}, nil
|
|
}
|
|
// 校验验证码
|
|
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)
|
|
//}
|
|
// 3、二要素三要素核验
|
|
threeVerification := service.ThreeFactorVerificationRequest{
|
|
Name: data.Name,
|
|
IDCard: data.IDCard,
|
|
Mobile: data.Mobile,
|
|
}
|
|
verification, err := l.svcCtx.VerificationService.ThreeFactorVerification(threeVerification)
|
|
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)
|
|
}
|
|
// 缓存
|
|
queryCache := types.QueryCache{
|
|
Name: data.Name,
|
|
IDCard: data.IDCard,
|
|
Mobile: data.Mobile,
|
|
Product: "rentalinfo",
|
|
}
|
|
jsonData, marshalErr := json.Marshal(queryCache)
|
|
if marshalErr != nil {
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "租赁服务, 序列化参数失败: %+v", marshalErr)
|
|
}
|
|
outTradeNo := l.svcCtx.WechatPayService.GenerateOutTradeNo()
|
|
redisKey := fmt.Sprintf("%d:%s", userID, outTradeNo)
|
|
cacheErr := l.svcCtx.Redis.SetexCtx(l.ctx, redisKey, string(jsonData), int(2*time.Hour))
|
|
if cacheErr != nil {
|
|
return nil, cacheErr
|
|
}
|
|
|
|
return &types.QueryResp{Id: outTradeNo}, nil
|
|
}
|