首页 > 微信小程序 > 微信开发 > 微信小程序调用图片安全API

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

hzc
发布: 2020-07-01 09:58:20
转载
2626 人浏览过

微信小程序审核拒绝,拒绝原因是用户上传图片可能存在违法违规问题,程序必须有审核机制。
解决方法如下(云开发):
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中文网其他相关文章!

相关标签:
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
微信小程序
来自于 1970-01-01 08:00:00
0
0
0
微信小程序轮播
来自于 1970-01-01 08:00:00
0
0
0
微信小程序自动补全工具
来自于 1970-01-01 08:00:00
0
0
0
现在个人嫩申请微信小程序
来自于 1970-01-01 08:00:00
0
0
0
javascript - 微信小程序如何写倒计时
来自于 1970-01-01 08:00:00
0
0
0
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板