const cloud = require('wx-server-sdk') cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV}) exports.main = async (event, context) => { console.log("调用云函数openapi, 参数event:", event); let {name, data} = event; switch (event.name||event.action) { case "wxacode.getUnlimited": case "getWXACodeUnlimited":{ return getWXACodeUnlimited(data) } case "wxacode.get": case 'getWXACode': { return getWXACode(data) } case "security.msgSecCheck":{ try{ console.log("检查文本安全, 参数:", data); var res = await cloud.openapi.security.msgSecCheck(data); console.log("返回结果: ", res); return res; }catch(e){ return e; } } case "security.imgSecCheck":{ try{ console.log("检查图片安全, 参数:", data); if(data.media) var value = Buffer.from(data.media); else if(data.fileID){ var {fileID} = data; var res = await cloud.downloadFile({fileID}); var value = res.fileContent; } var res = await cloud.openapi.security.imgSecCheck({ media:{ contentType:"image/png", value } }); console.log("返回结果: ", res); return res; }catch(e){ return e; } } case 'getOpenData': { return cloud.getOpenData(data); } case "search.siteSearch":{ return cloud.openapi.search.siteSearch(data); } default: { return } } } async function getWXACodeUnlimited(data){ let {scene, page} = data; const wxacodeResult = await cloud.openapi.wxacode.getUnlimited({ scene, page }) const fileExtensionMatches = wxacodeResult.contentType.match(/\/([^\/]+)/) const fileExtension = (fileExtensionMatches && fileExtensionMatches[1]) || 'jpg' return cloud.uploadFile({ cloudPath: `images/wxacode/${(page+"?"+scene).replace(/[\/?&]/g, "_")}.${fileExtension}`, fileContent: wxacodeResult.buffer, }) } async function getWXACode(data) { let {path} = data; const wxacodeResult = await cloud.openapi.wxacode.get({ path }) const fileExtensionMatches = wxacodeResult.contentType.match(/\/([^\/]+)/) const fileExtension = (fileExtensionMatches && fileExtensionMatches[1]) || 'jpg' return cloud.uploadFile({ cloudPath: `images/wxacode/${path.replace(/[\/?&]/g,"_")}.${fileExtension}`, fileContent: wxacodeResult.buffer, }); }