index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // pages/message/index.js
  2. import { getProductHot } from '../../api/store.js';
  3. import { getMessages, readMessages } from "../../api/message"
  4. var base64 = require("../../utils/base64")
  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. unread: 0,
  21. messages: []
  22. },
  23. /**
  24. * 生命周期函数--监听页面加载
  25. */
  26. onLoad: function (options) {
  27. if (app.globalData.token) this.setData({ iShidden:true});
  28. },
  29. /**
  30. * 生命周期函数--监听页面初次渲染完成
  31. */
  32. onReady: function () {
  33. },
  34. onLoadFun: function() {
  35. this.pullMessageList();
  36. this.get_host_product();
  37. },
  38. /**
  39. * 生命周期函数--监听页面显示
  40. */
  41. onShow: function () {
  42. if (app.globalData.isLog) {
  43. this.data.page = 1;
  44. this.get_host_product();
  45. this.pullMessageList();
  46. this.setData({
  47. iShidden: true
  48. });
  49. }
  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. var unread = 0;
  74. res.data.forEach(function(row){
  75. row.ts = app.tsToString(row.ts)
  76. row.read = row.read == null ? false : true
  77. if (!row.read){
  78. unread += 1
  79. }
  80. })
  81. app.updateUnread(unread)
  82. that.setData({
  83. messages: res.data,
  84. unread: unread
  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. that.setData({
  94. unread: that.data.unread-1,
  95. })
  96. app.updateUnread(that.data.unread)
  97. })
  98. }
  99. wx.navigateTo({
  100. url: '/pages/message_details/index?data=' + encodeURIComponent(JSON.stringify(e.currentTarget.dataset.items)),
  101. });
  102. }
  103. })