import { createRouter,createWebHashHistory } from 'vue-router'
import { useUserStore } from '@/store'
import { basicRoutes as routes } from './routes'

const router = createRouter({
    history:createWebHashHistory(),
    routes,
    scrollBehavior: () => ({ left: 0, top: 0 }),
})

let flag = 0
router.beforeEach(async (to, from, next) => {
    if(to.path == '/login' || to.path == "/register" || to.path == "/wxlogin") {
        next()
    } else {
        const userStore = useUserStore()
        if(userStore.isLoggedIn) {
            if(flag === 0 && to.matched.length == 0) {
                flag++
                router.push(to.path)
            } else if (flag !== 0 && to.matched.length == 0) {
                next('/')
            } else {
                next()
            }
        } else {
            next('/login');
        }
    }
})

export default router