router.js 1.72 KB
import Vue from 'vue'
import Router from 'vue-router'
import routes from './routes'
import store from '@/store'
import { util } from '@/utils'
// hack router push callback
const originalPush = Router.prototype.push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router)

const router = createRouter()

function createRouter() {
  return new Router({
    // mode: 'history', // 如果是 history模式 需要配置vue.config.js publicPath
    base: process.env.BASE_URL,
    scrollBehavior: function (to, from, savedPosition) {
      return savedPosition || { x: 0, y: 0 }
    },
    routes
  })
}
// http://scrm.dev.glsx.com.cn/dj-scrm-wx-work-attachment-activity/
router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面title */
  if (to.meta.title) store.dispatch('setPageMeta', to.meta)
  store.commit('SET_LAST_PATH', to.name)
  // 收款测试页面直接进入
  if (to.path && to.path.indexOf('testPayMerchant/index/split') > -1) store.commit('SET_LAST_PATH', 'testPayMerchant.index')
  /* 路由发生变化修改页面背景色 */
  if (to.meta.backgroundColor) document.documentElement.style.backgroundColor = to.meta.backgroundColor
  if (to.matched.some(recode => recode.meta.noAuth)) {
    store.state.app.showWindow = true
    store.dispatch('registerHttp')
  } else store.dispatch('navigation')
  next()
})

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router