小程式雲開發–內容安全審查API雲調用
- 2019 年 11 月 20 日
- 筆記
雲調用
雲調用是小程式·雲開發提供的在雲函數中調用微信開放介面的能力,需要在雲函數中通過
wx-server-sdk
使用。
介面方法
openapi.security.msgSecCheck
需在
config.json
中配置security.msgSecCheck
API 的許可權,詳情
請求參數
屬性 |
類型 |
默認值 |
必填 |
說明 |
---|---|---|---|---|
content |
string |
|
是 |
要檢測的文本內容,長度不超過 500KB |
返回值
Object
返回的 JSON 數據包
屬性 |
類型 |
說明 |
---|---|---|
errCode |
number |
錯誤碼 |
errMsg |
string |
錯誤資訊 |
errCode 的合法值
值 |
說明 |
最低版本 |
---|---|---|
0 |
成功 |
|
異常
Object
拋出的異常
屬性 |
類型 |
說明 |
---|---|---|
errCode |
number |
錯誤碼 |
errMsg |
string |
錯誤資訊 |
errCode 的合法值
值 |
說明 |
最低版本 |
---|---|---|
87014 |
內容含有違法違規內容 |
|
errMsg 的合法值
值 |
說明 |
最低版本 |
---|---|---|
"ok" |
內容正常 |
|
"risky |
content" 內容含有違法違規內容 |
|
雲函數msgcheck寫法
const cloud = require('wx-server-sdk') cloud.init({ env: 'cloud-18aa6f' }) // 雲函數入口函數 exports.main = async (event, context) => { const { content } = event;//可以省略 try { const res = await cloud.openapi.security.msgSecCheck({ content: event.content }) return res; } catch (err) { return err; } }
本地函數調用和回調寫法
wx.cloud.callFunction({ name:'msgcheck', data:{ content:that.data.text } }).then(ckres=>{ //寫審核通過之後的操作 if == 0 if (ckres.result.errCode == 0){ /*wx.cloud.callFunction({ name: '雲函數名', data: { }, success(res) { } })*/ }else{ wx.hideLoading(); wx.showModal({ title: '提醒', content: '請注意言論', showCancel:false }) } })