附近小店
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.

sec_check.go 2.5 KiB

3 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package weapp
  2. // 检测地址
  3. const (
  4. apiIMGSecCheck = "/wxa/img_sec_check"
  5. apiMSGSecCheck = "/wxa/msg_sec_check"
  6. apiMediaCheckAsync = "/wxa/media_check_async"
  7. )
  8. // IMGSecCheck 本地图片检测
  9. // 官方文档: https://developers.weixin.qq.com/miniprogram/dev/api/imgSecCheck.html
  10. //
  11. // filename 要检测的图片本地路径
  12. // token 接口调用凭证(access_token)
  13. func IMGSecCheck(token, filename string) (*CommonError, error) {
  14. api := baseURL + apiIMGSecCheck
  15. return imgSecCheck(api, token, filename)
  16. }
  17. func imgSecCheck(api, token, filename string) (*CommonError, error) {
  18. url, err := tokenAPI(api, token)
  19. if err != nil {
  20. return nil, err
  21. }
  22. res := new(CommonError)
  23. if err := postFormByFile(url, "media", filename, res); err != nil {
  24. return nil, err
  25. }
  26. return res, nil
  27. }
  28. // MSGSecCheck 文本检测
  29. // 官方文档: https://developers.weixin.qq.com/miniprogram/dev/api/msgSecCheck.html
  30. //
  31. // content 要检测的文本内容,长度不超过 500KB,编码格式为utf-8
  32. // token 接口调用凭证(access_token)
  33. func MSGSecCheck(token, content string) (*CommonError, error) {
  34. api := baseURL + apiMSGSecCheck
  35. return msgSecCheck(api, token, content)
  36. }
  37. func msgSecCheck(api, token, content string) (*CommonError, error) {
  38. url, err := tokenAPI(api, token)
  39. if err != nil {
  40. return nil, err
  41. }
  42. params := requestParams{
  43. "content": content,
  44. }
  45. res := new(CommonError)
  46. if err = postJSON(url, params, res); err != nil {
  47. return nil, err
  48. }
  49. return res, nil
  50. }
  51. // MediaType 检测内容类型
  52. type MediaType = uint8
  53. // 所有检测内容类型
  54. const (
  55. _ MediaType = iota
  56. MediaTypeAudio // 音频
  57. MediaTypeImage // 图片
  58. )
  59. // CheckMediaResponse 异步校验图片/音频返回数据
  60. type CheckMediaResponse struct {
  61. CommonError
  62. TraceID string `json:"trace_id"`
  63. }
  64. // MediaCheckAsync 异步校验图片/音频是否含有违法违规内容。
  65. //
  66. // mediaURL 要检测的多媒体url
  67. // mediaType 接口调用凭证(access_token)
  68. func MediaCheckAsync(token, mediaURL string, mediaType MediaType) (*CheckMediaResponse, error) {
  69. api := baseURL + apiMediaCheckAsync
  70. return mediaCheckAsync(api, token, mediaURL, mediaType)
  71. }
  72. func mediaCheckAsync(api, token, mediaURL string, mediaType MediaType) (*CheckMediaResponse, error) {
  73. url, err := tokenAPI(api, token)
  74. if err != nil {
  75. return nil, err
  76. }
  77. params := requestParams{
  78. "media_url": mediaURL,
  79. "media_type": mediaType,
  80. }
  81. res := new(CheckMediaResponse)
  82. if err = postJSON(url, params, res); err != nil {
  83. return nil, err
  84. }
  85. return res, nil
  86. }