Skip to content

Commit ede94f9

Browse files
committed
init
0 parents  commit ede94f9

File tree

7 files changed

+160
-0
lines changed

7 files changed

+160
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
go.sum

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# 云码打码平台工具
2+
by: Jary

c1.jpg

2.83 KB
Loading

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module ym_captcha
2+
3+
go 1.16

ym/captcha.go

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
}

ym/captcha_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package ym
2+
3+
import (
4+
"encoding/base64"
5+
"io/ioutil"
6+
"testing"
7+
)
8+
9+
const (
10+
token = "ssssssssssss" // 你的token,从用户中心获取
11+
)
12+
13+
func TestYmCaptcha_CommonVerify(t *testing.T) {
14+
readFileBytes, err := ioutil.ReadFile("../c1.jpg")
15+
if err != nil {
16+
t.Fatal(err)
17+
}
18+
imgBase64Str := base64.StdEncoding.EncodeToString(readFileBytes)
19+
var y = NewYmCaptcha(token)
20+
verify, err := y.CommonVerify(imgBase64Str, "10111")
21+
if err != nil {
22+
t.Fatal(err)
23+
}
24+
t.Log("验证结果是", verify)
25+
}

ym/model.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package ym
2+
3+
type Result struct {
4+
Code int `json:"code"`
5+
Msg string `json:"msg"`
6+
Data Data `json:"data"`
7+
}
8+
9+
type Data struct {
10+
Code int `json:"code"`
11+
CaptchaId string `json:"captchaId"`
12+
RecordId string `json:"recordId"`
13+
Data string `json:"data"`
14+
}

0 commit comments

Comments
 (0)