he wei
2023-12-20 d216ef279fee1f19e835b44dd5e6c40af5bf17ac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import router from "./router";
import { Message } from "element-ui";
import Cookies from "js-cookie";
import NProgress from "nprogress"; // progress bar
import "nprogress/nprogress.css"; // progress bar style
 
NProgress.configure({ showSpinner: false }); // NProgress Configuration
const whiteList = ["/login"]; // no redirect whitelist
router.beforeEach(async (to, from, next) => {
  // determine whether the user has logged in
  const username = Cookies.get("uname");
  const uid = Cookies.get("uid");
 
  // TODO
  const isAdmin = uid && uid <= 1000;
 
  if (username) {
    if (to.path === "/login") {
      // if is logged in, redirect to the home page
      next({ path: "/" });
      NProgress.done();
    } else if (to.path.indexOf("/system/") > -1 ) {
      if (isAdmin) {
        next();
      } else {
        next({ path: "/" });
      }
    } else {
      next();
    }
  } else {
    /* has no token*/
    if (whiteList.indexOf(to.path) !== -1) {
      next();
    } else {
      next(`/login?redirect=${to.path}`);
      NProgress.done();
    }
  }
});
 
router.afterEach(() => {
  // finish progress bar
  NProgress.done();
});