小程序云开发基本操作(附开源云开发项目)

云数据库,云函数,云储存(附开源云开发项目)

欢迎点击: 个人官网博客.
github源码: 多功能记账本

扫码体验:

在这里插入图片描述

云数据库基本操作(增删改查,链表)

云开发极大的方便了前端开发者,是前端工程师的一个福音,没事就赶快学起来吧!

注意:云数据库获取数据一次最多20条,如果要不受限制,则调用云函数进行操作
    const db=wx.cloud.database() 连接云数据库
    const teachers=db.collection('teachers') 拿到teachers表(模型)

-----------------------------------------------增
    teachers.add({
    
    
      data:{
    
     name:''}
    }) 增加
-----------------------------------------------删除
    teachers.doc('b1a52c595fb4d8770002aede69836369').remove() 查询到指定id并删除
    teachers.where({
    
     name:''}).remove()  批量删除
    
------------------------------------------------改
    teachers.doc('b1a52c595fb4d8770002aede69836369').update({
    
    
      data:{
    
     name:''}
    }) 查询到指定id并更新

    teachers.where({
    
     name:''}).update({
    
    
      data:{
    
     name:''}
    }) 批量更新
-------------------------------------------------查
    teachers.doc('b1a52c595fb4d8770002aede69836369') 查询到指定id
    teachers.get() 查询全部
    teachers.where({
    
     name:''}).get()  条件查询
    teachers.orderBy('age', 'desc').get()  条件查询升序  ---asc倒
    teachers.limit(10).get()  条件查询10条
    teachers.skip(10).limit(10).get()  查询10条后的10-----------------------------------------------  链表

    const db = cloud.database()  //-----------------
    db.collection('orders').aggregate()  //父项表名
      .lookup({
    
    
        from: 'books',         //子项表名
        localField: 'book',      //父项要匹配的字段
        foreignField: 'title',    //子项要匹配的字段
        as: 'bookList',          //放在父项那个字段里面
      })
      .end()
      .then(res => console.log(res))
      .catch(err => console.error(err))


云函数

// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
    
    
  env: 'demo-4cb91'
})

// 云函数入口函数
exports.main = async (event, context) => {
    
    
  const wxContext = cloud.getWXContext()
  const db =cloud.database()
  const teachers = db.collection('teachers')  操作云数据库
  let result= teachers.get().then(res => {
    
    
      return res
    })
    .catch(err => {
    
    
      return err
    })
    return result
}

调用云函数

 wx.cloud.callFunction({
    
    
      // 云函数名称
      name: 'add',
      // 传给云函数的参数
      data: {
    
    
        name,
        pwd
      },
    })
    .then(res => {
    
    
      console.log(res) // 3
    })
    .catch(console.error)

云存储基本操作

    wx.chooseImage  选择图片
    wx.chooseVideo 选择视频
    wx.chooseMessageFile  选择文件

    wx.openDocument 打开文档 如excel

    wx.cloud.uploadFile  上传所选择的文件
    wx.cloud.downloadFile  下载
例如:
   上传图片
    wx.chooseImage({
    
    
      success: (res) => {
    
    
        console.log(res.tempFilePaths[0])
        wx.cloud.uploadFile({
    
    
          cloudPath: 'example.png',
          filePath: res.tempFilePaths[0], // 文件路径
          success: res => {
    
    
            console.log(res)
          },
          fail: err => {
    
    
            // handle error
          }
        })
      },
      complete:()=>{
    
    

      }
    })

猜你喜欢

转载自blog.csdn.net/qq_41560520/article/details/110238011