activity.js 6.4 KB

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