index.js 2.3 KB

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