App.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <template>
  2. <div id="app" class="custom-theme">
  3. <router-view
  4. :is-animation="isAnimation"
  5. :menu-router="menuRouter"
  6. :menu-active="menuActive"
  7. :breadcrumb="breadcrumb"
  8. :is-collapse="isCollapse"
  9. @changeCollapse="isCollapse = !isCollapse"
  10. />
  11. </div>
  12. </template>
  13. <script>
  14. import router from '@/router'
  15. import { canIShow } from '@/permission'
  16. import hugerte from 'hugerte'
  17. export default {
  18. name: 'App',
  19. data() {
  20. return {
  21. roles: [],
  22. isAnimation: false,
  23. menuRouter: [],
  24. menuActive: 0,
  25. breadcrumb: [],
  26. isCollapse: false,
  27. }
  28. },
  29. created() {
  30. this.initGuards()
  31. },
  32. mounted() {
  33. this.initStore()
  34. },
  35. methods: {
  36. initStore() {
  37. window.addEventListener('beforeunload', () => {
  38. localStorage.setItem('store', JSON.stringify(this.$store.state))
  39. })
  40. try {
  41. localStorage.getItem('store') && this.$store.replaceState(JSON.parse(localStorage.getItem('store')))
  42. } catch (e) {
  43. localStorage.removeItem('store')
  44. }
  45. },
  46. initGuards() {
  47. this.$router.afterEach((to, from, next) => {
  48. this.refreshRoute()
  49. })
  50. this.$router.beforeEach((to, from, next) => {
  51. console.log(to)
  52. if (router.app.$store.getters.token) {
  53. if (router.app.$store.getters.user.isAdmin) {
  54. canIShow(to.meta.roles).then(res => {
  55. this.isAnimation = true
  56. hugerte.remove('#editor-creat')
  57. hugerte.remove('#editor')
  58. setTimeout(() => {
  59. this.isAnimation = false
  60. if (res) {
  61. next()
  62. } else {
  63. next({
  64. name: '401'
  65. })
  66. }
  67. }, 400)
  68. })
  69. } else {
  70. next({
  71. name: 'login'
  72. })
  73. }
  74. } else {
  75. if (to.name === 'login' || to.name === 'userRegister' || to.name === 'userForm' || to.name === 'thirdLogin') {
  76. next()
  77. } else {
  78. next({
  79. name: 'userRegister'
  80. })
  81. }
  82. }
  83. })
  84. },
  85. refreshRoute() {
  86. this.menuRouter = this.$router.options.routes[0].children
  87. this.breadcrumb = this.$route.matched
  88. this.menuRouter.forEach((item, index) => {
  89. if (item.name === this.$route.name) {
  90. this.menuActive = index
  91. if (item.meta.collapse !== undefined) {
  92. this.isCollapse = !!item.meta.collapse
  93. }
  94. }
  95. if (item.children) {
  96. item.children.forEach((child, subIndex) => {
  97. if (child.name === this.$route.name) {
  98. this.menuActive = index + '-' + subIndex
  99. if (child.meta.collapse !== undefined) {
  100. this.isCollapse = !!child.meta.collapse
  101. }
  102. }
  103. })
  104. }
  105. })
  106. }
  107. }
  108. }
  109. </script>
  110. <style lang="scss">
  111. body,html,#app{
  112. margin: 0;
  113. height: 100%;
  114. width: 100%;
  115. overflow: hidden;
  116. }
  117. textarea{
  118. font-family: inherit;
  119. }
  120. .loading{
  121. animation: loading 800ms infinite alternate;
  122. }
  123. @keyframes loading {
  124. from{
  125. background: #e1e1e1;
  126. }
  127. to{
  128. background: #f1f1f1;
  129. }
  130. }
  131. *{
  132. box-sizing: border-box;
  133. }
  134. input[aria-hidden="true"]{
  135. display: none !important;
  136. }
  137. </style>