From c4cc15f853c258628bf619611c219fb17df7a9ac Mon Sep 17 00:00:00 2001 From: Mrx <18278715334@163.com> Date: Sat, 28 Feb 2026 17:41:30 +0800 Subject: [PATCH] f --- .../internal/handler/auth/sendsmshandler.go | 6 ++- .../api/internal/logic/auth/sendsmslogic.go | 2 +- pkg/captcha/aliyun.go | 38 +++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/app/main/api/internal/handler/auth/sendsmshandler.go b/app/main/api/internal/handler/auth/sendsmshandler.go index e9d49d6..643b958 100644 --- a/app/main/api/internal/handler/auth/sendsmshandler.go +++ b/app/main/api/internal/handler/auth/sendsmshandler.go @@ -1,6 +1,7 @@ package auth import ( + "context" "net/http" "tyass-server/app/main/api/internal/logic/auth" @@ -23,7 +24,10 @@ func SendSmsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { result.ParamValidateErrorResult(r, w, err) return } - l := auth.NewSendSmsLogic(r.Context(), svcCtx) + + // 将 User-Agent 添加到上下文中 + ctx := context.WithValue(r.Context(), "user-agent", r.Header.Get("User-Agent")) + l := auth.NewSendSmsLogic(ctx, svcCtx) err := l.SendSms(&req) result.HttpResult(r, w, nil, err) } diff --git a/app/main/api/internal/logic/auth/sendsmslogic.go b/app/main/api/internal/logic/auth/sendsmslogic.go index e2d44f8..14a3c50 100644 --- a/app/main/api/internal/logic/auth/sendsmslogic.go +++ b/app/main/api/internal/logic/auth/sendsmslogic.go @@ -38,7 +38,7 @@ func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLo func (l *SendSmsLogic) SendSms(req *types.SendSmsReq) error { // 图形验证码校验 captchaCfg := l.svcCtx.Config.Captcha - if err := captcha.Verify(captcha.Config{ + if err := captcha.VerifyWithContext(l.ctx, captcha.Config{ AccessKeyID: captchaCfg.AccessKeyID, AccessKeySecret: captchaCfg.AccessKeySecret, EndpointURL: captchaCfg.EndpointURL, diff --git a/pkg/captcha/aliyun.go b/pkg/captcha/aliyun.go index 940a637..4208f64 100644 --- a/pkg/captcha/aliyun.go +++ b/pkg/captcha/aliyun.go @@ -1,7 +1,9 @@ package captcha import ( + "context" "os" + "strings" "github.com/pkg/errors" @@ -19,10 +21,23 @@ type Config struct { SceneID string } +// Verify 验证图形验证码,支持微信环境跳过验证 func Verify(cfg Config, captchaVerifyParam string) error { + return VerifyWithContext(context.Background(), cfg, captchaVerifyParam) +} + +// VerifyWithContext 带上下文的验证函数 +func VerifyWithContext(ctx context.Context, cfg Config, captchaVerifyParam string) error { + // 开发环境直接通过 if os.Getenv("ENV") == "development" { return nil } + + // 检查是否为微信环境 + if isWechatEnvironment(ctx) { + return nil + } + if captchaVerifyParam == "" { return errors.Wrapf(xerr.NewErrMsg("图形验证码校验失败"), "empty captchaVerifyParam") } @@ -50,3 +65,26 @@ func Verify(cfg Config, captchaVerifyParam string) error { } return errors.Wrapf(xerr.NewErrMsg("图形验证码校验失败"), "verifyResult=false") } + +// isWechatEnvironment 检查是否为微信环境 +func isWechatEnvironment(ctx context.Context) bool { + // 从上下文中获取请求头 + userAgent := "" + if val := ctx.Value("user-agent"); val != nil { + if ua, ok := val.(string); ok { + userAgent = ua + } + } + if val := ctx.Value("User-Agent"); val != nil { + if ua, ok := val.(string); ok { + userAgent = ua + } + } + + // 检查User-Agent是否包含微信标识 + if strings.Contains(strings.ToLower(userAgent), "micromessenger") { + return true + } + + return false +}