import routes from 'virtual:generated-pages'
import { createRouter, createWebHashHistory } from 'vue-router'
import { user } from '~/store/user'
import { urlParams } from '~/utils/utils'
import { showFailToast } from 'vant'

console.log('routes : ', routes)
const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes,
})

router.beforeEach((to, from) => {
  if (to.path === "/" || to.path === "/home") {
    return { path: '/home/caiji' }
  }
  // if (user.value?.token)
  return true
  // else if (!user.value?.token && to.name !== 'login' && to.name !== 'forgetPsw' && to.path !== '/forgetPsw/newPsw')
  //   return { name: 'login' }
})

export default router



const token = urlParams?.token ?? sessionStorage.getItem('token');
if (token) {
  sessionStorage.setItem('token', token)
  const { code, data } = await request({ api: 'openapi', url: '/user/main/detail', data: { token } });
  if (code == 1) {
    user.value = { token, ...data.one_info }
  }
} else {
  showFailToast('登录信息已过期，请重新扫码使用')
}
