import router from './router'
import store from './store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { getToken } from './assets/js/utils/auth'
import { rewriteToFixed, customFunction } from '@/assets/js/utils/windowFn.js'// window对象-操作

NProgress.configure({ showSpinner: false })

// 不需要验证的 TODO:2
const whiteList = [
  '/login', // 登录首页
  '/loginError', // 登陆失败页面
  '/RetrievePassword',
  '/expert/notice',
  '/expert/baseInfo',
  '/expert/finishReg',
  // 电商
  '/shopLogin',
  '/shop/shopHome',
  '/shop/goodsClassList',
  '/shop/goodsClassDetail',
  '/navbarAdd',
  // 供应商-注册-Strt
  '/register/registerType',
  '/register/registerRules',
  '/register/registerInfo',
  '/register/registerFinishReg',
  // 供应商-注册-End
  // 专家-注册-Strt
  '/expert/register/expertRegisterRules',
  '/expert/register/expertRegisterInfo',
  '/expert/register/expertRegisterFinishReg',
  // 专家-供应商-注册-End
  '/payCenter/pay',
  // 工具端立项
  '/independentBid',
  '/independentBid/bidProjectStep',
  '/independentBid/bidProjectStep/bidProjectClassification',
  '/independentBid/bidProjectStep/bidProjectBase',
  '/independentBid/bidProjectStep/bidProjectDepart',
  '/independentBid/bidProjectStep/bidProjectMateriel',
  '/independentBid/bidProjectStep/bidProjectPackage',
  '/independentBid/bidProjectStep/bidProjectOther',
  '/independentBid/bidProjectStep/bidProjectPreview',
  '/independentBid/bidProjectPreview',
  '/independentBid/excessivePage',
  '/bidOpeningHall/openingHall',
  '/toolProject/bidOpen',
  '/toolProject/noPackageBidOpeningDecryptionRedis', // 工具端指定不分包页面
  '/toolProject/packageBidOpeningDecryptionRedis' // 工具端指定分包页面
]
// 重写 Number.prototype.toFixed 去掉四舍五入
rewriteToFixed()
// 自定义加法函数/乘法函数，解决商品金额小数精度不准确的问题
customFunction()
// 初始化/刷新时时回到首页
// windowReady(whiteList)
// 导航守卫
router.beforeEach((to, from, next) => {
  NProgress.start()
  if (getToken()) {
    if (to.path === '/login') {
      NProgress.done()
      next('/home')
    } else {
      // 判断如果是工具端进入项目到立项页面 防止跳不过去问题
      if (to.path === '/home' && from.path === '/independentBid/excessivePage') {
        next({
          path: '/independentBid/excessivePage'
        })
        NProgress.done()
      } else if (to.path === '/home' && (from.path === '/independentBid/bidProjectStep' || from.path === '/independentBid/bidProjectStep/bidProjectClassification' || from.path === '/independentBid/bidProjectStep/bidProjectBase')) {
        // 工具端进入立项页面后根据判断是否有项目id 进入不同页面
        if (from.params.id) {
          next({
            path: '/independentBid/bidProjectStep/bidProjectBase'
          })
        } else {
          next({
            path: '/independentBid/bidProjectStep/bidProjectClassification'
          })
        }
        NProgress.done()
      } else if (whiteList.indexOf(to.path) !== -1) {
        next()
        // 如果是电商首页||商品列表||商品详情页且没有获取到用户信息ID 重新获取
        if ((to.path === '/shop/shopHome') && !store.getters.info.id) {
          store.dispatch('setUserInfo')// 获取用户信息
        }
      } else if (!store.getters.info.id) {
        store.dispatch('setUserInfo')// 获取用户信息
      } else {
        next()
      }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      store.dispatch('logout')
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})
