小程序雲函數驗證敏感信息

首先開始寫的時候遇到的問題:

Uncaught (in promise) Error: errCode: -404011 cloud function execution error | errMsg: cloud.callFunction:fail requestID xxxxxxxxxxxxxx, cloud function service error code -504002, error message incorrect header check; at cloud.callFunction api; 

網上搜到的很多類似的寫法:

exports.main = async (event, context) => {
  const tokenRes =  await got(tokenUrl)
  const token = JSON.parse(tokenRes.body).access_token //拿到token
  
  const res = await got.post(checkUrl+token, {
    'Content-Type': 'application/json',
    body: JSON.stringify({
      content: event.message
    })
  })
  return res.body
}

問題查找了很久,不知道爲什麼就是一直有問題,最終沒有找到問題的原因,事實上通過postman是可以得到檢測結果的。

最終,看了官方文檔雲調用的部分,其實敏感信息驗證的接口是可以通過雲調用完成驗證的,

實現代碼如下

// 雲函數入口函數
exports.main = async (event, context) => {
  const result = await cloud.openapi.security.msgSecCheck({
    content: event.text
	});
	return result;
}

調用部分

//內容合規性檢查
wx.cloud.callFunction({
	name: 'msgSecCheck',
	data: {
		text: '這是沒有問題的文字'
	}
}).then(res => {
	console.log(res);
}).catch(console.err);

 最終結果

驗證一個包含敏感信息的結果如下:

text: '有違規文字內容測試特3456書yuuo莞6543李zxcz蒜7782法fgnv級'

符合errCode:87014

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章