import Vue from 'vue'
import VueRouter from 'vue-router'
// 加载条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

Vue.use(VueRouter);


const routes = [
    {
        path: '/',
        name: 'Index',
        component: () => import( '../views/Index.vue'),
        children: [
            {
                path: '/',
                name: 'Main',
                component: () => import('@/views/Main.vue'),
                meta: {
                    title: "首页",
                }
            },{
                path: '/phoneSign',
                name: 'PhoneSign',
                component: () => import('@/views/PhoneSign.vue'),
                meta: {
                    title: "WiFi信号记录",
                }
            },{
                path: '/wifiSign',
                name: 'WifiSign',
                component: () => import('@/views/WifiSign.vue'),
                meta: {
                    title: "WiFi信号记录",
                }
            },{
                path: '/GSign',
                name: 'GSign',
                component: () => import('@/views/GSign.vue'),
                meta: {
                    title: "0-6G扫描记录",
                }
            },{
                path: '/equip',
                name: 'Equip',
                component: () => import('@/views/Equip.vue'),
                meta: {
                    title: "设备管理",
                }
            },{
                path: '/qrcodemanage',
                name: 'qrcodemanage',
                component: () => import('@/views/Qrcodemanage.vue'),
                meta: {
                    title: "二维码管理",
                }
            },{
                path: '/yhgl',
                name: 'Yhgl',
                component: () => import('@/views/Yhgl.vue'),
                meta: {
                    title: "用户管理",
                }
            },{
                path: '/record',
                name: 'record',
                component: () => import('@/views/Record.vue'),
                meta: {
                    title: "监测记录",
                }
            },{
                path: '/mockPage',
                name: 'MockPage',
                component: () => import('@/views/MockPage.vue'),
                meta: {
                    title: "用户管理",
                }
            },{
                path: '/aboutUs',
                name: 'AboutUs',
                component: () => import('@/views/AboutUs.vue'),
                meta: {
                    title: "关于我们",
                }
            },{
                path: '/region',
                name: 'Region',
                component: () => import('@/views/Region.vue'),
                meta: {
                    title: "关于我们",
                }
            },{
                path: '/hardware',
                name: 'Hardware',
                component: () => import('@/views/Hardware.vue'),
                meta: {
                    title: "固件管理",
                }
            },{
                path: '/police',
                name: 'Police',
                component: () => import('@/views/Police.vue'),
                meta: {
                    title: "警务通管理",
                }
            },{
                path: '/pwd',
                name: 'Pwd',
                component: () => import('@/views/Pwd.vue'),
                meta: {
                    title: "密码修改",
                }
            },{
                path: '/basic',
                name: 'Basic',
                component: () => import('@/views/Basic.vue'),
                meta: {
                    title: "no use",
                }
            },{
                path: '/setting',
                name: 'Setting',
                component: () => import('@/views/Setting.vue'),
                meta: {
                    title: "基本设置",
                }
            },{
                path: '/createmeetingqrcode',
                name: 'Createmeetingqrcode',
                component: () => import('@/views/Createmeetingqrcode.vue'),
                meta: {
                    title: "二维码设置",
                }
            },
        ]
    }, {
        path: '/login',
        name: 'Login',
        component: () => import( '../views/Login.vue')
    },{
        path: '/wifiSign',
        name: 'wifiSign',
        component: () => import( '../components/WifiSign.vue')
    },{
        path: '/equipInfo',
        name: 'equipInfo',
        component: () => import( '../components/EquipInfo.vue')
    },{
        path: '/poster',
        name: 'Poster',
        component: () => import( '../views/Poster.vue')
    }, {
        path: '*',
        name: '404',
        component: () => import( '../views/404.vue')
    },
];

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
};

const router = new VueRouter({
    // mode: 'history',
    mode: 'hash',
    base: process.env.BASE_URL,
    routes
});

// 路由守卫
router.beforeEach((to,from,next)=>{
    NProgress.start();
    const isLogin = localStorage.token? true : false;

    if(to.path == '/login' || to.path == '/register'){//'login'和'register'相当于是路由白名单
        next();
    }else{
        //如果token存在，就正常跳转，如果不存在，则说明未登陆，则跳转到'login'
        isLogin? next() : next("/login");
    }
});

router.afterEach(() => {
    NProgress.done()
});

export default router
