123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- // 参考文档:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/database.html
- const app = getApp()
- Page({
- onShareAppMessage() {
- return {
- title: '基本操作',
- path: 'packageCloud/pages/database/crud/crud'
- }
- },
- data: {
- theme: 'light',
- openid: '',
- todoListFetched: false,
- todoList: [],
- searchContent: '',
- newContent: '',
- filtered: false,
- loading: false
- },
- onLoad() {
- this.setData({
- theme: wx.getSystemInfoSync().theme || 'light'
- })
- if (wx.onThemeChange) {
- wx.onThemeChange(({theme}) => {
- this.setData({theme})
- })
- }
- if (app.globalData.openid) {
- this.setData({
- openid: app.globalData.openid
- })
- this.queryTodoList()
- } else {
- wx.showLoading({
- title: '正在初始化...'
- })
- app.getUserOpenIdViaCloud()
- .then(openid => {
- this.setData({
- openid
- })
- wx.hideLoading()
- this.queryTodoList()
- return openid
- }).catch(err => {
- console.error(err)
- wx.hideLoading()
- wx.showToast({
- icon: 'none',
- title: '初始化失败,请检查网络'
- })
- })
- }
- },
- onShow() {
- if (this.data.openid) {
- this.queryTodoList()
- }
- },
- createTodo() {
- if (this.data.loading) {
- return
- }
- const {newContent} = this.data
- if (!newContent) {
- return
- }
- this.setData({loading: true})
- const db = wx.cloud.database()
- db.collection('todos').add({
- data: {
- description: newContent,
- done: false,
- },
- success: res => {
- // 在返回结果中会包含新创建的记录的 _id
- this.setData({
- todoList: [
- ...this.data.todoList,
- {
- _id: res._id,
- _openid: this.data.openid,
- description: newContent,
- done: false,
- }
- ],
- newContent: ''
- })
- wx.showToast({
- title: '新增记录成功',
- })
- console.log('[数据库] [新增记录] 成功,记录 _id: ', res._id)
- },
- fail: err => {
- wx.showToast({
- icon: 'none',
- title: '新增记录失败'
- })
- console.error('[数据库] [新增记录] 失败:', err)
- },
- complete: () => {
- this.setData({loading: false})
- }
- })
- },
- queryTodoList() {
- wx.showLoading({
- title: '正在查询...'
- })
- const db = wx.cloud.database()
- db.collection('todos').where({
- _openid: this.data.openid
- }).get({
- success: res => {
- this.setData({
- todoListFetched: true,
- todoList: res.data,
- filtered: false
- })
- console.log('[数据库] [查询记录] 成功: ', res)
- },
- fail: err => {
- wx.showToast({
- icon: 'none',
- title: '查询记录失败'
- })
- console.error('[数据库] [查询记录] 失败:', err)
- },
- complete: () => {
- wx.hideLoading()
- }
- })
- },
- searchTodo() {
- const {searchContent} = this.data
- if (!searchContent) {
- this.queryTodoList()
- return
- }
- const db = wx.cloud.database()
- let descriptionCondition = searchContent
- const execResult = /^\/([\s\S]*)\//.exec(searchContent)
- if (execResult) {
- const reStr = execResult[1].trim().replace(/\s+/g, '|')
- descriptionCondition = db.RegExp({
- regexp: reStr
- })
- }
- wx.showLoading({
- title: '正在查询...'
- })
- db.collection('todos').where({
- _openid: this.data.openid,
- description: descriptionCondition
- }).get({
- success: res => {
- this.setData({
- todoList: res.data,
- filtered: true
- })
- console.log('[数据库] [查询记录] 成功: ', res)
- },
- fail: err => {
- wx.showToast({
- icon: 'none',
- title: '查询记录失败'
- })
- console.error('[数据库] [查询记录] 失败:', err)
- },
- complete: () => {
- wx.hideLoading()
- }
- })
- },
- toggleComplete(e) {
- if (this.data.loading) {
- return
- }
- const {id: todoId, index} = e.currentTarget.dataset
- const todo = this.data.todoList[index]
- this.setData({loading: true})
- const db = wx.cloud.database()
- db.collection('todos').doc(todoId).update({
- data: {done: !todo.done},
- success: () => {
- this.setData({
- [`todoList[${index}].done`]: !todo.done
- })
- },
- fail: err => {
- wx.showToast({
- icon: 'none',
- title: '更新失败',
- })
- console.error('[数据库] [更新记录] 失败:', err)
- },
- complete: () => {
- this.setData({loading: false})
- }
- })
- },
- toDetail(e) {
- const {id: todoId} = e.currentTarget.dataset
- wx.navigateTo({
- url: `/page/cloud/pages/crud-detail/crud-detail?todoId=${todoId}`,
- })
- },
- onInputSearchContent(e) {
- this.setData({
- searchContent: e.detail.value
- })
- },
- onInputNewContent(e) {
- this.setData({
- newContent: e.detail.value
- })
- }
- })
|