Home  >  Article  >  WeChat Applet  >  WeChat applet calls image security API

WeChat applet calls image security API

hzc
hzcforward
2020-07-01 09:57:322441browse

The WeChat mini program was reviewed and rejected. The reason for rejection was that the pictures uploaded by the user may have illegal or illegal issues, and the program must have an audit mechanism.
The solution is as follows (cloud development):
config.json

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

Cloud function

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)
          }
        })

      }
    });
  },

Recommended tutorial: "微信小program"

The above is the detailed content of WeChat applet calls image security API. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jianshu.com. If there is any infringement, please contact admin@php.cn delete