const util = require('../../utils/util.js') Page({ /** * 页面的初始数据 */ data: { publisherInfo: [], mainDatas: [], like: -1, likeEnable: true }, detail: function () { wx.navigateTo({ url: '/pages/publisherDetail/publisherDetail', }) }, processData: function (start, arr) { for (let i = start; i < arr.length; i++) { arr[i].time = util.handleDate(arr[i].time) } this.setData({ mainDatas: arr }) wx.hideLoading() }, toggleLike: function () { if (!this.data.likeEnable) return this.setData({ likeEnable: false }) const db = wx.cloud.database() if (this.data.like == 0) { db.collection("likeData").add({ data: { type: "publisher", id: this.data.publisherInfo._id }, success: function () { this.setData({ like: 1, likeEnable: true }) wx.showToast({ title: "已关注", }) }.bind(this), fail: function () { wx.showToast({ title: "网络错误", icon: "none" }) } }) } else { db.collection("likeData").where({ type: "publisher", id: this.data.publisherInfo._id }).remove({ success: function () { this.setData({ like: 0, likeEnable: true }) wx.showToast({ title: "已取消关注", }) }.bind(this), fail: function () { wx.showToast({ title: "网络错误", icon: "none" }) } }) } }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { wx.showLoading({ title: "加载中" }) const db = wx.cloud.database() db.collection("publisherInfoData").doc(options.id).get({ success: function (res) { this.setData({ publisherInfo: res.data }) db.collection("mainData").where({ publisherName: this.data.publisherInfo.publisherName }).orderBy("time", "desc").limit(20).get({ success: function (res) { this.processData(0, res.data) }.bind(this) }) }.bind(this) }) db.collection("likeData").where({ type: "publisher", id: options.id }).get({ success: function (res) { this.setData({ like: res.data.length }) }.bind(this) }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })