| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- <script>
- export default {
- onLaunch: function() {
- // console.log('App Launch');
- // 检查登录状态
- this.checkLoginStatus();
- },
- onShow: function() {
- // console.log('App Show');
- // 每次显示应用时检查登录状态
- this.checkLoginStatus();
- },
- onHide: function() {
- // console.log('App Hide');
- // 应用进入后台时释放资源
- this.releaseGlobalResources();
- },
-
- // 全局错误捕获
- onError: function(error) {
- this.handleGlobalError(error);
- },
- methods: {
- // 释放全局资源
- releaseGlobalResources: function() {
- console.log('Releasing global resources');
- // 清除可能存在的定时器
- try {
- // 这里可以添加更多的资源释放逻辑
- } catch (e) {
- console.error('Error releasing resources:', e);
- }
- },
-
- // 全局错误处理
- handleGlobalError: function(error) {
- console.error('Global error captured:', error);
-
- // 处理特定类型的错误
- if (error.message && error.message.includes('Failed to receiveTasks')) {
- // 提取实例ID
- const instanceIdMatch = error.message.match(/instance \((\d+)\)/);
- const instanceId = instanceIdMatch ? instanceIdMatch[1] : 'unknown';
-
- // 记录详细错误信息
- console.error(`UHF plugin instance (${instanceId}) error: Failed to receiveTasks`);
-
- // 显示错误提示
- uni.showToast({
- title: `设备通信错误(实例${instanceId}),请重试`,
- icon: 'none',
- duration: 3000
- });
-
- // 尝试重置插件实例
- setTimeout(() => {
- this.resetPluginInstance();
- }, 1000);
- }
- },
-
- // 重置应用状态
- resetAppState: function() {
- console.log('Resetting app state due to plugin error');
- try {
- // 清除所有相关缓存数据
- uni.removeStorageSync('uhf_plugin_state');
- uni.removeStorageSync('equipment_token');
-
- // 重新加载当前页面
- const pages = getCurrentPages();
- if (pages && pages.length > 0) {
- const currentPage = pages[pages.length - 1];
- // 如果当前是首页,则刷新页面
- if (currentPage.route === 'pages/index/index') {
- currentPage.$vm.$destroy();
- uni.redirectTo({
- url: '/pages/index/index'
- });
- } else {
- // 否则跳转到首页
- uni.redirectTo({
- url: '/pages/index/index'
- });
- }
- } else {
- // 如果没有页面,跳转到首页
- uni.redirectTo({
- url: '/pages/index/index'
- });
- }
- } catch (e) {
- console.error('Error resetting app state:', e);
- }
- },
-
- // 检查并清理插件实例
- checkAndCleanupPluginInstance: function() {
- console.log('Checking and cleaning up plugin instance');
- try {
- // 如果存在页面实例
- const pages = getCurrentPages();
- if (pages && pages.length > 0) {
- const currentPage = pages[pages.length - 1];
- if (currentPage && currentPage.$vm) {
- // 如果页面有uhfSFHelper实例,尝试释放设备
- if (currentPage.$vm.uhfSFHelper) {
- try {
- console.log('Releasing device from current page');
- currentPage.$vm.releaseDevice();
- } catch (e) {
- console.error('Error releasing device:', e);
- }
- // 清除插件实例
- currentPage.$vm.uhfSFHelper = null;
- console.log('Plugin instance cleared from current page');
- }
- }
- }
-
- // 清除全局插件相关缓存
- uni.removeStorageSync('uhf_plugin_instance');
- console.log('Plugin related storage cleared');
- } catch (e) {
- console.error('Error checking and cleaning up plugin instance:', e);
- }
- },
-
- // Vue2风格的checkLoginStatus方法实现 - 优化版
- checkLoginStatus: function() {
- // 防止短时间内重复导航到登录页面
- if (this.isNavigatingToLogin) {
- console.log('正在导航到登录页面,忽略重复调用');
- return;
- }
-
- try {
- // 1. 首先尝试从全局数据获取
- let token = this.globalData.token;
- let expireAt = this.globalData.expireAt;
- let userInfo = this.globalData.userInfo;
-
- // 2. 如果全局数据不可用,尝试从本地存储恢复
- if (!token || !expireAt) {
- token = uni.getStorageSync('equipment_token');
- expireAt = uni.getStorageSync('token_expire_time');
- userInfo = uni.getStorageSync('user_info');
-
- // 添加详细日志,帮助调试
- console.log('从本地存储获取登录信息 - token:', !!token, 'expireAt:', !!expireAt);
-
- // 如果从本地存储恢复成功,更新全局数据
- if (token && expireAt) {
- this.globalData.token = token;
- this.globalData.expireAt = expireAt;
- this.globalData.userInfo = userInfo;
- this.globalData.isLoggedIn = true;
- console.log('从本地存储恢复登录信息成功');
- }
- }
-
- // 获取当前页面路径
- const pages = getCurrentPages();
- let currentPath = '';
- if (pages && pages.length > 0) {
- const currentPage = pages[pages.length - 1];
- currentPath = currentPage.route;
- console.log('当前页面路径:', currentPath);
- }
-
- // 移除特殊账号处理逻辑,统一按token和过期时间判断
- if (!token || !expireAt) {
- // 未登录或缺少过期时间,跳转到登录页面
- // 检查当前是否已经在登录页面,避免重复跳转
- if (currentPath !== 'pages/login/login') {
- console.log('未登录或缺少过期时间,跳转到登录页面');
- this.isNavigatingToLogin = true;
- uni.redirectTo({
- url: '/pages/login/login',
- success: function() {
- console.log('导航到登录页面成功');
- // 导航成功后重置标志
- setTimeout(function() {
- this.isNavigatingToLogin = false;
- }.bind(this), 1000);
- }.bind(this),
- fail: function(err) {
- console.error('导航到登录页面失败:', err);
- this.isNavigatingToLogin = false;
- }.bind(this)
- });
- } else {
- console.log('已经在登录页面,无需跳转');
- }
- } else {
- // 检查token是否过期
- let expireTime;
- try {
- // 健壮的过期时间解析逻辑
- if (typeof expireAt === 'string') {
- // 尝试直接解析为数字
- const timestamp = parseInt(expireAt);
- if (!isNaN(timestamp)) {
- expireTime = timestamp;
- } else {
- // 如果不是数字字符串,尝试作为日期字符串解析
- expireTime = new Date(expireAt).getTime();
- }
- } else if (typeof expireAt === 'number') {
- // 如果已经是数字类型,直接使用
- expireTime = expireAt;
- }
- } catch (e) {
- console.error('解析过期时间失败:', e);
- }
-
- // 添加详细日志,记录过期时间解析结果
- console.log('过期时间解析结果 - expireAt:', expireAt, 'expireTime:', expireTime);
-
- // 确保过期时间有效
- if (isNaN(expireTime)) {
- console.error('无效的过期时间:', expireAt);
- // 跳转到登录页
- if (currentPath !== 'pages/login/login') {
- this.isNavigatingToLogin = true;
- uni.redirectTo({
- url: '/pages/login/login',
- success: function() {
- setTimeout(function() {
- this.isNavigatingToLogin = false;
- }.bind(this), 1000);
- }.bind(this),
- fail: function(err) {
- console.error('导航到登录页面失败:', err);
- this.isNavigatingToLogin = false;
- }.bind(this)
- });
- }
- return;
- }
-
- const currentTime = new Date().getTime();
-
- // 提前1分钟检查过期,给用户预留时间
- if (currentTime + 60 * 1000 > expireTime) {
- console.log('登录即将过期或已过期,需要重新登录 - 当前时间:', currentTime, '过期时间:', expireTime);
- // 清除登录信息
- this.globalData.token = '';
- this.globalData.expireAt = '';
- this.globalData.userInfo = {};
- this.globalData.isLoggedIn = false;
- uni.removeStorageSync('equipment_token');
- uni.removeStorageSync('token_expire_time');
- uni.removeStorageSync('user_info');
-
- // 跳转到登录页面
- if (currentPath !== 'pages/login/login') {
- this.isNavigatingToLogin = true;
- uni.redirectTo({
- url: '/pages/login/login',
- success: function() {
- console.log('导航到登录页面成功');
- // 导航成功后重置标志
- setTimeout(function() {
- this.isNavigatingToLogin = false;
- }.bind(this), 1000);
- }.bind(this),
- fail: function(err) {
- console.error('导航到登录页面失败:', err);
- this.isNavigatingToLogin = false;
- }.bind(this)
- });
- }
- } else {
- // 已登录且token有效,设置全局登录状态
- this.globalData.isLoggedIn = true;
- this.globalData.token = token;
- this.globalData.expireAt = expireAt;
- this.globalData.equipmentManage = this.globalData.equipmentManage || 0;
- this.globalData.userInfo = userInfo;
-
- // 计算剩余时间并记录日志,帮助调试
- const remainingTime = (expireTime - currentTime) / 1000 / 60; // 转换为分钟
- console.log('登录状态有效,无需重新登录 - 剩余时间约:', Math.round(remainingTime), '分钟');
-
- // 如果已经在登录页面,自动跳转到首页
- if (currentPath === 'pages/login/login') {
- console.log('登录状态有效但当前在登录页面,自动跳转到首页');
- this.isNavigatingToLogin = true;
- setTimeout(function() {
- uni.switchTab({
- url: '/pages/index/index',
- success: function() {
- console.log('从登录页跳转到首页成功');
- this.isNavigatingToLogin = false;
- }.bind(this),
- fail: function(err) {
- console.error('从登录页跳转到首页失败:', err);
- this.isNavigatingToLogin = false;
- }.bind(this)
- });
- }.bind(this), 500);
- }
- }
- }
- } catch (e) {
- console.error('检查登录状态时发生错误:', e);
- }
- },
-
- // 重置插件实例
- resetPluginInstance: function() {
- console.log('Resetting plugin instance globally');
- try {
- this.checkAndCleanupPluginInstance();
-
- // 延迟一段时间后重置应用状态
- setTimeout(() => {
- this.resetAppState();
- }, 1000);
- } catch (e) {
- console.error('Error resetting plugin instance:', e);
- }
- }
- },
- globalData: {
- isLoggedIn: false,
- token: '',
- expireAt: '',
- equipmentManage: 0,
- userInfo: {}
- },
- // 导航状态控制
- data() {
- return {
- isNavigatingToLogin: false
- }
- }
- }
- </script>
- <style>
- /*每个页面公共css */
- @import "components/colorui/main.css";
- @import "components/colorui/icon.css";
- </style>
|