wlzboy
2025-11-07 2aebbc9601ab439707f69b08e467808df9f7549c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<script>
  import config from './config'
  import store from '@/store'
  import { getToken } from '@/utils/auth'
  import { getUnreadCount } from '@/api/message'
 
  export default {
    data() {
      return {
        messagePollingTimer: null,
        lastToken: null // 用于检测 token 变化
      }
    },
    onLaunch: function() {
      this.lastToken = getToken()
      this.initApp()
      
      // 监听用户登录事件
      uni.$on('user-login', () => {
        console.log('接收到用户登录事件,启动消息轮询')
        const token = getToken()
        if (token) {
          this.lastToken = token
          this.updateUnreadMessageBadge()
          this.startMessagePolling()
        }
      })
      
      // 监听用户登出事件
      uni.$on('user-logout', () => {
        console.log('接收到用户登出事件,停止消息轮询')
        this.stopMessagePolling()
        this.lastToken = null
        // 清除消息徽标
        uni.removeTabBarBadge({ index: 3 })
      })
    },
    onShow: function() {
      const currentToken = getToken()
      
      // 检测 token 变化:从有到无(登出)
      if (this.lastToken && !currentToken) {
        console.log('检测到用户已登出,停止消息轮询')
        this.stopMessagePolling()
        this.lastToken = currentToken
        return
      }
      
      // 检测 token 变化:从无到有(登录)
      if (!this.lastToken && currentToken) {
        console.log('检测到用户已登录,启动消息轮询')
        this.lastToken = currentToken
      }
      
      // 应用显示时刷新未读消息数量
      // 注意:只有已登录且不在登录页面时才调用
      if (currentToken && !this.isLoginPage()) {
        this.updateUnreadMessageBadge()
        // 重新启动轮询(如果之前已停止)
        if (!this.messagePollingTimer) {
          this.startMessagePolling()
        }
      }
    },
    onHide: function() {
      // 应用隐藏时停止轮询,节省资源
      this.stopMessagePolling()
    },
    methods: {
      // 初始化应用
      initApp() {
        // 初始化应用配置
        this.initConfig()
        // 检查用户登录状态
        //#ifdef H5
        this.checkLogin()
        //#endif
        
        // 注意:不在应用启动时自动启动轮询
        // 只有在用户主动登录成功后才启动(通过 user-login 事件触发)
      },
      initConfig() {
        this.globalData.config = config
      },
      checkLogin() {
        if (!getToken()) {
          this.$tab.reLaunch('/pages/login') 
        }
      },
      
      // 判断当前是否在登录页面
      isLoginPage() {
        const pages = getCurrentPages()
        if (pages.length === 0) {
          return false
        }
        const currentPage = pages[pages.length - 1]
        const route = currentPage.route || ''
        // 判断是否为登录相关页面
        return route.includes('login') || route.includes('register')
      },
      
      // 更新未读消息徽标
      updateUnreadMessageBadge() {
        // 检查是否已登录,未登录则不请求
        if (!getToken()) {
          console.log('用户未登录,跳过获取未读消息数量')
          return
        }
        
        getUnreadCount().then(response => {
          const count = response.data || 0
          console.log('未读消息数量:', count)
          
          if (count > 0) {
            // 设置徽标
            uni.setTabBarBadge({
              index: 3, // 消息页面在tabBar中的索引位置(0开始)
              text: count > 99 ? '99+' : count.toString()
            })
          } else {
            // 移除徽标
            uni.removeTabBarBadge({
              index: 3
            })
          }
        }).catch(error => {
          console.error('获取未读消息数量失败:', error)
        })
      },
      
      // 启动消息轮询
      startMessagePolling() {
        // 每30秒轮询一次
        this.messagePollingTimer = setInterval(() => {
          if (getToken()) {
            this.updateUnreadMessageBadge()
          } else {
            // 如果用户已登出,停止轮询
            this.stopMessagePolling()
          }
        }, 30000) // 30秒
      },
      
      // 停止消息轮询
      stopMessagePolling() {
        if (this.messagePollingTimer) {
          clearInterval(this.messagePollingTimer)
          this.messagePollingTimer = null
        }
      }
    }
  }
</script>
 
<style lang="scss">
  @import '@/static/scss/index.scss'
</style>