You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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 getOpenData(event)
}
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,
});
}
async function getOpenData(event) {
return cloud.getOpenData({
list: event.openData.list,
})
}