这里是文章模块栏目内容页
uniapp在app.vue中onlaunch加入拦截器

对于错误的入口 页面,希望做提示和跳转;

我们可以在 App.vue中 onlaunch方法加入 处理逻辑;

例如下面的代码:

判断用户权限:

import { pages, tabBar } from '@/pages.json';
import { useStoreUser } from '@/store/user.js';

/**
 * router路由拦截器
 */
export function routeInterceptor() {
  const list = ['navigateTo', 'redirectTo', 'reLaunch', 'switchTab'];
  list.forEach((item) => {
    uni.addInterceptor(item, {
      invoke(args) {
        const fullPath = args.url;
        // console.log('router-invoke', args, fullPath);
        return routeAuth(fullPath);
      },
      success(args) {
        // console.log("router-success", args);
      },
      fail(err) {
        // console.log("router-fail", err);
      },
      complete(res) {
        // console.log("router-complete", res);
      }
    });
  });

  // [TODO] 第一个页面加载处理
  setTimeout(() => {
    const currentPath = getCurrentFullPath();
    if (currentPath) routeAuth(currentPath);
  }, 500);
}

/**
 * 路由权限判断
 * @param {string} fullPath 路由全路径
 * @returns
 */
export function routeAuth(fullPath) {
  let res = true;
  let url = fullPath.split('?')[0];
  let page = pages.find((item) => '/' + item.path == url);
  if (page?.auth) {
    const user = useStoreUser();
    if (user.getToken == null) {
      goLogin();
      res = false;
    }
  }
  return res;
}

/**
 * 是否是TabBar页面
 */
export function isTabBar(url) {
  const tabBarPages = tabBar.list;
  const res = !!tabBarPages.find((item) => url.indexOf(item.pagePath) != -1);
  return res;
}

/**
 * 关闭当前页面,跳转到应用内的某个页面
 * @param {string} url 跳转路由
 */
export function routeRedirect(url) {
  // tabBar 跳转
  if (isTabBar(url)) {
    uni.switchTab({
      url
    });
  } else {
    uni.redirectTo({
      url
    });
  }
}

/**
 * 跳转路由(自动处理tabBar页面跳转)
 */
export function routeTo(url) {
  // tabBar 跳转
  if (isTabBar(url)) {
    uni.switchTab({
      url
    });
  } else {
    uni.navigateTo({
      url
    });
  }
}

/**
 * 返回页面
 */
export function routeBack() {
  const pages = getCurrentPages();
  const user = useStoreUser();
  console.log('routeBack', pages, user.getToken);
  // 如果从登录页登录成功就不能返回登录页
  if (pages.length > 1 && (pages[pages.length - 2].route != 'pages/login/login' || !user.getToken)) {
    uni.navigateBack();
  } else {
    uni.switchTab({
      url: '/pages/index/index'
    });
  }
}

/**
 * 跳转登录
 * @param {string} reUrl 登录成功返回url
 */
export function goLogin(reUrl) {
  let login = '/pages/login/login';
  let currentFullPath = getCurrentFullPath();
  // console.log('跳转登录reUrl:', reUrl);
  // 已经在登录页,不做处理
  if (currentFullPath.indexOf(login) == -1 && reUrl != login) {
    let url = login;
    if (reUrl) url += '?reUrl=' + encodeURIComponent(reUrl);
    else url += '?reUrl=' + currentFullPath;

    setTimeout(() => {
      uni.navigateTo({
        url
      });
    });
  }
}

export function getCurrentFullPath() {
  let routes = getCurrentPages();
  let fullPath = '';
  if (routes.length) fullPath = routes[routes.length - 1].$page?.fullPath;
  console.log('当前路由', fullPath);
  return fullPath;
}


更多栏目
相关内容