微信小程序之调用图片安全API

在小程序开发中,如果小程序涉及到文本或者图片的上传,以前没有这层限制,但是如今微信官方已经要求必须在小程序中加入内容审核,不然就审核拒绝,拒绝原因是用户上传图片可能存在违法违规问题,程序必须有审核机制。
基于以上问题,参考了微信官方API文档,其中提到了图片检测和文本检测
图片检测
在这里插入图片描述
文本检测
在这里插入图片描述
[小程序文档]:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.imgSecCheck.html
今天要弄的就是图片的检测,方法如下,是基于云开发的。
首先在云开发模式下的小程序中,新建一个云函数。
一、config配置

{
  "permissions": {
    "openapi": [
      "security.imgSecCheck"
    ]
  }
}

二、云函数

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

cloud.init()
 
exports.main = async (event, context) => {
  const { value } = event;
  try {
    const res = await cloud.openapi.security.imgSecCheck({
      media: {
        header: {
          'Content-Type': 'application/octet-stream'},
        contentType: 'image/png',
        value: Buffer.from(value)
        }
      })
    return res;
  } catch (err) {
    return err;
  }
}

三、图片验证

ChooseImage() {
    wx.chooseImage({
      count: 1, 
      sizeType: ['original', 'compressed'], 
      sourceType: ['album'], 
      success: (res) => {
        if (res.tempFiles[0] && res.tempFiles[0].size > 1024 * 1024) {
          wx.showToast({
            title: '图片不能大于1M',
            icon: 'none'
          })
          return;
        }
        //校验图片

        wx.getFileSystemManager().readFile({
          filePath: res.tempFilePaths[0],
          success: buffer => {
            console.log(buffer.data)
            wx.cloud.callFunction({
              name: 'checkImg',
              data: {
                value: buffer.data
              }
            }).then(
              imgRes => {
                if (imgRes.result.errCode == '87014') {
                  wx.showToast({
                    title: '图片含有违法违规内容',
                    icon: 'none'
                  })
                  return
                } else {
                  //图片正常,do something
                }

              }
            )
          },
          fail: err => {
            console.log(err)
          }
        })

      }
    });
  },

以上就是微信小程序图片校验的全部过程。

评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码的灵魂是bug!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值