This commit is contained in:
Mrx
2026-02-27 12:06:51 +08:00
parent 7805d795c3
commit 38c4f65b5d
9 changed files with 137 additions and 22 deletions

View File

@@ -142,7 +142,8 @@ service main {
type (
sendSmsReq {
Mobile string `json:"mobile" validate:"required,mobile"`
ActionType string `json:"actionType" validate:"required,oneof=login register query agentApply realName bindMobile"`
CaptchaVerifyParam string `json:"captchaVerifyParam"`
ActionType string `json:"actionType,optional" validate:"oneof=login register query agentApply realName bindMobile"`
}
)

View File

@@ -17,6 +17,12 @@ VerifyCode:
SignName: "海南海宇大数据"
TemplateCode: "SMS_302641455"
ValidTime: 300
Captcha:
AccessKeyID: "LTAI5tKGB3TVJbMHSoZN3yr9"
AccessKeySecret: "OCQ30GWp4yENMjmfOAaagksE18bp65"
EndpointURL: "captcha.cn-shanghai.aliyuncs.com"
SceneID: "wynt39to"
EKey: ""
Encrypt:
SecretKey: "ff83609b2b24fc73196aac3d3dfb874f"
WestConfig:
@@ -89,4 +95,4 @@ Tianyuanapi:
Timeout: 60
Authorization:
FileBaseURL: "https://www.tianyuandb.com/api/v1/auth-docs" # 授权书文件访问基础URL
ExtensionTime: 24 # 佣金解冻延迟时间单位24小时
ExtensionTime: 24 # 佣金解冻延迟时间单位24小时

View File

@@ -19,6 +19,14 @@ VerifyCode:
SignName: "海南海宇大数据"
TemplateCode: "SMS_302641455"
ValidTime: 300
Captcha:
AccessKeyID: "LTAI5tKGB3TVJbMHSoZN3yr9"
AccessKeySecret: "OCQ30GWp4yENMjmfOAaagksE18bp65"
EndpointURL: "captcha.cn-shanghai.aliyuncs.com"
SceneID: "wynt39to"
EKey: ""
Encrypt:
SecretKey: "ff83609b2b24fc73196aac3d3dfb874f"
WestConfig:

View File

@@ -11,6 +11,7 @@ type Config struct {
CacheRedis cache.CacheConf
JwtAuth JwtAuth // JWT 鉴权相关配置
VerifyCode VerifyCode
Captcha CaptchaConfig
Encrypt Encrypt
Alipay AlipayConfig
Wxpay WxpayConfig
@@ -42,6 +43,14 @@ type VerifyCode struct {
TemplateCode string
ValidTime int
}
type CaptchaConfig struct {
AccessKeyID string
AccessKeySecret string
EndpointURL string
SceneID string
EKey string
}
type Encrypt struct {
SecretKey string
}

View File

@@ -2,16 +2,17 @@ package auth
import (
"context"
"tydata-server/common/xerr"
"tydata-server/pkg/lzkit/crypto"
"fmt"
"math/rand"
"time"
"tydata-server/common/xerr"
"tydata-server/pkg/lzkit/crypto"
"github.com/pkg/errors"
"tydata-server/app/main/api/internal/svc"
"tydata-server/app/main/api/internal/types"
"tydata-server/pkg/captcha"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
dysmsapi "github.com/alibabacloud-go/dysmsapi-20170525/v3/client"
@@ -35,13 +36,27 @@ 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
}
// 默认action类型当未传入时默认为login便于小程序环境兼容
action := req.ActionType
if action == "" {
action = "login"
}
secretKey := l.svcCtx.Config.Encrypt.SecretKey
encryptedMobile, err := crypto.EncryptMobile(req.Mobile, secretKey)
if err != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "短信发送, 加密手机号失败: %v", err)
}
// 检查手机号是否在一分钟内已发送过验证码
limitCodeKey := fmt.Sprintf("limit:%s:%s", req.ActionType, encryptedMobile)
limitCodeKey := fmt.Sprintf("limit:%s:%s", action, encryptedMobile)
exists, err := l.svcCtx.Redis.Exists(limitCodeKey)
if err != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "短信发送, 读取redis缓存失败: %s", encryptedMobile)
@@ -62,7 +77,7 @@ func (l *SendSmsLogic) SendSms(req *types.SendSmsReq) error {
if *smsResp.Body.Code != "OK" {
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "短信发送, 阿里客户端响应失败: %s", *smsResp.Body.Message)
}
codeKey := fmt.Sprintf("%s:%s", req.ActionType, encryptedMobile)
codeKey := fmt.Sprintf("%s:%s", action, encryptedMobile)
// 将验证码保存到 Redis设置过期时间
err = l.svcCtx.Redis.Setex(codeKey, code, l.svcCtx.Config.VerifyCode.ValidTime) // 验证码有效期5分钟
if err != nil {

View File

@@ -2220,6 +2220,7 @@ type GetAppVersionResp struct {
}
type SendSmsReq struct {
Mobile string `json:"mobile" validate:"required,mobile"`
ActionType string `json:"actionType" validate:"required,oneof=login register query agentApply realName bindMobile"`
Mobile string `json:"mobile" validate:"required,mobile"`
CaptchaVerifyParam string `json:"captchaVerifyParam"`
ActionType string `json:"actionType" validate:"omitempty,oneof=login register query agentApply realName bindMobile"`
}