From 16b1d0e1f001c1162a263ed50c1a892c947d5783 Mon Sep 17 00:00:00 2001 From: Yaodong Yu <2814461814@qq.com> Date: Mon, 6 Mar 2023 21:23:03 +0800 Subject: [PATCH] fix: handle aliyun captcha error (#1624) --- captcha/aliyun.go | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/captcha/aliyun.go b/captcha/aliyun.go index 17695ce8..e275c395 100644 --- a/captcha/aliyun.go +++ b/captcha/aliyun.go @@ -31,6 +31,16 @@ import ( 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{} func NewAliyunCaptchaProvider() *AliyunCaptchaProvider { @@ -85,19 +95,20 @@ func (captcha *AliyunCaptchaProvider) VerifyCaptcha(token, clientSecret string) return false, err } - type captchaResponse struct { - Code int `json:"Code"` - Msg string `json:"Msg"` - } - captchaResp := &captchaResponse{} + return handleCaptchaResponse(body) +} - err = json.Unmarshal(body, captchaResp) +func handleCaptchaResponse(body []byte) (bool, error) { + captchaResp := &captchaSuccessResponse{} + err := json.Unmarshal(body, captchaResp) 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(captchaResp.Msg) + return false, errors.New(captchaFailResp.Message) } return true, nil