版权声明: https://blog.csdn.net/qq_40999496/article/details/82773484
开发者可以使用云开发开发微信小程序、小游戏,无需搭建服务器,即可使用云端能力。
云开发为开发者提供完整的云端支持,弱化后端和运维概念,无需搭建服务器,使用平台提供的 API 进行核心业务开发,即可实现快速上线和迭代,同时这一能力,同开发者已经使用的云服务相互兼容,并不互斥。
目前提供三大基础能力支持:
1、云函数:在云端运行的代码,微信私有协议天然鉴权,开发者只需编写自身业务逻辑代码
2、数据库:一个既可在小程序前端操作,也能在云函数中读写的 JSON 数据库
3、存储:在小程序前端直接上传/下载云端文件,在云开发控制台可视化管理
具体的可以去小程序文档上查看,下面用一个登录注册的案例来演示小程序云开发数据库的运用
注册


在创建的时候,要在点下一步的时候,调数据库来看用户名有没有重复的。在点击同意的时候来调用数据库,然后把所有的判断放到下一步来判断。所有条件都满足就将用户名和密码放到全局变量中。
-
-
-
-
-
-
-
-
-
-
-
backHomeTap: function() {
-
-
-
-
-
-
bindingTap: function () {
-
-
-
-
-
-
userNameInput: function(e) {
-
-
-
-
-
-
userPasswordInput: function(e) {
-
-
userPassword: e.detail.value
-
-
-
-
userPasswordAgainInput: function(e) {
-
-
userPasswordAgain: e.detail.value
-
-
-
-
checkboxChange: function() {
-
if (this.data.checkbox === false) {
-
-
-
-
-
-
-
-
-
-
var userName = this.data.userName;
-
-
-
-
-
-
-
const db = wx.cloud.database();
-
-
db.collection(‘userInformation‘).where({
-
-
-
success: function (res) {
-
if (res.data.length === 1) {
-
-
-
-
-
-
-
-
-
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;
-
-
-
-
-
-
-
-
} else if (!name.test(userName)) {
-
-
-
-
-
-
-
} else if (repetition === true) {
-
-
-
-
-
-
-
} else if (userPassword === ‘‘) {
-
-
-
-
-
-
-
} else if (userPassword.length < 6) {
-
-
-
-
-
-
-
} else if (userPassword !== userPasswordAgain) {
-
-
-
-
-
-
-
} else if (checkbox === false) {
-
-
-
-
-
-
-
-
-
url: ‘perfectInfor/perfectInfor‘,
-
-
-
-
-
userPassword: userPassword
-
-
-
-
在完善信息的时候获取所有的变量(用户名和密码也在内),然后在点击下一步完成按钮将数据上传到数据库。
-
-
-
-
-
-
-
-
-
-
-
-
-
backHomeTap: function() {
-
-
url: ‘../../index/index‘,
-
-
-
-
phoneInput: function(e) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
emailInput: function(e) {
-
-
-
-
-
-
registerSuccessTap: function() {
-
var phone = this.data.phone;
-
var realName = this.data.realName;
-
var card = this.data.card;
-
var email = this.data.email;
-
var userName = this.data.userName;
-
var userPassword = this.data.userPassword;
-
var phonereg = /^1[345789]\d{9}$/;
-
var namereg = /^[\u4E00-\u9FA5]+$/;
-
var cardreg = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|[xX])$/;
-
var emailreg = /^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/;
-
-
-
-
-
-
-
-
-
} else if (!phonereg.test(phone)) {
-
-
-
-
-
-
-
} else if (!namereg.test(realName)) {
-
-
-
-
-
-
-
} else if (card === ‘‘) {
-
-
-
-
-
-
-
} else if (!cardreg.test(card)) {
-
-
-
-
-
-
-
} else if (email === ‘‘) {
-
-
-
-
-
-
-
} else if (!emailreg.test(email)) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
const db = wx.cloud.database();
-
db.collection(‘userInformation‘).add({
-
-
-
-
-
userPassword: userPassword,
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
userName: app.appData.account.userName,
-
userPassword: app.appData.account.userPassword
-
-
-
-
-
登录
在登录页面,先获取用户输入的用户名和密码。在点击登录的时候,先根据userName调数据库的密码和用户输入的密码是否相等。如果相等将用户的信息保存到全局变量中。
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
registerTap: function() {
-
-
url: ‘../register/register‘
-
-
-
-
bindNameInput: function(e) {
-
-
-
-
-
if (that.data.bindName.length !== 0 && that.data.bindPassword.length !== 0) {
-
-
-
-
} else if (that.data.bindName.length === 0) {
-
-
-
-
-
-
-
bindPasswordInput: function(e) {
-
-
bindPassword: e.detail.value
-
-
-
if (that.data.bindName.length !== 0 && that.data.bindPassword.length !== 0) {
-
-
-
-
} else if (that.data.bindPassword.length === 0) {
-
-
-
-
-
-
-
bindingSuccess: function() {
-
-
var bindName = that.data.bindName;
-
var bindPassword = that.data.bindPassword;
-
if (bindName.length !== 0 && bindPassword.length !== 0) {
-
-
-
-
-
-
-
const db = wx.cloud.database();
-
db.collection(‘userInformation‘).where({
-
-
-
-
if (res.data[0].userPassword === bindPassword) {
-
-
-
-
userName: res.data[0].userName,
-
phone: res.data[0].phone,
-
realName: res.data[0].realName,
-
-
email: res.data[0].email,
-
-
-
-
-
realName: that.data.realName,
-
-
-
-
-
userName: that.data.userName
-
-
-
-
-
-
url: ‘../personalCenter/personalCenter‘,
-
-
-
-
-
-
-
-
-
-
-
-
-
-
登录WXML
-
<view class=‘phoneNumberContainer‘>
-
<input placeholder=‘用户名‘ maxlength=‘11‘ bindinput="bindNameInput"></input>
-
-
<view class=‘passwordContainer‘>
-
<input placeholder=‘密码‘ password="true" bindinput="bindPasswordInput"></input>
-
-
<view class="{{isChecked?‘bindingChecked‘:‘bindingNormal‘}}" bindtap=‘bindingSuccess‘>立即登录</view>
-
<view class=‘registerContainer‘ bindtap=‘registerTap‘>注册账号</view>
注册第一步的WXML
-
-
<view class=‘backHome‘ bindtap=‘backHomeTap‘>
-
<image src=‘/images/homeIcon.png‘ class=‘backHomeImg‘></image>
-
-
-
<view class=‘headerContainer‘>
-
-
<view class=‘headerListContainer headerListActive‘>
-
<view class=‘headerListView‘>1</view>
-
<text class=‘headerListText‘>创建账户</text>
-
-
-
<view class=‘headerListContainer‘>
-
<view class=‘headerListView‘>2</view>
-
<text class=‘headerListText‘>完善个人信息</text>
-
-
-
<view class=‘headerListContainer‘>
-
<view class=‘headerListView‘>3</view>
-
<text class=‘headerListText‘>注册成功</text>
-
-
<view class=‘transverseLineLeft‘></view>
-
<view class=‘transverseLineright‘></view>
-
-
<view class=‘mainContainer‘>
-
-
<view class=‘mainListContainer‘>
-
<view class=‘mainListText‘>用户名</view>
-
<input class=‘mainListInput‘ placeholder=‘请输入数字,字母或中文‘ maxlength=‘25‘ bindinput=‘userNameInput‘></input>
-
-
-
<view class=‘mainListContainer‘>
-
<view class=‘mainListText‘>密码</view>
-
<input class=‘mainListInput‘ placeholder=‘长度6~14位‘ password=‘true‘ maxlength=‘14‘ bindinput=‘userPasswordInput‘></input>
-
-
-
<view class=‘mainListContainer‘>
-
<view class=‘mainListText‘>确认密码</view>
-
<input class=‘mainListInput‘ placeholder=‘请再次输入密码‘ password=‘true‘ maxlength=‘14‘ bindinput=‘userPasswordAgainInput‘></input>
-
-
-
-
<view class=‘agreeContainer‘>
-
<checkbox class=‘agreeCheckbox‘ checked="{{check}}" bindtap="checkboxChange"/>
-
-
-
-
-
<view class=‘nextButton‘ bindtap=‘perfectInforTap‘>下一步,完善个人信息</view>
-
-
<view class=‘bindingContainer‘>
-
-
<text class=‘binding‘ bindtap=‘bindingTap‘>请绑定</text>
-
注册第二步WXML
-
-
<view class=‘backHome‘ bindtap=‘backHomeTap‘>
-
<image src=‘/images/homeIcon.png‘ class=‘backHomeImg‘></image>
-
-
-
<view class=‘headerContainer‘>
-
-
<view class=‘headerListContainer headerListOldActive‘>
-
<view class=‘headerListView‘>1</view>
-
<text class=‘headerListText‘>创建账户</text>
-
-
-
<view class=‘headerListContainer headerListActive‘>
-
<view class=‘headerListView‘>2</view>
-
<text class=‘headerListText‘>完善个人信息</text>
-
-
-
<view class=‘headerListContainer‘>
-
<view class=‘headerListView‘>3</view>
-
<text class=‘headerListText‘>注册成功</text>
-
-
<view class=‘transverseLineLeft‘></view>
-
<view class=‘transverseLineright‘></view>
-
-
-
<view class=‘mainContainer‘>
-
-
<view class=‘mainListContainer‘>
-
<view class=‘mainListText‘>手机</view>
-
<input class=‘mainListInput‘ placeholder=‘请输入手机号码‘ maxlength="11" bindinput=‘phoneInput‘></input>
-
-
-
<view class=‘mainListContainer‘>
-
<view class=‘mainListText‘>真实姓名</view>
-
<input class=‘mainListInput‘ placeholder=‘请输入真实姓名‘ maxlength=‘25‘ bindinput=‘nameInput‘></input>
-
-
-
<view class=‘mainListContainer‘>
-
<view class=‘mainListText‘>证件类型</view>
-
<view class=‘cardText‘>中华人民共和国居民身份证</view>
-
-
-
<view class=‘mainListContainer‘>
-
<view class=‘mainListText‘>证件号码</view>
-
<input class=‘mainListInput‘ type=‘idcard‘ placeholder=‘请输入身份证号码‘ maxlength="18" bindinput=‘cardInput‘></input>
-
-
-
<view class=‘mainListContainer‘>
-
<view class=‘mainListText‘>邮箱</view>
-
<input class=‘mainListInput‘ placeholder=‘请输入常用的邮箱地址‘ bindinput=‘emailInput‘></input>
-
-
-
-
<view class=‘nextButton‘ bindtap=‘registerSuccessTap‘>下一步,完成</view>