import { createRouter, createWebHashHistory, 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'

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