 /*
  作者: moxianchengbao
  创建日期: 2023-9-03
  版本: 1.0
*/
import { createRouter, createWebHistory } from 'vue-router'
import request from '@/request/request'
const routes = [
  {
    path: '/login',
    name: '登陆',
    component: ()=>import('@/views/login/index'),
  },
  {
    path: '/',
    name: '仪表盘',
    component: ()=>import('@/views/index'),
    redirect: '/home',
    children:[
      {
        path: '/home',
        name: '首页',
        component: ()=>import('@/views/homeData/index'),
      }
    ]
  },
  {
    path: '/stockpredit',
    name: '股价预测',
    component: ()=>import('@/views/index'),
  },
  {
    path: '/factormining',
    name: '因子挖掘',
    component: ()=>import('@/views/index'),
  },
  {
    path: '/selectmodel',
    name: '模型选择',
    component: ()=>import('@/views/index'),
  },
  {
    path: '/mymodel',
    name: '我的模型',
    component: ()=>import('@/views/index'),
  },
  {
    path: '/sys',
    name: '权限设置',
    component: ()=>import('@/views/index'),
    children:[
      {
        path: '/auth',
        name: '权限管理',
        component: ()=>import('@/views/sys/auth/index'),
      },
      {
        path: '/role',
        name: '角色管理',
        component: ()=>import('@/views/sys/role/index'),
      },
      {
        path: '/mans',
        name: '人员管理',
        component: ()=>import('@/views/sys/mans/index'),
      }
    ],
  },
  {
    path: '/sett',
    name: '系统设置',
    component: ()=>import('@/views/index'),
    children:[
      {
        path: '/pwd',
        name: '修改密码',
        component: ()=>import('@/views/sys/updatePwd/index'),
      },
      {
        path: '/perPwd',
        name: '个人密码',
        component: ()=>import('@/views/sys/perPwd/index'),
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to,from,next)=>{
  if(to.path!='/login'){ //需要登录
    let token = localStorage.getItem('token')
    let logindto = {}
    logindto.token=token
    logindto.urlpath=to.path
    if(token!=null){ //登录过
      //验证token
      request.post("/user/checkToken",token).then(res=>{
        //成功
        if(res.statusCode == '200'){
          // next()
          if(to.path=='/' || to.path=='/home'){
            next()
          }else {
            request.post("/user/checkURL",logindto).then(res=>{
              if(res.statusCode == '200'){
                next()
              }else{
                next({path:'/login'})
              }
            })
          }

        }else{
          next({path:'/login'})
        }

      }).catch(()=>{
        next({path:'/login'})
      })

    }else{ //没有登录
      next({path:'/login'})
    }

  }else{ //不需要登录
    next()
  }
})

export default router
