crud.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // 参考文档:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/database.html
  2. const app = getApp()
  3. Page({
  4. onShareAppMessage() {
  5. return {
  6. title: '基本操作',
  7. path: 'page/cloud/pages/crud/crud'
  8. }
  9. },
  10. data: {
  11. openid: '',
  12. todoListFetched: false,
  13. todoList: [],
  14. searchContent: '',
  15. newContent: '',
  16. filtered: false,
  17. loading: false
  18. },
  19. onLoad() {
  20. if (app.globalData.openid) {
  21. this.setData({
  22. openid: app.globalData.openid
  23. })
  24. this.queryTodoList()
  25. } else {
  26. wx.showLoading({
  27. title: '正在初始化...'
  28. })
  29. app.getUserOpenIdViaCloud()
  30. .then(openid => {
  31. this.setData({
  32. openid
  33. })
  34. wx.hideLoading()
  35. this.queryTodoList()
  36. return openid
  37. }).catch(err => {
  38. console.error(err)
  39. wx.hideLoading()
  40. wx.showToast({
  41. icon: 'none',
  42. title: '初始化失败,请检查网络'
  43. })
  44. })
  45. }
  46. },
  47. onShow() {
  48. if (this.data.openid) {
  49. this.queryTodoList()
  50. }
  51. },
  52. createTodo() {
  53. if (this.data.loading) {
  54. return
  55. }
  56. const {newContent} = this.data
  57. if (!newContent) {
  58. return
  59. }
  60. this.setData({loading: true})
  61. const db = wx.cloud.database()
  62. db.collection('todos').add({
  63. data: {
  64. description: newContent,
  65. done: false,
  66. },
  67. success: res => {
  68. // 在返回结果中会包含新创建的记录的 _id
  69. this.setData({
  70. todoList: [
  71. ...this.data.todoList,
  72. {
  73. _id: res._id,
  74. _openid: this.data.openid,
  75. description: newContent,
  76. done: false,
  77. }
  78. ],
  79. newContent: ''
  80. })
  81. wx.showToast({
  82. title: '新增记录成功',
  83. })
  84. console.log('[数据库] [新增记录] 成功,记录 _id: ', res._id)
  85. },
  86. fail: err => {
  87. wx.showToast({
  88. icon: 'none',
  89. title: '新增记录失败'
  90. })
  91. console.error('[数据库] [新增记录] 失败:', err)
  92. },
  93. complete: () => {
  94. this.setData({loading: false})
  95. }
  96. })
  97. },
  98. queryTodoList() {
  99. wx.showLoading({
  100. title: '正在查询...'
  101. })
  102. const db = wx.cloud.database()
  103. db.collection('todos').where({
  104. _openid: this.data.openid
  105. }).get({
  106. success: res => {
  107. this.setData({
  108. todoListFetched: true,
  109. todoList: res.data,
  110. filtered: false
  111. })
  112. console.log('[数据库] [查询记录] 成功: ', res)
  113. },
  114. fail: err => {
  115. wx.showToast({
  116. icon: 'none',
  117. title: '查询记录失败'
  118. })
  119. console.error('[数据库] [查询记录] 失败:', err)
  120. },
  121. complete: () => {
  122. wx.hideLoading()
  123. }
  124. })
  125. },
  126. searchTodo() {
  127. const {searchContent} = this.data
  128. if (!searchContent) {
  129. this.queryTodoList()
  130. return
  131. }
  132. const db = wx.cloud.database()
  133. let descriptionCondition = searchContent
  134. const execResult = /^\/([\s\S]*)\//.exec(searchContent)
  135. if (execResult) {
  136. const reStr = execResult[1].trim().replace(/\s+/g, '|')
  137. descriptionCondition = db.RegExp({
  138. regexp: reStr
  139. })
  140. }
  141. wx.showLoading({
  142. title: '正在查询...'
  143. })
  144. db.collection('todos').where({
  145. _openid: this.data.openid,
  146. description: descriptionCondition
  147. }).get({
  148. success: res => {
  149. this.setData({
  150. todoList: res.data,
  151. filtered: true
  152. })
  153. console.log('[数据库] [查询记录] 成功: ', res)
  154. },
  155. fail: err => {
  156. wx.showToast({
  157. icon: 'none',
  158. title: '查询记录失败'
  159. })
  160. console.error('[数据库] [查询记录] 失败:', err)
  161. },
  162. complete: () => {
  163. wx.hideLoading()
  164. }
  165. })
  166. },
  167. toggleComplete(e) {
  168. if (this.data.loading) {
  169. return
  170. }
  171. const {id: todoId, index} = e.currentTarget.dataset
  172. const todo = this.data.todoList[index]
  173. this.setData({loading: true})
  174. const db = wx.cloud.database()
  175. db.collection('todos').doc(todoId).update({
  176. data: {done: !todo.done},
  177. success: () => {
  178. this.setData({
  179. [`todoList[${index}].done`]: !todo.done
  180. })
  181. },
  182. fail: err => {
  183. wx.showToast({
  184. icon: 'none',
  185. title: '更新失败',
  186. })
  187. console.error('[数据库] [更新记录] 失败:', err)
  188. },
  189. complete: () => {
  190. this.setData({loading: false})
  191. }
  192. })
  193. },
  194. toDetail(e) {
  195. const {id: todoId} = e.currentTarget.dataset
  196. wx.navigateTo({
  197. url: `/page/cloud/pages/crud-detail/crud-detail?todoId=${todoId}`,
  198. })
  199. },
  200. onInputSearchContent(e) {
  201. this.setData({
  202. searchContent: e.detail.value
  203. })
  204. },
  205. onInputNewContent(e) {
  206. this.setData({
  207. newContent: e.detail.value
  208. })
  209. }
  210. })