| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- const util = require('../../utils/util.js')
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- publisherId: '',
- publisherInfo: {},
- pageToken: 0,
- mainDatas: [],
- like: false,
- likeEnable: true
- },
- detail: function () {
- wx.navigateTo({
- url: '/pages/publisherDetail/publisherDetail',
- }).then(res => {
- res.eventChannel.emit('loadCommonData', {
- data: this.data.publisherInfo
- })
- })
- },
- loadExtraData: function () {
- wx.showLoading({
- title: '加载中'
- })
- wx.cloud.callFunction({
- name: 'listMessages',
- data: {
- pub_id: this.data.publisherId,
- page_token: this.data.pageToken,
- page_size: 25
- }
- }).then(res => {
- wx.hideLoading()
- if (res.result.status !== 'OK') {
- wx.showToast({
- title: res.result.errMsg,
- icon: 'none'
- })
- return
- }
- for (let i = 0; i < res.result.list.length; i++) {
- res.result.list[i] = util.dbToMsg(res.result.list[i])
- res.result.list[i].publisher = this.data.publisherInfo
- }
- this.setData({
- mainDatas: this.data.mainDatas.concat(res.result.list),
- pageToken: res.result.next_page_token
- })
- })
- },
- 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) {
- if (options.id) {
- this.setData({
- publisherId: options.id
- })
- wx.showLoading({
- title: "加载中"
- })
- wx.cloud.callFunction({
- name: 'getPublisher',
- data: {
- pub_id: this.data.publisherId
- }
- }).then(res => {
- wx.hideLoading()
- if (res.result.status !== 'OK') {
- wx.showToast({
- title: res.result.errMsg,
- icon: 'none'
- })
- return
- }
- this.setData({
- publisherInfo: res.result.data
- })
- this.loadExtraData()
- })
- } else {
- const eventChannel = this.getOpenerEventChannel()
- eventChannel.on('loadCommonData', res => {
- this.setData({
- publisherId: res.data._id,
- publisherInfo: res.data
- })
- this.loadExtraData()
- })
- }
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function () {
- this.loadMessageData()
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function () {
- }
- })
|