index.js 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import * as VueRouter from 'vue-router'
  2. import NProgress from 'nprogress';
  3. import 'nprogress/nprogress.css'
  4. import store from '@/store';
  5. const routes = [
  6. { path: '/', name: 'index', component: () => import('@/pages/index.vue') },
  7. { path: '/invoice/fill', name: 'invoice.fill', component: () => import('@/pages/invoice/fill.vue') },
  8. { path: '/invoice/history', name: 'invoice.history', component: () => import('@/pages/invoice/history.vue') },
  9. { path: '/publicity/list', name: 'publicity.list', component: () => import('@/pages/publicity/list.vue') },
  10. { path: '/login', name: 'login', component: () => import('@/pages/login.vue') },
  11. ];
  12. const router = VueRouter.createRouter({
  13. // history: VueRouter.createWebHistory(import.meta.env.BASE_URL),
  14. history: VueRouter.createWebHashHistory(),
  15. routes,
  16. })
  17. router.beforeEach((to, from, next) => {
  18. NProgress.start();
  19. if (to.name === 'login') {
  20. NProgress.done();
  21. return next();
  22. }
  23. const { user, getCurrent } = store.getState('user');
  24. if (!user || !user.personId) {
  25. getCurrent().then(() => {
  26. NProgress.done();
  27. next();
  28. }).catch((err) => {
  29. console.error(err)
  30. NProgress.done();
  31. next({ name: 'login', query: { from: encodeURIComponent(to.fullPath) } });
  32. })
  33. } else {
  34. next();
  35. NProgress.done();
  36. }
  37. })
  38. export default router;