Files
tyass-server/pkg/captcha/aliyun.go
2026-02-27 12:53:27 +08:00

53 lines
1.5 KiB
Go

package captcha
import (
"os"
"github.com/pkg/errors"
"tyass-server/common/xerr"
captcha20230305 "github.com/alibabacloud-go/captcha-20230305/client"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
"github.com/alibabacloud-go/tea/tea"
)
type Config struct {
AccessKeyID string
AccessKeySecret string
EndpointURL string
SceneID string
}
func Verify(cfg Config, captchaVerifyParam string) error {
if os.Getenv("ENV") == "development" {
return nil
}
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)
client, err := captcha20230305.NewClient(clientCfg)
if err != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "captcha client create error: %+v", err)
}
req := &captcha20230305.VerifyIntelligentCaptchaRequest{
SceneId: tea.String(cfg.SceneID),
CaptchaVerifyParam: tea.String(captchaVerifyParam),
}
resp, err := client.VerifyIntelligentCaptcha(req)
if err != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "captcha verify error: %+v", err)
}
if tea.BoolValue(resp.Body.Result.VerifyResult) {
return nil
}
return errors.Wrapf(xerr.NewErrMsg("图形验证码校验失败"), "verifyResult=false")
}