import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import {getToken} from  "@/utils/token"
import Login from '@/views/login/index.vue'
import Home from '@/views/home/index.vue'
import Error from '@/views/404/index.vue'
import { onGetRouter } from '@/utils/userInfo'
console.log(onGetRouter(),'getRouter');

const routes: Array<RouteRecordRaw> = [
  {
    path:'/',
    redirect:'/login'
  },  
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    redirect:'/home/paybind',
    children:[
      {
        path: '/home/payconfig',
        name: '支付配置',
        component:  ()=>import('@/views/pay-config/index.vue'),
        children:[
          {
            path: '/home/paybind',
            component:  ()=>import('@/views/pay-bind/index.vue')
          },
        ]
      },
      {
        path: '/home/goodscabinet',
        name: '商品橱柜',
        component:  ()=>import('@/views/goods-cabinet/index.vue')
      },
      {
        path: '/home/paylist',
        name: '支付链接',
        component:  ()=>import('@/views/pay-list/index.vue')
      },
    ]
  },
  {
    path: '/404',
    name: '404',
    component: Error
  }
]
const router = createRouter({
  history: createWebHistory(),
  routes
})
router.beforeEach((to,from,next)=>{
  const token:any = getToken
  if(token){
    next()
  }else{
    router.push('/login')
  }
})
export default router
