index.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // pages/message/index.js
  2. import { getProductHot } from '../../api/store.js'
  3. import { getMessages, readMessages } from '../../api/message'
  4. import { tsToString } from '../../utils/util'
  5. const app = getApp()
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. parameter: {
  12. title: '消息',
  13. navbar: '1',
  14. return: '1',
  15. color: false
  16. },
  17. host_product: [],
  18. isHidden: false,
  19. page: 0,
  20. messages: []
  21. },
  22. /**
  23. * 生命周期函数--监听页面加载
  24. */
  25. onLoad: function (options) {
  26. if (app.globalData.token) {
  27. this.setData({
  28. isHidden: true
  29. })
  30. }
  31. },
  32. /**
  33. * 生命周期函数--监听页面初次渲染完成
  34. */
  35. onReady: function () {},
  36. onLoadFun: function () {
  37. this.pullMessageList()
  38. },
  39. /**
  40. * 生命周期函数--监听页面显示
  41. */
  42. onShow: function () {
  43. if (app.globalData.isLog) {
  44. this.data.page = 1
  45. this.pullMessageList()
  46. this.setData({
  47. isHidden: true
  48. })
  49. }
  50. app.setUnread(app.globalData.unread) // 从详情页面返回,更新已读
  51. },
  52. /**
  53. * 获取我的推荐
  54. */
  55. get_host_product: function () {
  56. var that = this
  57. getProductHot().then(res => {
  58. that.setData({
  59. host_product: res.data
  60. })
  61. })
  62. },
  63. /**
  64. * 生命周期函数--监听页面隐藏
  65. */
  66. onHide: function () {},
  67. /**
  68. * 生命周期函数--监听页面卸载
  69. */
  70. onUnload: function () {},
  71. pullMessageList() {
  72. var that = this
  73. getMessages(that.data.page).then(
  74. res => {
  75. res.data.forEach(function (row) {
  76. row.ts = tsToString(row.ts)
  77. row.read = row.read == null ? false : true
  78. })
  79. that.setData({
  80. messages: res.data
  81. })
  82. if (!this.message || this.messages.length <= 0) {
  83. this.get_host_product()
  84. }
  85. },
  86. err => {}
  87. )
  88. },
  89. onTapMessageItem(e) {
  90. if (!e.currentTarget.dataset.items.read) {
  91. var that = this
  92. readMessages([e.currentTarget.dataset.items.id]).then(res => {
  93. app.globalData.unread -= 1
  94. })
  95. }
  96. wx.navigateTo({
  97. url: '/pages/message_details/index?data=' + encodeURIComponent(JSON.stringify(e.currentTarget.dataset.items))
  98. })
  99. }
  100. })