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. processCommonData: function (data) {
  98. let edit = false
  99. for (let i = 0; i < app.globalData.pubInfo.length; i++) {
  100. if (data.pub_id === app.globalData.pubInfo[i].pub_id) {
  101. edit = true
  102. }
  103. }
  104. this.setData({
  105. activityInfo: data,
  106. showEdit: edit
  107. })
  108. },
  109. loadExtraData: function () {
  110. wx.showNavigationBarLoading()
  111. const arr = []
  112. arr.push(wx.cloud.callFunction({
  113. name: 'listQuestions',
  114. data: {
  115. msg_id: this.data.messageId,
  116. page_token: this.data.pageToken,
  117. page_size: 20
  118. }
  119. }))
  120. arr.push(wx.cloud.callFunction({
  121. name: 'getFavorite',
  122. data: {
  123. msg_id: this.data.messageId
  124. }
  125. }))
  126. Promise.all(arr).then(res => {
  127. wx.hideNavigationBarLoading()
  128. if (res[0].result.status !== 'OK' || res[1].result.status !== 'OK') {
  129. wx.showToast({
  130. title: res[0].result.errMsg || res[1].result.errMsg,
  131. icon: 'none'
  132. })
  133. return
  134. }
  135. for (let i = 0; i < res[0].result.list.length; i++) {
  136. res[0].result.list[i].a_time = util.handleDate(res[0].result.list[i].a_time)
  137. }
  138. this.setData({
  139. activityComment: this.data.activityComment.concat(res[0].result.list),
  140. pageToken: res[0].result.next_page_token,
  141. like: res[1].result.total === 1,
  142. likeDisabled: false
  143. })
  144. })
  145. },
  146. comment: function () {
  147. if (this.data.commentText.length < 5) {
  148. wx.showToast({
  149. title: '提问字数至少为5',
  150. icon: 'none'
  151. })
  152. } else {
  153. wx.showLoading({
  154. title: '发送中'
  155. })
  156. wx.cloud.callFunction({
  157. name: 'createQuestion',
  158. data: {
  159. msg_id: this.data.messageId,
  160. question: this.data.commentText
  161. }
  162. }).then(res => {
  163. wx.hideLoading()
  164. if (res.result.status !== 'OK') {
  165. wx.showToast({
  166. title: res.result.errMsg,
  167. icon: 'none'
  168. })
  169. return
  170. }
  171. wx.showToast({
  172. title: '发送成功,请等待发布者回复',
  173. icon: 'none'
  174. })
  175. this.setData({
  176. commentText: ''
  177. })
  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. })