图片识别不了小程序怎么办_微信小程序调用图片安全API

微信小程序审核拒绝,拒绝原因是用户上传图片可能存在违法违规问题,程序必须有审核机制。

解决方法如下(云开发):

config.json

{
  "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;
  }
}

js

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 {
                  //图片正常

                  if (this.data.imgList.length != 0) {
                    this.setData({
                      imgList: this.data.imgList.concat(res.tempFilePaths)
                    })
                  } else {
                    this.setData({
                      imgList: res.tempFilePaths
                    })
                  }


                }

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

      }
    });
  },

我在开发中遇到的更多的坑都已经记录在easyDemo小程序中,希望我的demo能给你easy。我还会持续更新easyDemo,欢迎收藏。

b2d3ff43c28d962d8a8ff1c37f90a932.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值