微信小程序 使用云调用进行内容安全审查

前段时间微信加强了小程序内容安全审查力度,由于“表情小作坊”未接入相关审查能力被封了,蓝瘦~ 查看小程序的文档发现微信还是很人性化的,提供了内容审查的开放能力,支持云调用,不需要服务器、后台开发就可以接入内容审查能力,开心

接入过程还是遇到了一点问题,这里记录下,希望大家少浪费点时间

云调用的文档:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/openapi/openapi.html#%E4%BA%91%E8%B0%83%E7%94%A8
云开发步骤:

1.开通云开发

在这里插入图片描述
由于小程序是新支持的云开发,所以需要先按照文档开通云开发,创建云开发的环境id后续会用到,在云控制台可以找到环境id
在这里插入图片描述

2.添加云函数

如 项目目录下新建云函数目录“cloudfunctions”,在project.config.json中注册该目录,注册后该目录会有云图标
在这里插入图片描述
然后在云函数根目录右键新建云函数
在这里插入图片描述
添加云函数成功后,需要在文档中找到对应云函数的相关调用方法和权限配置
如:security.imgSecCheck
可以看到它支持云调用,下拉查看该云函数云调用的相关配置
在这里插入图片描述
在这里插入图片描述

  • 云函数代码
    在这里插入图片描述

  • 云函数权限配置
    config.json文件需要新建在这里插入图片描述

3.小程序端调用

需要注意:env的值就是之前云函数环境id
在这里插入图片描述

4.最后

在云函数上右键点击上传并部署 (之前没有上传部署,导致无论怎么调用都返回成功 😓)
在这里插入图片描述
接下来就大功告成了
下面代码 以供cv

图片审查:

  • 小程序调用
			wx.cloud.init({
              env: '环境id'
            })
            wx.cloud.callFunction({
              name: "imgseccheck",
              data: {
                value: buffer.data
              }
            }).then(
              imgRes => {
                wx.hideLoading()
                //console.log(imgRes)
                if (imgRes.result.errCode == 87014) {
                  wx.showToast({
                    title: '图片含有违法违规内容',
                    icon: 'none',
                    duration: 1500
                  })
                } else {
                  //图片合法
                  
                }
              }
            ).catch(error => {
              wx.hideLoading()
              wx.showToast({
                title: '图片含有违法违规内容',
                icon: 'none',
                duration: 1500
              })
            })
  • imgseccheck index.js
// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: '环境id'
})

// 云函数入口函数
exports.main = async(event, context) => {
  const {
    value
  } = event;
  try {
    const res = await cloud.openapi.security.imgSecCheck({
      media: {
        contentType: 'image/*',
        value: Buffer.from(value)
      }
    })
    return res;
  } catch (err) {
    return err;
  }
}
  • imgseccheck config.json
{
  "permissions": {
    "openapi": [
      "security.imgSecCheck"
    ]
  }
}

文字审查

  • 小程序调用
    注意:在catch中捕获
	wx.cloud.init({
      env: '环境id'
    })
    wx.cloud.callFunction({
      name: "msgseccheck",
      data: {
        content: content
      }
    }).then(
      msgRes => {
        //console.log(msgRes)
        //内容正常
      }
    ).catch(error => {
      wx.showToast({
        title: '文字含有违法违规内容',
        icon: 'none',
        duration: 1500
      })
    })
  • msgseccheck index.js
// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: '环境id'
})

// 云函数入口函数
exports.main = async(event, context) => {
  try {
    const res = await cloud.openapi.security.msgSecCheck({
      content: event.content
    })
    return res;
  } catch (err) {
    return err;
  }
}
  • msgseccheck config.json
{
  "permissions": {
    "openapi": [
      "security.msgSecCheck"
    ]
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章