首頁  >  文章  >  微信小程式  >  微信小程式呼叫圖片安全API

微信小程式呼叫圖片安全API

hzc
hzc轉載
2020-07-01 09:57:322422瀏覽

微信小程式審核拒絕,拒絕原因是使用者上傳圖片可能有違法違規問題,程式必須有審核機制。
解決方法如下(雲端開發):
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)
          }
        })

      }
    });
  },

推薦教學:《微信小程式"

以上是微信小程式呼叫圖片安全API的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:jianshu.com。如有侵權,請聯絡admin@php.cn刪除