123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- // pages/databaseGuide/databaseGuide.js
- const app = getApp()
- Page({
- data: {
- step: 1,
- counterId: '',
- openid: '',
- count: null,
- queryResult: '',
- },
- onLoad: function (options) {
- if (app.globalData.openid) {
- this.setData({
- openid: app.globalData.openid
- })
- }
- },
- onAdd: function () {
- const db = wx.cloud.database()
- db.collection('counters').add({
- data: {
- count: 1
- },
- success: res => {
- // 在返回结果中会包含新创建的记录的 _id
- this.setData({
- counterId: res._id,
- count: 1
- })
- wx.showToast({
- title: '新增记录成功',
- })
- console.log('[数据库] [新增记录] 成功,记录 _id: ', res._id)
- },
- fail: err => {
- wx.showToast({
- icon: 'none',
- title: '新增记录失败'
- })
- console.error('[数据库] [新增记录] 失败:', err)
- }
- })
- },
- onQuery: function() {
- // const db = wx.cloud.database()
- // // 查询当前用户所有的 counters
- // db.collection('counters').where({
- // _openid: this.data.openid
- // }).get({
- // success: res => {
- // this.setData({
- // queryResult: JSON.stringify(res.data, null, 2)
- // })
- // console.log('[数据库] [查询记录] 成功: ', res)
- // },
- // fail: err => {
- // wx.showToast({
- // icon: 'none',
- // title: '查询记录失败'
- // })
- // console.error('[数据库] [查询记录] 失败:', err)
- // }
- // })
- },
- onCounterInc: function() {
- // const db = wx.cloud.database()
- // const newCount = this.data.count + 1
- // db.collection('counters').doc(this.data.counterId).update({
- // data: {
- // count: newCount
- // },
- // success: res => {
- // this.setData({
- // count: newCount
- // })
- // },
- // fail: err => {
- // icon: 'none',
- // console.error('[数据库] [更新记录] 失败:', err)
- // }
- // })
- },
- onCounterDec: function() {
- // const db = wx.cloud.database()
- // const newCount = this.data.count - 1
- // db.collection('counters').doc(this.data.counterId).update({
- // data: {
- // count: newCount
- // },
- // success: res => {
- // this.setData({
- // count: newCount
- // })
- // },
- // fail: err => {
- // icon: 'none',
- // console.error('[数据库] [更新记录] 失败:', err)
- // }
- // })
- },
- onRemove: function() {
- // if (this.data.counterId) {
- // const db = wx.cloud.database()
- // db.collection('counters').doc(this.data.counterId).remove({
- // success: res => {
- // wx.showToast({
- // title: '删除成功',
- // })
- // this.setData({
- // counterId: '',
- // count: null,
- // })
- // },
- // fail: err => {
- // wx.showToast({
- // icon: 'none',
- // title: '删除失败',
- // })
- // console.error('[数据库] [删除记录] 失败:', err)
- // }
- // })
- // } else {
- // wx.showToast({
- // title: '无记录可删,请见创建一个记录',
- // })
- // }
- },
- nextStep: function () {
- // 在第一步,需检查是否有 openid,如无需获取
- if (this.data.step === 1 && !this.data.openid) {
- wx.cloud.callFunction({
- name: 'login',
- data: {},
- success: res => {
- app.globalData.openid = res.result.openid
- this.setData({
- step: 2,
- openid: res.result.openid
- })
- },
- fail: err => {
- wx.showToast({
- icon: 'none',
- title: '获取 openid 失败,请检查是否有部署 login 云函数',
- })
- console.log('[云函数] [login] 获取 openid 失败,请检查是否有部署云函数,错误信息:', err)
- }
- })
- } else {
- const callback = this.data.step !== 6 ? function() {} : function() {
- console.group('数据库文档')
- console.log('https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/database.html')
- console.groupEnd()
- }
- this.setData({
- step: this.data.step + 1
- }, callback)
- }
- },
- prevStep: function () {
- this.setData({
- step: this.data.step - 1
- })
- },
- goHome: function() {
- const pages = getCurrentPages()
- if (pages.length === 2) {
- wx.navigateBack()
- } else if (pages.length === 1) {
- wx.redirectTo({
- url: '../index/index',
- })
- } else {
- wx.reLaunch({
- url: '../index/index',
- })
- }
- }
- })
|