/*
 * @Author: jixiang
 * @Date: 2021-07-07 16:47:54
 * @Description: 权限
 * @FilePath: /pc/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';
import { useStore } from 'vuex';
const store = useStore();
configure({ showSpinner: false });

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

router.beforeEach(async (to, from) => {
  start();
  //p判断是否是模版地址

  if (to.path.toLocaleLowerCase() === loginRoutePath.toLocaleLowerCase()) {
    done();
    if (getToken('fighter-auth-token'))
      return typeof to.query.from === 'string' ? decode(to.query.from) : defaultRoutePath;
    return;
  }
  //判断是否是首页
  if (to.path.toLocaleLowerCase() === defaultRoutePath) {
    done();
    return;
  }
  // 判断是否登录
  // if (!getToken() && !store.state.user?.userInfo) {
  //   router.push(loginRoutePath);
  // }
  // console.log(getToken(), store.state.user?.userInfo, '是否登录');
  // //判断是否是首页
  // console.log('eeee', to.path.toLocaleLowerCase() == defaultRoutePath.toLocaleLowerCase());
  // if (to.path.toLocaleLowerCase() == defaultRoutePath.toLocaleLowerCase()) {
  //   done();
  //   return;
  // }

  // 前端检查token是否失效
  // useLocal('token').then((d) => setToken(d.ACCESS_TOKEN));
  // .catch(() => logout());
});

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