msgSecCheck 检查一段文本是否含有违法违规内容
请求地址 :
POST https://api.weixin.qq.com/wxa/msg_sec_check?access_token=ACCESS_TOKEN
msgSecCheck 的实现需要借助库来请求地址, 例如 got
使用 npm 安装 got 库 :
npm install got

- 还需要获取小程序全局唯一后台接口调用凭据
access_token
请求地址 :
GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
APPID 和 APPSECRET 能在微信开发平台 - 开发 - 开发设置 中找到.
功能实现如下 :
// 新建个云函数文件, 例如我将其命名为 msgSecCheckconst cloud = require('wx-server-sdk')const got = require('got') // 引入 got 库cloud.init()var appid = '你的 APPID';var appsecret = '你的 APPSECRET';// 获取 access_token 值let tokenUrl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + appid + '&secret=' + appsecret // 文本内容检测接口let checkUrl = 'https://api.weixin.qq.com/wxa/msg_sec_check?access_token=' // 云函数入口函数exports.main = async (event, context) => { let tokenResponse = await got(tokenUrl); // 通过 got 请求 api let token = JSON.parse(tokenResponse.body).access_token; // JSON.parse 将数据转换成对象获取到具体 access_token 值 // 文本内容检测接口拼接 access_token 值, JSON.stringIfy 将值转换成 JSON 字符串 let checkResponse = await got(checkUrl + token, { body: JSON.stringify({ content: event.text }) }); return checkResponse.body }// 新增 msgSecCheck page// pages/msgSecCheck/msgSecCheck.jsPage({ msgSecCheck: function(event) { wx.cloud.callFunction({ name: 'msgSecCheck', data: { // text: '有违规文字内容测试特3456书yuuo莞6543李zxcz蒜7782法fgnv级' // text: '这是个正常文字测试' } }).then(res => { console.log(res.result); }) } })
















