uniapp之云開發(fā)uniCloud入門

1.什么是uniCloud?

  • 是Dcloud聯(lián)合阿里云,騰訊云,為uniapp的開發(fā)者提供的基于serverless模式和js編程的云開發(fā)平臺

2.uniCloud的價值?

  • 對于程序員來說,用熟悉的js,輕松搞定前臺整體業(yè)務(wù)。
  • 對于開發(fā)商:
    1.開發(fā)成本大幅下降
    2.不管用什么服務(wù)器運維,彈性擴(kuò)容,防DDos攻擊,全都不需要操心
    3.如果不發(fā)布H5版,你講不需要購買備案域名。小程序和APP可以免域名使用服務(wù)器
    4.可以考慮按照業(yè)務(wù)負(fù)責(zé)分工,而不是按前后臺分工

3.創(chuàng)建uniCloud項目(看不懂的可以到uniapp官網(wǎng)看,會詳細(xì)一點)

  • 在創(chuàng)建新項目,選擇uni-app項目,并勾選啟動uniCloud
  • 在右側(cè)選擇服務(wù)供應(yīng)商(阿里云,騰訊云)
  • 對于老的uniapp項目,也可以對項目點右鍵,菜單中選擇‘創(chuàng)建uniCloud云開發(fā)環(huán)境’
  • 新建uniapp項目中的模板,有一個hello uniCloud 項目模板,演示了各種云函數(shù)的使用。(創(chuàng)建了一個為cloudfunctions)

4.創(chuàng)建和綁定服務(wù)空間

  • 注:如果是一個服務(wù)空間的話,系統(tǒng)會幫我們綁定,要是多個的話,需要我們手動綁定一下
  • 項目創(chuàng)建好后,需要我們?yōu)檫@個項目創(chuàng)建一個服務(wù)空間(如果沒有實名認(rèn)證的話,首先需要實名認(rèn)證)
  • 在云函數(shù)目錄cloudfunctions,右鍵菜單創(chuàng)建服務(wù)空間,會打開web控制臺跳轉(zhuǎn)進(jìn)行創(chuàng)建
  • 創(chuàng)建好服務(wù)空間后,對目錄cloudfunctions,點擊右鍵菜單中,選擇云服務(wù)空間,點擊你創(chuàng)建的服務(wù)空間

5.客戶端調(diào)用云函數(shù)

  uniCloud.callFunction()//調(diào)用

6.創(chuàng)建數(shù)據(jù)庫

  • 可以在uniCloud(后臺)中直接創(chuàng)建
  • 可以通過node.js的后臺代碼來創(chuàng)建
  • 注:在uniapp中,為了安全起見,是不允許客戶端直接調(diào)用數(shù)據(jù)庫,而是通過客戶端調(diào)用云函數(shù),云函數(shù)調(diào)用數(shù)據(jù)庫的形式

7.數(shù)據(jù)庫和集合的引用

  const db=uniCloud.database();
  const ban=db.collection('集合名');//集合名就是數(shù)據(jù)表的名稱
  db.createCollection(collectionName)//創(chuàng)建集合

8.簡單的介紹一下數(shù)據(jù)庫的使用(增、刪、改、查)

  const db=uniCloud.database();
  const ban=db.collection('集合名');
  ban.get().then(res=>{console.log(res)}).catch(msg=>{console.log(msg)});//獲取集合中的數(shù)據(jù),promise寫法
  ban.get({sucess:(res)=>{console.log(res)},fail:(msg)=>{console.log(msg)}});//第二種寫法
  //這里只簡單的介紹,詳細(xì)的可查看 官方文檔
  ban.where({//查詢
    name:"查詢的name",
    index:查詢的下標(biāo)等  
  }).get().then(res=>{console.log(res)}).catch(e=>{console.log(msg)});
  //添加
  ban.add({
    data:{
        id:要添加的id,
        name:要添加的name
    }
  }).then(res=>{console.log(res)}).catch(e=>{console.log(msg)});
  //更新:局部更新(只能更新一條數(shù)據(jù)),全局更新(覆寫)
  ban.doc('id').update({//局部更新
    data:{
      name:'要替換的屬性名'
    }
  }).then(res=>{console.log(res)}).catch(e=>{console.log(msg)});
  ban.doc('id').set({//全局更新:相當(dāng)于在重寫數(shù)據(jù)
    data:{
      name:'要替換的屬性名',
      id:要替換的id名
    }
  }).then(res=>{console.log(res)}).catch(e=>{console.log(msg)});
//刪除數(shù)據(jù),只能每次刪除一條
  ban.doc('id').remove({ }).then(res=>{console.log(res)}).catch(e=>{console.log(msg)});

9.云儲存

上傳文件到云存儲,阿里云單文件大小限制為100M,騰訊云單文件最大為5G

        uni.chooseImage({
        count:1,
        success: (res) => {
          console.log(res);
          var path=res.tempFilePaths[0];//獲取當(dāng)前圖片的路徑
          uniCloud.uploadFile({
            filePath:path,//當(dāng)前圖片路徑
            cloudPath:'img',//文件夾名
            success: (res1) => {
            console.log(res1);
            }
        })
    }
 })

            // promise寫法
            const result = await uniCloud.uploadFile({
                filePath: filePath
            });
  // promise寫法
