|
| 1 | +package ym |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io/ioutil" |
| 8 | + "net/http" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +const CustomUrl = "https://www.jfbym.com/api/YmServer/customApi" |
| 13 | +const OkCode = 10000 |
| 14 | +const DataOkCode = 0 |
| 15 | + |
| 16 | +type YmCaptcha struct { |
| 17 | + Token string |
| 18 | + h http.Client |
| 19 | +} |
| 20 | + |
| 21 | +func NewYmCaptcha(token string) *YmCaptcha { |
| 22 | + return &YmCaptcha{ |
| 23 | + Token: token, |
| 24 | + h: http.Client{Timeout: time.Second * 10}, |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// CommonVerify 通用验证 |
| 29 | +// # 数英汉字类型 |
| 30 | +// # 通用数英1-4位 10110 |
| 31 | +// # 通用数英5-8位 10111 |
| 32 | +// # 通用数英9~11位 10112 |
| 33 | +// # 通用数英12位及以上 10113 |
| 34 | +// # 通用数英1~6位plus 10103 |
| 35 | +// # 定制-数英5位~qcs 9001 |
| 36 | +// # 定制-纯数字4位 193 |
| 37 | +// # 中文类型 |
| 38 | +// # 通用中文字符1~2位 10114 |
| 39 | +// # 通用中文字符 3~5位 10115 |
| 40 | +// # 通用中文字符6~8位 10116 |
| 41 | +// # 通用中文字符9位及以上 10117 |
| 42 | +// # 定制-XX西游苦行中文字符 10107 |
| 43 | +// # 计算类型 |
| 44 | +// # 通用数字计算题 50100 |
| 45 | +// # 通用中文计算题 50101 |
| 46 | +// # 定制-计算题 cni 452 |
| 47 | +func (m *YmCaptcha) CommonVerify(image, captchaType string) (res string, err error) { |
| 48 | + config := map[string]interface{}{} |
| 49 | + config["image"] = image |
| 50 | + config["type"] = captchaType |
| 51 | + config["token"] = m.Token |
| 52 | + configBytes, _ := json.Marshal(config) |
| 53 | + bodyReader := bytes.NewReader(configBytes) |
| 54 | + request, err := http.NewRequest(http.MethodPost, CustomUrl, bodyReader) |
| 55 | + if err != nil { |
| 56 | + return |
| 57 | + } |
| 58 | + request.Header.Add("Content-Type", "application/json;charset=utf-8") |
| 59 | + response, err := m.h.Do(request) |
| 60 | + if err != nil { |
| 61 | + return |
| 62 | + } |
| 63 | + defer response.Body.Close() |
| 64 | + resBytes, err := ioutil.ReadAll(response.Body) |
| 65 | + if err != nil { |
| 66 | + return |
| 67 | + } |
| 68 | + var resData Result |
| 69 | + err = json.Unmarshal(resBytes, &resData) |
| 70 | + if err != nil { |
| 71 | + return |
| 72 | + } |
| 73 | + if resData.Code == OkCode && resData.Data.Code == DataOkCode { |
| 74 | + return resData.Data.Data, nil |
| 75 | + } else { |
| 76 | + return resData.Msg, fmt.Errorf("响应信息是%s", string(resBytes)) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// SlideVerify # 滑块类型 |
| 81 | +// # 通用双图滑块 20111 |
| 82 | +// # slide_image 需要识别图片的小图片的base64字符串 |
| 83 | +// # 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) |
| 94 | + defer resp.Body.Close() |
| 95 | + data, _ := ioutil.ReadAll(resp.Body) |
| 96 | + fmt.Println(string(data), err) |
| 97 | + return string(data) |
| 98 | +} |
| 99 | + |
| 100 | +func (m *YmCaptcha) SinSlideVerify(image string) string { |
| 101 | + // # 滑块类型 |
| 102 | + // # 通用单图滑块(截图) 20110 |
| 103 | + config := map[string]interface{}{} |
| 104 | + config["image"] = image |
| 105 | + config["type"] = "20110" |
| 106 | + config["token"] = m.Token |
| 107 | + configData, _ := json.Marshal(config) |
| 108 | + body := bytes.NewBuffer([]byte(configData)) |
| 109 | + resp, err := http.Post(CustomUrl, "application/json;charset=utf-8", body) |
| 110 | + defer resp.Body.Close() |
| 111 | + data, _ := ioutil.ReadAll(resp.Body) |
| 112 | + fmt.Println(string(data), err) |
| 113 | + return string(data) |
| 114 | +} |
0 commit comments