mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-04 12:52:27 +08:00
小程序内容安全 (#415)
* 增加小程序内容安全接口 * 内容安全接口 按照golint规范进行优化 * 内容安全接口 按照golint规范进行优化 * 删除CheckImage中的输出代码 * 小程序内容安全接口 * 小程序内容安全接口 * 小程序内容安全接口 1:修改返回值 改为error异常统一返回 Co-authored-by: root <admin@example.com>
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -27,3 +27,4 @@ _testmain.go
|
|||||||
vendor
|
vendor
|
||||||
.idea/
|
.idea/
|
||||||
example/*
|
example/*
|
||||||
|
/test
|
||||||
|
|||||||
61
miniprogram/content/content.go
Normal file
61
miniprogram/content/content.go
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
package content
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/miniprogram/context"
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
checkTextURL = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=%s"
|
||||||
|
checkImageURL = "https://api.weixin.qq.com/wxa/img_sec_check?access_token=%s"
|
||||||
|
)
|
||||||
|
|
||||||
|
//Content 内容安全
|
||||||
|
type Content struct {
|
||||||
|
*context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
//NewContent 内容安全接口
|
||||||
|
func NewContent(ctx *context.Context) *Content {
|
||||||
|
return &Content{ctx}
|
||||||
|
}
|
||||||
|
|
||||||
|
//CheckText 检测文字
|
||||||
|
//@text 需要检测的文字
|
||||||
|
func (content *Content) CheckText(text string) error {
|
||||||
|
accessToken, err := content.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
response, err := util.PostJSON(
|
||||||
|
fmt.Sprintf(checkTextURL, accessToken),
|
||||||
|
map[string]string{
|
||||||
|
"content": text,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return util.DecodeWithCommonError(response, "ContentCheckText")
|
||||||
|
}
|
||||||
|
|
||||||
|
//CheckImage 检测图片
|
||||||
|
//所传参数为要检测的图片文件的绝对路径,图片格式支持PNG、JPEG、JPG、GIF, 像素不超过 750 x 1334,同时文件大小以不超过 300K 为宜,否则可能报错
|
||||||
|
//@media 图片文件的绝对路径
|
||||||
|
func (content *Content) CheckImage(media string) error {
|
||||||
|
accessToken, err := content.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
response, err := util.PostFile(
|
||||||
|
"media",
|
||||||
|
media,
|
||||||
|
fmt.Sprintf(checkImageURL, accessToken),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return util.DecodeWithCommonError(response, "ContentCheckImage")
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"github.com/silenceper/wechat/v2/miniprogram/analysis"
|
"github.com/silenceper/wechat/v2/miniprogram/analysis"
|
||||||
"github.com/silenceper/wechat/v2/miniprogram/auth"
|
"github.com/silenceper/wechat/v2/miniprogram/auth"
|
||||||
"github.com/silenceper/wechat/v2/miniprogram/config"
|
"github.com/silenceper/wechat/v2/miniprogram/config"
|
||||||
|
"github.com/silenceper/wechat/v2/miniprogram/content"
|
||||||
"github.com/silenceper/wechat/v2/miniprogram/context"
|
"github.com/silenceper/wechat/v2/miniprogram/context"
|
||||||
"github.com/silenceper/wechat/v2/miniprogram/encryptor"
|
"github.com/silenceper/wechat/v2/miniprogram/encryptor"
|
||||||
"github.com/silenceper/wechat/v2/miniprogram/message"
|
"github.com/silenceper/wechat/v2/miniprogram/message"
|
||||||
@@ -78,3 +79,8 @@ func (miniProgram *MiniProgram) GetCustomerMessage() *message.Manager {
|
|||||||
func (miniProgram *MiniProgram) GetWeRun() *werun.WeRun {
|
func (miniProgram *MiniProgram) GetWeRun() *werun.WeRun {
|
||||||
return werun.NewWeRun(miniProgram.ctx)
|
return werun.NewWeRun(miniProgram.ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetContentSecurity 内容安全接口
|
||||||
|
func (miniProgram *MiniProgram) GetContentSecurity() *content.Content {
|
||||||
|
return content.NewContent(miniProgram.ctx)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user