| | |
| | | import { getUnreadCount } from '@/api/message' |
| | | import storage from '@/utils/storage' |
| | | import { redirectToLoginByEnvironment } from '@/utils/wechat' |
| | | import { getAppFeatures } from '@/api/appConfig' |
| | | |
| | | export default { |
| | | data() { |
| | |
| | | this.lastToken = token |
| | | this.updateUnreadMessageBadge() |
| | | // this.startMessagePolling() |
| | | // 加载服务器配置 |
| | | this.loadServerConfig() |
| | | } |
| | | }) |
| | | |
| | |
| | | this.updateUnreadMessageBadge() |
| | | // 重新启动轮询(如果之前已停止) |
| | | if (!this.messagePollingTimer) { |
| | | this.startMessagePolling() |
| | | // this.startMessagePolling() |
| | | } |
| | | } |
| | | }, |
| | |
| | | // 注意:不在应用启动时自动启动轮询 |
| | | // 只有在用户主动登录成功后才启动(通过 user-login 事件触发) |
| | | }, |
| | | |
| | | // 初始化配置:合并本地配置和服务器配置 |
| | | initConfig() { |
| | | // 先使用本地默认配置 |
| | | this.globalData.config = config |
| | | |
| | | // 如果用户已登录,尝试加载服务器配置 |
| | | if (getToken()) { |
| | | this.loadServerConfig() |
| | | } else { |
| | | console.log('用户未登录,使用本地默认配置') |
| | | } |
| | | }, |
| | | |
| | | // 从服务器加载配置 |
| | | loadServerConfig() { |
| | | getAppFeatures().then(response => { |
| | | console.log('加载服务器配置成功:', response.data) |
| | | // 合并配置:服务器配置覆盖本地配置 |
| | | if (response.data) { |
| | | this.globalData.config.features = Object.assign({}, |
| | | this.globalData.config.features || {}, |
| | | response.data |
| | | ) |
| | | // 更新全局config对象,确保其他地方也能获取到最新配置 |
| | | config.features = this.globalData.config.features |
| | | } |
| | | }).catch(error => { |
| | | console.error('加载服务器配置失败:', error) |
| | | console.log('使用本地默认配置') |
| | | }) |
| | | }, |
| | | // 检查登录状态并自动跳转到合适的登录页面 |
| | | checkLoginAndRedirect(options) { |
| | | 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); |
| | | } |
| | | } |
| | | |
| | | // 使用工具类根据环境自动跳转到合适的登录页面 |
| | | redirectToLoginByEnvironment(options, this.$tab); |
| | | } else { |