const app = getApp() const util = require('../../utils/util.js') Page({ /** * 页面的初始数据 */ data: { activityInfo: {}, activityComment: [], like: -1, likeEnable: true, commentText: '' }, getPublisherInfo: function () { wx.navigateTo({ url: '/pages/publisher/publisher?id=' + this.data.activityInfo.pub_id }) }, editActivity: function () { wx.navigateTo({ url: '/pages/activityPublish/activityPublish?id=' + this.data.activityInfo.pub_id + '&msg_id=' + this.data.activityInfo._id }) }, 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: 'message', id: this.data.activityInfo._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: 'message', id: this.data.activityInfo._id }).remove({ success: function () { this.setData({ like: 0, likeEnable: true }) wx.showToast({ title: '已取消收藏', }) }.bind(this), fail: function () { wx.showToast({ title: '网络错误', icon: 'none' }) } }) } }, comment: function () { if (this.data.commentText.length < 5) { wx.showToast({ title: '提问字数至少为5', icon: 'none' }) } else { wx.showLoading({ title: '发送中' }) wx.cloud.callFunction({ name: 'createQuestion', data: { msg_id: this.data.activityInfo._id, question: this.data.commentText } }).then(res => { wx.hideLoading() wx.showToast({ title: '发送成功,请等待发布者回复', icon: 'none' }) this.setData({ commentText: '' }) }) // const db = wx.cloud.database() // db.collection('qaData').where({ // _openid: app.globalData.openId, // activityId: this.data.activityInfo._id // }).get({ // success: function (res) { // console.log(res) // if (res.data.length >= 10) { // wx.showToast({ // title: '为防止刷屏,每人每消息至多提问10条', // icon: 'none' // }) // } else { // db.collection('qaData').add({ // data: { // activityId: this.data.activityInfo._id, // publisherId: this.data.activityInfo.publisherId, // answer: '', // answerTime: '', // question: this.data.commentText, // questionTime: new Date(), // rank: '' // }, // success: function () { // this.setData({ // commentText: '' // }) // wx.showToast({ // title: '提问成功' // }) // }.bind(this) // }) // } // }.bind(this) // }) } }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { wx.showLoading({ title: '加载中' }) wx.cloud.callFunction({ name: 'getMessage', data: { msg_id: options.id } }).then(res => { wx.hideLoading() res.result.list[0] = util.dbToMsg(res.result.list[0]) this.setData({ activityInfo: res.result.list[0] }) }) // const db = wx.cloud.database() // const _ = db.command // db.collection('mainData').doc(options.id).get({ // success: function (res) { // res.data.time = util.handleDate(res.data.time) // this.setData({ // activityInfo: res.data // }) // wx.hideLoading() // }.bind(this) // }) // db.collection('qaData').where({ // activityId: options.id, // answer: _.neq('') // }).get({ // success: function (res) { // for (let i = 0; i < res.data.length; i++) { // if (res.data[i].answerTime !== '') { // res.data[i].time = util.handleDate(res.data[i].answerTime) // } else { // res.data[i].time = '' // } // } // this.setData({ // activityComment: res.data // }) // }.bind(this) // }) // db.collection('likeData').where({ // type: 'message', // 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 () { } })