Files
ycc-proxy-server/app/main/api/internal/logic/query/queryservicelogic.go

737 lines
25 KiB
Go
Raw Normal View History

2025-11-27 13:09:54 +08:00
package query
import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
2025-12-02 19:57:10 +08:00
"os"
"time"
2025-11-27 13:09:54 +08:00
"ycc-server/app/main/api/internal/service"
"ycc-server/common/ctxdata"
"ycc-server/common/xerr"
"ycc-server/pkg/lzkit/crypto"
"ycc-server/pkg/lzkit/validator"
"github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/stores/redis"
"ycc-server/app/main/api/internal/svc"
"ycc-server/app/main/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type QueryServiceLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewQueryServiceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryServiceLogic {
return &QueryServiceLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *QueryServiceLogic) QueryService(req *types.QueryServiceReq) (resp *types.QueryServiceResp, err error) {
if req.AgentIdentifier != "" {
l.ctx = context.WithValue(l.ctx, "agentIdentifier", req.AgentIdentifier)
} else if req.App {
l.ctx = context.WithValue(l.ctx, "app", req.App)
}
return l.PreprocessLogic(req, req.Product)
}
var productProcessors = map[string]func(*QueryServiceLogic, *types.QueryServiceReq) (*types.QueryServiceResp, error){
"marriage": (*QueryServiceLogic).ProcessMarriageLogic,
"homeservice": (*QueryServiceLogic).ProcessHomeServiceLogic,
"riskassessment": (*QueryServiceLogic).ProcessRiskAssessmentLogic,
"companyinfo": (*QueryServiceLogic).ProcessCompanyInfoLogic,
"rentalinfo": (*QueryServiceLogic).ProcessRentalInfoLogic,
"preloanbackgroundcheck": (*QueryServiceLogic).ProcessPreLoanBackgroundCheckLogic,
"backgroundcheck": (*QueryServiceLogic).ProcessBackgroundCheckLogic,
"personalData": (*QueryServiceLogic).ProcessPersonalDataLogic,
"consumerFinanceReport": (*QueryServiceLogic).ProcessConsumerFinanceReportLogic,
}
func (l *QueryServiceLogic) PreprocessLogic(req *types.QueryServiceReq, product string) (*types.QueryServiceResp, error) {
if processor, exists := productProcessors[product]; exists {
2025-12-10 13:10:39 +08:00
return processor(l, req) // 璋冪敤瀵瑰簲鐨勫鐞嗗嚱鏁?
2025-11-27 13:09:54 +08:00
}
2025-12-10 13:10:39 +08:00
return nil, errors.New("鏈壘鍒扮浉搴旂殑澶勭悊绋嬪簭")
2025-11-27 13:09:54 +08:00
}
func (l *QueryServiceLogic) ProcessMarriageLogic(req *types.QueryServiceReq) (*types.QueryServiceResp, error) {
2025-12-10 13:10:39 +08:00
// AES瑙
2025-11-27 13:09:54 +08:00
decryptData, DecryptDataErr := l.DecryptData(req.Data)
if DecryptDataErr != nil {
return nil, DecryptDataErr
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙鍙傛暟
2025-11-27 13:09:54 +08:00
var data types.MarriageReq
if unmarshalErr := json.Unmarshal(decryptData, &data); unmarshalErr != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 瑙e瘑鍚庣殑鏁版嵁鏍煎紡涓嶆纭? %+v", unmarshalErr)
2025-11-27 13:09:54 +08:00
}
if validatorErr := validator.Validate(data); validatorErr != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCodeMsg(xerr.PARAM_VERIFICATION_ERROR, validatorErr.Error()), "鏌ヨ鏈嶅姟, 鍙傛暟涓嶆纭? %+v", validatorErr)
2025-11-27 13:09:54 +08:00
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙楠岃瘉鐮?
2025-11-27 13:09:54 +08:00
verifyCodeErr := l.VerifyCode(data.Mobile, data.Code)
if verifyCodeErr != nil {
return nil, verifyCodeErr
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙涓夎绱?
2025-11-27 13:09:54 +08:00
verifyErr := l.Verify(data.Name, data.IDCard, data.Mobile)
if verifyErr != nil {
return nil, verifyErr
}
2025-12-10 13:10:39 +08:00
// 缂撳瓨
2025-11-27 13:09:54 +08:00
params := map[string]interface{}{
"name": data.Name,
"id_card": data.IDCard,
"mobile": data.Mobile,
}
2025-12-10 13:10:39 +08:00
userID, err := l.GetOrCreateUser()
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 澶勭悊鐢ㄦ埛澶辫触: %v", err)
}
2025-11-27 13:09:54 +08:00
cacheNo, cacheDataErr := l.CacheData(params, "marriage", userID)
if cacheDataErr != nil {
return nil, cacheDataErr
}
2025-12-10 13:10:39 +08:00
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID)
2025-11-27 13:09:54 +08:00
if err != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 鐢熸垚token澶辫触 : %v", err)
2025-11-27 13:09:54 +08:00
}
2025-12-10 13:10:39 +08:00
// 鑾峰彇褰撳墠鏃堕棿鎴?
2025-11-27 13:09:54 +08:00
now := time.Now().Unix()
return &types.QueryServiceResp{
Id: cacheNo,
AccessToken: token,
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
}, nil
}
2025-12-10 13:10:39 +08:00
// 澶勭悊瀹舵斂鏈嶅姟鐩稿叧閫昏緫
2025-11-27 13:09:54 +08:00
func (l *QueryServiceLogic) ProcessHomeServiceLogic(req *types.QueryServiceReq) (*types.QueryServiceResp, error) {
2025-12-10 13:10:39 +08:00
// AES瑙
2025-11-27 13:09:54 +08:00
decryptData, DecryptDataErr := l.DecryptData(req.Data)
if DecryptDataErr != nil {
return nil, DecryptDataErr
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙鍙傛暟
2025-11-27 13:09:54 +08:00
var data types.HomeServiceReq
if unmarshalErr := json.Unmarshal(decryptData, &data); unmarshalErr != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 瑙e瘑鍚庣殑鏁版嵁鏍煎紡涓嶆纭? %+v", unmarshalErr)
2025-11-27 13:09:54 +08:00
}
if validatorErr := validator.Validate(data); validatorErr != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCodeMsg(xerr.PARAM_VERIFICATION_ERROR, validatorErr.Error()), "鏌ヨ鏈嶅姟, 鍙傛暟涓嶆纭? %+v", validatorErr)
2025-11-27 13:09:54 +08:00
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙楠岃瘉鐮?
2025-11-27 13:09:54 +08:00
verifyCodeErr := l.VerifyCode(data.Mobile, data.Code)
if verifyCodeErr != nil {
return nil, verifyCodeErr
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙涓夎绱?
2025-11-27 13:09:54 +08:00
verifyErr := l.Verify(data.Name, data.IDCard, data.Mobile)
if verifyErr != nil {
return nil, verifyErr
}
2025-12-10 13:10:39 +08:00
// 缂撳瓨
2025-11-27 13:09:54 +08:00
params := map[string]interface{}{
"name": data.Name,
"id_card": data.IDCard,
"mobile": data.Mobile,
}
2025-12-10 13:10:39 +08:00
userID, err := l.GetOrCreateUser()
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 澶勭悊鐢ㄦ埛澶辫触: %v", err)
}
2025-11-27 13:09:54 +08:00
cacheNo, cacheDataErr := l.CacheData(params, "homeservice", userID)
if cacheDataErr != nil {
return nil, cacheDataErr
}
2025-12-10 13:10:39 +08:00
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID)
2025-11-27 13:09:54 +08:00
if err != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 鐢熸垚token澶辫触 : %d", userID)
2025-11-27 13:09:54 +08:00
}
2025-12-10 13:10:39 +08:00
// 鑾峰彇褰撳墠鏃堕棿鎴?
2025-11-27 13:09:54 +08:00
now := time.Now().Unix()
return &types.QueryServiceResp{
Id: cacheNo,
AccessToken: token,
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
}, nil
}
2025-12-10 13:10:39 +08:00
// 澶勭悊椋庨櫓璇勪及鐩稿叧閫昏緫
2025-11-27 13:09:54 +08:00
func (l *QueryServiceLogic) ProcessRiskAssessmentLogic(req *types.QueryServiceReq) (*types.QueryServiceResp, error) {
2025-12-10 13:10:39 +08:00
// AES瑙
2025-11-27 13:09:54 +08:00
decryptData, DecryptDataErr := l.DecryptData(req.Data)
if DecryptDataErr != nil {
return nil, DecryptDataErr
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙鍙傛暟
2025-11-27 13:09:54 +08:00
var data types.RiskAssessmentReq
if unmarshalErr := json.Unmarshal(decryptData, &data); unmarshalErr != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 瑙e瘑鍚庣殑鏁版嵁鏍煎紡涓嶆纭? %+v", unmarshalErr)
2025-11-27 13:09:54 +08:00
}
if validatorErr := validator.Validate(data); validatorErr != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCodeMsg(xerr.PARAM_VERIFICATION_ERROR, validatorErr.Error()), "鏌ヨ鏈嶅姟, 鍙傛暟涓嶆纭? %+v", validatorErr)
2025-11-27 13:09:54 +08:00
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙楠岃瘉鐮?
2025-11-27 13:09:54 +08:00
verifyCodeErr := l.VerifyCode(data.Mobile, data.Code)
if verifyCodeErr != nil {
return nil, verifyCodeErr
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙涓夎绱?
2025-11-27 13:09:54 +08:00
verifyErr := l.Verify(data.Name, data.IDCard, data.Mobile)
if verifyErr != nil {
return nil, verifyErr
}
2025-12-10 13:10:39 +08:00
// 缂撳瓨
2025-11-27 13:09:54 +08:00
params := map[string]interface{}{
"name": data.Name,
"id_card": data.IDCard,
"mobile": data.Mobile,
}
2025-12-10 13:10:39 +08:00
userID, err := l.GetOrCreateUser()
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 澶勭悊鐢ㄦ埛澶辫触: %v", err)
}
2025-11-27 13:09:54 +08:00
cacheNo, cacheDataErr := l.CacheData(params, "riskassessment", userID)
if cacheDataErr != nil {
return nil, cacheDataErr
}
2025-12-10 13:10:39 +08:00
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID)
2025-11-27 13:09:54 +08:00
if err != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 鐢熸垚token澶辫触 : %d", userID)
2025-11-27 13:09:54 +08:00
}
2025-12-10 13:10:39 +08:00
// 鑾峰彇褰撳墠鏃堕棿鎴?
2025-11-27 13:09:54 +08:00
now := time.Now().Unix()
return &types.QueryServiceResp{
Id: cacheNo,
AccessToken: token,
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
}, nil
}
2025-12-10 13:10:39 +08:00
// 澶勭悊鍏徃淇℃伅鏌ヨ鐩稿叧閫昏緫
2025-11-27 13:09:54 +08:00
func (l *QueryServiceLogic) ProcessCompanyInfoLogic(req *types.QueryServiceReq) (*types.QueryServiceResp, error) {
2025-12-10 13:10:39 +08:00
// AES瑙
2025-11-27 13:09:54 +08:00
decryptData, DecryptDataErr := l.DecryptData(req.Data)
if DecryptDataErr != nil {
return nil, DecryptDataErr
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙鍙傛暟
2025-11-27 13:09:54 +08:00
var data types.CompanyInfoReq
if unmarshalErr := json.Unmarshal(decryptData, &data); unmarshalErr != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 瑙e瘑鍚庣殑鏁版嵁鏍煎紡涓嶆纭? %+v", unmarshalErr)
2025-11-27 13:09:54 +08:00
}
if validatorErr := validator.Validate(data); validatorErr != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCodeMsg(xerr.PARAM_VERIFICATION_ERROR, validatorErr.Error()), "鏌ヨ鏈嶅姟, 鍙傛暟涓嶆纭? %+v", validatorErr)
2025-11-27 13:09:54 +08:00
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙楠岃瘉鐮?
2025-11-27 13:09:54 +08:00
verifyCodeErr := l.VerifyCode(data.Mobile, data.Code)
if verifyCodeErr != nil {
return nil, verifyCodeErr
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙涓夎绱?
2025-11-27 13:09:54 +08:00
verifyErr := l.Verify(data.Name, data.IDCard, data.Mobile)
if verifyErr != nil {
return nil, verifyErr
}
2025-12-10 13:10:39 +08:00
// 缂撳瓨
2025-11-27 13:09:54 +08:00
params := map[string]interface{}{
"name": data.Name,
"id_card": data.IDCard,
"mobile": data.Mobile,
}
2025-12-10 13:10:39 +08:00
userID, err := l.GetOrCreateUser()
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 澶勭悊鐢ㄦ埛澶辫触: %v", err)
}
2025-11-27 13:09:54 +08:00
cacheNo, cacheDataErr := l.CacheData(params, "companyinfo", userID)
if cacheDataErr != nil {
return nil, cacheDataErr
}
2025-12-10 13:10:39 +08:00
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID)
2025-11-27 13:09:54 +08:00
if err != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 鐢熸垚token澶辫触 : %d", userID)
2025-11-27 13:09:54 +08:00
}
2025-12-10 13:10:39 +08:00
// 鑾峰彇褰撳墠鏃堕棿鎴?
2025-11-27 13:09:54 +08:00
now := time.Now().Unix()
return &types.QueryServiceResp{
Id: cacheNo,
AccessToken: token,
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
}, nil
}
2025-12-10 13:10:39 +08:00
// 澶勭悊绉熻祦淇℃伅鏌ヨ鐩稿叧閫昏緫
2025-11-27 13:09:54 +08:00
func (l *QueryServiceLogic) ProcessRentalInfoLogic(req *types.QueryServiceReq) (*types.QueryServiceResp, error) {
2025-12-10 13:10:39 +08:00
// AES瑙
2025-11-27 13:09:54 +08:00
decryptData, DecryptDataErr := l.DecryptData(req.Data)
if DecryptDataErr != nil {
return nil, DecryptDataErr
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙鍙傛暟
2025-11-27 13:09:54 +08:00
var data types.RentalInfoReq
if unmarshalErr := json.Unmarshal(decryptData, &data); unmarshalErr != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 瑙e瘑鍚庣殑鏁版嵁鏍煎紡涓嶆纭? %+v", unmarshalErr)
2025-11-27 13:09:54 +08:00
}
if validatorErr := validator.Validate(data); validatorErr != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCodeMsg(xerr.PARAM_VERIFICATION_ERROR, validatorErr.Error()), "鏌ヨ鏈嶅姟, 鍙傛暟涓嶆纭? %+v", validatorErr)
2025-11-27 13:09:54 +08:00
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙楠岃瘉鐮?
2025-11-27 13:09:54 +08:00
verifyCodeErr := l.VerifyCode(data.Mobile, data.Code)
if verifyCodeErr != nil {
return nil, verifyCodeErr
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙涓夎绱?
2025-11-27 13:09:54 +08:00
verifyErr := l.Verify(data.Name, data.IDCard, data.Mobile)
if verifyErr != nil {
return nil, verifyErr
}
2025-12-10 13:10:39 +08:00
// 缂撳瓨
2025-11-27 13:09:54 +08:00
params := map[string]interface{}{
"name": data.Name,
"id_card": data.IDCard,
"mobile": data.Mobile,
}
2025-12-10 13:10:39 +08:00
userID, err := l.GetOrCreateUser()
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 澶勭悊鐢ㄦ埛澶辫触: %v", err)
}
2025-11-27 13:09:54 +08:00
cacheNo, cacheDataErr := l.CacheData(params, "rentalinfo", userID)
if cacheDataErr != nil {
return nil, cacheDataErr
}
2025-12-10 13:10:39 +08:00
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID)
2025-11-27 13:09:54 +08:00
if err != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 鐢熸垚token澶辫触 : %d", userID)
2025-11-27 13:09:54 +08:00
}
2025-12-10 13:10:39 +08:00
// 鑾峰彇褰撳墠鏃堕棿鎴?
2025-11-27 13:09:54 +08:00
now := time.Now().Unix()
return &types.QueryServiceResp{
Id: cacheNo,
AccessToken: token,
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
}, nil
}
2025-12-10 13:10:39 +08:00
// 澶勭悊璐峰墠鑳屾櫙妫€鏌ョ浉鍏抽€昏緫
2025-11-27 13:09:54 +08:00
func (l *QueryServiceLogic) ProcessPreLoanBackgroundCheckLogic(req *types.QueryServiceReq) (*types.QueryServiceResp, error) {
2025-12-10 13:10:39 +08:00
// AES瑙
2025-11-27 13:09:54 +08:00
decryptData, DecryptDataErr := l.DecryptData(req.Data)
if DecryptDataErr != nil {
return nil, DecryptDataErr
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙鍙傛暟
2025-11-27 13:09:54 +08:00
var data types.PreLoanBackgroundCheckReq
if unmarshalErr := json.Unmarshal(decryptData, &data); unmarshalErr != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 瑙e瘑鍚庣殑鏁版嵁鏍煎紡涓嶆纭? %+v", unmarshalErr)
2025-11-27 13:09:54 +08:00
}
if validatorErr := validator.Validate(data); validatorErr != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCodeMsg(xerr.PARAM_VERIFICATION_ERROR, validatorErr.Error()), "鏌ヨ鏈嶅姟, 鍙傛暟涓嶆纭? %+v", validatorErr)
2025-11-27 13:09:54 +08:00
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙楠岃瘉鐮?
2025-11-27 13:09:54 +08:00
verifyCodeErr := l.VerifyCode(data.Mobile, data.Code)
if verifyCodeErr != nil {
return nil, verifyCodeErr
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙涓夎绱?
2025-11-27 13:09:54 +08:00
verifyErr := l.Verify(data.Name, data.IDCard, data.Mobile)
if verifyErr != nil {
return nil, verifyErr
}
2025-12-10 13:10:39 +08:00
// 缂撳瓨
2025-11-27 13:09:54 +08:00
params := map[string]interface{}{
"name": data.Name,
"id_card": data.IDCard,
"mobile": data.Mobile,
}
2025-12-10 13:10:39 +08:00
userID, err := l.GetOrCreateUser()
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 澶勭悊鐢ㄦ埛澶辫触: %v", err)
}
2025-11-27 13:09:54 +08:00
cacheNo, cacheDataErr := l.CacheData(params, "preloanbackgroundcheck", userID)
if cacheDataErr != nil {
return nil, cacheDataErr
}
2025-12-10 13:10:39 +08:00
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID)
2025-11-27 13:09:54 +08:00
if err != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 鐢熸垚token澶辫触 : %d", userID)
2025-11-27 13:09:54 +08:00
}
2025-12-10 13:10:39 +08:00
// 鑾峰彇褰撳墠鏃堕棿鎴?
2025-11-27 13:09:54 +08:00
now := time.Now().Unix()
return &types.QueryServiceResp{
Id: cacheNo,
AccessToken: token,
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
}, nil
}
2025-12-10 13:10:39 +08:00
// 澶勭悊浜轰簨鑳岃皟鐩稿叧閫昏緫
2025-11-27 13:09:54 +08:00
func (l *QueryServiceLogic) ProcessBackgroundCheckLogic(req *types.QueryServiceReq) (*types.QueryServiceResp, error) {
2025-12-10 13:10:39 +08:00
// AES瑙
2025-11-27 13:09:54 +08:00
decryptData, DecryptDataErr := l.DecryptData(req.Data)
if DecryptDataErr != nil {
return nil, DecryptDataErr
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙鍙傛暟
2025-11-27 13:09:54 +08:00
var data types.BackgroundCheckReq
if unmarshalErr := json.Unmarshal(decryptData, &data); unmarshalErr != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 瑙e瘑鍚庣殑鏁版嵁鏍煎紡涓嶆纭? %+v", unmarshalErr)
2025-11-27 13:09:54 +08:00
}
if validatorErr := validator.Validate(data); validatorErr != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCodeMsg(xerr.PARAM_VERIFICATION_ERROR, validatorErr.Error()), "鏌ヨ鏈嶅姟, 鍙傛暟涓嶆纭? %+v", validatorErr)
2025-11-27 13:09:54 +08:00
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙楠岃瘉鐮?
2025-11-27 13:09:54 +08:00
verifyCodeErr := l.VerifyCode(data.Mobile, data.Code)
if verifyCodeErr != nil {
return nil, verifyCodeErr
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙涓夎绱?
2025-11-27 13:09:54 +08:00
verifyErr := l.Verify(data.Name, data.IDCard, data.Mobile)
if verifyErr != nil {
return nil, verifyErr
}
2025-12-10 13:10:39 +08:00
// 缂撳瓨
2025-11-27 13:09:54 +08:00
params := map[string]interface{}{
"name": data.Name,
"id_card": data.IDCard,
"mobile": data.Mobile,
}
2025-12-10 13:10:39 +08:00
userID, err := l.GetOrCreateUser()
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 澶勭悊鐢ㄦ埛澶辫触: %v", err)
}
2025-11-27 13:09:54 +08:00
cacheNo, cacheDataErr := l.CacheData(params, "backgroundcheck", userID)
if cacheDataErr != nil {
return nil, cacheDataErr
}
2025-12-10 13:10:39 +08:00
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID)
2025-11-27 13:09:54 +08:00
if err != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 鐢熸垚token澶辫触 : %d", userID)
2025-11-27 13:09:54 +08:00
}
2025-12-10 13:10:39 +08:00
// 鑾峰彇褰撳墠鏃堕棿鎴?
2025-11-27 13:09:54 +08:00
now := time.Now().Unix()
return &types.QueryServiceResp{
Id: cacheNo,
AccessToken: token,
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
}, nil
}
func (l *QueryServiceLogic) ProcessPersonalDataLogic(req *types.QueryServiceReq) (*types.QueryServiceResp, error) {
2025-12-10 13:10:39 +08:00
// AES瑙
2025-11-27 13:09:54 +08:00
decryptData, DecryptDataErr := l.DecryptData(req.Data)
if DecryptDataErr != nil {
return nil, DecryptDataErr
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙鍙傛暟
2025-11-27 13:09:54 +08:00
var data types.PersonalDataReq
if unmarshalErr := json.Unmarshal(decryptData, &data); unmarshalErr != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 瑙e瘑鍚庣殑鏁版嵁鏍煎紡涓嶆纭? %+v", unmarshalErr)
2025-11-27 13:09:54 +08:00
}
if validatorErr := validator.Validate(data); validatorErr != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCodeMsg(xerr.PARAM_VERIFICATION_ERROR, validatorErr.Error()), "鏌ヨ鏈嶅姟, 鍙傛暟涓嶆纭? %+v", validatorErr)
2025-11-27 13:09:54 +08:00
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙楠岃瘉鐮?
2025-11-27 13:09:54 +08:00
verifyCodeErr := l.VerifyCode(data.Mobile, data.Code)
if verifyCodeErr != nil {
return nil, verifyCodeErr
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙涓夎绱?
2025-11-27 13:09:54 +08:00
verifyErr := l.Verify(data.Name, data.IDCard, data.Mobile)
if verifyErr != nil {
return nil, verifyErr
}
2025-12-10 13:10:39 +08:00
// 缂撳瓨
2025-11-27 13:09:54 +08:00
params := map[string]interface{}{
"name": data.Name,
"id_card": data.IDCard,
"mobile": data.Mobile,
}
2025-12-10 13:10:39 +08:00
userID, err := l.GetOrCreateUser()
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 澶勭悊鐢ㄦ埛澶辫触: %v", err)
}
2025-11-27 13:09:54 +08:00
cacheNo, cacheDataErr := l.CacheData(params, "personalData", userID)
if cacheDataErr != nil {
return nil, cacheDataErr
}
2025-12-10 13:10:39 +08:00
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID)
2025-11-27 13:09:54 +08:00
if err != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 鐢熸垚token澶辫触 : %d", userID)
2025-11-27 13:09:54 +08:00
}
2025-12-10 13:10:39 +08:00
// 鑾峰彇褰撳墠鏃堕棿鎴?
2025-11-27 13:09:54 +08:00
now := time.Now().Unix()
return &types.QueryServiceResp{
Id: cacheNo,
AccessToken: token,
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
}, nil
}
func (l *QueryServiceLogic) ProcessConsumerFinanceReportLogic(req *types.QueryServiceReq) (*types.QueryServiceResp, error) {
2025-12-10 13:10:39 +08:00
// AES瑙
2025-11-27 13:09:54 +08:00
decryptData, DecryptDataErr := l.DecryptData(req.Data)
if DecryptDataErr != nil {
return nil, DecryptDataErr
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙鍙傛暟
2025-11-27 13:09:54 +08:00
var data types.ConsumerFinanceReportReq
if unmarshalErr := json.Unmarshal(decryptData, &data); unmarshalErr != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 瑙e瘑鍚庣殑鏁版嵁鏍煎紡涓嶆纭? %+v", unmarshalErr)
2025-11-27 13:09:54 +08:00
}
if validatorErr := validator.Validate(data); validatorErr != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCodeMsg(xerr.PARAM_VERIFICATION_ERROR, validatorErr.Error()), "鏌ヨ鏈嶅姟, 鍙傛暟涓嶆纭? %+v", validatorErr)
2025-11-27 13:09:54 +08:00
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙楠岃瘉鐮?
2025-11-27 13:09:54 +08:00
verifyCodeErr := l.VerifyCode(data.Mobile, data.Code)
if verifyCodeErr != nil {
return nil, verifyCodeErr
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙涓夎绱?
2025-11-27 13:09:54 +08:00
verifyErr := l.Verify(data.Name, data.IDCard, data.Mobile)
if verifyErr != nil {
return nil, verifyErr
}
2025-12-10 13:10:39 +08:00
// 缂撳瓨
2025-11-27 13:09:54 +08:00
params := map[string]interface{}{
"name": data.Name,
"id_card": data.IDCard,
"mobile": data.Mobile,
}
2025-12-10 13:10:39 +08:00
userID, err := l.GetOrCreateUser()
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 澶勭悊鐢ㄦ埛澶辫触: %v", err)
}
2025-12-02 19:57:10 +08:00
cacheNo, cacheDataErr := l.CacheData(params, "consumerFinanceReport", userID)
2025-11-27 13:09:54 +08:00
if cacheDataErr != nil {
return nil, cacheDataErr
}
2025-12-10 13:10:39 +08:00
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID)
2025-11-27 13:09:54 +08:00
if err != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 鐢熸垚token澶辫触 : %d", userID)
2025-11-27 13:09:54 +08:00
}
2025-12-10 13:10:39 +08:00
// 鑾峰彇褰撳墠鏃堕棿鎴?
2025-11-27 13:09:54 +08:00
now := time.Now().Unix()
return &types.QueryServiceResp{
Id: cacheNo,
AccessToken: token,
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
}, nil
}
func (l *QueryServiceLogic) DecryptData(data string) ([]byte, error) {
secretKey := l.svcCtx.Config.Encrypt.SecretKey
key, decodeErr := hex.DecodeString(secretKey)
if decodeErr != nil {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "瀵嗛挜鑾峰彇澶辫触: %+v", decodeErr)
2025-11-27 13:09:54 +08:00
}
decryptData, aesDecryptErr := crypto.AesDecrypt(data, key)
if aesDecryptErr != nil || len(decryptData) == 0 {
2025-12-10 13:10:39 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "瑙e瘑澶辫触: %+v", aesDecryptErr)
2025-11-27 13:09:54 +08:00
}
return decryptData, nil
}
2025-12-10 13:10:39 +08:00
// 鏍¢獙楠岃瘉鐮?
2025-11-27 13:09:54 +08:00
func (l *QueryServiceLogic) VerifyCode(mobile string, code string) error {
2025-12-10 13:10:39 +08:00
// 寮€鍙戠幆澧冧笅璺宠繃楠岃瘉鐮佹牎楠?
2025-12-02 19:57:10 +08:00
if os.Getenv("ENV") == "development" {
return nil
}
2025-11-27 13:09:54 +08:00
secretKey := l.svcCtx.Config.Encrypt.SecretKey
encryptedMobile, err := crypto.EncryptMobile(mobile, secretKey)
if err != nil {
2025-12-10 13:10:39 +08:00
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鍔犲瘑鎵嬫満鍙峰け璐? %+v", err)
2025-11-27 13:09:54 +08:00
}
codeRedisKey := fmt.Sprintf("%s:%s", "query", encryptedMobile)
cacheCode, err := l.svcCtx.Redis.Get(codeRedisKey)
if err != nil {
if errors.Is(err, redis.Nil) {
2025-12-10 13:10:39 +08:00
return errors.Wrapf(xerr.NewErrMsg("楠岃瘉鐮佸凡杩囨湡"), "楠岃瘉鐮佽繃鏈? %s", mobile)
2025-11-27 13:09:54 +08:00
}
2025-12-10 13:10:39 +08:00
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "璇诲彇楠岃瘉鐮乺edis缂撳瓨澶辫触, mobile: %s, err: %+v", mobile, err)
2025-11-27 13:09:54 +08:00
}
if cacheCode != code {
2025-12-10 13:10:39 +08:00
return errors.Wrapf(xerr.NewErrMsg("楠岃瘉鐮佷笉姝g‘"), "楠岃瘉鐮佷笉姝g‘: %s", mobile)
2025-11-27 13:09:54 +08:00
}
return nil
}
2025-12-10 13:10:39 +08:00
// 浜屻€佷笁瑕佺礌楠岃瘉
2025-11-27 13:09:54 +08:00
func (l *QueryServiceLogic) Verify(Name string, IDCard string, Mobile string) error {
2025-12-10 13:10:39 +08:00
// 寮€鍙戠幆澧冧笅璺宠繃浜?涓夎绱犻獙璇?
2025-12-02 19:57:10 +08:00
if os.Getenv("ENV") == "development" {
return nil
}
2025-11-27 13:09:54 +08:00
if !l.svcCtx.Config.SystemConfig.ThreeVerify {
twoVerification := service.TwoFactorVerificationRequest{
Name: Name,
IDCard: IDCard,
}
verification, err := l.svcCtx.VerificationService.TwoFactorVerification(twoVerification)
if err != nil {
2025-12-10 13:10:39 +08:00
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "浜岃绱犻獙璇佸け璐? %v", err)
2025-11-27 13:09:54 +08:00
}
if !verification.Passed {
2025-12-10 13:10:39 +08:00
return errors.Wrapf(xerr.NewErrCodeMsg(xerr.SERVER_COMMON_ERROR, verification.Err.Error()), "浜岃绱犻獙璇佷笉閫氳繃: %v", err)
2025-11-27 13:09:54 +08:00
}
} else {
2025-12-10 13:10:39 +08:00
// 涓夎绱犻獙璇?
2025-11-27 13:09:54 +08:00
threeVerification := service.ThreeFactorVerificationRequest{
Name: Name,
IDCard: IDCard,
Mobile: Mobile,
}
verification, err := l.svcCtx.VerificationService.ThreeFactorVerification(threeVerification)
if err != nil {
2025-12-10 13:10:39 +08:00
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "涓夎绱犻獙璇佸け璐? %v", err)
2025-11-27 13:09:54 +08:00
}
if !verification.Passed {
2025-12-10 13:10:39 +08:00
return errors.Wrapf(xerr.NewErrCodeMsg(xerr.SERVER_COMMON_ERROR, verification.Err.Error()), "涓夎绱犻獙璇佷笉閫氳繃: %v", err)
2025-11-27 13:09:54 +08:00
}
}
return nil
}
2025-12-10 13:10:39 +08:00
// 缂撳瓨
2025-12-09 18:55:28 +08:00
func (l *QueryServiceLogic) CacheData(params map[string]interface{}, Product string, userID string) (string, error) {
2025-11-27 13:09:54 +08:00
agentIdentifier, _ := l.ctx.Value("agentIdentifier").(string)
secretKey := l.svcCtx.Config.Encrypt.SecretKey
key, decodeErr := hex.DecodeString(secretKey)
if decodeErr != nil {
2025-12-10 13:10:39 +08:00
return "", errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 鑾峰彇AES瀵嗛挜澶辫触: %+v", decodeErr)
2025-11-27 13:09:54 +08:00
}
paramsMarshal, marshalErr := json.Marshal(params)
if marshalErr != nil {
2025-12-10 13:10:39 +08:00
return "", errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 搴忓垪鍖栧弬鏁板け璐? %+v", marshalErr)
2025-11-27 13:09:54 +08:00
}
encryptParams, aesEncryptErr := crypto.AesEncrypt(paramsMarshal, key)
if aesEncryptErr != nil {
2025-12-10 13:10:39 +08:00
return "", errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 鍔犲瘑鍙傛暟澶辫触: %+v", aesEncryptErr)
2025-11-27 13:09:54 +08:00
}
queryCache := types.QueryCacheLoad{
Params: encryptParams,
Product: Product,
AgentIdentifier: agentIdentifier,
}
jsonData, marshalErr := json.Marshal(queryCache)
if marshalErr != nil {
2025-12-10 13:10:39 +08:00
return "", errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "鏌ヨ鏈嶅姟, 搴忓垪鍖栧弬鏁板け璐? %+v", marshalErr)
2025-11-27 13:09:54 +08:00
}
outTradeNo := "Q_" + l.svcCtx.AlipayService.GenerateOutTradeNo()
redisKey := fmt.Sprintf(types.QueryCacheKey, userID, outTradeNo)
cacheErr := l.svcCtx.Redis.SetexCtx(l.ctx, redisKey, string(jsonData), int(2*time.Hour))
if cacheErr != nil {
return "", cacheErr
}
return outTradeNo, nil
}
2025-12-12 13:14:04 +08:00
// GetOrCreateUser 获取或创建用户
// 1. 如果已登录,使用当前登录用户
// 2. 如果未登录创建临时用户UUID用户
// 注意:查询服务不负责手机号绑定,手机号绑定由用户在其他地方自主选择
2025-12-10 13:10:39 +08:00
func (l *QueryServiceLogic) GetOrCreateUser() (string, error) {
2025-12-12 13:14:04 +08:00
// 获取当前登录态
2025-12-10 13:10:39 +08:00
claims, err := ctxdata.GetClaimsFromCtx(l.ctx)
2025-12-12 13:14:04 +08:00
logx.Infof("claims: %+v", claims)
if err != nil && !errors.Is(err, ctxdata.ErrNoInCtx) {
return "", errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取用户信息失败: %v", err)
}
// 如果已登录,使用当前登录用户
if claims != nil {
2025-12-10 13:10:39 +08:00
return claims.UserId, nil
}
2025-12-12 13:14:04 +08:00
// 未登录创建临时用户UUID用户
userID, err := l.svcCtx.UserService.RegisterUUIDUser(l.ctx)
if err != nil {
return "", errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "创建临时用户失败: %v", err)
2025-12-10 13:10:39 +08:00
}
2025-12-12 13:14:04 +08:00
2025-12-10 13:10:39 +08:00
return userID, nil
2025-11-27 13:09:54 +08:00
}