activity.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. const app = getApp()
  2. const util = require('../../utils/util.js')
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. messageId: '',
  9. activityInfo: {},
  10. like: false,
  11. showEdit: false,
  12. pageToken: 0,
  13. activityComment: [],
  14. commentText: ''
  15. },
  16. getPublisherInfo: function () {
  17. wx.navigateTo({
  18. url: '/pages/publisher/publisher?id=' + this.data.activityInfo.pub_id
  19. })
  20. },
  21. editActivity: function () {
  22. wx.navigateTo({
  23. url: '/pages/activityPublish/activityPublish?id=' + this.data.activityInfo.pub_id +
  24. '&msg_id=' + this.data.messageId
  25. })
  26. },
  27. toggleLike: function () {
  28. wx.showLoading({
  29. title: this.data.like ? '取消收藏' : '收藏中'
  30. })
  31. wx.cloud.callFunction({
  32. name: this.data.like ? 'deleteFavorite' : 'createFavorite',
  33. data: {
  34. msg_id: this.data.messageId
  35. }
  36. }).then(res => {
  37. wx.hideLoading()
  38. if (res.result.status !== 'OK') {
  39. wx.showToast({
  40. title: res.result.errMsg,
  41. icon: 'none'
  42. })
  43. return
  44. }
  45. wx.showToast({
  46. title: this.data.like ? '取消收藏成功' : '收藏成功',
  47. icon: 'none'
  48. })
  49. this.setData({
  50. like: !this.data.like
  51. })
  52. })
  53. // if (!this.data.likeEnable) return
  54. // this.setData({
  55. // likeEnable: false
  56. // })
  57. // const db = wx.cloud.database()
  58. // if (this.data.like == 0) {
  59. // db.collection('likeData').add({
  60. // data: {
  61. // type: 'message',
  62. // id: this.data.activityInfo._id
  63. // },
  64. // success: function () {
  65. // this.setData({
  66. // like: 1,
  67. // likeEnable: true
  68. // })
  69. // wx.showToast({
  70. // title: '已收藏',
  71. // })
  72. // }.bind(this),
  73. // fail: function () {
  74. // wx.showToast({
  75. // title: '网络错误',
  76. // icon: 'none'
  77. // })
  78. // }
  79. // })
  80. // } else {
  81. // db.collection('likeData').where({
  82. // type: 'message',
  83. // id: this.data.activityInfo._id
  84. // }).remove({
  85. // success: function () {
  86. // this.setData({
  87. // like: 0,
  88. // likeEnable: true
  89. // })
  90. // wx.showToast({
  91. // title: '已取消收藏',
  92. // })
  93. // }.bind(this),
  94. // fail: function () {
  95. // wx.showToast({
  96. // title: '网络错误',
  97. // icon: 'none'
  98. // })
  99. // }
  100. // })
  101. // }
  102. },
  103. loadQuestionData: function () {
  104. wx.showNavigationBarLoading()
  105. wx.cloud.callFunction({
  106. name: 'listQuestions',
  107. data: {
  108. msg_id: this.data.messageId,
  109. page_token: this.data.pageToken,
  110. page_size: 25
  111. }
  112. }).then(res => {
  113. wx.hideNavigationBarLoading()
  114. if (res.result.status !== 'OK') {
  115. wx.showToast({
  116. title: res.result.errMsg,
  117. icon: 'none'
  118. })
  119. return
  120. }
  121. for (let i = 0; i < res.result.list.length; i++) {
  122. res.result.list[i].a_time = util.handleDate(res.result.list[i].a_time)
  123. }
  124. this.setData({
  125. activityComment: this.data.activityComment.concat(res.result.list),
  126. pageToken: res.result.next_page_token
  127. })
  128. })
  129. },
  130. comment: function () {
  131. if (this.data.commentText.length < 5) {
  132. wx.showToast({
  133. title: '提问字数至少为5',
  134. icon: 'none'
  135. })
  136. } else {
  137. wx.showLoading({
  138. title: '发送中'
  139. })
  140. wx.cloud.callFunction({
  141. name: 'createQuestion',
  142. data: {
  143. msg_id: this.data.messageId,
  144. question: this.data.commentText
  145. }
  146. }).then(res => {
  147. wx.hideLoading()
  148. if (res.result.status !== 'OK') {
  149. wx.showToast({
  150. title: res.result.errMsg,
  151. icon: 'none'
  152. })
  153. return
  154. }
  155. wx.showToast({
  156. title: '发送成功,请等待发布者回复',
  157. icon: 'none'
  158. })
  159. this.setData({
  160. commentText: ''
  161. })
  162. })
  163. }
  164. },
  165. /**
  166. * 生命周期函数--监听页面加载
  167. */
  168. onLoad: function (options) {
  169. this.setData({
  170. messageId: options.id
  171. })
  172. wx.showLoading({
  173. title: '加载中'
  174. })
  175. wx.cloud.callFunction({
  176. name: 'getMessage',
  177. data: {
  178. msg_id: this.data.messageId
  179. }
  180. }).then(res => {
  181. wx.hideLoading()
  182. if (res.result.status !== 'OK') {
  183. wx.showToast({
  184. title: res.result.errMsg,
  185. icon: 'none'
  186. })
  187. return
  188. }
  189. res.result.data = util.dbToMsg(res.result.data)
  190. let edit = false
  191. for (let i = 0; i < app.globalData.pubInfo.length; i++) {
  192. if (res.result.data.pub_id === app.globalData.pubInfo[i].pub_id) {
  193. edit = true
  194. }
  195. }
  196. this.setData({
  197. activityInfo: res.result.data,
  198. like: res.result.data.favorite,
  199. showEdit: edit
  200. })
  201. this.loadQuestionData()
  202. })
  203. },
  204. /**
  205. * 生命周期函数--监听页面初次渲染完成
  206. */
  207. onReady: function () {
  208. },
  209. /**
  210. * 生命周期函数--监听页面显示
  211. */
  212. onShow: function () {
  213. },
  214. /**
  215. * 生命周期函数--监听页面隐藏
  216. */
  217. onHide: function () {
  218. },
  219. /**
  220. * 生命周期函数--监听页面卸载
  221. */
  222. onUnload: function () {
  223. },
  224. /**
  225. * 页面相关事件处理函数--监听用户下拉动作
  226. */
  227. onPullDownRefresh: function () {
  228. },
  229. /**
  230. * 页面上拉触底事件的处理函数
  231. */
  232. onReachBottom: function () {
  233. },
  234. /**
  235. * 用户点击右上角分享
  236. */
  237. onShareAppMessage: function () {
  238. }
  239. })