import Vue from "vue";
import VueRouter from "vue-router";
import platform from "./platform";
import region from "./region";
import school from "./school";

Vue.use(VueRouter);
function queryStorage(location) {
  if (location instanceof Object && location.path) {
    let _location = {};
    _location.path = location.path;
    let _queryStorage = sessionStorage.getItem("_queryStorage");
    _queryStorage = _queryStorage ? JSON.parse(_queryStorage) : {};
    if (location.query) {
      _queryStorage[location.path] = location.query;
      _location.query = location.query;
    } else {
      if (
        _queryStorage[location.path] &&
        window.location.href.includes(location.path)
      ) {
        _location.query = _queryStorage[location.path];
      }
    }
    sessionStorage.setItem("_queryStorage", JSON.stringify(_queryStorage));
    return _location;
  }
  return location;
}
const routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  let newLocation = queryStorage(location);
  return routerPush.call(this, newLocation).catch((error) => error);
};
const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
  let newLocation = queryStorage(location);
  return originalReplace.call(this, newLocation).catch((err) => err);
};
const createRouter = () =>
  new VueRouter({
    scrollBehavior: () => ({
      y: 0
    }),
    routes: [
      {
        path: "/login",
        name: "login",
        component: () => import("@/views/login/login.vue"),
        meta: {
          // 缓存
          keepAlive: true,
          // 需要登录权限
          isAuth: false,
          // 路由名称
          title: "登录"
        }
      },
      {
        path: "/",
        component: () => import("@/views/layout.vue"),
        name: "layout",
        redirect: {
          path: "/platform"
        },
        children: [platform, region, school]
      }
    ]
  });
const router = createRouter();
/**
 * 路由守卫(拦截)
 */
router.beforeEach((to, from, next) => {
  if (!to.meta.isAuth) {
    next();
  } else {
    /**
     * xh_control_userInfo 登录用户信息
     * {content, datetime}
     */
    let _userInfo = localStorage.getItem("xh_control_userInfo");
    if (_userInfo) {
      _userInfo = JSON.parse(_userInfo) || {};
      if (_userInfo.content) {
        const xh_control_userInfo = _userInfo.content;
        const datetime = _userInfo.datetime;
        const nowtime = new Date().getTime();
        let diff = nowtime - datetime;
        if (xh_control_userInfo && diff < 1000 * 60 * 60 * 24) {
          _userInfo.datetime = nowtime;
          localStorage.setItem(
            "xh_control_userInfo",
            JSON.stringify(_userInfo)
          );
          next();
        } else {
          console.log("文件名：router index.vue，退出原因：超过24小时未登录");
          next({
            path: "/login",
            query: to.query
          });
        }
      } else {
        console.log("文件名：router index.vue，退出原因：userInfo不存在1");
        next({
          path: "/login",
          query: to.query
        });
      }
    } else {
      console.log("文件名：router index.vue，退出原因：userInfo不存在2");
      next({
        path: "/login",
        query: to.query
      });
    }
  }
});
export default router;
