// 云函数入口文件 const cloud = require('wx-server-sdk') cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV}) // 云函数入口函数 exports.main = async (event, context) => { console.log(event) switch (event.action||event.name) { case "wxacode.getUnlimited": case "getWXACodeUnlimited":{ return getWXACodeUnlimited(event) } case "wxacode.get": case 'getWXACode': { return getWXACode(event) } case "security.msgSecCheck":{ //return cloud.openapi.security.msgSecCheck(event.data); try{ var res = await cloud.openapi.security.msgSecCheck(event.data); return res; }catch(e){ return e; //e = new Error("输入内容有敏感词汇"); //e.code = e.errCode = 87014; //throw e; } } case 'getOpenData': { return getOpenData(event) } default: { return } } } async function getWXACodeUnlimited(event){ let {scene, page} = event; const wxacodeResult = await cloud.openapi.wxacode.getUnlimited({ scene, page }) const fileExtensionMatches = wxacodeResult.contentType.match(/\/([^\/]+)/) const fileExtension = (fileExtensionMatches && fileExtensionMatches[1]) || 'jpg' const uploadResult = await cloud.uploadFile({ cloudPath: `images/wxacode/${(page+"?"+scene).replace(/[\/?&]/g, "_")}.${fileExtension}`, fileContent: wxacodeResult.buffer, }) if (!uploadResult.fileID) { throw new Error(`upload failed with empty fileID and storage server status code ${uploadResult.statusCode}`) } return uploadResult.fileID } async function getWXACode(event) { let {path} = event.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, }); } async function getOpenData(event) { return cloud.getOpenData({ list: event.openData.list, }) }