activity.js 6.5 KB

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