| | |
| | | import { getToken } from '@/utils/auth' |
| | | import { getUnreadCount } from '@/api/message' |
| | | import storage from '@/utils/storage' |
| | | import { redirectToLoginByEnvironment } from '@/utils/wechat' |
| | | |
| | | export default { |
| | | data() { |
| | |
| | | } |
| | | }, |
| | | onLaunch: function(options) { |
| | | console.log('App onLaunch, options:', options); |
| | | this.lastToken = getToken() |
| | | this.initApp(options) |
| | | |
| | |
| | | if (token) { |
| | | this.lastToken = token |
| | | this.updateUnreadMessageBadge() |
| | | this.startMessagePolling() |
| | | // this.startMessagePolling() |
| | | } |
| | | }) |
| | | |
| | |
| | | this.stopMessagePolling() |
| | | this.lastToken = null |
| | | // 清除消息徽标 |
| | | uni.removeTabBarBadge({ index: 3 }) |
| | | try { |
| | | uni.removeTabBarBadge({ index: 3 }) |
| | | } catch (e) { |
| | | console.error('清除消息徽标失败:', e) |
| | | } |
| | | }) |
| | | }, |
| | | onShow: function() { |
| | |
| | | this.updateUnreadMessageBadge() |
| | | // 重新启动轮询(如果之前已停止) |
| | | if (!this.messagePollingTimer) { |
| | | this.startMessagePolling() |
| | | // this.startMessagePolling() |
| | | } |
| | | } |
| | | }, |
| | |
| | | methods: { |
| | | // 初始化应用 |
| | | initApp(options) { |
| | | // 初始化应用配置 |
| | | this.initConfig() |
| | | // 检查用户登录状态并自动跳转到合适的登录页面 |
| | | this.checkLoginAndRedirect(options) |
| | | try { |
| | | // 初始化应用配置 |
| | | this.initConfig() |
| | | // 检查用户登录状态并自动跳转到合适的登录页面 |
| | | this.checkLoginAndRedirect(options) |
| | | } catch (e) { |
| | | console.error('初始化应用失败:', e) |
| | | } |
| | | |
| | | // 注意:不在应用启动时自动启动轮询 |
| | | // 只有在用户主动登录成功后才启动(通过 user-login 事件触发) |
| | |
| | | }, |
| | | // 检查登录状态并自动跳转到合适的登录页面 |
| | | checkLoginAndRedirect(options) { |
| | | if (!getToken()) { |
| | | // 检查运行环境并跳转到对应的登录页面 |
| | | // #ifdef MP-WEIXIN |
| | | // 在微信小程序环境中 |
| | | try { |
| | | // 获取系统信息 |
| | | const systemInfo = uni.getSystemInfoSync() |
| | | console.log('系统信息:', systemInfo) |
| | | try { |
| | | if (!getToken()) { |
| | | console.log('用户未登录,准备跳转到登录页面') |
| | | // 保存目标页面用于登录后跳转 |
| | | if (options && options.path && |
| | | options.path !== 'pages/login' && |
| | | options.path !== 'pages/qylogin') { |
| | | // 构造完整的路径和查询参数 |
| | | let fullPath = '/' + options.path; |
| | | if (options.query) { |
| | | const queryString = Object.keys(options.query).map(key => |
| | | `${key}=${encodeURIComponent(options.query[key])}`).join('&'); |
| | | if (queryString) { |
| | | fullPath += '?' + queryString; |
| | | } |
| | | } |
| | | |
| | | // 保存目标URL到本地存储 |
| | | try { |
| | | uni.setStorageSync('targetUrl', fullPath); |
| | | } catch (e) { |
| | | console.error('保存目标URL失败:', e); |
| | | } |
| | | } |
| | | |
| | | // 检查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') |
| | | // 使用工具类根据环境自动跳转到合适的登录页面 |
| | | redirectToLoginByEnvironment(options, this.$tab); |
| | | } else { |
| | | console.log('用户已登录,无需跳转') |
| | | } |
| | | // #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 |
| | | } |
| | | } catch (e) { |
| | | console.error('检查登录状态并跳转失败:', e) |
| | | // fallback到普通登录页面 |
| | | try { |
| | | this.$tab.reLaunch('/pages/login'); |
| | | } catch (fallbackError) { |
| | | console.error('fallback跳转也失败了:', fallbackError); |
| | | } |
| | | this.$tab.reLaunch(url) |
| | | // #endif |
| | | } |
| | | }, |
| | | |
| | | // 判断当前是否在登录页面 |
| | | isLoginPage() { |
| | | const pages = getCurrentPages() |
| | | if (pages.length === 0) { |
| | | try { |
| | | 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') |
| | | } catch (e) { |
| | | console.error('判断是否为登录页面失败:', e) |
| | | return false |
| | | } |
| | | const currentPage = pages[pages.length - 1] |
| | | const route = currentPage.route || '' |
| | | // 判断是否为登录相关页面 |
| | | return route.includes('login') || route.includes('register') |
| | | }, |
| | | |
| | | // 更新未读消息徽标 |
| | |
| | | |
| | | if (count > 0) { |
| | | // 设置徽标 |
| | | uni.setTabBarBadge({ |
| | | index: 3, // 消息页面在tabBar中的索引位置(0开始) |
| | | text: count > 99 ? '99+' : count.toString() |
| | | }) |
| | | try { |
| | | uni.setTabBarBadge({ |
| | | index: 3, // 消息页面在tabBar中的索引位置(0开始) |
| | | text: count > 99 ? '99+' : count.toString() |
| | | }) |
| | | } catch (e) { |
| | | console.error('设置消息徽标失败:', e) |
| | | } |
| | | } else { |
| | | // 移除徽标 |
| | | uni.removeTabBarBadge({ |
| | | index: 3 |
| | | }) |
| | | try { |
| | | uni.removeTabBarBadge({ |
| | | index: 3 |
| | | }) |
| | | } catch (e) { |
| | | console.error('移除消息徽标失败:', e) |
| | | } |
| | | } |
| | | }).catch(error => { |
| | | console.error('获取未读消息数量失败:', error) |
| | |
| | | |
| | | // 启动消息轮询 |
| | | startMessagePolling() { |
| | | // 每30秒轮询一次 |
| | | this.messagePollingTimer = setInterval(() => { |
| | | if (getToken()) { |
| | | this.updateUnreadMessageBadge() |
| | | } else { |
| | | // 如果用户已登出,停止轮询 |
| | | this.stopMessagePolling() |
| | | } |
| | | }, 30000) // 30秒 |
| | | try { |
| | | // 每30秒轮询一次 |
| | | this.messagePollingTimer = setInterval(() => { |
| | | if (getToken()) { |
| | | this.updateUnreadMessageBadge() |
| | | } else { |
| | | // 如果用户已登出,停止轮询 |
| | | this.stopMessagePolling() |
| | | } |
| | | }, 30000) // 30秒 |
| | | } catch (e) { |
| | | console.error('启动消息轮询失败:', e) |
| | | } |
| | | }, |
| | | |
| | | // 停止消息轮询 |
| | | stopMessagePolling() { |
| | | if (this.messagePollingTimer) { |
| | | clearInterval(this.messagePollingTimer) |
| | | this.messagePollingTimer = null |
| | | try { |
| | | if (this.messagePollingTimer) { |
| | | clearInterval(this.messagePollingTimer) |
| | | this.messagePollingTimer = null |
| | | } |
| | | } catch (e) { |
| | | console.error('停止消息轮询失败:', e) |
| | | } |
| | | }, |
| | | |