云函数做图片内容检测img_sec_check
我想利用云函数使用img_sec_check接口进行对图片内容进行检测,但不知道怎样写,请问有人这样子玩过吗?
1 回复
// 云函数入口文件 const cloud = require( 'wx-server-sdk' ) //npm install request-promise const rp = require( 'request-promise' ); cloud.init() // 云函数入口函数 exports.main = async (event, context) => { //appid 和秘钥 const appid = 'wxxxxxxxxxxx' , secret = 'aaaaaaaaaa' ; const AccessToken_options = { method: 'GET' , url: 'https://api.weixin.qq.com/cgi-bin/token' , qs: { appid, secret, grant_type: 'client_credential' }, json: true }; //获取AccessToken const resultValue = await rp(AccessToken_options); const token = resultValue.access_token; //下载图片 //从云存储取出文件 或者直接从用户选择上传而来 const res = await cloud.downloadFile({ fileID: "cloud://xxxxxx" }); //上传图片 const upload_options = { method: 'POST' , uri: 'https://api.weixin.qq.com/wxa/img_sec_check?access_token=' + token, formData: { name: 'xxx' , file: { value: res.fileContent, //res.fileContent options: { filename: 'test.jpg' , contentType: 'image/jpg' } } }, headers: { /* 'content-type': 'multipart/form-data' */ } }; //检测图片 const checkRes =await rp(upload_options); return checkRes; } |