开发者可以使用云开发开发微信小程序、小游戏,无需搭建服务器,即可使用云端能力。

云开发为开发者提供完整的云端支持,弱化后端和运维概念,无需搭建服务器,使用平台提供的 API 进行核心业务开发,即可实现快速上线和迭代,同时这一能力,同开发者已经使用的云服务相互兼容,并不互斥。

目前提供三大基础能力支持:

1、云函数:在云端运行的代码,微信私有协议天然鉴权,开发者只需编写自身业务逻辑代码

2、数据库:一个既可在小程序前端操作,也能在云函数中读写的 JSON 数据库

3、存储:在小程序前端直接上传/下载云端文件,在云开发控制台可视化管理

具体的可以去小程序文档上查看,下面用一个登录注册的案例来演示小程序云开发数据库的运用

注册

在创建的时候,要在点下一步的时候,调数据库来看用户名有没有重复的。在点击同意的时候来调用数据库,然后把所有的判断放到下一步来判断。所有条件都满足就将用户名和密码放到全局变量中。

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});// 初始化数据库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}}}
})

在完善信息的时候获取所有的变量(用户名和密码也在内),然后在点击下一步完成按钮将数据上传到数据库。

​
var app = getApp();
Page({data: {userName: '',userPassword: '',phone: '',realName: '',card: '',email: '',},// 返回主界面backHomeTap: function() {wx.switchTab({url: '../../index/index',})},// 手机号phoneInput: function(e) {this.setData({phone: e.detail.value});},// 真实姓名nameInput: function(e) {this.setData({realName: e.detail.value});},// 身份证cardInput: function(e) {this.setData({card: e.detail.value})},// emailemailInput: function(e) {this.setData({email: e.detail.value})},// 下一步完成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+)+)$/;var that = this;if (phone === '') {wx.showToast({title: '请输入手机号',icon: 'none',duration: 2000,mask: true});} else if (!phonereg.test(phone)) {wx.showToast({title: '请输入正确的手机号',icon: 'none',duration: 2000,mask: true})} else if (!namereg.test(realName)) {wx.showToast({title: '请输入正确的名字',icon: 'none',duration: 2000,mask: true})} else if (card === '') {wx.showToast({title: '请输入身份证',icon: 'none',duration: 2000,mask: true})} else if (!cardreg.test(card)) {wx.showToast({title: '请输入正确的身份证',icon: 'none',duration: 2000,mask: true})} else if (email === '') {wx.showToast({title: '请输入邮箱',icon: 'none',duration: 2000,mask: true})} else if (!emailreg.test(email)) {wx.showToast({title: '请输入正确的邮箱',icon: 'none',duration: 2000,mask: true})} else {// 初始化云wx.cloud.init({env: 'wubaib-9543f7',traceUser: true});// 初始化数据库const db = wx.cloud.database();db.collection('userInformation').add({// data 字段表示需新增的 JSON 数据data: {realName: realName,userName: userName,userPassword: userPassword,phone: phone,email: email,card: card},success: function(res) {// res 是一个对象,其中有 _id 字段标记刚创建的记录的 idconsole.log(res);console.log(res.errMsg);}})}},/*** 生命周期函数--监听页面显示*/onShow: function() {this.setData({userName: app.appData.account.userName,userPassword: app.appData.account.userPassword})},
})​

登录

在登录页面,先获取用户输入的用户名和密码。在点击登录的时候,先根据userName调数据库的密码和用户输入的密码是否相等。如果相等将用户的信息保存到全局变量中。

​
var app = getApp();
Page({data: {bindName: '',bindPassword: '',isChecked: false,userName: '',phone: '',realName: '',card: '',email: '',userId: ''},// 点击注册账号registerTap: function() {wx.redirectTo({url: '../register/register'})},// 获取用户名bindNameInput: function(e) {this.setData({bindName: e.detail.value})var that = this;if (that.data.bindName.length !== 0 && that.data.bindPassword.length !== 0) {this.setData({isChecked: true})} else if (that.data.bindName.length === 0) {this.setData({isChecked: false})}},// 获取密码bindPasswordInput: function(e) {this.setData({bindPassword: e.detail.value})var that = this;if (that.data.bindName.length !== 0 && that.data.bindPassword.length !== 0) {this.setData({isChecked: true})} else if (that.data.bindPassword.length === 0) {this.setData({isChecked: false})}},// 点击登录bindingSuccess: function() {var that = this;var bindName = that.data.bindName;var bindPassword = that.data.bindPassword;if (bindName.length !== 0 && bindPassword.length !== 0) {// 初始化云wx.cloud.init({env: 'wubaib-9543f7',traceUser: true});// 初始化数据库const db = wx.cloud.database();db.collection('userInformation').where({userName: bindName}).get().then(res => {console.log(res.data);if (res.data[0].userPassword === bindPassword) {console.log("登录成功");// 保存手机号,真实姓名,身份证号,邮箱 保存用户名that.setData({userName: res.data[0].userName,phone: res.data[0].phone,realName: res.data[0].realName,card: res.data[0].card,email: res.data[0].email,userId: res.data[0]._id})app.appData.userinfo = {phone: that.data.phone,realName: that.data.realName,card: that.data.card,email: that.data.email}app.appData.account = {userName: that.data.userName}app.appData.userId = {userId: that.data.userId}wx.switchTab({url: '../personalCenter/personalCenter',})} else {wx.showToast({title: '用户名或密码错误',icon: 'none',duration: 2000})}})}},
})​

