/*
 * @Author: lixg lixg@dongwu-inc.com
 * @Date: 2023-02-08 09:50:10
 * @LastEditors: lixg lixg@dongwu-inc.com
 * @LastEditTime: 2023-03-08 13:40:18
 * @FilePath: /stu_h5/src/router/index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router';
import routesConfig from './config';
import { getCookie } from "@/api/utils";
// console.log('routesConfig', routesConfig)
const routes = [
    {
        path: '/',
        name: '首页',
        redirect: routesConfig[0].path
    },
    ...routesConfig
]
const router = createRouter({
    history: createWebHistory(import.meta.env.VITE_BASE),
    routes
})
router.beforeEach((to, from, next) => {
    console.log('toooooooo', to)
    console.log('encodeURI', encodeURI(to.fullPath),)
    console.log('encodeURIComponent', encodeURIComponent(window.location.protocol + import.meta.env.VITE_BOE_BASE_URL + import.meta.env.VITE_BASE + to.fullPath))
    if (!getCookie('token')) {
        if (import.meta.env.MODE === "development" || import.meta.env.MODE === "test") {
            to.path.includes('/login') ? next() : next({ path: '/login', query: { returnUrl: to.fullPath } })
        } else {
            window.location.href = window.location.protocol + import.meta.env.VITE_BASE_LOGIN_URL + encodeURIComponent(window.location.protocol + import.meta.env.VITE_BOE_BASE_URL + import.meta.env.VITE_BASE + to.fullPath)
        }
    }
    next()
})
export default router