main.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import Vue from 'vue'
  2. import App from './App'
  3. Vue.config.productionTip = false
  4. import uView from '@/uni_modules/uview-ui'
  5. Vue.use(uView)
  6. // uni.$u.config.unit = 'rpx'
  7. import utils from './utils/common.js'
  8. Vue.prototype.utils = utils;
  9. import {setData} from './utils/common.js'
  10. Vue.prototype.setData = setData;
  11. App.mpType = 'app'
  12. const app = new Vue({
  13. ...App
  14. })
  15. app.$mount()
  16. // 辅助函数:检查页面是否需要登录
  17. function checkNeedLogin(targetPath) {
  18. // 不需要登录的页面:首页、个人中心、登录相关页面、启动页
  19. const noLoginPages = [
  20. 'home/index',
  21. 'user/index',
  22. 'auth/login',
  23. 'auth/forgetPassword',
  24. 'auth/changePassword',
  25. 'auth/wxLogin',
  26. 'auth/setting',
  27. 'common/launch',
  28. 'pages_user/userAgreement',
  29. 'pages_user/privacyPolicy',
  30. 'pages/auth/register',
  31. 'pages/auth/registerUser',
  32. ]
  33. // 移除路径中的斜杠和协议,只保留核心路径部分
  34. const normalizedPath = targetPath.replace(/^\/|^.*?:\/\//g, '')
  35. // 检查目标页面是否在不需要登录的列表中
  36. return !noLoginPages.some(page => normalizedPath.includes(page))
  37. }
  38. // 添加导航守卫,检查用户登录状态
  39. uni.addInterceptor('navigateTo', {
  40. invoke(e) {
  41. // 检查是否登录
  42. const userInfo = uni.getStorageSync('userInfo')
  43. // 目标页面路径
  44. const targetPath = e.url
  45. // 检查目标页面是否需要登录
  46. const needLogin = checkNeedLogin(targetPath)
  47. // 如果需要登录且未登录,则跳转到登录页面
  48. if (needLogin && !userInfo) {
  49. uni.navigateTo({
  50. url: '/pages/auth/login'
  51. })
  52. // 阻止原导航
  53. return false
  54. }
  55. return true
  56. }
  57. })
  58. // 同样拦截redirectTo
  59. uni.addInterceptor('redirectTo', {
  60. invoke(e) {
  61. const userInfo = uni.getStorageSync('userInfo')
  62. const targetPath = e.url
  63. const needLogin = checkNeedLogin(targetPath)
  64. if (needLogin && !userInfo) {
  65. uni.navigateTo({
  66. url: '/pages/auth/login'
  67. })
  68. return false
  69. }
  70. return true
  71. }
  72. })
  73. // 同样拦截reLaunch
  74. uni.addInterceptor('reLaunch', {
  75. invoke(e) {
  76. const userInfo = uni.getStorageSync('userInfo')
  77. const targetPath = e.url
  78. const needLogin = checkNeedLogin(targetPath)
  79. if (needLogin && !userInfo) {
  80. uni.navigateTo({
  81. url: '/pages/auth/login'
  82. })
  83. return false
  84. }
  85. return true
  86. }
  87. })
  88. // 同样拦截switchTab
  89. uni.addInterceptor('switchTab', {
  90. invoke(e) {
  91. const userInfo = uni.getStorageSync('userInfo')
  92. const targetPath = e.url
  93. const needLogin = checkNeedLogin(targetPath)
  94. if (needLogin && !userInfo) {
  95. uni.navigateTo({
  96. url: '/pages/auth/login'
  97. })
  98. return false
  99. }
  100. return true
  101. }
  102. })