crud.js 5.2 KB

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