1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- package security
- import (
- "encoding/json"
- "errors"
- "fmt"
- "github.com/silenceper/wechat/v2/miniprogram"
- "github.com/silenceper/wechat/v2/miniprogram/content"
- "github.com/silenceper/wechat/v2/util"
- )
- const SecurityMsgCheckUrl = "https://api.weixin.qq.com/wxa/msg_sec_check"
- type Security struct {
- *content.Content
- }
- type MyMiniprogram struct {
- *miniprogram.MiniProgram
- }
- func NewMyMiniprogram(miniprogram *miniprogram.MiniProgram) *MyMiniprogram {
- return &MyMiniprogram{miniprogram}
- }
- type BodyContent struct {
- Version int8 `json:"version"`
- Openid string `json:"openid"`
- Scene int8 `json:"scene"`
- Content string `json:"content"`
- Nickname string `json:"nickname"`
- Title string `json:"title"`
- Signature string `json:"signature"`
- }
- func (s *MyMiniprogram) MsgSecCheckWithResult(bodyContent *BodyContent) (result Result, err error) {
- var accessToken string
- accessToken, err = s.GetContentSecurity().GetAccessToken()
- if err != nil {
- return
- }
- uri := fmt.Sprintf("%s?access_token=%s", SecurityMsgCheckUrl, accessToken)
- response, err := util.PostJSON(uri, bodyContent)
- if err != nil {
- return
- }
- return DecodeWithResult(response, "MsgSecCheck")
- }
- type Result struct {
- Result *ResultBody
- }
- type ResultBody struct {
- Suggest string
- Label int
- }
- func DecodeWithResult(response []byte, apiName string) (result Result, err error) {
- var commError util.CommonError
- err = json.Unmarshal(response, &commError)
- if err != nil {
- return
- }
- if commError.ErrCode != 0 {
- err = errors.New(fmt.Sprintf("%s Error , errcode=%d , errmsg=%s", apiName, commError.ErrCode, commError.ErrMsg))
- return
- }
- err = json.Unmarshal(response, &result)
- if err != nil {
- return
- }
- return
- }
|