import router from '@/router'
import store from '@/store'
// import {
//     Notify
// } from 'vant'
import {
    getToken
} from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'

const whiteList = ['/', '/validation','/hisEnter'] // 白名单列表 , 

router.beforeEach((to, from, next) => {

    // 设置页面标题
    document.title = getPageTitle(to.meta.title)
    const hasToken = getToken()
    if (hasToken) {
        if (from.path === '/') {
            // 已经登录，跳转到首页
            next({
                path: '/index/todaypatients'
            })
        } else {
            // 获取用户信息
            const hasGetUserInfo = localStorage.getItem('TOKEN') // 判断是否存在token
            if (hasGetUserInfo) {
                next()
            } else {
                try {
                    next()
                } catch (error) {
                    // 清除用户信息，退出登录，跳转登录页
                    store.commit('user/LOGOUT')
                    // Notify.error(error || 'Has Error')
                    this.$notify({
                        title: '标题名称',
                        message: error || 'Has Error'
                    });
                    next(`/login?redirect=${to.path}`)
                }
            }
        }
    } else {
        /* has no token */
        if (whiteList.indexOf(to.path) !== -1) {
            // 白名单中，无需验证
            next()
        } else {
            next(`/?redirect=${to.path}`)
        }
    }
})