云开发
官网:developers.weixin.qq.com/miniprogram…
优点:无需搭建服务器,免登录、免鉴权调用微信开放服务,统一开发多端应用,不限开发语言和框架,按量计费,成本更低
1.初始化
需要app.js文件中初始化
App({
onLaunch() {
wx.cloud.init({
env:'demo1-3gudvkec5648f3f1' //evn为环境id,去云开发控制台设置中找
})
}
})
2.数据库
(以下所有代码是在小程序中执行的,默认在控制台已经操作完成,当然你可以直接去云开发控制台直接进行增删改查操作)
2.1 增加数据
wx.cloud.database().collection('person').add({
data:{
name:'foo',
age:18
}
})
.then(res=>{})
.catch(ree=>{})
//person是在数据库中添加的集合名
//name,age是person集合中记录中的字段名
2.2 查询数据
1.查询一条数据doc()
wx.cloud.database().collection('person').doc('fa24ce1a618d1e0805cbcdce43bc9b3a').get()
.then(res=>{})
.catch(ree=>{})
//fa24ce1a618d1e0805cbcdce43bc9b3a是指修改那条记录的id
2.查询多条数据where({}) 里面是查询条件
wx.cloud.database().collection('person').where({
name:'foo'
})
.then(res=>{})
.catch(ree=>{})
2.3 修改数据
wx.cloud.database().collection('person').doc('fa24ce1a618d1e0805cbcdce43bc9b3a').update({
data:{
name:'bar', //修改名字bar
}
})
.then(res=>{})
.catch(ree=>{})
2.4 删除数据
wx.cloud.database().collection('person').doc('fa24ce1a618d1e0805cbcdce43bc9b3a').remove()
.then(res=>{})
.catch(ree=>{})
//fa24ce1a618d1e0805cbcdce43bc9b3a是指修改那条记录的id
更多详细的操作请参考官方文档
3.云函数
3.1 初始化
1.根目录创建文件夹cloud
2.project.config.json配置云函数
{
"cloudfunctionRoot":"/cloud",
}
3.新建云函数getData
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
env:cloud.DYNAMIC_CURRENT_ENV //推荐使用常量
})
// 云函数入口函数
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
return {
event,
openid: wxContext.OPENID,
appid: wxContext.APPID,
unionid: wxContext.UNIONID,
}
}
4.使用云函数
wx.cloud.callFunction({
name:'getData', //对应新建的云函数
})
.then(res=>{
console.log(res);
})
.catch(err=>{
console.log(err);
})
3.2 获取数据
1.云函数getData
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
env:cloud.DYNAMIC_CURRENT_ENV
})
// 云函数入口函数
exports.main = async (event, context) => {
return cloud.database().collection('person').get() //相当于数据中的查询操作
}
2.小程序调用
wx.cloud.callFunction({
name:'getData',
})
.then(res=>{
console.log(res);
})
.catch(err=>{
console.log(err);
})
3.3 修改数据
1.云函数 updata
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
env:cloud.DYNAMIC_CURRENT_ENV
})
// 云函数入口函数
exports.main = async (event, context) => {
return cloud.database().collection('person').doc(event.id).update({
data:{
name:event.name
}
})
}
//通过event接收传递进来的参数
2.小程序调用
wx.cloud.callFunction({
name:'updata',
data:{
id:'fa24ce1a618d1e0805cbcdce43bc9b3a',
name:'jack111'
}
})
.then(res=>{
console.log(res);
})
.catch(err=>{
console.log(err);
})
//通过data进行传值
3.4 删除数据
1.云函数 removeData
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
env:cloud.DYNAMIC_CURRENT_ENV
})
// 云函数入口函数
exports.main = async (event, context) => {
return cloud.database().collection('person').doc(event.id).remove()
}
2.小程序调用
wx.cloud.callFunction({
name:'removeData',
data:{
id:'fa24ce1a618d1e0805cbcdce43bc9b3a',
}
})
.then(res=>{
console.log(res);
})
.catch(err=>{
console.log(err);
})
4.云存储
4.1上传图片
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success (res) {
// tempFilePath可以作为img标签的src属性显示图片
const tempFilePaths = res.tempFilePaths
wx.cloud.uploadFile({
cloudPath: 'example.png', // 上传至云端的路径
filePath: tempFilePaths[0], // 小程序临时文件路径
success: res => {
// 返回文件 ID
console.log(res.fileID)
},
fail: console.error
})
}
})
4.2选择、上传、下载、打开文件
1.选择文件
chooseFlie(){
wx.chooseMessageFile({
count: 10,
type: 'flie',
success :res=> {
const {name,path} = res.tempFiles[0]
this.uploadFile(name,path)
}
})
},
2.上传文件
uploadFile(name,path){
wx.cloud.uploadFile({
cloudPath: name, // 上传至云端的路径
filePath: path, // 小程序临时文件路径
success: res => {
this.downFlie(res.fileID) // 返回文件 ID
},
fail: console.error
})
},
3.下载文件
downFlie(fileID){
wx.cloud.downloadFile({
fileID, // 文件 ID
success: res => {
console.log(res.tempFilePath) // 返回临时文件路径
this.openDocument(res.tempFilePath)
},
fail: console.error
})
},
4.打开文件
openDocument(filePath){
wx.openDocument({
filePath,
success: function (res) {
console.log('打开文档成功')
}
})
}
\