notification.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. const app = getApp()
  2. const util = require('../../utils/util.js')
  3. Page({
  4. data: {
  5. hasUserInfo: false,
  6. pageToken: 0,
  7. notificationDatas: [],
  8. loading: true
  9. },
  10. loadNotificationData: function (refresh) {
  11. this.setData({
  12. loading: true
  13. })
  14. wx.showNavigationBarLoading()
  15. wx.cloud.callFunction({
  16. name: 'listNotifications',
  17. data: {
  18. page_token: refresh ? 0 : this.data.pageToken,
  19. page_size: 20
  20. }
  21. }).then(res => {
  22. wx.hideNavigationBarLoading()
  23. wx.stopPullDownRefresh()
  24. if (res.result.status !== 'OK') {
  25. wx.showToast({
  26. title: res.result.errMsg,
  27. icon: 'none'
  28. })
  29. return
  30. }
  31. for (let i = 0; i < res.result.list.length; i++) {
  32. res.result.list[i].message = util.dbToMsg(res.result.list[i].message)
  33. }
  34. this.setData({
  35. notificationDatas: refresh ? res.result.list : this.data.notificationDatas.concat(res.result.list),
  36. pageToken: res.result.next_page_token,
  37. loading: false
  38. })
  39. })
  40. },
  41. onShow: function () {
  42. if (!this.data.hasUserInfo && app.globalData.hasUserInfo) {
  43. this.loadNotificationData(true)
  44. } else {
  45. this.setData({
  46. loading: false
  47. })
  48. }
  49. this.setData({
  50. hasUserInfo: app.globalData.hasUserInfo
  51. })
  52. },
  53. onPullDownRefresh: function () {
  54. if (this.data.loading) {
  55. return
  56. }
  57. this.setData({
  58. hasUserInfo: app.globalData.hasUserInfo
  59. })
  60. if (this.data.hasUserInfo) {
  61. this.loadNotificationData(true)
  62. }
  63. },
  64. onReachBottom: function () {
  65. if (this.data.loading) {
  66. return
  67. }
  68. this.setData({
  69. hasUserInfo: app.globalData.hasUserInfo
  70. })
  71. if (this.data.hasUserInfo) {
  72. this.loadNotificationData(false)
  73. }
  74. },
  75. })