| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- import Vue from 'vue'
- import App from './App'
- Vue.config.productionTip = false
- import uView from '@/uni_modules/uview-ui'
- Vue.use(uView)
- // uni.$u.config.unit = 'rpx'
-
- import utils from './utils/common.js'
- Vue.prototype.utils = utils;
-
- import {setData} from './utils/common.js'
- Vue.prototype.setData = setData;
- App.mpType = 'app'
- const app = new Vue({
- ...App
- })
- app.$mount()
- // 辅助函数:检查页面是否需要登录
- function checkNeedLogin(targetPath) {
- // 不需要登录的页面:首页、个人中心、登录相关页面、启动页
- const noLoginPages = [
- 'home/index',
- 'user/index',
- 'auth/login',
- 'auth/forgetPassword',
- 'auth/changePassword',
- 'auth/wxLogin',
- 'auth/setting',
- 'common/launch'
- ]
-
- // 移除路径中的斜杠和协议,只保留核心路径部分
- const normalizedPath = targetPath.replace(/^\/|^.*?:\/\//g, '')
-
- // 检查目标页面是否在不需要登录的列表中
- return !noLoginPages.some(page => normalizedPath.includes(page))
- }
- // 添加导航守卫,检查用户登录状态
- uni.addInterceptor('navigateTo', {
- invoke(e) {
- // 检查是否登录
- const userInfo = uni.getStorageSync('userInfo')
- // 目标页面路径
- const targetPath = e.url
-
- // 检查目标页面是否需要登录
- const needLogin = checkNeedLogin(targetPath)
-
- // 如果需要登录且未登录,则跳转到登录页面
- if (needLogin && !userInfo) {
- uni.navigateTo({
- url: '/pages/auth/login'
- })
- // 阻止原导航
- return false
- }
- return true
- }
- })
- // 同样拦截redirectTo
- uni.addInterceptor('redirectTo', {
- invoke(e) {
- const userInfo = uni.getStorageSync('userInfo')
- const targetPath = e.url
- const needLogin = checkNeedLogin(targetPath)
-
- if (needLogin && !userInfo) {
- uni.navigateTo({
- url: '/pages/auth/login'
- })
- return false
- }
- return true
- }
- })
- // 同样拦截reLaunch
- uni.addInterceptor('reLaunch', {
- invoke(e) {
- const userInfo = uni.getStorageSync('userInfo')
- const targetPath = e.url
- const needLogin = checkNeedLogin(targetPath)
-
- if (needLogin && !userInfo) {
- uni.navigateTo({
- url: '/pages/auth/login'
- })
- return false
- }
- return true
- }
- })
- // 同样拦截switchTab
- uni.addInterceptor('switchTab', {
- invoke(e) {
- const userInfo = uni.getStorageSync('userInfo')
- const targetPath = e.url
- const needLogin = checkNeedLogin(targetPath)
-
- if (needLogin && !userInfo) {
- uni.navigateTo({
- url: '/pages/auth/login'
- })
- return false
- }
- return true
- }
- })
-
-
|