• 技术文章 >微信小程序 >微信开发

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

    hzchzc2020-07-01 09:58:20转载964
    微信小程序审核拒绝,拒绝原因是用户上传图片可能存在违法违规问题,程序必须有审核机制。
    解决方法如下(云开发):
    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删除
    专题推荐:微信小程序
    上一篇:微信公众号文章删除不了怎么办? 下一篇:微信小程序页面开发
    大前端线上培训班

    相关文章推荐

    • 微信小程序看不了广告怎么办?• 用mpvue开发微信小程序基础知识• 记一次微信小程序在安卓手机上的白屏问题• 微信小程序中自定义select下拉选项框组件

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网