微信相关接口
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

  1. package wechat
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "mime/multipart"
  9. "net/http"
  10. "git.tetele.net/tgo/helper"
  11. )
  12. //检测图片内容是否合规
  13. func ImgSecCheck(access_token, imgUrl string) error {
  14. var err error
  15. checkUrl := "https://api.weixin.qq.com/wxa/img_sec_check?access_token=%s"
  16. checkUrl = fmt.Sprintf(checkUrl, access_token)
  17. imgResponse, err := http.Get(imgUrl) //获取图片
  18. if err != nil {
  19. return err
  20. }
  21. defer imgResponse.Body.Close()
  22. if imgResponse.StatusCode != 200 {
  23. return errors.New("resp status:" + fmt.Sprint(imgResponse.StatusCode))
  24. }
  25. buf := new(bytes.Buffer)
  26. w := multipart.NewWriter(buf)
  27. bin, err := ioutil.ReadAll(imgResponse.Body)
  28. if err != nil {
  29. fmt.Println(err)
  30. return err
  31. }
  32. fw, err := w.CreateFormFile("media", "fijgrpgpegjrepoikr")
  33. if err != nil {
  34. fmt.Println(err)
  35. return err
  36. }
  37. _, err = fw.Write(bin)
  38. if err != nil {
  39. fmt.Println(err)
  40. return err
  41. }
  42. w.Close()
  43. req, err := http.NewRequest("POST", checkUrl, buf)
  44. if err != nil {
  45. fmt.Println("req err: ", err)
  46. return err
  47. }
  48. req.Header.Set("Content-Type", w.FormDataContentType())
  49. checkResponse, err := http.DefaultClient.Do(req)
  50. if err != nil {
  51. fmt.Println("resp err: ", err)
  52. return err
  53. }
  54. defer checkResponse.Body.Close()
  55. if checkResponse.StatusCode != 200 {
  56. return errors.New("resp status:" + fmt.Sprint(checkResponse.StatusCode))
  57. }
  58. response, err := ioutil.ReadAll(checkResponse.Body)
  59. if err != nil {
  60. return err
  61. }
  62. responseData := map[string]interface{}{}
  63. err = json.Unmarshal(response, &responseData)
  64. if err != nil {
  65. // 有错,但能解析
  66. log.Println(err)
  67. }
  68. if _, exist := responseData["errcode"]; exist && helper.ToInt(responseData["errcode"]) != 0 {
  69. return errors.New(helper.ToStr(responseData["errmsg"]))
  70. }
  71. return nil
  72. }
  73. //检测文本内容是否合规
  74. func MsgSecCheck(access_token string, data map[string]interface{}, retry ...int) error {
  75. var err error
  76. data["version"] = 2
  77. checkUrl := "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=%s"
  78. checkUrl = fmt.Sprintf(checkUrl, access_token)
  79. requestDataJson, err := json.Marshal(data)
  80. if err != nil {
  81. return err
  82. }
  83. response, err := PostJson(checkUrl, requestDataJson)
  84. if err != nil {
  85. return err
  86. }
  87. responseData := map[string]interface{}{}
  88. err = json.Unmarshal(response, &responseData)
  89. if err != nil {
  90. // 有错,但能解析
  91. log.Println(err)
  92. }
  93. if _, exist := responseData["errcode"]; exist && helper.ToInt(responseData["errcode"]) != 0 {
  94. return errors.New(helper.ToStr(responseData["errmsg"]))
  95. }
  96. resultData, err := helper.InterfaceToMapInterface(responseData["result"])
  97. if err != nil {
  98. return err
  99. }
  100. if resultData["suggest"] != "pass" {
  101. return errors.New("内容不能包含" + GetCheckContentByCode(helper.ToStr(resultData["label"])))
  102. }
  103. return nil
  104. }
  105. func GetCheckContentByCode(code string) string {
  106. codeList := map[string]string{
  107. "10001": "广告",
  108. "20001": "时政",
  109. "20002": "色情",
  110. "20003": "辱骂",
  111. "20006": "违法犯罪",
  112. "20008": "欺诈",
  113. "20012": "低俗",
  114. "20013": "版权",
  115. "21000": "其他",
  116. }
  117. return codeList[code]
  118. }