Browse Source

信息数量更新

xiaohaizhao 2 years ago
parent
commit
0b34b6b5b0
4 changed files with 91 additions and 88 deletions
  1. 1 4
      app.js
  2. 86 82
      custom-tab-bar/index.js
  3. 2 1
      pages/tabbar/home/index.js
  4. 2 1
      pages/tabbar/message/index.js

+ 1 - 4
app.js

@@ -53,9 +53,6 @@ App({
         handleSelect: null, //处理选择结果  函数
         socketEstablish: false, //是否已经建立socket
         SocketTask: '', // Socket方法
-        socket: { // Socket回调
-            that: null, //保存this
-            callback: function () {}
-        },
+        socketCallback: null, // Socket回调
     }
 })

+ 86 - 82
custom-tab-bar/index.js

@@ -1,85 +1,89 @@
 const _Http = getApp().globalData.http;
 Component({
-    lifetimes: {
-        ready: function () {
-            if (this.data.count == 1) return;
-            if (wx.getStorageSync('userauth') != 0) {
-                //判断页面 以免一直触发
-                const pages = getCurrentPages(),
-                    prevPage = pages[pages.length - 1];
-                if (prevPage.route == 'pages/login/selectSite' || prevPage.route.includes('pages/tabbar/')) this.setData({
-                    Permission: ['首页', '信息', '我的']
-                })
-            }
-        },
-    },
-    data: {
-        active: 'home',
-        Permission: ['首页', '信息', '我的'], //可看模块
-        tabbarList: [{
-                icon: 'icon-a-biaoqianlanshouyexuanzhong',
-                acicon: 'icon-a-biaoqianlanshouyexuanzhong',
-                text: '首页',
-                url: '/pages/tabbar/home/index',
-                name: 'home'
-            },
-            {
-                icon: 'icon-a-biaoqianlanxiaoxi',
-                acicon: 'icon-a-biaoqianlanxiaoxixuanzhong',
-                text: '信息',
-                url: '/pages/tabbar/message/index',
-                name: 'message'
-            },
-            {
-                icon: 'icon-a-biaoqianlanwode',
-                acicon: 'icon-a-biaoqianlanwodexuanzhong',
-                text: '我的',
-                url: '/pages/tabbar/mine/index',
-                name: 'mine'
-            }
-        ],
-    },
-    methods: {
-        onChange(event) {
-            let active = event.detail,
-                obj = this.data.tabbarList.find(v => v.name == active);
-            wx.switchTab({
-                url: obj.url
-            });
-            this.setData({
-                active
-            });
-        },
-        init() {
-            const page = getCurrentPages().pop();
-            let obj = this.data.tabbarList.find(item => item.url === `/${page.route}`);
-            this.setData({
-                active: obj.name
-            });
-            //socket
-            if (!getApp().globalData.socketEstablish) getApp().initSocket();
-            getApp().globalData.socket = {
-                that: this,
-                callback: this.unReadMessageCount
-            }
-            this.unReadMessageCount()
-        },
-        /* 更新信息数量 */
-        unReadMessageCount() {
-            _Http.basic({
-                "classname": "system.message.Message",
-                "method": "unReadMessageCount",
-                "content": {
-                    nocache: true
-                }
-            }, false).then(res => {
-                if (res.msg != '成功') return;
-                let fcount = res.data.fcount > 99 ? '99+' : res.data.fcount;
-                if (res.data.fcount == 0) fcount = "";
-                getApp().globalData.socket.that.setData({
-                    'tabbarList[1].fcount': fcount
-                })
-            })
-        }
-    },
+	lifetimes: {
+		ready: function () {
+			if (this.data.count == 1) return;
+			if (wx.getStorageSync('userauth') != 0) {
+				//判断页面 以免一直触发
+				const pages = getCurrentPages(),
+					prevPage = pages[pages.length - 1];
+				if (prevPage.route == 'pages/login/selectSite' || prevPage.route.includes('pages/tabbar/')) this.setData({
+					Permission: ['首页', '信息', '我的']
+				})
+			}
+		},
+	},
+	data: {
+		active: 'home',
+		Permission: ['首页', '信息', '我的'], //可看模块
+		tabbarList: [{
+				icon: 'icon-a-biaoqianlanshouyexuanzhong',
+				acicon: 'icon-a-biaoqianlanshouyexuanzhong',
+				text: '首页',
+				url: '/pages/tabbar/home/index',
+				name: 'home'
+			},
+			{
+				icon: 'icon-a-biaoqianlanxiaoxi',
+				acicon: 'icon-a-biaoqianlanxiaoxixuanzhong',
+				text: '信息',
+				url: '/pages/tabbar/message/index',
+				name: 'message'
+			},
+			{
+				icon: 'icon-a-biaoqianlanwode',
+				acicon: 'icon-a-biaoqianlanwodexuanzhong',
+				text: '我的',
+				url: '/pages/tabbar/mine/index',
+				name: 'mine'
+			}
+		],
+	},
+	methods: {
+		onChange(event) {
+			let active = event.detail,
+				obj = this.data.tabbarList.find(v => v.name == active);
+			wx.switchTab({
+				url: obj.url
+			});
+			this.setData({
+				active
+			});
+		},
+		init() {
+			const page = getCurrentPages().pop();
+			let obj = this.data.tabbarList.find(item => item.url === `/${page.route}`);
+			this.setData({
+				active: obj.name
+			});
+			//socket
+			if (!getApp().globalData.socketEstablish) getApp().initSocket();
+			if (!getApp().globalData.socketCallback) getApp().globalData.socketCallback = this.unReadMessageCount.bind(this);
+			this.setUnReadMessageCount(getApp().globalData.fcount);
+		},
+		/* 设置未读数量 */
+		setUnReadMessageCount(fcount) {
+			let i = this.data.tabbarList.findIndex(v => v.text == '信息');
+			this.setData({
+				[`tabbarList[${i}].fcount`]: fcount
+			})
+		},
+		/* 更新信息数量 */
+		unReadMessageCount() {
+			_Http.basic({
+				"classname": "system.message.Message",
+				"method": "unReadMessageCount",
+				"content": {
+					nocache: true
+				}
+			}, false).then(res => {
+				console.log('信息数量', res)
+				if (res.msg != '成功') return;
+				let fcount = res.data.fcount > 99 ? '99+' : res.data.fcount;
+				if (res.data.fcount == 0) fcount = "";
+				getApp().globalData.fcount = fcount;
+				this.setUnReadMessageCount(fcount);
+			})
+		}
+	},
 })

+ 2 - 1
pages/tabbar/home/index.js

@@ -13,7 +13,8 @@ Page({
         this.setData({ //获取胶囊位置信息
             capsule: wx.getMenuButtonBoundingClientRect()
         })
-        this.refreshData() //更新权限等信息
+				this.refreshData() //更新权限等信息
+        this.getTabBar().unReadMessageCount();//更新信息数量
     },
     /* 更新站点信息 */
     refreshData() {

+ 2 - 1
pages/tabbar/message/index.js

@@ -50,7 +50,7 @@ Page({
         } = e.currentTarget.dataset;
         wx.navigateTo({
             url: './details?id=' + item.messageid,
-        })
+				})
     },
 
     getlist(init = false) {
@@ -84,6 +84,7 @@ Page({
     onShow() {
         this.getlist(true);
         this.getTabBar().init();
+        this.getTabBar().unReadMessageCount();//更新信息数量
     },
     onShareAppMessage() {}
 })