微信小程序实现文本安全监测

对于微信小程序云开发中的文本安全监测,我真的很是无语,虽然经历了许多BUG,但是好在最后自己也终于解决这个问题了,下面我将介绍两种文本安全监测方法。
调用云函数文本index.js

onSubmitEvent:function(event){
    const content = event.detail.value.content;
    const location = this.data.location;
    const author = app.globalData.userInfo;
    wx.cloud.callFunction({
      name:"#云函数名",
      data:{
        content:content,
        location:location,
        author:author
      },
      success:res=>{
        // if(res.result.errCode === 0){
        //   db.collection("#数据库名").add({
        //     data:{
        //       content:content,
        //       location:location,
        //       author:author
        //     }

        //   }).then(res=>{
        //     console.log(res)
        //   })
        // }
        console.log(res)
        
      },
      fail:err=>{
        console.error(err)
      }

第一种,HTTPS监测方法

云函数index.js

const cloud = require('wx-server-sdk')
const got  = require("got")  //got包---这个位置要在cloud.init()上面,否则可能会报错!

cloud.init()

const db = cloud.database() //数据库---位置要在cloud.init()下面
const APPID = "#自己的APPID";  
const APPSECRET = "#自己的密钥"; 
const tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+APPID+"&secret="+APPSECRET;
const checkUrl = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=";

// 云函数入口函数
exports.main = async (event, context) => {
 
  const content = event.content;
  const location = event.location;
  const author = event.author;
  const tokenResponse = await got(tokenUrl);
  const access_token = JSON.parse(tokenResponse.body).access_token;
  const CheckUrl = checkUrl + access_token;
  const checkResponse = await got.post(CheckUrl,{
    body:JSON.stringify({
      content:content
    })
  })
  const checkBody = JSON.parse(checkResponse.body)
  const errcode = checkBody.errcode;
  //存储到数据库中
  if(errcode == 0){
    return await db.collection("#数据库名").add({
      data:{
        content:content,
        location:location,
        author:author
      }
    })
  }else{
    return {"errcode":1,"errmsg":"你的发布的内容有风险,请修改后再发布"}
  }

}

第二种,云调用方法

云函数index.js

// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database()
// 云函数入口函数
exports.main = async (event, context) => {
  try{
    
    const e = await cloud.openapi.security.msgSecCheck({
     
        content:event.content
       
    })
    
   
    const res = e.errCode;
  
    // console.log(res)
    // return res
    if(res == 0){
     return await db.collection("#数据库名").add({
        data:{
          content:event.content,
          location:event.location,
          author:event.author
        }
      })
    }else{
      
      return {"errCode":1,"errMsg":"你提交的内容有风险,请修改后在提交!"}
     }
    
  } catch(err){
    return err
  }
}

发了一天的时间终于将这个问题解决了,期间遇到许多BUG,但是通过网上查阅资料终于解决,如果有小伙伴在运行我的程序出现问题,欢迎在评论区下方留言!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值