微信相关接口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
3.1 KiB

package wechat
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"git.tetele.net/tgo/helper"
)
//检测图片内容是否合规
func ImgSecCheck(access_token, imgUrl string) error {
var err error
checkUrl := "https://api.weixin.qq.com/wxa/img_sec_check?access_token=%s"
checkUrl = fmt.Sprintf(checkUrl, access_token)
imgResponse, err := http.Get(imgUrl) //获取图片
if err != nil {
return err
}
defer imgResponse.Body.Close()
if imgResponse.StatusCode != 200 {
return errors.New("resp status:" + fmt.Sprint(imgResponse.StatusCode))
}
buf := new(bytes.Buffer)
w := multipart.NewWriter(buf)
bin, err := ioutil.ReadAll(imgResponse.Body)
if err != nil {
fmt.Println(err)
return err
}
fw, err := w.CreateFormFile("media", "fijgrpgpegjrepoikr")
if err != nil {
fmt.Println(err)
return err
}
_, err = fw.Write(bin)
if err != nil {
fmt.Println(err)
return err
}
w.Close()
req, err := http.NewRequest("POST", checkUrl, buf)
if err != nil {
fmt.Println("req err: ", err)
return err
}
req.Header.Set("Content-Type", w.FormDataContentType())
checkResponse, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println("resp err: ", err)
return err
}
defer checkResponse.Body.Close()
if checkResponse.StatusCode != 200 {
return errors.New("resp status:" + fmt.Sprint(checkResponse.StatusCode))
}
response, err := ioutil.ReadAll(checkResponse.Body)
if err != nil {
return err
}
responseData := map[string]interface{}{}
err = json.Unmarshal(response, &responseData)
if err != nil {
// 有错,但能解析
log.Println(err)
}
if _, exist := responseData["errcode"]; exist && helper.ToInt(responseData["errcode"]) != 0 {
return errors.New(helper.ToStr(responseData["errmsg"]))
}
return nil
}
//检测文本内容是否合规
func MsgSecCheck(access_token string, data map[string]interface{}, retry ...int) error {
var err error
data["version"] = 2
checkUrl := "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=%s"
checkUrl = fmt.Sprintf(checkUrl, access_token)
requestDataJson, err := json.Marshal(data)
if err != nil {
return err
}
response, err := PostJson(checkUrl, requestDataJson)
if err != nil {
return err
}
responseData := map[string]interface{}{}
err = json.Unmarshal(response, &responseData)
if err != nil {
// 有错,但能解析
log.Println(err)
}
if _, exist := responseData["errcode"]; exist && helper.ToInt(responseData["errcode"]) != 0 {
return errors.New(helper.ToStr(responseData["errmsg"]))
}
resultData, err := helper.InterfaceToMapInterface(responseData["result"])
if err != nil {
return err
}
if resultData["suggest"] != "pass" {
return errors.New("内容不能包含" + GetCheckContentByCode(helper.ToStr(resultData["label"])))
}
return nil
}
func GetCheckContentByCode(code string) string {
codeList := map[string]string{
"10001": "广告",
"20001": "时政",
"20002": "色情",
"20003": "辱骂",
"20006": "违法犯罪",
"20008": "欺诈",
"20012": "低俗",
"20013": "版权",
"21000": "其他",
}
return codeList[code]
}