f
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user