import Vue from "vue";
import Router from "vue-router";
Vue.use(Router);

const index = "opay";

// 通用路由: 全局
import universal from "./modules/universal";
const routes = [...universal];

// 项目路由: modules 内
const modulesFiles = require.context(
  "@/modules",
  true,
  /\/router\/([a-zA-Z0-9_-])+\.js$/
);
const modules = modulesFiles.keys().reduce((route, path) => {
  const value = modulesFiles(path).default;
  route.push(...value);
  return route;
}, []);

const router = new Router({
  routes: [
    {
      path: "/",
      redirect: index
    },
    {
      path: "*",
      redirect: "/not-found"
    },
    ...routes,
    ...modules
  ]
});

export default router;

import progress from "@/service/progress";

// 重定向首地址
export function redirectIndex (message) {
  // 避免死循坏
  if (router.history.current.path === index) return;
  else if (message) progress.showErrorMessage(message + ", 尝试重新登录"); // 若当前路由非首页, 提示登录失效信息, 跳转首页
  return router.replace(index);
}
