permission.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * @Author: wt 1241351815@qq.com
  3. * @Date: 2022-04-25 10:21:18
  4. * @LastEditors: wt 1241351815@qq.com
  5. * @LastEditTime: 2022-05-09 13:24:45
  6. * @FilePath: \securityHtml\src\permission.js
  7. * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  8. */
  9. import router from './router'
  10. import store from './store'
  11. import { Message } from 'element-ui'
  12. import NProgress from 'nprogress' // progress bar
  13. import 'nprogress/nprogress.css' // progress bar style
  14. import { getToken } from '@/utils/auth' // get token from cookie
  15. import getPageTitle from '@/utils/get-page-title'
  16. NProgress.configure({ showSpinner: false }) // NProgress Configuration
  17. const whiteList = ['/login'] // no redirect whitelist
  18. router.beforeEach(async(to, from, next) => {
  19. // start progress bar
  20. NProgress.start()
  21. // set page title
  22. document.title = getPageTitle(to.meta.title)
  23. // determine whether the user has logged in
  24. const hasToken = getToken()
  25. if (hasToken) {
  26. if (to.path === '/login') {
  27. console.log(1)
  28. // if is logged in, redirect to the home page
  29. next({ path: '/' })
  30. NProgress.done()
  31. } else {
  32. console.log(2)
  33. if (to.path === '/index') {
  34. store.getters.sidebar.opened = true
  35. }
  36. // get user info
  37. var list = {
  38. parentId: '0',
  39. userId: '1'
  40. }
  41. store.dispatch('getInfo', list);
  42. next()
  43. }
  44. } else {
  45. /* has no token*/
  46. if (whiteList.indexOf(to.path) !== -1) {
  47. // in the free login whitelist, go directly
  48. next()
  49. } else {
  50. // other pages that do not have permission to access are redirected to the login page.
  51. next(`/login?redirect=${to.path}`)
  52. NProgress.done()
  53. }
  54. }
  55. })
  56. router.afterEach(() => {
  57. // finish progress bar
  58. NProgress.done()
  59. })