123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- // pages/messageCenter/messageCenter.js
- import {
- get
- } from '../../../utils/http'
- var WxParse = require('../../../components/local/wxParse/wxParse.js');
- const app = getApp()
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- current: 1,
- page: 1,
- limit: 10,
- messageList: '', //通知信息列表
- total: "", //通知列表总数
- notMessageTotal: '', //未读通知消息数量
- workMessageList: [], //业务消息列表
- pageA: 1,
- limitA: 10,
- totalA: '', //业务消息总数
- workMessageTotal: '', //未读消息总数
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad(options) {
- // 禁用分享
- wx.hideShareMenu()
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady() {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
- // 获取未读通知消息数量
- this.getNotMessageTotal()
- // 获取通知消息列表
- this.getMessageList(1)
- // 接受消息
- this.getMessage()
- // 获取业务消息列表
- this.getWorkMessageList(1)
- // 获取所有未读业务消息数量
- this.getWorkMessageTotal()
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide() {
- // 打开别的页面清除当前业务消息通知
- this.clearWorkMessage()
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload() {
- // 打开别的页面清除当前业务消息通知
- this.clearWorkMessage()
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh() {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom() {
- // 通知
- if (this.data.messageList.length < this.data.total) {
- this.getMessageList(++this.data.page)
- }
- // 业务消息
- if (this.data.workMessageList.length < this.data.totalA) {
- this.getWorkMessageList(++this.data.pageA)
- }
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage() {
- },
- // 切换tab
- onTabsChange(e) {
- let current = e.currentTarget.dataset.id
- if (current == this.data.current) {
- return
- }
- if (current == 2) {
- // 切换到通知清除业务消息未读
- this.clearWorkMessage()
- // 获取未读通知消息数量
- this.getNotMessageTotal()
- // 获取通知消息列表
- this.getMessageList(1)
- } else {
- // 切换到业务消息清除通知未读
- this.clearMessageTotal()
- this.getWorkMessageList(1)
- this.getWorkMessageTotal()
- }
- this.setData({
- current,
- }, )
- },
- // 获取通知列表
- getMessageList(_page) {
- let that = this
- get('v2/api/message/list', {
- page: _page || that.data.page,
- limit: 10
- }, (res) => {
- if (res.code == 200) {
- let list = that.data.messageList
- if (_page == 1 | that.data.page == 1) {
- list = []
- }
- list.push(...res.data.list)
- // 截取通知列表content中的文字
- list.forEach(item => {
- let str = ''
- if (item.content.indexOf('img') != -1) {
- str = item.content.match(/(\S*)<img/)[1] + '</p>'
- item.content = str
- } else if (item.content.indexOf('video') != -1) {
- str = item.content.match(/(\S*)<video/)[1] + '</p>'
- item.content = str
- }
- })
- // 解析富文本
- let length = list.length
- list.forEach((item, index) => {
- WxParse.wxParse('article' + index, 'html', item.content, that, 5);
- if (index === length - 1) {
- WxParse.wxParseTemArray("askItemsArr", 'article', length, that)
- }
- })
- that.setData({
- messageList: list,
- total: res.data.total
- })
- }
- })
- },
- // 获取未读通知消息数量
- getNotMessageTotal() {
- get('v2/api/message/total', {}, (res) => {
- if (res.code == 200) {
- this.setData({
- notMessageTotal: res.data
- })
- }
- })
- },
- // 跳转通知消息详情
- toMessageDetail(e) {
- let id = e.currentTarget.dataset.id //消息ID
- let url = e.currentTarget.dataset.url //跳转链接
- let type = e.currentTarget.dataset.type //跳转链接类型 (0不跳转1内部链接url2外部链接)
- if (type == 1) {
- if (url == '/pages/home/home' || url == '/pages/shoppingMall/shoppingMall' || url == '/pages/orderBy/orderBy' || url == '/pages/article/article' || url == '/pages/member/member') {
- wx.switchTab({
- url: url,
- })
- return
- } else {
- wx.navigateTo({
- url: url,
- })
- return
- }
- } else {
- wx.navigateTo({
- url: `/subPackagesC/pages/messageDetail/messageDetail?id=${id}`,
- })
- }
- },
- // 清除所有通知
- clearMessage() {
- if (this.data.current == 1) {
- this.clearWorkMessage()
- } else {
- this.clearMessageTotal()
- }
- },
- clearMessageTotal() {
- get('v2/api/message/clear', {}, (res) => {
- // 获取未读通知消息数量
- this.getNotMessageTotal()
- // 获取通知消息列表
- this.getMessageList(1)
- })
- },
- // 接受消息通知
- getMessage() {
- wx.onSocketMessage((e) => {
- //当客户端收到服务端发来的消息时,触发onSocketMessage事件,参数e.data包含server传递过来的数据
- // console.log('收到服务端消息', e.data)
- let data = JSON.parse(e.data);
- // console.log(data)
- // 收到服务端订单添加消息后,若订单确认loading层未关闭,则直接跳转订单确认页;否则,对服务端推送消息不做任何逻辑处理
- // 收到消息通知
- if (data.data.type === 'instation') {
- console.log('接收到发来的消息');
- // 重新获取消息列表
- this.getMessageList(1)
- // 重新获取未读数量
- this.getNotMessageTotal()
- // 重新获取业务消息列表
- this.getWorkMessageList()
- // 重新获取业务消息未读数量
- this.getWorkMessageTotal()
- }
- })
- },
- // 获取业务消息列表
- getWorkMessageList(_pageA) {
- let that = this
- get('v2/api/message/notice', {
- page: _pageA || that.data.pageA,
- limit: that.data.limitA,
- }, (res) => {
- if (res.code == 200) {
- let workMessageList = that.data.workMessageList
- if (_pageA == 1 || that.data.pageA == 1) {
- workMessageList = []
- }
- workMessageList.push(...res.data.list)
- workMessageList.forEach(item => {
- let str = new RegExp('<br>', 'g')
- let strA = item.content.replace(str, '')
- item.content = strA
- })
- this.setData({
- workMessageList,
- totalA: res.data.total
- })
- }
- })
- },
- // 获取所有未读业务消息数量
- getWorkMessageTotal() {
- get('v2/api/message/all', {}, (res) => {
- if (res.code == 200) {
- this.setData({
- workMessageTotal: res.data
- })
- }
- })
- },
- // 清除所有业务消息通知
- clearWorkMessage() {
- get('v2/api/message/mark', {}, (res) => {
- this.getWorkMessageList(1)
- this.getWorkMessageTotal()
- })
- }
- })
|