微信小程序图片与文字安全检测security.msgSecCheck和security.imgSecCheck

微信小程序线上版本涉及到内容发布评论等,就需要进行安全检测,否则官方会上传一些huang图等敏感信息,这样就对我们的小程序的运行非常的不友好。

微信小程序图片与文字安全检测security.msgSecCheck和security.imgSecCheck

一、security.msgSecCheck文字安全检测的使用

security.msgSecCheck | 微信开放文档 (qq.com)

由于我的项目使用的是云开发,所以我就演示下云函数中如何使用

//index.js中
//openid为用户的唯一标识,我是通过参数传递过来的。当然你也可以这样获取,更方便cloud.getWXContext().OPENID
//text为要检测的值
exports.main = async (event, context) => {
  const { openid, text } = event;
  try {
    const result = await cloud.openapi.security.msgSecCheck({
      openid,
      scene: 2,
      version: 2,
      content: text,
    });
    return result;
  } catch (err) {
    return err;
  }
};
//config.json中
{
	"permissions": {
		"openapi": [
			"security.msgSecCheck"
		]
	}
}
//js中
wx.cloud.callFunction({
          name: "定义的云函数名",
          data: {
            openid: '用户的openid',
            text: '要检测的值',
          },
        })
        .then((checkTextRes) => {
          const resultSuggest = checkTextRes.result.result.suggest;
          if(resultSuggest === 'pass') {
             console.log('通过')      
          }else {
             console.log('不通过') 
          }
        });

非常的简单,官方也提供了详细的文档。

二、security.imgSecCheck图片安全检测的使用

HTTPS 调用 | 微信开放文档 (qq.com)

这个的使用就相对来说比较有点繁琐了,要考虑的东西很多

//index.js中

const cloud = require("wx-server-sdk");
const axios = require("axios");
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }); // 使用当前云环境

// 云函数入口函数
exports.main = async (event, context) => {
  let buffer = null;
  //通过这个请求拿到图片的arraybuffer流,因为检测图片使用的是arraybuffer
  await axios({
    method: "get",
    url: event.imgData, // imgData是刚才传过来的
    responseType: "arraybuffer",
    headers: { "Content-Type": "*" },
  }).then((res) => {
    buffer = res.data;
  });

  try {
    var result = await cloud.openapi.security.imgSecCheck({
      media: {
        contentType: "image/png",
        value: Buffer.from(buffer),
      },
    });
    return result;
  } catch (err) {
    return err;
  }
};
//config.json
{
	"permissions": {
		"openapi": [
			"security.imgSecCheck"
		]
	}
}
//js中
    wx.cloud
      .callFunction({
        name: "云函数名称",
        data: {
        //使用wx.cloud.CDN避免传输的图片过大
          imgData: wx.cloud.CDN({
            type: "filePath",
            filePath: '选择的图片的路径',
          }),
        },
      })
      .then((res) => {
        if (res.result.errCode === 87014) {
          wx.showToast({
            title: "图片可能违规,请仔细检查后再试!",
            icon: "none",
            duration: 5000,
          });
        } else {
          wx.showToast({
            title: "图片检测通过",
            icon: "none",
            duration: 1000,
          })
        }
      });

其实还挺简单的,就是细节比较多,当时做的时候踩了一堆坑,所以发一下,大家就避免少踩坑了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值