图片检查云函数imgSecCheck 踩坑指南
发布于 4 年前 作者 tangyang 3787 次浏览 来自 分享
	  //小程序客户端代码	
          wx.chooseImage({
		  	count:'1',
		  	sizeType: ['compressed'],
			  sourceType: ['album', 'camera']
		  }).then((res)=>{
			  	wx.showLoading();
				 let opt= {src:res.tempFilePaths[0], quality:5}
				 return wx.compressImage(opt);//压缩图片发送至CDN暂存
			}).then((res)=>{
				 let cdnUrl=wx.cloud.CDN({  
					type: 'filePath',
					filePath: res.tempFilePath,//压缩图片发送至CDN暂存
				});
				 let opt={
					name:'imgSecCheck',
					data:{
						imgcontent: cdnUrl
					}
				};
				return wx.cloud.callFunction(opt) //调用云函数检查图片是否合法
			}).then((res)=>{
				wx.hideLoading();
				if (res.result.errCode == 0) {
					wx.showModal({
						title: '提醒',
						content: '检查成功!',
						showCancel: false
					}) 
					//todo 上传图片
				}else{
					wx.showModal({
						title: '提醒',
						content: '违规图片!',
						showCancel: false
					})
				}
			}).catch((err)=>{
				console.error(err);
				wx.hideLoading();
				if(err.errMsg!='chooseImage:fail cancel'){
					wx.showModal({
						title: '提醒',
						content: '出错了请稍后再试!',
						showCancel: true
					})
				}
			});
//图片检查云函数定义

const cloud = require('wx-server-sdk')
const axios = require('axios');
cloud.init()

exports.main = async (event, context) => {
  let buffer=null;
// 下载CDN图片进行检查
  await axios({
    method'get',
    url: event.imgcontent,
    responseType'arraybuffer'
  }).then(res => {
     buffer=res.data;
  })
  try{
    return await cloud.openapi.security.imgSecCheck({
      media: {
        contentType'image/png',
        value:buffer
      }
    })
  }catch(e){
    return e;
  }
}
回到顶部