1. 程式人生 > >小程式 雲開發 資料庫操作

小程式 雲開發 資料庫操作

開發者可以使用雲開發開發微信小程式、小遊戲,無需搭建伺服器,即可使用雲端能力。

雲開發為開發者提供完整的雲端支援,弱化後端和運維概念,無需搭建伺服器,使用平臺提供的 API 進行核心業務開發,即可實現快速上線和迭代,同時這一能力,同開發者已經使用的雲服務相互相容,並不互斥。

目前提供三大基礎能力支援:

           1、雲函式:在雲端執行的程式碼,微信私有協議天然鑑權,開發者只需編寫自身業務邏輯程式碼

           2、資料庫:一個既可在小程式前端操作,也能在雲函式中讀寫的 JSON 資料庫

           3、儲存:在小程式前端直接上傳/下載雲端檔案,在雲開發控制檯視覺化管理

具體的可以去小程式文件上檢視,下面用一個登入註冊的案例來演示小程式雲開發資料庫的運用

註冊

在建立的時候,要在點下一步的時候,調資料庫來看使用者名稱有沒有重複的。在點選同意的時候來呼叫資料庫,然後把所有的判斷放到下一步來判斷。所有條件都滿足就將使用者名稱和密碼放到全域性變數中。

  1. // 同意

  2. checkboxChange: function() {

  3. if (this.data.checkbox === false) {

  4. this.setData({

  5. checkbox: true

  6. })

  7. } else {

  8. this.setData({

  9. checkbox: false

  10. })

  11. }

  12. var that = this;

  13. var userName = this.data.userName;

  14. // 初始化雲

  15. wx.cloud.init({

  16. env: 'wubaib-9543f7',

  17. traceUser: true

  18. });

  19. // 初始化資料庫

  20. const db = wx.cloud.database();

  21. const _ = db.command;

  22. db.collection('userInformation').where({

  23. userName: _.eq(userName)

  24. }).get({

  25. success: function (res) {

  26. if (res.data.length === 1) {

  27. that.setData({

  28. repetition: true

  29. })

  30. }

  31. }

  32. })

  33. },

在完善資訊的時候獲取所有的變數(使用者名稱和密碼也在內),然後在點選下一步完成按鈕將資料上傳到資料庫。

  1. // 初始化雲

  2. wx.cloud.init({

  3. env: 'wubaib-9543f7',

  4. traceUser: true

  5. });

  6. // 初始化資料庫

  7. const db = wx.cloud.database();

  8. db.collection('userInformation').add({

  9. // data 欄位表示需新增的 JSON 資料

  10. data: {

  11. realName: realName,

  12. userName: userName,

  13. userPassword: userPassword,

  14. phone: phone,

  15. email: email,

  16. card: card

  17. },

  18. success: function(res) {

  19. // res 是一個物件,其中有 _id 欄位標記剛建立的記錄的 id

  20. console.log(res);

  21. console.log(res.errMsg);

  22. }

  23. })

登入

在登入頁面,先獲取使用者輸入的使用者名稱和密碼。在點選登入的時候,先根據userName調資料庫的密碼和使用者輸入的密碼是否相等。如果相等將使用者的資訊儲存到全域性變數中。

  1. // 點選登入

  2. bindingSuccess: function() {

  3. var that = this;

  4. var bindName = that.data.bindName;

  5. var bindPassword = that.data.bindPassword;

  6. if (bindName.length !== 0 && bindPassword.length !== 0) {

  7. // 初始化雲

  8. wx.cloud.init({

  9. env: 'wubaib-9543f7',

  10. traceUser: true

  11. });

  12. // 初始化資料庫

  13. const db = wx.cloud.database();

  14. db.collection('userInformation').where({

  15. userName: bindName

  16. }).get().then(res => {

  17. console.log(res.data);

  18. if (res.data[0].userPassword === bindPassword) {

  19. console.log("登入成功");

  20. // 儲存手機號,真實姓名,身份證號,郵箱 儲存使用者名稱

  21. that.setData({

  22. userName: res.data[0].userName,

  23. phone: res.data[0].phone,

  24. realName: res.data[0].realName,

  25. card: res.data[0].card,

  26. email: res.data[0].email,

  27. userId: res.data[0]._id

  28. })

  29. app.appData.userinfo = {

  30. phone: that.data.phone,

  31. realName: that.data.realName,

  32. card: that.data.card,

  33. email: that.data.email

  34. }

  35. app.appData.account = {

  36. userName: that.data.userName

  37. }

  38. app.appData.userId = {

  39. userId: that.data.userId

  40. }

  41. wx.switchTab({

  42. url: '../personalCenter/personalCenter',

  43. })

  44. } else {

  45. wx.showToast({

  46. title: '使用者名稱或密碼錯誤',

  47. icon: 'none',

  48. duration: 2000

  49. })

  50. }

  51. })

  52. }

  53. },

--------------------- 本文來自 一個減肥的小胖子 的CSDN 部落格 ,全文地址請點選:https://blog.csdn.net/qq_40999496/article/details/82773484?utm_source=copy