import router from './router' // vue官方的路由
import store from './store' // vue状态管理
import { Message, MessageBox } from 'element-ui' // 引入Message、MessageBox
import NProgress from 'nprogress' // 进度条
import 'nprogress/nprogress.css' // 进度条样式
import getPageTitle from '@/utils/get-page-title' // 获取页面标题
import { request } from '@/utils/request'
import { setToken } from '@/utils/token'

NProgress.configure({ showSpinner: false }) // NProgress配置

const whiteList = ['', '/', '/indexLayout', '/indexLayout/', 
    '/indexLayout/home', '/indexLayout/data', '/indexLayout/cae', 
    '/indexLayout/xitech', '/indexLayout/guanyuwe', 
    '/indexLayout/workbench', 
    '/indexLayout/design', 
    '/indexLayout/megagame',
    '/matchLayout',
    '/megagame',
    '/megagame/xiangqin',
    '/megagame/topic',
    '/login', 
    '/register', 
    '/forget', 
    '/protocol', 
    '/fail404',
    '/detailmw',
    '/detailaw',
    '/detailbyy',
    '/detailbzs1',
    '/detailbzs2',
    '/detailbzs3',
    '/detailbzs4',
] // 免登录的白名单

// 前置路由守卫
router.beforeEach(async(to, from, next) => {
    NProgress.start() // 开启进度条
    document.title = getPageTitle(to.meta.title) // 设置页面标题
    document.body.style.overflow = 'auto';
    let loginStatus = store.getters.loginStatus // 确定用户是否已登录
    if (to.query.authCode) {
        const params = {
            transCode: 'A00007',
            authCode: to.query.authCode,
        }
        request(params)
            .then((res) => {
                store.dispatch('user/changeState', {
                    key: 'token',
                    value: res.authUserToken,
                })
                store.dispatch('user/changeState', {
                    key: 'name',
                    value: res.authUserName,
                })
                store.dispatch('user/changeState', {
                    key: 'userId',
                    value: res.authUserId,
                })
                store.dispatch('user/changeState', {
                    key: 'userType',
                    value: res.userType,
                })
                store.dispatch('user/changeState', {
                    key: 'realStatus',
                    value: res.authenticationState,
                })
                store.dispatch('user/changeState', {
                    key: 'cfdUrl',
                    value: res.cfdUrl,
                })
                store.dispatch('user/changeState', {
                    key: 'loginStatus',
                    value: true,
                })
                setToken(res.authUserToken)
                delete to.query.authCode
                next({ path: to.path, query: to.query }) // 进入
                NProgress.done() // 关闭进度条
            })
            .catch((err) => {
                console.log("err",err );
                Message.error('加载失败，请重新进入')
                NProgress.done() // 关闭进度条
            })
    } else {
        if (loginStatus || whiteList.indexOf(to.path) != -1) {
            next() // 直接进入
            NProgress.done() // 关闭进度
        } else {
            MessageBox.confirm('你还未登录，请登录后再操作, 是否登录?', '提示', {
                    confirmButtonText: '登录',
                    cancelButtonText: '取消',
                    center: true,
                    closeOnClickModal: false,
                    type: 'warning'
                })
                .then(() => {
                    // next(`/login?redirect=${to.path}`) // 在没有访问权限的其他页面将重定向到登录页
                    next(`/indexLayout/home?type=reLogin`) // 在没有访问权限的其他页面将重定向到登录页
                    NProgress.done() // 关闭进度条
                })
                .catch(() => {
                    NProgress.done() // 关闭进度条
                    Message.info('已取消')
                })
        }
    }
})

// 后置路由守卫
router.afterEach(() => {
    NProgress.done() // 关闭进度条
})