/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2022-08-02 18:10:30
 * @LastEditors: wenjie 1454560336@qq.com
 * @LastEditTime: 2024-02-28 15:30:43
 * @FilePath: \admin-manage\src\permission.js
 * @Description: 
 * 
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved. 
 */
import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'
import Layout from '@/layout'
import {getType} from '@/api/common'
// import { getMenu } from '@/api/menu'
NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login'] // no redirect whitelist

router.beforeEach((to, from, next) => {
  // start progress bar
  NProgress.start()

  // set page title
  document.title = getPageTitle(to.meta.title)
  // console.log(constantRoutes);
  // determine whether the user has logged in
  const hasToken = getToken()
  if (hasToken ) {
    // 存储商品类型字段
    if(!sessionStorage.getItem('goodsTypeObj')){
      getType().then(res=>{
        if(res.state == 'Success'){
          sessionStorage.setItem('goodsTypeObj',JSON.stringify(res.content))
        }
      })
    }
    if (to.path === '/login') {
      // if is logged in, redirect to the home page
      next({ path: '/' })
      NProgress.done()
    } else {
      next()
      NProgress.done()
      // const hasGetUserInfo = store.getters.menu
      // if (store.getters.menus) {
      //   next()
      // } else {
      //   try {
      //     // 获取用户信息
      //     store.dispatch('user/getMenu').then(res=>{
      //       // res.push({ path: '*', redirect: '/404', hidden: true })

      //       router.addRoutes(res)
            
      //       next({ ...to, replace: true })
      //     })
      //     // let asyncRouterMap = asyncRouter


      //   } catch (error) {
      //     // remove token and go to login page to re-login
      //     // await store.dispatch('user/resetToken')
      //     // Message.error(error || 'Has Error')
      //     // next(`/login?redirect=${to.path}`)
      //     // NProgress.done()
      //   }
      // }

    }
  } else {
    /* has no token*/
    if (whiteList.indexOf(to.path) !== -1) {
      // if (whiteList.indexOf(to.path) !== 1000) {
      // in the free login whitelist, go directly
      next()

    } else {
      // other pages that do not have permission to access are redirected to the login page.
      next(`/login?redirect=${to.path}`)
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
 