// pages/message/index.js import { getProductHot } from '../../api/store.js'; import { getMessages, readMessages } from "../../api/message" import { tsToString } from "../../utils/util" const app = getApp(); Page({ /** * 页面的初始数据 */ data: { parameter: { 'title': "消息", 'navbar': '1', 'return': '1', 'color': false, }, host_product: [], iShidden: false, page: 0, messages: [] }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { if (app.globalData.token) { this.setData({ iShidden: true }); } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () {}, onLoadFun: function () { this.pullMessageList(); }, /** * 生命周期函数--监听页面显示 */ onShow: function () { if (app.globalData.isLog) { this.data.page = 1; this.pullMessageList(); this.setData({ iShidden: true }); } app.setUnread(app.globalData.unread); // 从详情页面返回,更新已读 }, /** * 获取我的推荐 */ get_host_product: function () { var that = this; getProductHot().then(res => { that.setData({ host_product: res.data }); }) }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () {}, /** * 生命周期函数--监听页面卸载 */ onUnload: function () {}, pullMessageList() { var that = this; getMessages(that.data.page).then(res => { res.data.forEach(function (row) { row.ts = tsToString(row.ts) row.read = row.read == null ? false : true }) that.setData({ messages: res.data, }); if (!this.message || this.messages.length <= 0) { this.get_host_product(); } }, err => {}); }, onTapMessageItem(e) { if (!e.currentTarget.dataset.items.read) { var that = this readMessages([e.currentTarget.dataset.items.id]).then(res => { app.globalData.unread -= 1; }) } wx.navigateTo({ url: '/pages/message_details/index?data=' + encodeURIComponent(JSON.stringify(e.currentTarget.dataset.items)), }); } })