msgSecCheck
检查一段文本是否含有违法违规内容npm
请求地址 :小程序
POST https://api.weixin.qq.com/wxa/msg_sec_check?access_token=ACCESS_TOKENapi
msgSecCheck
的实现须要借助库来请求地址, 例如 got
微信
使用 npm
安装 got
库 :微信开发
npm install gotapp
access_token
请求地址 :async
GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET函数
APPID
和 APPSECRET
能在微信开发平台 - 开发 - 开发设置 中找到.测试
功能实现以下 :ui
// 新建个云函数文件, 例如我将其命名为 msgSecCheck const 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.js Page({ msgSecCheck: function(event) { wx.cloud.callFunction({ name: 'msgSecCheck', data: { // text: '有违规文字内容测试特3456书yuuo莞6543李zxcz蒜7782法fgnv级' // text: '这是个正常文字测试' } }).then(res => { console.log(res.result); }) } })