/**
 * ---------------- 定义导航守卫 ----------------
 */
import router from "./index";
import NProgress from "nprogress";
import 'nprogress/nprogress.css';
import {setTitle} from "./router-util";
import {useAppStore} from "../store/app";
import {isStaticRoute} from "./routes-static";
import initAdmin from "../init/init-admin";

// 进度条配置：隐藏右上角进度环
NProgress.configure({ showSpinner: false });

// 路由初始化完毕钩子
router.isReady().then(() => {
    // 首次访问的不是登录界面，就去初始化 
    if(router.currentRoute.value.path !== '/login') {
        initAdmin();
    }
});

// 路由加载前
router.beforeEach(async (to, from, next) => {
    // 进度条和loading 
    NProgress.start();
    useAppStore().showOsLoading();
    
    // 设置标题 
    setTitle(to);
    
    // 鉴权 
    const menu = to.meta.menu;
    if(isStaticRoute(to) || menu === undefined || menu.show === 'yes' || menu.auth === false || sa.isAuth(menu.id)) {
        next();
    } else {
        next('/403');
    }
})

// 路由加载后
router.afterEach(() => {
    // 关闭进度条
    NProgress.done();
    // 关闭全局loading 
    useAppStore().hideOsLoading2();
    // setTimeout(function (){
    //     useAppStore().isOsLoading = false;
    // }, 10)
})

