index.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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({ iShidden:true});
  28. }
  29. },
  30. /**
  31. * 生命周期函数--监听页面初次渲染完成
  32. */
  33. onReady: function () {
  34. },
  35. onLoadFun: function() {
  36. this.pullMessageList();
  37. },
  38. /**
  39. * 生命周期函数--监听页面显示
  40. */
  41. onShow: function () {
  42. if (app.globalData.isLog) {
  43. this.data.page = 1;
  44. this.pullMessageList();
  45. this.setData({
  46. iShidden: true
  47. });
  48. }
  49. app.setUnread(app.globalData.unread); // 从详情页面返回,更新已读
  50. },
  51. /**
  52. * 获取我的推荐
  53. */
  54. get_host_product:function(){
  55. var that=this;
  56. getProductHot().then(res=>{
  57. that.setData({ host_product: res.data });
  58. })
  59. },
  60. /**
  61. * 生命周期函数--监听页面隐藏
  62. */
  63. onHide: function () {
  64. },
  65. /**
  66. * 生命周期函数--监听页面卸载
  67. */
  68. onUnload: function () {
  69. },
  70. pullMessageList() {
  71. var that = this;
  72. getMessages(that.data.page).then(res=>{
  73. res.data.forEach(function(row){
  74. row.ts = tsToString(row.ts)
  75. row.read = row.read == null ? false : true
  76. })
  77. that.setData({
  78. messages: res.data,
  79. });
  80. if (!this.message || this.messages.length <= 0) {
  81. this.get_host_product();
  82. }
  83. }, err=> {
  84. });
  85. },
  86. onTapMessageItem(e) {
  87. if (!e.currentTarget.dataset.items.read) {
  88. var that = this
  89. readMessages([e.currentTarget.dataset.items.id]).then(res=>{
  90. app.globalData.unread -= 1;
  91. })
  92. }
  93. wx.navigateTo({
  94. url: '/pages/message_details/index?data=' + encodeURIComponent(JSON.stringify(e.currentTarget.dataset.items)),
  95. });
  96. }
  97. })