/*
 * @Author: jixiang
 * @Date: 2021-07-07 16:47:54
 * @Description: 权限
 * @FilePath: /suqian_pc_new/src/permission.ts
 * One World One Drefeel(56580223@qq.com)
 */
import router from './router';
import { configure, start, done } from 'nprogress';
import { RouteRecordRaw } from 'vue-router';
import { getToken, setToken } from './utils/cookie';
import { decode, encode, useLocal } from './utils/tools';
import useLayoutStore from './store';

configure({ showSpinner: false });

const loginRoutePath = '/toCasLogin';
const loginRoutePath2 = '/casLogin';
const defaultRoutePath = '/bookList';

router.beforeEach(async (to, from) => {
  start();
  // 判断当前是否在登陆页面
  if (
    to.path.toLocaleLowerCase() === loginRoutePath.toLocaleLowerCase() ||
    to.path.toLocaleLowerCase() === loginRoutePath2.toLocaleLowerCase()
  ) {
    done();
    if (getToken())
      return typeof to.query.from === 'string' ? decode(to.query.from) : defaultRoutePath;
    return;
  }
  console.log(to.path.toLocaleLowerCase(), 'ddddd');
  // debugger;
  //判断是否是首页
  console.log('eeee', to.path.toLocaleLowerCase() == defaultRoutePath.toLocaleLowerCase());
  if (to.path.toLocaleLowerCase() == defaultRoutePath.toLocaleLowerCase()) {
    done();
    return;
  }
  console.log(getToken(), '是否登录');

  // 判断是否登录
  if (!getToken()) {
    router.push(loginRoutePath);
    // window.location.href = import.meta.env.VITE_APP_BOOK_STORE_URL as string;
    // return loginRoutePath + (to.fullPath ? `?from=${encode(to.fullPath)}` : '');
  }
  // 前端检查token是否失效
  // useLocal('token').then((d) => setToken(d.ACCESS_TOKEN));
  // .catch(() => logout());
});

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