解决方法如下(云开发):
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) } }) } }); },
推荐教程:《微信小程序》
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。