
// import router from './router'
// import store from './store'
// import { Message } from 'element-ui'
// import NProgress from 'nprogress' // progress bar
// import 'nprogress/nprogress.css' // progress bar style
// import { getToken } from '@/utils/auth' // get token from cookie
// import getPageTitle from '@/utils/get-page-title'

//const { default: router } = require("./router");

// NProgress.configure({ showSpinner: false }) // NProgress Configuration

// const whiteList = ['/login'] // no redirect whitelist

// router.beforeEach(async (to, from, next) => {
//     // start progress bar
//     NProgress.start()

//     // set page title
//     document.title = getPageTitle(to.meta.title)

//     // determine whether the user has logged in
//     const hasToken = getToken()

//     if (hasToken) {
//         if (to.path === '/login') {
//             // if is logged in, redirect to the home page
//             next({ path: '/' })
//             NProgress.done()
//         } else {
//             const hasGetUserInfo = store.getters.name
//             if (hasGetUserInfo) {
//                 next()
//             } else {
//                 try {
//                     // get user info
//                     await store.dispatch('user/getInfo')

//                     next()
//                 } catch (error) {
//                     // remove token and go to login page to re-login
//                     await store.dispatch('user/resetToken')
//                     Message.error(error || 'Has Error')
//                     next(`/login?redirect=${to.path}`)
//                     NProgress.done()
//                 }
//             }
//         }
//     } else {
//         /* has no token*/

//         if (whiteList.indexOf(to.path) !== -1) {
//             // in the free login whitelist, go directly
//             next()
//         } else {
//             // other pages that do not have permission to access are redirected to the login page.
//             next(`/login?redirect=${to.path}`)
//             NProgress.done()
//         }
//     }
// })

// router.afterEach(() => {
//     // finish progress bar
//     NProgress.done()
// })


import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import router from "@/router"
import store from "@/store"
import { syncRoutes } from './router'

//白名单页面数组
const whiteList = ["/login", "/register", "/404"]

//注册路由前置守卫
//to:目的页
//from:来源页
//next:是否放行
router.beforeEach(async (to, from, next) => {
    NProgress.start()
    const token = store.state.user.token
    if (token) {//已登录
        //获取用户访问权限，包括页面和按钮访问权限
        if (store.state.user.menuPermission.length < 1) {
            try {
                const res = await store.dispatch("user/getOperatorPermission")
                //根据权限获取可访问的菜单项
                let menuList = res.menus
                let tmpRoutes = []
                let newRoutes = []
                let isExist = false

                for (let i = 0; i < syncRoutes.length; i++) {
                    let oneRoute = syncRoutes[i]
                    isExist = false
                    tmpRoutes = []
                    for (let j = 0; j < oneRoute.children.length; j++) {
                        let onePage = oneRoute.children[j]
                        for (let k = 0; k < menuList.length; k++) {
                            let oneMenu = menuList[k]
                            if (onePage.name === oneMenu.flag) {
                                tmpRoutes.push(onePage)
                                isExist = true
                                break
                            }
                        }
                    }

                    if (isExist) {
                        oneRoute.children = tmpRoutes
                        newRoutes.push(oneRoute)
                    }
                }

                let item404 = { path: '*', redirect: '/404', hidden: true }
                newRoutes.push(item404)

                router.addRoutes(newRoutes)
                store.commit("menu/updateMenuList", newRoutes)

                next({//解决刷新页面出现白屏的bug
                    ...to,
                    replace: true
                })
            } catch (e) {
                next("/login")
                NProgress.done()
            }
        }

        if (to.path === "/login") {//已登录不能去登录页
            NProgress.done()
            next("/")
        } else {
            next()
        }
    } else {//未登录
        if (whiteList.includes(to.path)) {//要去的页面在白名单内
            next()
        } else {//只能去登录
            next("/login")
            NProgress.done()
        }
    }
})

//注册后置路由守卫
//路由跳转完成后执行
router.afterEach(() => {
    NProgress.done()
})
