云函数实现敏感文本检测:小程序系列01

       小程序开发中,经常会遇到标题,备注,详细信息等各种文本输入展示,如果不加后台审核的就直接展示给用户的话,就会涉及到敏感信息的检测问题。为了安全起见,微信小程序后台不支持api.weixin.qq.com配置https请求,所以需要在服务端做内容检测。我们这里分享一下小程序云函数提供的验证接口和使用方式。

1、新建云函数:sec_check

2、云函数目录下新建config.json文件,添加函数调用权限

  1. {

  2. "permissions":{

  3. "openapi":[

  4. "security.msgSecCheck"

  5. ]

  6. }

  7. }

  8.  

3、调用微信官方提供的验证方法:openapi.security.msgSecCheck

  1. // 云函数入口文件 index.js

  2. const cloud =require('wx-server-sdk')

  3. cloud.init()

  4. // 云函数入口函数

  5. exports.main = async (event, context)=>{

  6. const wxContext = cloud.getWXContext()

  7. try{

  8. const result = await cloud.openapi.security.msgSecCheck({

  9. content:event.content

  10. })

  11. if(result && result.errCode.toString()==='87014'){

  12. return{ code:500, msg:'内容异常,请修改', data: result }

  13. }else{

  14. return{ code:200, msg:'ok', data: result }

  15. }

  16. }catch(err){

  17. // 错误处理

  18. if(err.errCode.toString()==='87014'){

  19. return{ code:500, msg:'内容异常,请修改', data: err }

  20. }

  21. return{ code:502, msg:'其他异常', data: err }

  22. }

  23. }

4、小程序调用云函数

  1. wx.cloud.init();

  2. wx.cloud.callFunction({

  3. name:'sec_check',

  4. data:{

  5. content:'检测文本'

  6. }

  7. }).then((res)=>{

  8. console.log(res.result.code + res.result.msg)

  9. })

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值