-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsec_check.go
123 lines (104 loc) · 2.69 KB
/
sec_check.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package weapp
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"strings"
"github.com/cnxfire/golang_weapp/util"
)
// 检测地址
const (
IMGSecCheckURL = "/wxa/img_sec_check"
MSGSecCheckURL = "/wxa/msg_sec_check"
)
// IMGSecCheckFromNet 网络图片检测
// 官方文档: https://developers.weixin.qq.com/miniprogram/dev/api/imgSecCheck.html
//
// @url 要检测的图片网络路径
// @token 接口调用凭证(access_token)
func IMGSecCheckFromNet(url, token string) (res Response, err error) {
resp, err := http.Get(url)
if err != nil {
return
}
defer resp.Body.Close()
bts, err := ioutil.ReadAll(resp.Body)
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
str := strings.Split(url, "/")
fmt.Println(str)
fmt.Println(str[len(str)-1])
fileWriter, err := writer.CreateFormFile("media", str[len(str)-1])
if err != nil {
return
}
_, err = fileWriter.Write(bts)
if err != nil {
return
}
contentType := writer.FormDataContentType()
writer.Close()
return imgSecCheck(body, contentType, token)
}
// IMGSecCheck 本地图片检测
// 官方文档: https://developers.weixin.qq.com/miniprogram/dev/api/imgSecCheck.html
//
// @filename 要检测的图片本地路径
// @token 接口调用凭证(access_token)
func IMGSecCheck(filename, token string) (res Response, err error) {
file, err := os.Open(filename)
if err != nil {
return
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
fileWriter, err := writer.CreateFormFile("media", filename)
if err != nil {
return
}
_, err = io.Copy(fileWriter, file)
if err != nil {
return
}
contentType := writer.FormDataContentType()
writer.Close()
return imgSecCheck(body, contentType, token)
}
func imgSecCheck(body io.Reader, contentType, token string) (res Response, err error) {
api, err := util.TokenAPI(BaseURL+IMGSecCheckURL, token)
if err != nil {
return
}
resp, err := http.Post(api, contentType, body)
if err != nil {
return
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&res)
return
}
// MSGSecCheck 文本检测
// 官方文档: https://developers.weixin.qq.com/miniprogram/dev/api/msgSecCheck.html
//
// @content 要检测的文本内容,长度不超过 500KB,编码格式为utf-8
// @token 接口调用凭证(access_token)
func MSGSecCheck(content, token string) (res Response, err error) {
api, err := util.TokenAPI(BaseURL+MSGSecCheckURL, token)
if err != nil {
return
}
body := fmt.Sprintf(`{"content": "%s"}`, content)
resp, err := http.Post(api, "application/json", strings.NewReader(body))
if err != nil {
return
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&res)
return
}