| | |
| | | import store from '@/store' |
| | | import { getToken } from '@/utils/auth' |
| | | import { getUnreadCount } from '@/api/message' |
| | | import storage from '@/utils/storage' |
| | | |
| | | export default { |
| | | data() { |
| | |
| | | lastToken: null // 用于检测 token 变化 |
| | | } |
| | | }, |
| | | onLaunch: function() { |
| | | onLaunch: function(options) { |
| | | this.lastToken = getToken() |
| | | this.initApp() |
| | | this.initApp(options) |
| | | |
| | | // 检查并清理存储空间 |
| | | this.checkStorage() |
| | | |
| | | // 监听用户登录事件 |
| | | uni.$on('user-login', () => { |
| | |
| | | } |
| | | |
| | | // 应用显示时刷新未读消息数量 |
| | | if (currentToken) { |
| | | // 注意:只有已登录且不在登录页面时才调用 |
| | | if (currentToken && !this.isLoginPage()) { |
| | | this.updateUnreadMessageBadge() |
| | | // 重新启动轮询(如果之前已停止) |
| | | if (!this.messagePollingTimer) { |
| | |
| | | }, |
| | | methods: { |
| | | // 初始化应用 |
| | | initApp() { |
| | | initApp(options) { |
| | | // 初始化应用配置 |
| | | this.initConfig() |
| | | // 检查用户登录状态 |
| | | //#ifdef H5 |
| | | this.checkLogin() |
| | | //#endif |
| | | // 检查用户登录状态并自动跳转到合适的登录页面 |
| | | this.checkLoginAndRedirect(options) |
| | | |
| | | // 注意:不在应用启动时自动启动轮询 |
| | | // 只有在用户主动登录成功后才启动(通过 user-login 事件触发) |
| | |
| | | initConfig() { |
| | | this.globalData.config = config |
| | | }, |
| | | checkLogin() { |
| | | // 检查登录状态并自动跳转到合适的登录页面 |
| | | checkLoginAndRedirect(options) { |
| | | if (!getToken()) { |
| | | this.$tab.reLaunch('/pages/login') |
| | | // 检查运行环境并跳转到对应的登录页面 |
| | | // #ifdef MP-WEIXIN |
| | | // 在微信小程序环境中 |
| | | try { |
| | | // 获取系统信息 |
| | | const systemInfo = uni.getSystemInfoSync() |
| | | console.log('系统信息:', systemInfo) |
| | | |
| | | // 检查environment字段是否为wxwork |
| | | if (systemInfo.environment === 'wxwork') { |
| | | console.log('检测到企业微信环境,跳转到企业微信免登页面') |
| | | // 构造带参数的URL |
| | | let url = '/pages/qylogin' |
| | | if (options && options.query) { |
| | | const queryParams = Object.keys(options.query).map(key => `${key}=${encodeURIComponent(options.query[key])}`).join('&') |
| | | if (queryParams) { |
| | | url += '?' + queryParams |
| | | } |
| | | } |
| | | this.$tab.reLaunch(url) |
| | | return |
| | | } else { |
| | | console.log('检测到普通微信环境,跳转到微信登录页面') |
| | | // 构造带参数的URL |
| | | let url = '/pages/login' |
| | | if (options && options.query) { |
| | | const queryParams = Object.keys(options.query).map(key => `${key}=${encodeURIComponent(options.query[key])}`).join('&') |
| | | if (queryParams) { |
| | | url += '?' + queryParams |
| | | } |
| | | } |
| | | this.$tab.reLaunch(url) |
| | | return |
| | | } |
| | | } catch (e) { |
| | | console.error('获取系统信息失败:', e) |
| | | // 默认跳转到普通登录页面 |
| | | this.$tab.reLaunch('/pages/login') |
| | | } |
| | | // #endif |
| | | |
| | | // #ifndef MP-WEIXIN |
| | | // 非微信小程序环境,跳转到普通登录页面 |
| | | console.log('非微信小程序环境,跳转到普通登录页面') |
| | | let url = '/pages/login' |
| | | if (options && options.query) { |
| | | const queryParams = Object.keys(options.query).map(key => `${key}=${encodeURIComponent(options.query[key])}`).join('&') |
| | | if (queryParams) { |
| | | url += '?' + queryParams |
| | | } |
| | | } |
| | | this.$tab.reLaunch(url) |
| | | // #endif |
| | | } |
| | | }, |
| | | |
| | | // 判断当前是否在登录页面 |
| | | 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') |
| | | }, |
| | | |
| | | // 更新未读消息徽标 |
| | |
| | | clearInterval(this.messagePollingTimer) |
| | | this.messagePollingTimer = null |
| | | } |
| | | }, |
| | | |
| | | // 检查存储空间 |
| | | checkStorage() { |
| | | try { |
| | | // 获取存储信息 |
| | | const info = storage.getStorageInfo() |
| | | |
| | | if (info) { |
| | | const usagePercent = (info.currentSize / info.limitSize) * 100 |
| | | console.log(`💾 存储使用情况: ${usagePercent.toFixed(2)}% (${info.currentSize}KB / ${info.limitSize}KB)`) |
| | | |
| | | // 如果使用超过 80%,自动清理 |
| | | if (usagePercent > 80) { |
| | | console.warn('⚠️ 存储空间使用超过 80%,开始自动清理...') |
| | | storage.checkAndClean() |
| | | |
| | | // 再次检查 |
| | | const newInfo = storage.getStorageInfo() |
| | | if (newInfo) { |
| | | const newUsagePercent = (newInfo.currentSize / newInfo.limitSize) * 100 |
| | | console.log(`✅ 清理后使用情况: ${newUsagePercent.toFixed(2)}%`) |
| | | } |
| | | } |
| | | } |
| | | } catch (e) { |
| | | console.error('检查存储失败:', e) |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | |
| | | <style lang="scss"> |
| | | @import '@/static/scss/index.scss' |
| | | </style> |
| | | </style> |