评论

云服务器调用security.imgSecCheck完成代码分享

云服务器调用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;
}
}

本地函数:
wx.chooseImage({count: 1}).then((res) => {
if(!res.tempFilePaths[0]){
return;
}
console.log(JSON.stringify(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 => {
console.log(JSON.stringify(imgRes))
if(imgRes.result.errorCode == ‘87014’){
wx.showToast({
title:‘图片含有违法违规内容’,
icon:‘none’
})
return
}else{
//图片正常
}

                        }
                    )
                },
                fail: err => {
                    console.log(err)
                }
            }
        )
        
        
        我相信做出来的人很多,但是没有分享出来,我今天分享出来就是为了避免更多程序员不要在这种简单的问题上,浪费太多的时间,我就浪费了很多时间,兼职太坑爹了
点赞 15
收藏
评论

12 个评论

  • MR.Liang
    MR.Liang
    2020-01-21

    感谢。看了你的代码。关键在 wx.getFileSystemManager().readFile

    我才能解决了问题。非常感谢

    2020-01-21
    赞同 2
    回复 2
    • 蝈蝈
      蝈蝈
      2020-02-04
      解决问题就好
      2020-02-04
      回复
    • 伟康
      伟康
      2020-10-08
      https 怎么调用?
      2020-10-08
      1
      回复
  • 伟康
    伟康
    2020-10-08

    请问 https 调用怎么搞? 我一直是41005,小程序好像是不能使用new formData() 来包裹

    2020-10-08
    赞同 1
    回复
  • 艳
    艳
    发表于移动端
    2020-05-01
    1人丶一人、。、 j. d. miV一一一|!。一
    2020-05-01
    赞同
    回复
  • 2020-04-10

    老师,我一直是41005,怎么解决呢

    2020-04-10
    赞同
    回复
  • 2020-04-07

    老哥,为什么我用你代码一直报41005

    2020-04-07
    赞同
    回复 1
    • 2021-05-25
      您的问题解决了吗,我也一直报,求解
      2021-05-25
      回复
  • Ashin
    Ashin
    2020-03-19

    楼主,我这么返回的result是空咋整啊

    2020-03-19
    赞同
    回复
  • Jason
    Jason
    2020-02-08

    感谢楼主,节约了不少时间。

    2020-02-08
    赞同
    回复
  • 🔅
    🔅
    2019-12-15

    Error: errCode: -404011 cloud function execution error | errMsg: cloud.callFunction:fail requestID local_debug_a707c1c0-0260-4050-a4bf-40bd37e478ea, cloud function service error code -1, error message request entity too large; at cloud.callFunction api; 啥情况

    2019-12-15
    赞同
    回复 2
    • 🔅
      🔅
      2019-12-15
      本地调试会这样,部署上去就不会
      2019-12-15
      回复
    • 蝈蝈
      蝈蝈
      2020-02-04回复🔅
      可能是一些奇怪的问题...解决就好
      2020-02-04
      回复
  • A 🍀  红孩子2
    A 🍀 红孩子2
    2019-12-06

    {"tempFilePaths":["wxfile://tmp_eb4763bc72a0aa7ee336d235bf1801d360bc4c081ce27e4b.jpg"],"errMsg":"chooseImage:ok","tempFiles":[{"path":"wxfile://tmp_eb4763bc72a0aa7ee336d235bf1801d360bc4c081ce27e4b.jpg","size":84035}]}

    choose.js:996 ArrayBuffer {}byteLength: 84035__proto__: ArrayBuffer

    [publib]:1429 [process] unhandledRejection Error: errCode: -404012 error while waiting for the result | errMsg: cloud.callFunction:fail error while waiting for the result; at cloud.callFunction api; 

        at new u ([publib]:1)

        at d ([publib]:1)

        at f ([publib]:1)

        at [publib]:1

        at [publib]:1

        at Object.next ([publib]:1)

        at n ([publib]:1)

        at <anonymous> 

    咋我用你的在开发者工具上可以,但在手机上直接报这个错呢,求指教

    2019-12-06
    赞同
    回复
  • Amber张欠鱼
    Amber张欠鱼
    2019-11-25

    万分感谢楼主

    2019-11-25
    赞同
    回复

正在加载...

登录 后发表内容