uniCloud
  .deleteFile({
    fileList: ['云儲存的ID'];//字符串類型的數(shù)組
  })
  .then(res => {});

// callback寫法
uniCloud.deleteFile(
  {
    fileList: ['云儲存的ID'],
    success(){},
    fail(){},
    complete(){}
  }
);

10.客戶端調(diào)用云函數(shù)(云函數(shù)調(diào)用數(shù)據(jù)庫)

  • 先新建一個云函數(shù)文件,寫完后點擊右鍵,上傳并部署
  //index.js云函數(shù)文件
  'use strict'; 注意啦L寫完后,要在文件夾上單機(jī)右鍵上傳部署,在客戶端才能請求到數(shù)據(jù)
  uniCloud.init();//當(dāng)使用一個服務(wù)空間的時候,不需要這一步,
  const db=uniCloud.database();
  exports.main = async (event, context) => {
  //event是客戶端傳入的參數(shù)  context可打印查看
    return new Promise((resolve,reject)=>{
    db.collection('banner').get().then(res=>{
        resolve(res);//成功返回數(shù)據(jù)
    }).catch(msg=>{
        reject(msg);//失敗返回數(shù)據(jù)
    })
  })
};
//在前端調(diào)用云函數(shù)
      onLoad() {
          uniCloud.callFunction({//客戶端調(diào)用云函數(shù),云函數(shù)調(diào)用數(shù)據(jù)庫
              name:'index',//在云函數(shù)中的函數(shù)名,也就是創(chuàng)建的云函數(shù)的文件夾的名字
              success:(res)=> {
                  console.log(res);//成功返回的數(shù)據(jù)
                  var ban=res.result.data[0].banner;//獲取我們所需要的數(shù)據(jù)
                  console.log(ban);
              }
          })
      },

11.客戶端動態(tài)傳值并將數(shù)據(jù)添加到數(shù)據(jù)庫中

  • 云函數(shù)文件add_one-->add_one.js
    'use strict';

//云函數(shù)可以添加數(shù)據(jù)到數(shù)據(jù)庫(在當(dāng)前文件中將name值為固定值張三)
//將name值為event.a通過客戶端傳參,可以直接添加數(shù)據(jù)到云函數(shù)和數(shù)據(jù)庫中
const db=uniCloud.database();//引用數(shù)據(jù)庫
exports.main = async (event, context) => {
  return new Promise((resolve,reject)=>{
      db.collection('add').add({//獲取當(dāng)前要添加數(shù)據(jù)的集合
          data:{
              name:event.a,
              age:event.b
          }
      }).then(res=>{
          resolve(res);
      }).catch(e=>{
          reject(e);
      })
  })
};

  • 前端頁面
  uniCloud.callFunction({//通過客戶端添加數(shù)據(jù)到云函數(shù)中,再到到數(shù)據(jù)庫中
                name:'add_one',//云函數(shù)名稱
                data:{
                    a:'小壞蛋111',
                    b:18
                },
                success: (res) => {
                    console.log(res);
                    
                },fail: (msg) => {
                    console.log(msg);
                },
                complete: (m) => {
                    console.log('在執(zhí)行');
                }
            });
  • 注:此時云函數(shù)add_one中和數(shù)據(jù)庫中都會有添加的數(shù)據(jù)
  • 注:增刪改查的寫法都是一樣的,詳細(xì)的可以去看看官方的文檔,歡迎隨時交流學(xué)習(xí),我也是剛開始研究,希望哪位大佬可以指點一下 !

  • 新增-----使用db_init.json直接寫入數(shù)據(jù)上傳到數(shù)據(jù)庫中

  • 首先右鍵創(chuàng)建db_init.json文件,在json文件中進(jìn)行寫入我們需要的數(shù)據(jù)格式,寫完后,在文件上點擊右鍵,初始化云數(shù)據(jù)庫,就會上傳到數(shù)據(jù)庫啦,注意:上傳一次之后,再次修改上傳的話,可能會上傳不上去,那就把數(shù)據(jù)庫中的刪了,再次上傳就可以了,或者寫一個新的數(shù)據(jù)再次上傳也是可以的
  
{
"list": { // 集合(表名)
        "data": [ // 數(shù)據(jù)(都在這個里面寫入數(shù)據(jù))
         
               {
                    "_id": "da51bd8c5e37ac14099ea43a2505a1a5",
                   "name": "tom",
                   "title":"我是寫入數(shù)據(jù)庫的數(shù)據(jù)1",
                   "list":[//里面又有很多的數(shù)據(jù),可以寫入成功,不能再一個上面連續(xù)的修改,覆蓋,上傳不上去
                       {"title":"我是誰,你是誰","id":1,"price":12},
                       {"title":"我是誰,你是誰","id":1,"price":12},
                       {"title":"我是誰,你是誰","id":1,"price":12},
                       {"title":"111","id":1,"price":12},
                       {"title":"我是誰,你是誰","id":1,"price":12},
                       {"title":"我是誰,你是誰","id":1,"price":12}
                   ]
               }
           
        ]
       
    }
}
  • 效果圖如下

2.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容