message.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. const app = getApp()
  2. const util = require('../../utils/util.js')
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. hasUserInfo: false,
  9. messageId: '',
  10. messageInfo: {},
  11. like: false,
  12. loading: true,
  13. showEdit: false,
  14. pageToken: 0,
  15. messageQuestion: [],
  16. questionText: ''
  17. },
  18. getPublisherInfo: function () {
  19. wx.navigateTo({
  20. url: '/pages/publisher/publisher',
  21. }).then(res => {
  22. res.eventChannel.emit('loadCommonData', {
  23. data: this.data.messageInfo.publisher
  24. })
  25. })
  26. },
  27. editMessage: function () {
  28. wx.navigateTo({
  29. url: '/pages/messagePublish/messagePublish?id=' + this.data.messageInfo.pub_id +
  30. '&msg_id=' + this.data.messageId
  31. })
  32. },
  33. deleteMessage: function () {
  34. wx.showModal({
  35. content: "确认删除信息?",
  36. confirmColor: "#009195"
  37. }).then(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. wx.showToast({
  58. title: "删除成功",
  59. icon: 'none'
  60. })
  61. const eventChannel = this.getOpenerEventChannel()
  62. eventChannel.emit('deleteMessage')
  63. })
  64. }
  65. })
  66. },
  67. toggleLike: function () {
  68. if (this.data.loading) {
  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. question: function () {
  98. if (this.data.questionText.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.questionText
  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. questionText: ''
  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. messageInfo: data,
  141. showEdit: edit
  142. })
  143. },
  144. loadExtraData: function () {
  145. this.setData({
  146. loading: true
  147. })
  148. wx.showNavigationBarLoading()
  149. const arr = []
  150. arr.push(wx.cloud.callFunction({
  151. name: 'listQuestions',
  152. data: {
  153. msg_id: this.data.messageId,
  154. page_token: this.data.pageToken,
  155. page_size: 20
  156. }
  157. }))
  158. arr.push(wx.cloud.callFunction({
  159. name: 'getFavorite',
  160. data: {
  161. msg_id: this.data.messageId
  162. }
  163. }))
  164. Promise.all(arr).then(res => {
  165. wx.hideNavigationBarLoading()
  166. if (res[0].result.status !== 'OK' || res[1].result.status !== 'OK') {
  167. wx.showToast({
  168. title: res[0].result.errMsg || res[1].result.errMsg,
  169. icon: 'none'
  170. })
  171. return
  172. }
  173. for (let i = 0; i < res[0].result.list.length; i++) {
  174. res[0].result.list[i].a_time = util.handleDate(res[0].result.list[i].a_time)
  175. }
  176. this.setData({
  177. messageQuestion: this.data.messageQuestion.concat(res[0].result.list),
  178. pageToken: res[0].result.next_page_token,
  179. like: res[1].result.total === 1,
  180. loading: false
  181. })
  182. })
  183. },
  184. /**
  185. * 生命周期函数--监听页面加载
  186. */
  187. onLoad: function (options) {
  188. this.setData({
  189. hasUserInfo: app.globalData.hasUserInfo
  190. })
  191. if (options.id) {
  192. this.setData({
  193. messageId: options.id
  194. })
  195. wx.showLoading({
  196. title: '正在加载'
  197. })
  198. wx.cloud.callFunction({
  199. name: 'getMessage',
  200. data: {
  201. msg_id: this.data.messageId
  202. }
  203. }).then(res => {
  204. wx.hideLoading()
  205. if (res.result.status !== 'OK') {
  206. wx.showToast({
  207. title: res.result.errMsg,
  208. icon: 'none'
  209. })
  210. return
  211. }
  212. res.result.data = util.dbToMsg(res.result.data)
  213. this.processCommonData(res.result.data)
  214. this.loadExtraData()
  215. })
  216. } else {
  217. const eventChannel = this.getOpenerEventChannel()
  218. eventChannel.once('loadCommonData', res => {
  219. this.setData({
  220. messageId: res.data._id,
  221. })
  222. this.processCommonData(res.data)
  223. this.loadExtraData()
  224. wx.cloud.callFunction({
  225. name: 'createRead',
  226. data: {
  227. msg_id: this.data.messageId
  228. }
  229. })
  230. })
  231. }
  232. },
  233. /**
  234. * 生命周期函数--监听页面初次渲染完成
  235. */
  236. onReady: function () {
  237. },
  238. /**
  239. * 生命周期函数--监听页面显示
  240. */
  241. onShow: function () {
  242. },
  243. /**
  244. * 生命周期函数--监听页面隐藏
  245. */
  246. onHide: function () {
  247. },
  248. /**
  249. * 生命周期函数--监听页面卸载
  250. */
  251. onUnload: function () {
  252. },
  253. /**
  254. * 页面相关事件处理函数--监听用户下拉动作
  255. */
  256. onPullDownRefresh: function () {
  257. },
  258. /**
  259. * 页面上拉触底事件的处理函数
  260. */
  261. onReachBottom: function () {
  262. if (this.data.loading) {
  263. return
  264. }
  265. this.loadExtraData()
  266. },
  267. /**
  268. * 用户点击右上角分享
  269. */
  270. onShareAppMessage: function () {
  271. }
  272. })