App.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <script>
  2. import storage from "@/utils/storage.js";
  3. export default {
  4. onLaunch: function() {
  5. console.log('App Launch');
  6. this.checkLoginStatus();
  7. // 添加全局页面生命周期监听
  8. // uni.onPageNotFound(function(e) {
  9. // console.error('页面不存在:', e);
  10. // uni.navigateTo({
  11. // url: '/pages/dashboard/index'
  12. // });
  13. // });
  14. },
  15. onShow: function() {
  16. console.log('App Show');
  17. },
  18. onHide: function() {
  19. console.log('App Hide');
  20. },
  21. methods: {
  22. checkLoginStatus() {
  23. // 登录状态检查,对敏感页面进行拦截
  24. const pages = ['pages/device/index'];
  25. uni.addInterceptor('navigateTo', {
  26. invoke(e) {
  27. const url = e.url;
  28. // 检查是否属于需要登录的页面
  29. const needLogin = pages.some(page => url.indexOf(page) > -1);
  30. if (needLogin && !storage.isLoggedIn()) {
  31. uni.navigateTo({
  32. url: '/pages/login/index'
  33. });
  34. return false;
  35. }
  36. return true;
  37. }
  38. });
  39. uni.addInterceptor('switchTab', {
  40. invoke(e) {
  41. const url = e.url;
  42. // 检查是否属于需要登录的页面
  43. const needLogin = pages.some(page => url.indexOf(page) > -1);
  44. if (needLogin && !storage.isLoggedIn()) {
  45. uni.navigateTo({
  46. url: '/pages/login/index'
  47. });
  48. return false;
  49. }
  50. return true;
  51. }
  52. });
  53. }
  54. }
  55. }
  56. </script>
  57. <!-- App.vue中不需要template,uni-app会自动处理页面跳转 -->
  58. <style lang="scss">
  59. /* 全局基础样式 */
  60. @import "uview-ui/index.scss";
  61. page {
  62. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
  63. font-size: 28rpx;
  64. line-height: 1.5;
  65. color: #333;
  66. background-color: #f5f5f5;
  67. }
  68. /* 去除按钮默认边框 */
  69. button::after {
  70. border: none;
  71. }
  72. /* 隐藏滚动条 */
  73. ::-webkit-scrollbar {
  74. width: 0;
  75. height: 0;
  76. color: transparent;
  77. }
  78. /* H5环境特殊样式 */
  79. /* #ifdef H5 */
  80. html, body {
  81. height: 100%;
  82. width: 100%;
  83. overflow-x: hidden;
  84. }
  85. .uni-page-head {
  86. display: flex !important;
  87. }
  88. /* #endif */
  89. </style>