index.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { createRouter, createWebHashHistory } from 'vue-router'
  2. import { ElMessageBox, ElMessage } from 'element-plus'
  3. import LeftLayout from "@/views/LeftLayout"
  4. const routes = [{
  5. path: "/left",
  6. name: 'left',
  7. component: LeftLayout,
  8. children: [
  9. {
  10. meta: {
  11. action: "menuList",
  12. title: '权限菜单'
  13. },
  14. path: 'menuList',
  15. name: 'menuList',
  16. component: () =>
  17. import ('@views/menu/index.vue')
  18. },
  19. {
  20. meta: {
  21. action: "roleList",
  22. title: '权限菜单'
  23. },
  24. path: 'roleList',
  25. name: 'roleList',
  26. component: () =>
  27. import ('@views/role/index.vue')
  28. },
  29. {
  30. meta: {
  31. action: "websiteList",
  32. title: '站点列表'
  33. },
  34. path: 'websiteList',
  35. name: 'websiteList',
  36. component: () =>
  37. import ('@views/website/index.vue')
  38. },
  39. ]
  40. },
  41. //======================================
  42. {
  43. meta: {
  44. action: "login",
  45. title: '登录',
  46. activity: true,
  47. requireAuth:false
  48. },
  49. path: '/login',
  50. name: 'login',
  51. component: () =>
  52. import ('@views/login/index.vue')
  53. }
  54. ]
  55. const router = createRouter({
  56. history: createWebHashHistory(),
  57. routes
  58. })
  59. const getCookie = function(objName) { //获取指定名称的cookie的值
  60. var arrStr = document.cookie.split("; ");
  61. for (var i = 0; i < arrStr.length; i++) {
  62. var temp = arrStr[i].split("=");
  63. if (temp[0] == objName) return unescape(temp[1]); //解码
  64. }
  65. return "";
  66. }
  67. router.beforeEach((to, from) => {
  68. if(to.meta.requireAuth){
  69. if(!document.getCookie("token")){
  70. router.push('/login');
  71. }
  72. }
  73. })
  74. router.afterEach((to, from) => {
  75. if (to.meta && to.meta.title) {
  76. document.title = to.meta.title
  77. }
  78. })
  79. export default router