add ali captcha

This commit is contained in:
2026-02-24 16:47:46 +08:00
parent 86bda66271
commit 2d7e241b76
16 changed files with 354 additions and 17 deletions

View File

@@ -6,6 +6,7 @@ import (
"math/rand"
"time"
"tyc-server/common/xerr"
"tyc-server/pkg/captcha"
"tyc-server/pkg/lzkit/crypto"
"github.com/pkg/errors"
@@ -35,6 +36,17 @@ func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLo
}
func (l *SendSmsLogic) SendSms(req *types.SendSmsReq) error {
// 图形验证码校验(开发环境可跳过)
cfg := l.svcCtx.Config.Captcha
if err := captcha.Verify(captcha.Config{
AccessKeyID: cfg.AccessKeyID,
AccessKeySecret: cfg.AccessKeySecret,
EndpointURL: cfg.EndpointURL,
SceneID: cfg.SceneID,
}, req.CaptchaVerifyParam); err != nil {
return err
}
secretKey := l.svcCtx.Config.Encrypt.SecretKey
encryptedMobile, err := crypto.EncryptMobile(req.Mobile, secretKey)
if err != nil {

View File

@@ -0,0 +1,38 @@
package captcha
import (
"context"
"tyc-server/app/main/api/internal/svc"
"tyc-server/app/main/api/internal/types"
"tyc-server/pkg/captcha"
"github.com/zeromicro/go-zero/core/logx"
)
type GetEncryptedSceneIdLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetEncryptedSceneIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetEncryptedSceneIdLogic {
return &GetEncryptedSceneIdLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetEncryptedSceneIdLogic) GetEncryptedSceneId() (resp *types.GetEncryptedSceneIdResp, err error) {
cfg := l.svcCtx.Config.Captcha
encrypted, genErr := captcha.GenerateEncryptedSceneID(cfg.SceneID, cfg.EKey, 3600)
if genErr != nil {
// 记录日志,返回通用错误
l.Errorf("generate encrypted scene id error: %+v", genErr)
return nil, genErr
}
return &types.GetEncryptedSceneIdResp{
EncryptedSceneId: encrypted,
}, nil
}

View File

@@ -13,6 +13,7 @@ import (
"tyc-server/common/ctxdata"
"tyc-server/common/globalkey"
"tyc-server/common/xerr"
"tyc-server/pkg/captcha"
"tyc-server/pkg/lzkit/crypto"
"tyc-server/pkg/lzkit/validator"
@@ -109,7 +110,52 @@ var productHandlers = map[string]queryHandlerFunc{
"toc_BankcardBlacklist": runVerifyBankBlackReq,
}
// productHasSmsCode 表示该 product 解密后的请求结构体中是否包含必填短信验证码 Code。
// 有 Code 的产品在「获取验证码」时已经做了滑块,这里不再强制要求 CaptchaVerifyParam。
// 其他产品(无 Code在查询时必须传并校验 CaptchaVerifyParam防止跳过图形验证。
func productHasSmsCode(product string) bool {
switch product {
case "marriage",
"homeservice",
"riskassessment",
"companyinfo",
"rentalinfo",
"preloanbackgroundcheck",
"backgroundcheck",
"personalData",
"toc_PersonalLawsuit",
"toc_EnterpriseLawsuit",
"toc_Marriage",
"toc_PersonalMarriageStatus",
"toc_MarriageStatusRegisterTime",
"toc_MarriageStatusSupplement",
"toc_MarriageStatusVerify",
"toc_DualMarriageStatusRegisterTime",
"toc_VehiclesUnderName",
"toc_VehiclesUnderNamePlate":
return true
default:
return false
}
}
func (l *QueryServiceLogic) PreprocessLogic(req *types.QueryServiceReq, product string) (*types.QueryServiceResp, error) {
// 无短信验证码 Code 的 product查询前必须传并校验滑块否则不允许跳过
if !productHasSmsCode(product) {
if req.CaptchaVerifyParam == "" {
return nil, errors.Wrapf(xerr.NewErrMsg("请完成图形验证"), "product %s requires captcha", product)
}
cfg := l.svcCtx.Config.Captcha
if err := captcha.Verify(captcha.Config{
AccessKeyID: cfg.AccessKeyID,
AccessKeySecret: cfg.AccessKeySecret,
EndpointURL: cfg.EndpointURL,
SceneID: cfg.SceneID,
}, req.CaptchaVerifyParam); err != nil {
return nil, err
}
}
decryptData, err := l.DecryptData(req.Data)
if err != nil {
return nil, err

View File

@@ -38,7 +38,7 @@ func (l *MobileCodeLoginLogic) MobileCodeLogin(req *types.MobileCodeLoginReq) (r
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机登录, 加密手机号失败: %+v", err)
}
// 检查验证码(开发环境可跳过)
// 短信验证码校验(开发环境可跳过)
if os.Getenv("ENV") != "development" {
redisKey := fmt.Sprintf("%s:%s", "login", encryptedMobile)
cacheCode, err := l.svcCtx.Redis.Get(redisKey)