// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
// 云函数入口函数
exports.main = async (context) => {
const db = cloud.database(); //云信息
const {
collectionInfo, // 操作的数据
collectionName, // 数据库表名
queryWay, // 操作方式
screen // 操作条件
} = context
try {
if (queryWay == 'add') {
return await db.collection(collectionName).add({
data: {
...collectionInfo
}
})
} else if (queryWay == 'get') {
if (!screen) {
return await db.collection(collectionName).get({})
} else {
return await db.collection(collectionName).doc(screen).get({})
}
} else if (queryWay == 'remove') {
return await db.collection(collectionName).doc(screen).remove({})
} else if (queryWay == 'update') {
return await db.collection(collectionName).doc(screen).set({
data: {
...collectionInfo
}
})
}
} catch (e) {
return e
}
}
wxml 调用
wx.cloud.callFunction({
name: 'callback',
data: {
queryWay: "add",//方式
collectionName: "todos",//表名
collectionInfo: {
test:"测试数据"
},
screen: "" //条件
}
}).then(res => {
console.log(res)
})