2026-02-24 16:47:46 +08:00
|
|
|
package captcha
|
|
|
|
|
|
|
|
|
|
import (
|
2026-02-24 18:42:06 +08:00
|
|
|
"tyc-server/common/xerr"
|
2026-02-24 16:47:46 +08:00
|
|
|
|
|
|
|
|
captcha20230305 "github.com/alibabacloud-go/captcha-20230305/client"
|
|
|
|
|
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
|
|
|
|
|
"github.com/alibabacloud-go/tea/tea"
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Config 阿里云验证码配置(与 api internal config 解耦,供 pkg 使用)
|
|
|
|
|
type Config struct {
|
|
|
|
|
AccessKeyID string
|
|
|
|
|
AccessKeySecret string
|
|
|
|
|
EndpointURL string
|
|
|
|
|
SceneID string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Verify 校验前端传入的 captchaVerifyParam。异常时视为通过以保证业务可用。
|
|
|
|
|
func Verify(cfg Config, captchaVerifyParam string) error {
|
2026-02-24 18:42:06 +08:00
|
|
|
// if os.Getenv("ENV") == "development" {
|
|
|
|
|
// return nil
|
|
|
|
|
// }
|
2026-02-24 16:47:46 +08:00
|
|
|
if captchaVerifyParam == "" {
|
|
|
|
|
return errors.Wrapf(xerr.NewErrMsg("图形验证码校验失败"), "empty captchaVerifyParam")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clientCfg := &openapi.Config{
|
|
|
|
|
AccessKeyId: tea.String(cfg.AccessKeyID),
|
|
|
|
|
AccessKeySecret: tea.String(cfg.AccessKeySecret),
|
|
|
|
|
}
|
|
|
|
|
clientCfg.Endpoint = tea.String(cfg.EndpointURL)
|
|
|
|
|
clientCfg.ConnectTimeout = tea.Int(5000)
|
|
|
|
|
clientCfg.ReadTimeout = tea.Int(5000)
|
|
|
|
|
|
|
|
|
|
client, err := captcha20230305.NewClient(clientCfg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Errorf("init aliyun captcha client error: %+v", err)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req := &captcha20230305.VerifyIntelligentCaptchaRequest{
|
|
|
|
|
SceneId: tea.String(cfg.SceneID),
|
|
|
|
|
CaptchaVerifyParam: tea.String(captchaVerifyParam),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp, err := client.VerifyIntelligentCaptcha(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Errorf("verify aliyun captcha error: %+v", err)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if resp.Body == nil || resp.Body.Result == nil {
|
|
|
|
|
logx.Errorf("verify aliyun captcha empty result, resp: %+v", resp)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if tea.BoolValue(resp.Body.Result.VerifyResult) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
verifyCode := tea.StringValue(resp.Body.Result.VerifyCode)
|
|
|
|
|
logx.Errorf("verify aliyun captcha failed, code: %s", verifyCode)
|
|
|
|
|
return errors.Wrapf(xerr.NewErrMsg("图形验证码校验失败"), "aliyun captcha verify fail, code: %s", verifyCode)
|
|
|
|
|
}
|