databaseGuide.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // pages/databaseGuide/databaseGuide.js
  2. const app = getApp()
  3. Page({
  4. data: {
  5. step: 1,
  6. counterId: '',
  7. openid: '',
  8. count: null,
  9. queryResult: '',
  10. },
  11. onLoad: function (options) {
  12. if (app.globalData.openid) {
  13. this.setData({
  14. openid: app.globalData.openid
  15. })
  16. }
  17. },
  18. onAdd: function () {
  19. // const db = wx.cloud.database()
  20. // db.collection('counters').add({
  21. // data: {
  22. // count: 1
  23. // },
  24. // success: res => {
  25. // // 在返回结果中会包含新创建的记录的 _id
  26. // this.setData({
  27. // counterId: res._id,
  28. // count: 1
  29. // })
  30. // wx.showToast({
  31. // title: '新增记录成功',
  32. // })
  33. // console.log('[数据库] [新增记录] 成功,记录 _id: ', res._id)
  34. // },
  35. // fail: err => {
  36. // wx.showToast({
  37. // icon: 'none',
  38. // title: '新增记录失败'
  39. // })
  40. // console.error('[数据库] [新增记录] 失败:', err)
  41. // }
  42. // })
  43. },
  44. onQuery: function() {
  45. // const db = wx.cloud.database()
  46. // // 查询当前用户所有的 counters
  47. // db.collection('counters').where({
  48. // _openid: this.data.openid
  49. // }).get({
  50. // success: res => {
  51. // this.setData({
  52. // queryResult: JSON.stringify(res.data, null, 2)
  53. // })
  54. // console.log('[数据库] [查询记录] 成功: ', res)
  55. // },
  56. // fail: err => {
  57. // wx.showToast({
  58. // icon: 'none',
  59. // title: '查询记录失败'
  60. // })
  61. // console.error('[数据库] [查询记录] 失败:', err)
  62. // }
  63. // })
  64. },
  65. onCounterInc: function() {
  66. // const db = wx.cloud.database()
  67. // const newCount = this.data.count + 1
  68. // db.collection('counters').doc(this.data.counterId).update({
  69. // data: {
  70. // count: newCount
  71. // },
  72. // success: res => {
  73. // this.setData({
  74. // count: newCount
  75. // })
  76. // },
  77. // fail: err => {
  78. // icon: 'none',
  79. // console.error('[数据库] [更新记录] 失败:', err)
  80. // }
  81. // })
  82. },
  83. onCounterDec: function() {
  84. // const db = wx.cloud.database()
  85. // const newCount = this.data.count - 1
  86. // db.collection('counters').doc(this.data.counterId).update({
  87. // data: {
  88. // count: newCount
  89. // },
  90. // success: res => {
  91. // this.setData({
  92. // count: newCount
  93. // })
  94. // },
  95. // fail: err => {
  96. // icon: 'none',
  97. // console.error('[数据库] [更新记录] 失败:', err)
  98. // }
  99. // })
  100. },
  101. onRemove: function() {
  102. // if (this.data.counterId) {
  103. // const db = wx.cloud.database()
  104. // db.collection('counters').doc(this.data.counterId).remove({
  105. // success: res => {
  106. // wx.showToast({
  107. // title: '删除成功',
  108. // })
  109. // this.setData({
  110. // counterId: '',
  111. // count: null,
  112. // })
  113. // },
  114. // fail: err => {
  115. // wx.showToast({
  116. // icon: 'none',
  117. // title: '删除失败',
  118. // })
  119. // console.error('[数据库] [删除记录] 失败:', err)
  120. // }
  121. // })
  122. // } else {
  123. // wx.showToast({
  124. // title: '无记录可删,请见创建一个记录',
  125. // })
  126. // }
  127. },
  128. nextStep: function () {
  129. // 在第一步,需检查是否有 openid,如无需获取
  130. if (this.data.step === 1 && !this.data.openid) {
  131. wx.cloud.callFunction({
  132. name: 'login',
  133. data: {},
  134. success: res => {
  135. app.globalData.openid = res.result.openid
  136. this.setData({
  137. step: 2,
  138. openid: res.result.openid
  139. })
  140. },
  141. fail: err => {
  142. wx.showToast({
  143. icon: 'none',
  144. title: '获取 openid 失败,请检查是否有部署 login 云函数',
  145. })
  146. console.log('[云函数] [login] 获取 openid 失败,请检查是否有部署云函数,错误信息:', err)
  147. }
  148. })
  149. } else {
  150. const callback = this.data.step !== 6 ? function() {} : function() {
  151. console.group('数据库文档')
  152. console.log('https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/database.html')
  153. console.groupEnd()
  154. }
  155. this.setData({
  156. step: this.data.step + 1
  157. }, callback)
  158. }
  159. },
  160. prevStep: function () {
  161. this.setData({
  162. step: this.data.step - 1
  163. })
  164. },
  165. goHome: function() {
  166. const pages = getCurrentPages()
  167. if (pages.length === 2) {
  168. wx.navigateBack()
  169. } else if (pages.length === 1) {
  170. wx.redirectTo({
  171. url: '../index/index',
  172. })
  173. } else {
  174. wx.reLaunch({
  175. url: '../index/index',
  176. })
  177. }
  178. }
  179. })