messageCenter.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // pages/messageCenter/messageCenter.js
  2. import {
  3. get
  4. } from '../../../utils/http'
  5. var WxParse = require('../../../components/local/wxParse/wxParse.js');
  6. const app = getApp()
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. current: 1,
  13. page: 1,
  14. limit: 10,
  15. messageList: '', //通知信息列表
  16. total: "", //通知列表总数
  17. notMessageTotal: '', //未读通知消息数量
  18. workMessageList: [], //业务消息列表
  19. pageA: 1,
  20. limitA: 10,
  21. totalA: '', //业务消息总数
  22. workMessageTotal: '', //未读消息总数
  23. },
  24. /**
  25. * 生命周期函数--监听页面加载
  26. */
  27. onLoad(options) {
  28. // 禁用分享
  29. wx.hideShareMenu()
  30. },
  31. /**
  32. * 生命周期函数--监听页面初次渲染完成
  33. */
  34. onReady() {
  35. },
  36. /**
  37. * 生命周期函数--监听页面显示
  38. */
  39. onShow() {
  40. // 获取未读通知消息数量
  41. this.getNotMessageTotal()
  42. // 获取通知消息列表
  43. this.getMessageList(1)
  44. // 接受消息
  45. this.getMessage()
  46. // 获取业务消息列表
  47. this.getWorkMessageList(1)
  48. // 获取所有未读业务消息数量
  49. this.getWorkMessageTotal()
  50. },
  51. /**
  52. * 生命周期函数--监听页面隐藏
  53. */
  54. onHide() {
  55. // 打开别的页面清除当前业务消息通知
  56. this.clearWorkMessage()
  57. },
  58. /**
  59. * 生命周期函数--监听页面卸载
  60. */
  61. onUnload() {
  62. // 打开别的页面清除当前业务消息通知
  63. this.clearWorkMessage()
  64. },
  65. /**
  66. * 页面相关事件处理函数--监听用户下拉动作
  67. */
  68. onPullDownRefresh() {
  69. },
  70. /**
  71. * 页面上拉触底事件的处理函数
  72. */
  73. onReachBottom() {
  74. // 通知
  75. if (this.data.messageList.length < this.data.total) {
  76. this.getMessageList(++this.data.page)
  77. }
  78. // 业务消息
  79. if (this.data.workMessageList.length < this.data.totalA) {
  80. this.getWorkMessageList(++this.data.pageA)
  81. }
  82. },
  83. /**
  84. * 用户点击右上角分享
  85. */
  86. onShareAppMessage() {
  87. },
  88. // 切换tab
  89. onTabsChange(e) {
  90. let current = e.currentTarget.dataset.id
  91. if (current == this.data.current) {
  92. return
  93. }
  94. if (current == 2) {
  95. // 切换到通知清除业务消息未读
  96. this.clearWorkMessage()
  97. // 获取未读通知消息数量
  98. this.getNotMessageTotal()
  99. // 获取通知消息列表
  100. this.getMessageList(1)
  101. } else {
  102. // 切换到业务消息清除通知未读
  103. this.clearMessageTotal()
  104. this.getWorkMessageList(1)
  105. this.getWorkMessageTotal()
  106. }
  107. this.setData({
  108. current,
  109. }, )
  110. },
  111. // 获取通知列表
  112. getMessageList(_page) {
  113. let that = this
  114. get('v2/api/message/list', {
  115. page: _page || that.data.page,
  116. limit: 10
  117. }, (res) => {
  118. if (res.code == 200) {
  119. let list = that.data.messageList
  120. if (_page == 1 | that.data.page == 1) {
  121. list = []
  122. }
  123. list.push(...res.data.list)
  124. // 截取通知列表content中的文字
  125. list.forEach(item => {
  126. let str = ''
  127. if (item.content.indexOf('img') != -1) {
  128. str = item.content.match(/(\S*)<img/)[1] + '</p>'
  129. item.content = str
  130. } else if (item.content.indexOf('video') != -1) {
  131. str = item.content.match(/(\S*)<video/)[1] + '</p>'
  132. item.content = str
  133. }
  134. })
  135. // 解析富文本
  136. let length = list.length
  137. list.forEach((item, index) => {
  138. WxParse.wxParse('article' + index, 'html', item.content, that, 5);
  139. if (index === length - 1) {
  140. WxParse.wxParseTemArray("askItemsArr", 'article', length, that)
  141. }
  142. })
  143. that.setData({
  144. messageList: list,
  145. total: res.data.total
  146. })
  147. }
  148. })
  149. },
  150. // 获取未读通知消息数量
  151. getNotMessageTotal() {
  152. get('v2/api/message/total', {}, (res) => {
  153. if (res.code == 200) {
  154. this.setData({
  155. notMessageTotal: res.data
  156. })
  157. }
  158. })
  159. },
  160. // 跳转通知消息详情
  161. toMessageDetail(e) {
  162. let id = e.currentTarget.dataset.id //消息ID
  163. let url = e.currentTarget.dataset.url //跳转链接
  164. let type = e.currentTarget.dataset.type //跳转链接类型 (0不跳转1内部链接url2外部链接)
  165. if (type == 1) {
  166. if (url == '/pages/home/home' || url == '/pages/shoppingMall/shoppingMall' || url == '/pages/orderBy/orderBy' || url == '/pages/article/article' || url == '/pages/member/member') {
  167. wx.switchTab({
  168. url: url,
  169. })
  170. return
  171. } else {
  172. wx.navigateTo({
  173. url: url,
  174. })
  175. return
  176. }
  177. } else {
  178. wx.navigateTo({
  179. url: `/subPackagesC/pages/messageDetail/messageDetail?id=${id}`,
  180. })
  181. }
  182. },
  183. // 清除所有通知
  184. clearMessage() {
  185. if (this.data.current == 1) {
  186. this.clearWorkMessage()
  187. } else {
  188. this.clearMessageTotal()
  189. }
  190. },
  191. clearMessageTotal() {
  192. get('v2/api/message/clear', {}, (res) => {
  193. // 获取未读通知消息数量
  194. this.getNotMessageTotal()
  195. // 获取通知消息列表
  196. this.getMessageList(1)
  197. })
  198. },
  199. // 接受消息通知
  200. getMessage() {
  201. wx.onSocketMessage((e) => {
  202. //当客户端收到服务端发来的消息时,触发onSocketMessage事件,参数e.data包含server传递过来的数据
  203. // console.log('收到服务端消息', e.data)
  204. let data = JSON.parse(e.data);
  205. // console.log(data)
  206. // 收到服务端订单添加消息后,若订单确认loading层未关闭,则直接跳转订单确认页;否则,对服务端推送消息不做任何逻辑处理
  207. // 收到消息通知
  208. if (data.data.type === 'instation') {
  209. console.log('接收到发来的消息');
  210. // 重新获取消息列表
  211. this.getMessageList(1)
  212. // 重新获取未读数量
  213. this.getNotMessageTotal()
  214. // 重新获取业务消息列表
  215. this.getWorkMessageList()
  216. // 重新获取业务消息未读数量
  217. this.getWorkMessageTotal()
  218. }
  219. })
  220. },
  221. // 获取业务消息列表
  222. getWorkMessageList(_pageA) {
  223. let that = this
  224. get('v2/api/message/notice', {
  225. page: _pageA || that.data.pageA,
  226. limit: that.data.limitA,
  227. }, (res) => {
  228. if (res.code == 200) {
  229. let workMessageList = that.data.workMessageList
  230. if (_pageA == 1 || that.data.pageA == 1) {
  231. workMessageList = []
  232. }
  233. workMessageList.push(...res.data.list)
  234. workMessageList.forEach(item => {
  235. let str = new RegExp('<br>', 'g')
  236. let strA = item.content.replace(str, '')
  237. item.content = strA
  238. })
  239. this.setData({
  240. workMessageList,
  241. totalA: res.data.total
  242. })
  243. }
  244. })
  245. },
  246. // 获取所有未读业务消息数量
  247. getWorkMessageTotal() {
  248. get('v2/api/message/all', {}, (res) => {
  249. if (res.code == 200) {
  250. this.setData({
  251. workMessageTotal: res.data
  252. })
  253. }
  254. })
  255. },
  256. // 清除所有业务消息通知
  257. clearWorkMessage() {
  258. get('v2/api/message/mark', {}, (res) => {
  259. this.getWorkMessageList(1)
  260. this.getWorkMessageTotal()
  261. })
  262. }
  263. })