/*
 * @Author: zuolingling zuolingling@jsnangao.com
 * @Date: 2023-08-10 15:29:48
 * @LastEditors: zuolingling zuolingling@jsnangao.com
 * @LastEditTime: 2023-08-11 14:56:30
 * @FilePath: \vue3portal\src\router.js
 * @Description: 
 * 
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved. 
 */

import { createRouter, createWebHistory,createWebHashHistory,useRoute } from 'vue-router'
import { ElMessage } from 'element-plus';
import{ defineAsyncComponent } from"vue"
import store from './store'

const router = createRouter({
  // history: createWebHistory(),
  history: createWebHashHistory(),
  routes :[
    {
      path: "/",
      name: "layout",
      redirect:"/home",
    },
    {
      path: "/home",
      name: "首页",
      component:()=>import("@/pages/layouts/index.vue"),
    }
  ]
});

/**
 * 全局前置路由守卫，每一次路由跳转前都进入这个 beforeEach 函数
 */
router.beforeEach((to, from, next) => {
  if (to.path == '/user/login' || to.path == '/user/register') {
      // 登录或者注册才可以往下进行
      next();
  } else {
      // 获取 token
      const token = localStorage.getItem('TOKENES'); 
      // token 不存在
      if (token === null || token === '') {
          ElMessage.error('您还没有登录，请先登录');
          // next('/');
      } else {
          next();
      }
  }
});



export default router;

