開發(fā)者可以使用云開發(fā)開發(fā)微信小程序、小游戲,無需搭建服務(wù)器,即可使用云端能力。
云開發(fā)為開發(fā)者提供完整的云端支持,弱化后端和運維概念,無需搭建服務(wù)器,使用平臺提供的 API 進行核心業(yè)務(wù)開發(fā),即可實現(xiàn)快速上線和迭代,同時這一能力,同開發(fā)者已經(jīng)使用的云服務(wù)相互兼容,并不互斥。
目前提供三大基礎(chǔ)能力支持:
1、云函數(shù):在云端運行的代碼,微信私有協(xié)議天然鑒權(quán),開發(fā)者只需編寫自身業(yè)務(wù)邏輯代碼
2、數(shù)據(jù)庫:一個既可在小程序前端操作,也能在云函數(shù)中讀寫的 JSON 數(shù)據(jù)庫
3、存儲:在小程序前端直接上傳/下載云端文件,在云開發(fā)控制臺可視化管理
具體的可以去小程序文檔上查看,下面用一個登錄注冊的案例來演示小程序云開發(fā)數(shù)據(jù)庫的運用
注冊


在創(chuàng)建的時候,要在點下一步的時候,調(diào)數(shù)據(jù)庫來看用戶名有沒有重復(fù)的。在點擊同意的時候來調(diào)用數(shù)據(jù)庫,然后把所有的判斷放到下一步來判斷。所有條件都滿足就將用戶名和密碼放到全局變量中。
var app = getApp();Page({ data: { userName: '', userPassword: '', userPasswordAgain: '', checkbox: false, repetition: false }, // 返回主頁面 backHomeTap: function() { wx.switchTab({ url: '../index/index', }) }, // 綁定 bindingTap: function () { wx.redirectTo({ url: '../login/login', }) }, // 用戶名 userNameInput: function(e) { this.setData({ userName: e.detail.value }); }, // 密碼 userPasswordInput: function(e) { this.setData({ userPassword: e.detail.value }); }, // 再次輸入密碼 userPasswordAgainInput: function(e) { this.setData({ userPasswordAgain: e.detail.value }); }, // 同意 checkboxChange: function() { if (this.data.checkbox === false) { this.setData({ checkbox: true }) } else { this.setData({ checkbox: false }) } var that = this; var userName = this.data.userName; // 初始化云 wx.cloud.init({ env: 'wubaib-9543f7', traceUser: true }); // 初始化數(shù)據(jù)庫 const db = wx.cloud.database(); const _ = db.command; db.collection('userInformation').where({ userName: _.eq(userName) }).get({ success: function (res) { if (res.data.length === 1) { that.setData({ repetition: true }) } } }) }, // 下一步,完善個人信息 perfectInforTap: function() { var userName = this.data.userName; var userPassword = this.data.userPassword; var checkbox = this.data.checkbox; var userPasswordAgain = this.data.userPasswordAgain; var name = /^[A-Za-z0-9/u4e00-/u9fa5]+$/; var repetition = this.data.repetition; if (userName === '') { wx.showToast({ title: '請輸入用戶名', icon: 'none', duration: 2000, mask: true }) } else if (!name.test(userName)) { wx.showToast({ title: '用戶名格式不正確', icon: 'none', duration: 2000, mask: true }) } else if (repetition === true) { wx.showToast({ title: '用戶名已存在', icon: 'none', duration: 2000, mask: true }) } else if (userPassword === '') { wx.showToast({ title: '請輸入密碼', icon: 'none', duration: 2000, mask: true }) } else if (userPassword.length < 6) { wx.showToast({ title: '密碼最少6位', icon: 'none', duration: 2000, mask: true }) } else if (userPassword !== userPasswordAgain) { wx.showToast({ title: '兩次密碼輸入不一致', icon: 'none', duration: 2000, mask: true }) } else if (checkbox === false) { wx.showToast({ title: '請選中已閱讀', icon: 'none', duration: 2000, mask: true }) } else { wx.redirectTo({ url: 'perfectInfor/perfectInfor', }) // 保存用戶名和密碼 app.appData.account = { userName: userName, userPassword: userPassword } } }})
新聞熱點
疑難解答