index.js 2.6 KB

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