import router from "@/router";
import { usePermissionStoreHook } from "@/store/modules/permission";
import { number } from "echarts";

import NProgress from "nprogress";
import "nprogress/nprogress.css";
NProgress.configure({ showSpinner: false }); // 进度条

const permissionStore = usePermissionStoreHook();

// 白名单路由
const whiteList = ["/login", "/signSucceed",'/createVideo','/moveImg'];

router.beforeEach(async (to, from, next) => {
  NProgress.start();
  const AccessToken = localStorage.getItem("AccessToken");
  // 已登录
  if (AccessToken) {
    // 重置密码时可以去往login页面
    if ((to.params.type as string) == "4") {
      next();
    }
    if (to.query.resetPwd) {
      next();
    }
    if (to.path === "/login") {
      next({ path: "/createVideo" });
    } else {
      next();
    }
    // 未登录
  } else {
    // 未登录可以访问白名单页面
    if (whiteList.indexOf(to.path) !== -1) {
      next();
    } else {
      // 不在白名单内，跳转登录页
      next({ path: "/login" });
    }
  }
});

router.afterEach(() => {
  NProgress.done();
});
