| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- const app = getApp()
- const util = require('../../utils/util.js')
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- messageId: '',
- activityInfo: {},
- like: false,
- likeDisabled: true,
- showEdit: false,
- pageToken: 0,
- activityComment: [],
- commentText: ''
- },
- getPublisherInfo: function () {
- wx.navigateTo({
- url: '/pages/publisher/publisher',
- }).then(res => {
- res.eventChannel.emit('loadCommonData', {
- data: this.data.activityInfo.publisher
- })
- })
- },
- editActivity: function () {
- wx.navigateTo({
- url: '/pages/activityPublish/activityPublish?id=' + this.data.activityInfo.pub_id +
- '&msg_id=' + this.data.messageId
- })
- },
- deleteActivity: function () {
- wx.showModal({
- content: "确认删除信息?",
- confirmColor: "#009195",
- success: function (res) {
- if (res.confirm) {
- wx.showLoading({
- title: '删除中'
- })
- wx.cloud.callFunction({
- name: 'deleteMessage',
- data: {
- msg_id: this.data.messageId
- }
- }).then(res => {
- wx.hideLoading()
- if (res.result.status !== 'OK') {
- wx.showToast({
- title: res.result.errMsg,
- icon: 'none'
- })
- return
- }
- wx.navigateBack()
- const eventChannel = this.getOpenerEventChannel()
- eventChannel.emit('deleteMessage')
- wx.showToast({
- title: "删除成功",
- icon: 'none'
- })
- })
- }
- }.bind(this)
- })
- },
- toggleLike: function () {
- if (this.data.likeDisabled) {
- return
- }
- wx.showLoading({
- title: this.data.like ? '取消收藏' : '收藏中'
- })
- wx.cloud.callFunction({
- name: this.data.like ? 'deleteFavorite' : 'createFavorite',
- data: {
- msg_id: this.data.messageId
- }
- }).then(res => {
- wx.hideLoading()
- if (res.result.status !== 'OK') {
- wx.showToast({
- title: res.result.errMsg,
- icon: 'none'
- })
- return
- }
- wx.showToast({
- title: this.data.like ? '取消收藏成功' : '收藏成功',
- icon: 'none'
- })
- this.setData({
- like: !this.data.like
- })
- })
- },
- 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.messageId,
- question: this.data.commentText
- }
- }).then(res => {
- wx.hideLoading()
- if (res.result.status !== 'OK') {
- wx.showToast({
- title: res.result.errMsg,
- icon: 'none'
- })
- return
- }
- wx.showToast({
- title: '发送成功,请等待发布者回复',
- icon: 'none'
- })
- this.setData({
- commentText: ''
- })
- })
- }
- },
- processCommonData: function (data) {
- let edit = false
- for (let i = 0; i < app.globalData.pubInfo.length; i++) {
- if (data.pub_id === app.globalData.pubInfo[i].pub_id) {
- edit = true
- }
- }
- this.setData({
- activityInfo: data,
- showEdit: edit
- })
- },
- loadExtraData: function () {
- wx.showNavigationBarLoading()
- const arr = []
- arr.push(wx.cloud.callFunction({
- name: 'listQuestions',
- data: {
- msg_id: this.data.messageId,
- page_token: this.data.pageToken,
- page_size: 20
- }
- }))
- arr.push(wx.cloud.callFunction({
- name: 'getFavorite',
- data: {
- msg_id: this.data.messageId
- }
- }))
- Promise.all(arr).then(res => {
- wx.hideNavigationBarLoading()
- if (res[0].result.status !== 'OK' || res[1].result.status !== 'OK') {
- wx.showToast({
- title: res[0].result.errMsg || res[1].result.errMsg,
- icon: 'none'
- })
- return
- }
- for (let i = 0; i < res[0].result.list.length; i++) {
- res[0].result.list[i].a_time = util.handleDate(res[0].result.list[i].a_time)
- }
- this.setData({
- activityComment: this.data.activityComment.concat(res[0].result.list),
- pageToken: res[0].result.next_page_token,
- like: res[1].result.total === 1,
- likeDisabled: false
- })
- })
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- if (options.id) {
- this.setData({
- messageId: options.id
- })
- wx.showLoading({
- title: '加载中'
- })
- wx.cloud.callFunction({
- name: 'getMessage',
- data: {
- msg_id: this.data.messageId
- }
- }).then(res => {
- wx.hideLoading()
- if (res.result.status !== 'OK') {
- wx.showToast({
- title: res.result.errMsg,
- icon: 'none'
- })
- return
- }
- res.result.data = util.dbToMsg(res.result.data)
- this.processCommonData(res.result.data)
- this.loadExtraData()
- })
- } else {
- const eventChannel = this.getOpenerEventChannel()
- eventChannel.on('loadCommonData', res => {
- this.setData({
- messageId: res.data._id,
- })
- this.processCommonData(res.data)
- this.loadExtraData()
- })
- }
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function () {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function () {
- }
- })
|