登录WXML

<view class='phoneNumberContainer'><input placeholder='用户名' maxlength='11' bindinput="bindNameInput"></input>
</view>
<view class='passwordContainer'><input placeholder='密码' password="true" bindinput="bindPasswordInput"></input>
</view>
<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>
<!--头部  -->
<view class='headerContainer'><!--创建账户  --><view class='headerListContainer headerListActive'><view class='headerListView'>1</view><text class='headerListText'>创建账户</text></view><!--完善个人信息  --><view class='headerListContainer'><view class='headerListView'>2</view><text class='headerListText'>完善个人信息</text></view><!--注册成功  --><view class='headerListContainer'><view class='headerListView'>3</view><text class='headerListText'>注册成功</text></view><view class='transverseLineLeft'></view><view class='transverseLineright'></view>
</view>
<view class='mainContainer'><!--用户名  --><view class='mainListContainer'><view class='mainListText'>用户名</view><input class='mainListInput' placeholder='请输入数字,字母或中文' maxlength='25' bindinput='userNameInput'></input></view><!--密码  --><view class='mainListContainer'><view class='mainListText'>密码</view><input class='mainListInput' placeholder='长度6~14位' password='true' maxlength='14' bindinput='userPasswordInput'></input></view><!--确认密码  --><view class='mainListContainer'><view class='mainListText'>确认密码</view><input class='mainListInput' placeholder='请再次输入密码' password='true' maxlength='14' bindinput='userPasswordAgainInput'></input></view>
</view>
<!--agree  -->
<view class='agreeContainer'><checkbox class='agreeCheckbox' checked="{{check}}" bindtap="checkboxChange"/><text>我已阅读并接受</text><text class='clause'>《用户注册条款》</text>
</view>
<!--nextButton  -->
<view class='nextButton' bindtap='perfectInforTap'>下一步,完善个人信息</view>
<!--binding  -->
<view class='bindingContainer'><text>已有账号</text><text class='binding' bindtap='bindingTap'>请绑定</text>
</view>

注册第二步WXML

<!--返回主页  -->
<view class='backHome' bindtap='backHomeTap'><image src='/images/homeIcon.png' class='backHomeImg'></image>
</view>
<!--头部  -->
<view class='headerContainer'><!--创建账户  --><view class='headerListContainer headerListOldActive'><view class='headerListView'>1</view><text class='headerListText'>创建账户</text></view><!--完善个人信息  --><view class='headerListContainer headerListActive'><view class='headerListView'>2</view><text class='headerListText'>完善个人信息</text></view><!--注册成功  --><view class='headerListContainer'><view class='headerListView'>3</view><text class='headerListText'>注册成功</text></view><view class='transverseLineLeft'></view><view class='transverseLineright'></view>
</view>
<!--main  -->
<view class='mainContainer'><!--手机  --><view class='mainListContainer'><view class='mainListText'>手机</view><input class='mainListInput' placeholder='请输入手机号码' maxlength="11" bindinput='phoneInput'></input></view><!--真实姓名  --><view class='mainListContainer'><view class='mainListText'>真实姓名</view><input class='mainListInput' placeholder='请输入真实姓名' maxlength='25' bindinput='nameInput'></input></view><!--证件类型  --><view class='mainListContainer'><view class='mainListText'>证件类型</view><view class='cardText'>中华人民共和国居民身份证</view></view><!--证件号码  --><view class='mainListContainer'><view class='mainListText'>证件号码</view><input class='mainListInput' type='idcard' placeholder='请输入身份证号码' maxlength="18" bindinput='cardInput'></input></view><!--邮箱  --><view class='mainListContainer'><view class='mainListText'>邮箱</view><input class='mainListInput' placeholder='请输入常用的邮箱地址' bindinput='emailInput'></input></view>
</view>
<!--nextButton  -->
<view class='nextButton' bindtap='registerSuccessTap'>下一步,完成</view>

好多人问我要源码,我就不一个一个发了。要源码的自己git下载吧

下载地址:https://github.com/wubaibin/wx_yunkaifa.git

