当前位置:网站首页>微信小程序实现文本安全监测
微信小程序实现文本安全监测
2022-08-02 03:22:00 【寒江飞冰】
对于微信小程序云开发中的文本安全监测,我真的很是无语,虽然经历了许多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,但是通过网上查阅资料终于解决,如果有小伙伴在运行我的程序出现问题,欢迎在评论区下方留言!
边栏推荐
- @Autowired注解的使用
- @Accessors 注解详解
- Error in render: “TypeError: Cannot read properties of null (reading ‘0‘)“ 报错解决方案
- 页面加载流程
- getattr() function analysis
- C语言 void和void *(无类型指针)
- JJWT工具类
- 活体检测 Adaptive Normalized Representation Learning for GeneralizableFace Anti-Spoofing 阅读笔记
- getattr()函数解析
- PCL—point cloud data segmentation
猜你喜欢

URL模块

STM32 map文件解析

Redis simple study notes

subprocess.CalledProcessError: Command ‘pip install ‘thop‘‘ returned non-zero exit status 1.

配置mmdet来训练Swin-Transformer之一配置环境

MySql创建数据表

Phospholipid-polyethylene glycol-azide, DSPE-PEG-Azide, DSPE-PEG-N3, MW: 5000

DSPE-PEG-DBCO 磷脂-聚乙二醇-二苯并环辛炔 一种线性杂双官能聚乙二醇化试剂

UserWarning:火炬。meshgrid:在以后的版本中,它将被要求通过索引ing argu

@Configuration详解
随机推荐
js 数组去重的常用方法
Redis simple study notes
【博学谷学习记录】超强总结,用心分享 | 软件测试 接口测试基础
How to check whether a table is locked in mysql
Mysql8.0安装教程
ssm各类配置模板
页面加载流程
3分钟带你了解微信小程序开发
MySQL分组后取最大一条数据【最优解】
科研试剂DMPE-PEG-Mal 二肉豆蔻酰磷脂酰乙醇胺-聚乙二醇-马来酰亚胺
DSPE-PEG-Silane,DSPE-PEG-SIL,磷脂-聚乙二醇-硅烷修饰活性基团
@ApiModel 和 @ApiModelProperty
ModuleNotFoundError No module named 'xxx' possible solutions
Error in render: “TypeError: Cannot read properties of null (reading ‘0‘)“ 报错解决方案
Phospholipid-polyethylene glycol-hydrazide, DSPE-PEG-Hydrazide, DSPE-PEG-HZ, MW: 5000
Amazon sellers how to improve the conversion
yolov5调用ip摄像头时出现的问题
Error: with open(txt_path,'r') as f: FileNotFoundError: [Errno 2] No such file or directory:
远程调试、无cuDnn、自定义模块无法导入问题记录
pyppeteer使用样例脚本