App.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <script>
  2. export default {
  3. onLaunch: function() {
  4. console.log('App Launch');
  5. // 检查登录状态
  6. this.checkLoginStatus();
  7. },
  8. onShow: function() {
  9. console.log('App Show');
  10. // 每次显示应用时检查登录状态
  11. this.checkLoginStatus();
  12. },
  13. onHide: function() {
  14. console.log('App Hide');
  15. // 应用进入后台时释放资源
  16. this.releaseGlobalResources();
  17. },
  18. // 全局错误捕获
  19. onError: function(error) {
  20. this.handleGlobalError(error);
  21. },
  22. methods: {
  23. // 释放全局资源
  24. releaseGlobalResources: function() {
  25. console.log('Releasing global resources');
  26. // 清除可能存在的定时器
  27. try {
  28. // 这里可以添加更多的资源释放逻辑
  29. } catch (e) {
  30. console.error('Error releasing resources:', e);
  31. }
  32. },
  33. // 全局错误处理
  34. handleGlobalError: function(error) {
  35. console.error('Global error captured:', error);
  36. // 处理特定类型的错误
  37. if (error.message && error.message.includes('Failed to receiveTasks')) {
  38. // 提取实例ID
  39. const instanceIdMatch = error.message.match(/instance \((\d+)\)/);
  40. const instanceId = instanceIdMatch ? instanceIdMatch[1] : 'unknown';
  41. // 记录详细错误信息
  42. console.error(`UHF plugin instance (${instanceId}) error: Failed to receiveTasks`);
  43. // 显示错误提示
  44. uni.showToast({
  45. title: `设备通信错误(实例${instanceId}),请重试`,
  46. icon: 'none',
  47. duration: 3000
  48. });
  49. // 尝试重置插件实例
  50. setTimeout(() => {
  51. this.resetPluginInstance();
  52. }, 1000);
  53. }
  54. },
  55. // 重置应用状态
  56. resetAppState: function() {
  57. console.log('Resetting app state due to plugin error');
  58. try {
  59. // 清除所有相关缓存数据
  60. uni.removeStorageSync('uhf_plugin_state');
  61. uni.removeStorageSync('equipment_token');
  62. // 重新加载当前页面
  63. const pages = getCurrentPages();
  64. if (pages && pages.length > 0) {
  65. const currentPage = pages[pages.length - 1];
  66. // 如果当前是首页,则刷新页面
  67. if (currentPage.route === 'pages/index/index') {
  68. currentPage.$vm.$destroy();
  69. uni.redirectTo({
  70. url: '/pages/index/index'
  71. });
  72. } else {
  73. // 否则跳转到首页
  74. uni.redirectTo({
  75. url: '/pages/index/index'
  76. });
  77. }
  78. } else {
  79. // 如果没有页面,跳转到首页
  80. uni.redirectTo({
  81. url: '/pages/index/index'
  82. });
  83. }
  84. } catch (e) {
  85. console.error('Error resetting app state:', e);
  86. }
  87. },
  88. // 检查并清理插件实例
  89. checkAndCleanupPluginInstance: function() {
  90. console.log('Checking and cleaning up plugin instance');
  91. try {
  92. // 如果存在页面实例
  93. const pages = getCurrentPages();
  94. if (pages && pages.length > 0) {
  95. const currentPage = pages[pages.length - 1];
  96. if (currentPage && currentPage.$vm) {
  97. // 如果页面有uhfSFHelper实例,尝试释放设备
  98. if (currentPage.$vm.uhfSFHelper) {
  99. try {
  100. console.log('Releasing device from current page');
  101. currentPage.$vm.releaseDevice();
  102. } catch (e) {
  103. console.error('Error releasing device:', e);
  104. }
  105. // 清除插件实例
  106. currentPage.$vm.uhfSFHelper = null;
  107. console.log('Plugin instance cleared from current page');
  108. }
  109. }
  110. }
  111. // 清除全局插件相关缓存
  112. uni.removeStorageSync('uhf_plugin_instance');
  113. console.log('Plugin related storage cleared');
  114. } catch (e) {
  115. console.error('Error checking and cleaning up plugin instance:', e);
  116. }
  117. },
  118. checkLoginStatus: function() {
  119. try {
  120. const token = uni.getStorageSync('equipment_token');
  121. // 获取当前页面路径
  122. const pages = getCurrentPages();
  123. let currentPath = '';
  124. if (pages && pages.length > 0) {
  125. const currentPage = pages[pages.length - 1];
  126. currentPath = currentPage.route;
  127. }
  128. if (!token) {
  129. // 未登录,跳转到登录页面
  130. // 检查当前是否已经在登录页面,避免重复跳转
  131. if (currentPath !== 'pages/login/login') {
  132. uni.redirectTo({
  133. url: '/pages/login/login'
  134. });
  135. }
  136. } else {
  137. // 已登录,设置全局登录状态
  138. const equipmentManage = uni.getStorageSync('equipment_manage');
  139. const userInfo = uni.getStorageSync('user_info') || {};
  140. this.globalData.isLoggedIn = true;
  141. this.globalData.token = token;
  142. this.globalData.equipmentManage = equipmentManage;
  143. this.globalData.userInfo = userInfo;
  144. }
  145. } catch (e) {
  146. // 错误处理
  147. console.error('检查登录状态失败:', e);
  148. // 跳转到登录页面
  149. // 获取当前页面路径
  150. const pages = getCurrentPages();
  151. let currentPath = '';
  152. if (pages && pages.length > 0) {
  153. const currentPage = pages[pages.length - 1];
  154. currentPath = currentPage.route;
  155. }
  156. // 检查当前是否已经在登录页面,避免重复跳转
  157. if (currentPath !== 'pages/login/login') {
  158. uni.redirectTo({
  159. url: '/pages/login/login'
  160. });
  161. }
  162. }
  163. },
  164. // 重置插件实例
  165. resetPluginInstance: function() {
  166. console.log('Resetting plugin instance globally');
  167. try {
  168. this.checkAndCleanupPluginInstance();
  169. // 延迟一段时间后重置应用状态
  170. setTimeout(() => {
  171. this.resetAppState();
  172. }, 1000);
  173. } catch (e) {
  174. console.error('Error resetting plugin instance:', e);
  175. }
  176. }
  177. },
  178. globalData: {
  179. isLoggedIn: false,
  180. token: '',
  181. equipmentManage: 0,
  182. userInfo: {}
  183. }
  184. }
  185. </script>
  186. <style>
  187. /*每个页面公共css */
  188. @import "components/colorui/main.css";
  189. @import "components/colorui/icon.css";
  190. </style>