fix: handle aliyun captcha error (#1624)

This commit is contained in:
Yaodong Yu 2023-03-06 21:23:03 +08:00 committed by GitHub
parent fea2a8cdbe
commit 16b1d0e1f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -31,6 +31,16 @@ import (
const AliyunCaptchaVerifyUrl = "http://afs.aliyuncs.com" const AliyunCaptchaVerifyUrl = "http://afs.aliyuncs.com"
type captchaSuccessResponse struct {
Code int `json:"Code"`
Msg string `json:"Msg"`
}
type captchaFailResponse struct {
Code string `json:"Code"`
Message string `json:"Message"`
}
type AliyunCaptchaProvider struct{} type AliyunCaptchaProvider struct{}
func NewAliyunCaptchaProvider() *AliyunCaptchaProvider { func NewAliyunCaptchaProvider() *AliyunCaptchaProvider {
@ -85,19 +95,20 @@ func (captcha *AliyunCaptchaProvider) VerifyCaptcha(token, clientSecret string)
return false, err return false, err
} }
type captchaResponse struct { return handleCaptchaResponse(body)
Code int `json:"Code"` }
Msg string `json:"Msg"`
}
captchaResp := &captchaResponse{}
err = json.Unmarshal(body, captchaResp) func handleCaptchaResponse(body []byte) (bool, error) {
captchaResp := &captchaSuccessResponse{}
err := json.Unmarshal(body, captchaResp)
if err != nil { if err != nil {
return false, err captchaFailResp := &captchaFailResponse{}
} err = json.Unmarshal(body, captchaFailResp)
if err != nil {
return false, err
}
if captchaResp.Code != 100 { return false, errors.New(captchaFailResp.Message)
return false, errors.New(captchaResp.Msg)
} }
return true, nil return true, nil