Skip to content

Commit 69b4cbf

Browse files
committed
SlideVerify修改
1 parent bc73d3b commit 69b4cbf

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
lines changed

ym/captcha.go

+21-14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7+
"io"
78
"io/ioutil"
89
"net/http"
910
"time"
@@ -61,7 +62,7 @@ func (m *YmCaptcha) CommonVerify(image, captchaType string) (res string, err err
6162
return
6263
}
6364
defer response.Body.Close()
64-
resBytes, err := ioutil.ReadAll(response.Body)
65+
resBytes, err := io.ReadAll(response.Body)
6566
if err != nil {
6667
return
6768
}
@@ -81,20 +82,26 @@ func (m *YmCaptcha) CommonVerify(image, captchaType string) (res string, err err
8182
// # 通用双图滑块 20111
8283
// # slide_image 需要识别图片的小图片的base64字符串
8384
// # background_image 需要识别图片的背景图片的base64字符串(背景图需还原)
84-
func (m *YmCaptcha) SlideVerify(slideImage string, backgroundImage string) string {
85-
86-
config := map[string]interface{}{}
87-
config["slide_image"] = slideImage
88-
config["background_image"] = backgroundImage
89-
config["type"] = "20111"
90-
config["token"] = m.Token
91-
configData, _ := json.Marshal(config)
92-
body := bytes.NewBuffer([]byte(configData))
93-
resp, err := http.Post(CustomUrl, "application/json;charset=utf-8", body)
85+
func (m *YmCaptcha) SlideVerify(slideImage string, backgroundImage string) (res string, err error) {
86+
req := map[string]interface{}{}
87+
req["slide_image"] = slideImage
88+
req["background_image"] = backgroundImage
89+
req["type"] = "20111"
90+
req["token"] = m.Token
91+
reqBytes, _ := json.Marshal(req)
92+
resp, err := http.Post(CustomUrl, "application/json;charset=utf-8", bytes.NewReader(reqBytes))
9493
defer resp.Body.Close()
95-
data, _ := ioutil.ReadAll(resp.Body)
96-
fmt.Println(string(data), err)
97-
return string(data)
94+
resBytes, _ := io.ReadAll(resp.Body)
95+
var resData Result
96+
err = json.Unmarshal(resBytes, &resData)
97+
if err != nil {
98+
return
99+
}
100+
if resData.Code == OkCode && resData.Data.Code == DataOkCode {
101+
return resData.Data.Data, nil
102+
} else {
103+
return resData.Msg, fmt.Errorf("响应信息是%s", string(resBytes))
104+
}
98105
}
99106

100107
func (m *YmCaptcha) SinSlideVerify(image string) string {

0 commit comments

Comments
 (0)