小程序云开发
项目的三大板块
数据库
存储
云函数
数据库
通过 wx.cloud.database().collection("list") 来导入数据库
对数据的增删改查
增: 需要通过e..detail.value来获取增加的数据,通过data值来接收增加的数据,之后调用成功失败的回调. 自动将数据保存到数据库
删:通过doc传递id,调用remove方法
改:通过doc传递id,调用update方法.通过data值来修改原有的数据
查:调用get方法
存储
存储指所有文件的存储,且拿图片为例
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: res => { // 这里使用箭头函数,可以解决调用云函数上传数据的this执行问题
console.log('上传图片', res.tempFilePaths[0])
this.upLoadImg(res.tempFilePaths[0])
}
})
upLoadImg(fileURL) {
wx.cloud.uploadFile({
cloudPath: new Date().getTime() + 'qq.png', // 上传至云端的路径 加一个时间戳
filePath: fileURL, // 小程序临时文件路径
success: res => {
// 返回文件 ID
console.log('上传图片的id', res.fileID)
this.setData({
imgURL: res.fileID // 将数据通过赋值的形式,渲染到页面
})
},
fail: console.error
})
},
云函数
1. 初步使用:在app.js初始化
// 云函数端初始化
wx.cloud.init({
env: 'dev-yyy-a5xlr'
})
2. 在project.config.json 添加配置项
"cloudfunctionRoot": "/cloud",
3. 然后新建云函数文件
4. 云函数的修改每次都要上传和部署
订阅消息的实现
用户授权
订阅消息实现的前提,是用户提前授权
wx.requestSubscribeMessage({
tmplIds: ['f0cH3FrHH02tE8YcEixva09B-BkdYYDISid1YmstQP4'],
success: res => {
console.log('授权成功', res)
},
fail: res => {
console.log('授权失败', res)
}
})
订阅消息模板
// 云函数入口函数
exports.main = async (event, context) => {
try {
const result = await cloud.openapi.subscribeMessage.send({
touser: event.openid, // 动态openid,如果想群体发送模板,就传入多个openid
page:'pages/index/index',
data:{
thing1: {
value: event.nameP // 动态数据
},
date3:{
value:'2020年3月23日 16:40'
},
thing4: {
value:'钱已经到账'
}
},
templateId: 'f0cH3FrHH02tE8YcEixva09B-BkdYYDISid1YmstQP4'
})
console.log(result)
return result
}
catch (err) {
console.log(err)
return err
}
}