微信小程序云开发(数据库)相关推荐

  1. 微信小程序+云开发+数据库使用

    微信小程序+云开发+数据库使用 参考地址:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/database/read.ht ...

  2. uniapp实现微信小程序云开发数据库访问,并解决云开发数据库获取不到数据问题

    uniapp实现微信小程序云开发数据库访问,并解决云开发数据库获取不到数据问题 使用工具是HBuilder X 1.配置好AppID(小程序ID) 在HBuilder X工具的manifest.js文 ...

  3. 【微信小程序】如何获取微信小程序云开发数据库的数据并渲染到页面?

    前言 上一篇博客我把微信小程序云开发数据库操作(增删改查)的实现方法都已经分享出来啦,可以戳链接进去阅读哦 [微信小程序]小程序云开发实现数据库增删改查(小白速度Get起来!!一步步教你如何实现) 基 ...

  4. 关于微信小程序云开发数据库中有数据查询不到的问题

    最近在学习过程中遇到一个微信小程序云开发数据库中有数据查询不到的问题 集合查询代码如下: Page({/* 采用了ES6的写法 */onLoad() {wx.cloud.database().coll ...

  5. 微信小程序云开发数据库操作

    1.在app.js中初始化云环境 // app.js App({onLaunch() {//初始化云服务if (!wx.cloud) {console.error('请使用 2.2.3 或以上的基础库 ...

  6. mysql导入微信小程序云开发_微信小程序-云开发数据库上传json文件

    小程序新增了云开发功能,对于个人开发者是个利好消息.可以省去购买服务器,购买域名以及繁琐配置等步骤,减轻了开发者的负担.至于如何云开发我就不在这里赘述了,请移步微信小程序云开发官方文档,说的很清楚.这 ...

  7. mysql批量导入json_微信小程序云开发---数据库批量导入json文件

    马上大学毕业了,于是最近做了一个关于班级信息的微信小程序,主要记录一些班级活动的照片.同学的通讯录...... 主要使用了微信小程序的云开发平台,因为班级同学的信息,班长都会有Excel表格统计的信息 ...

  8. 微信小程序云开发---数据库批量导入json文件

    马上大学毕业了,于是最近做了一个关于班级信息的微信小程序,主要记录一些班级活动的照片.同学的通讯录...... 主要使用了微信小程序的云开发平台,因为班级同学的信息,班长都会有Excel表格统计的信息 ...

  9. 微信小程序云开发—数据库增删改查

    首先新建小程序项目,后端服务选择"小程序云开发",新建项目成功后,开通云开发,在app.js中添加 wx.cloud.init({traceUser: true,}) 如下图所示, ...

  10. 微信小程序--云开发数据库操作之where()

    where() 定义:指定查询条件,返回带新查询条件的新的集合引用 我的理解:where(),其中括号中指的是查询条件,最终返回符合该条件的数据记录 官方示例用法 const _ = db.comma ...

最新文章

  1. Oracle统计信息不准(谓词越界)造成的性能问题
  2. 和华为hr电话面试的反思
  3. PHP的chunk_split() 函数把字符串分割为一连串更小的部分
  4. 模板方法模式coding
  5. 计算机广告制作未来发展还行吗,计算机多媒体设计专业和广告设计制作那个好...
  6. rabbitmq 延迟队列_Delayed Message 插件实现 RabbitMQ 延迟队列
  7. 11个让你吃惊的 Linux 终端命令
  8. 17位业内专家解析2018年物联网重要趋势
  9. Java数组基础笔记(引用、字符串数组)
  10. 终端天线—9.4G手机调试
  11. 《人月神话》:人月神话
  12. Linux:使用libgen.h:basename,dirname
  13. 使用linux,导出mysql数据库信息,连接聚石塔
  14. 用什么擦地最干净脑筋急转弯_你没想过的“脑筋急转弯”,才是启发孩子智力的法宝(附资源下载)...
  15. 激活windows系列地址
  16. 使用地点云实现企业官网中的门店地图
  17. 解决火狐浏览器提示连接不安全或证书错误的问题
  18. 百度人脸识别SDK的坑
  19. Mac快捷键及手势基本操作
  20. 淘宝/天猫如何获得店铺的所有商品?

热门文章

  1. 电脑上微信公众平台进不去
  2. 荔枝FM、喜马拉雅FM、蜻蜓FM竞品分析
  3. lm80认证_什么是LM-80测试什么产品需要做LM-80测试
  4. Git版本控制工具和Github代码托管平台
  5. python区间分布统计_常见概率统计分布及Python实现
  6. 组件绑定事件与源生事件
  7. 使用Yara规则静态扫描方法
  8. iOS支付指南:POS终端刷卡流程、银联55域TLV子域信息说明、银联前置、NFC
  9. 一个毕业6年的程序员工作经历和成长感悟(终)
  10. protobuf根据有关联的.proto文件进行编译