index.js 3.4 KB
Newer Older
yma16's avatar
yma16 已提交
1
import Vue from "vue";
yma16's avatar
yma16 已提交
2

yma16's avatar
yma16 已提交
3 4 5 6 7
import Nprogress from "nprogress";
import "nprogress/nprogress.css";
import { getToken } from "@/utils/auth";
import { isEmpty } from "@/utils";
import VueRouter from "vue-router";
yma16's avatar
yma16 已提交
8
// import Router from 'vue-router'
yma16's avatar
yma16 已提交
9
import store from "@/store";
yma16's avatar
yma16 已提交
10
// store
yma16's avatar
yma16 已提交
11 12 13 14 15 16 17 18
import Article from "@/components/Article";
import Login from "@/components/Login";
import Register from "@/components/Register";
import Onlinewebsocket from "@/components/Onlinewebsocket";
import Home from "@/components/Home";
import Bilicom from "@/components/Bilicom";
import Mavoneditor from "@/components/Mavoneditor";
import Loading from "@/components/Loading";
yma16's avatar
yma16 已提交
19

yma16's avatar
yma16 已提交
20 21
import GrilShow from "@/components/GrilShow";
import Csslearn from "@/components/Csslearn";
yma16's avatar
yma16 已提交
22

yma16's avatar
yma16 已提交
23
Vue.use(VueRouter);
yma16's avatar
yma16 已提交
24 25
const defaultRoutes = [
  {
yma16's avatar
yma16 已提交
26 27 28
    path: "/",
    name: "Article",
    component: Article,
yma16's avatar
yma16 已提交
29 30
  },
  {
yma16's avatar
yma16 已提交
31 32 33
    path: "/login",
    name: "Login",
    component: Login,
yma16's avatar
yma16 已提交
34 35
  },
  {
yma16's avatar
yma16 已提交
36 37 38
    path: "/register",
    name: "Register",
    component: Register,
yma16's avatar
yma16 已提交
39 40
  },
  {
yma16's avatar
yma16 已提交
41 42 43
    path: "/home",
    name: "Home",
    component: Home,
yma16's avatar
yma16 已提交
44 45
  },
  {
yma16's avatar
yma16 已提交
46 47 48
    path: "/onlinewebsocket",
    name: "Onlinewebsocket",
    component: Onlinewebsocket,
yma16's avatar
yma16 已提交
49 50
  },
  {
yma16's avatar
yma16 已提交
51 52 53
    path: "/bilicom",
    name: "Bilicom",
    component: Bilicom,
yma16's avatar
yma16 已提交
54 55
  },
  {
yma16's avatar
yma16 已提交
56 57 58
    path: "/mavoneditor",
    name: "Mavoneditor",
    component: Mavoneditor,
yma16's avatar
yma16 已提交
59 60
  },
  {
yma16's avatar
yma16 已提交
61 62 63
    path: "/loading",
    name: "loading",
    component: Loading,
yma16's avatar
yma16 已提交
64 65
  },
  {
yma16's avatar
yma16 已提交
66 67 68
    path: "/gril",
    name: "grilshow",
    component: GrilShow,
yma16's avatar
yma16 已提交
69 70
  },
  {
yma16's avatar
yma16 已提交
71 72 73 74
    path: "/css",
    name: "css",
    component: Csslearn,
  },
yma16's avatar
yma16 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  // {
  //     path: '/register',
  //     name: 'Register',
  //     component: () =>
  //         import ('../components/Register.vue')
  // },
  // {
  //     path: '/home',
  //     name: 'Home',
  //     component: () =>
  //         import ('../components/Home.vue')
  // },
  // {
  //     path: '/onlinewebsocket',
  //     name: 'Onlinewebsocket',
  //     component: () =>
  //         import ('../components/Onlinewebsocket.vue')
  // },
  // {
  //     path: '/mavoneditor',
  //     name: 'Mavoneditor',
  //     component: () =>
  //         import ('../components/Mavoneditor.vue')
  // },
  // {
  //     path: '/bilicom',
  //     name: 'Bilicom',
  //     component: () =>
  //         import ('../components/Bilicom.vue')
  // }
yma16's avatar
yma16 已提交
105
];
yma16's avatar
yma16 已提交
106

yma16's avatar
yma16 已提交
107
let routes = [
yma16's avatar
yma16 已提交
108
  ...defaultRoutes,
yma16's avatar
yma16 已提交
109 110 111
  // ...modulesRoutes,
  // ...errorRoutes,
  // ...dashboardRouters
yma16's avatar
yma16 已提交
112 113 114 115 116
];
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
yma16's avatar
yma16 已提交
117
const router = new VueRouter({
yma16's avatar
yma16 已提交
118 119
  routes,
});
yma16's avatar
yma16 已提交
120

yma16's avatar
yma16 已提交
121
const whiteList = ["/login", "/register"];
yma16's avatar
yma16 已提交
122 123

router.beforeEach(async (to, from, next) => {
yma16's avatar
yma16 已提交
124 125 126 127 128 129 130
  next();
  let yma16siteUserInfo = localStorage.getItem("yma16siteUserInfo")
    ? JSON.parse(localStorage.getItem("yma16siteUserInfo"))
    : "";
  let name = yma16siteUserInfo.username,
    pwd = yma16siteUserInfo.password;

yma16's avatar
yma16 已提交
131 132
  let hasToken = {
    name: name,
yma16's avatar
yma16 已提交
133 134 135
    password: pwd,
  };
  console.log("localStorage", hasToken);
yma16's avatar
yma16 已提交
136 137 138
  if (hasToken.name && hasToken.password) {
    if (isEmpty(store.state.user.userInfo)) {
      // 空的 modules下的user
yma16's avatar
yma16 已提交
139
      console.log("路由的登录认证");
yma16's avatar
yma16 已提交
140
      // 用户自主登录
yma16's avatar
yma16 已提交
141 142
      await store.dispatch("user/loginUserInfo", hasToken);
      next();
yma16's avatar
yma16 已提交
143
    } else {
yma16's avatar
yma16 已提交
144
      next();
yma16's avatar
yma16 已提交
145
    }
yma16's avatar
yma16 已提交
146
  } else {
yma16's avatar
yma16 已提交
147 148
    // next({ path: "/login" }); //去登录
    next();
yma16's avatar
yma16 已提交
149
  }
yma16's avatar
yma16 已提交
150
});
yma16's avatar
yma16 已提交
151 152

router.afterEach((to, from) => {
yma16's avatar
yma16 已提交
153 154
  Nprogress.done();
});
yma16's avatar
yma16 已提交
155

yma16's avatar
yma16 已提交
156
export default router;