1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
| <template> <div id="app" class="app-main"> <router-view></router-view> </div> </template>
<script> import Vue from 'vue' import {getBankAuth} from '@/api/auth' import userPath from '@/router/fullpath' import * as util from '@/utils/util.js' export default { name: 'App', data () { return { menuData: null, userAuth: null } }, methods: { getRoutes (userAuth) { if (!userAuth[0].children) { return console.warn(userAuth) } let allowedRouter = [] let arrayMenus = util.buildMenu(userAuth[0].children) let hashMenus = {} hashMenus = util.getPath(arrayMenus) this.$root.hashMenus = hashMenus let findLocalRoute = function (array, base) { let replyResult = [] array.forEach(function (route) { let pathKey = (base ? base + '/' : '') + route.path if (hashMenus.hasOwnProperty(pathKey)) { if (Object.prototype.toString.call(route.children) === '[object Array]') { route.children = findLocalRoute(route.children, pathKey) } replyResult.push(route) } }) if (base) { return replyResult } else { allowedRouter = allowedRouter.concat(replyResult) } } let originPath = util.deepcopy(userPath) findLocalRoute(originPath) return allowedRouter }, extendRoutes (allowedRouter) { let vm = this let actualRouter = util.deepcopy(allowedRouter) actualRouter.map(e => { if (e.children) { if (!e.meta) e.meta = {} e.meta.children = e.children } return e.beforeEnter = function (to, from, next) { if (vm.$root.hashMenus[to.path]) { next() Vue.prototype.$_has = function (p) { let permission = false this.hashButtons.forEach(item => { if (item.hasOwnProperty(to.path)) { if (item[to.path].indexOf(p) !== -1) { permission = true } } }) return permission } } else { next('/401') } } }) let originPath = actualRouter vm.$router.addRoutes(originPath.concat([{ path: '*', redirect: '/login' }])) }, getAuthority (role) { let vm = this let localUser = util.session('token') if (!localUser || !localUser.authorities) { return vm.$router.push({ path: '/login', query: { from: vm.$router.currentRoute.path } }) } if (role === 'bank') { getBankAuth().then(data => { let userAuth = data let allowedRouter = vm.getRoutes(userAuth) if (!allowedRouter || !allowedRouter.length) { util.session('token', '') return document.body.innerHTML = ('<h1>账号访问受限,请联系系统管理员!</h1>') } vm.extendRoutes(allowedRouter) vm.menuData = allowedRouter vm.userAuth = userAuth }).catch(error => { console.log(error) }) } } }, created () { this.getAuthority('bank') } } </script>
<style lang="scss"> @import 'assets/sass/sks.scss'; @import 'assets/fonts/iconfont.css'; @import 'assets/sass/table.scss'; @import 'assets/sass/dialog.scss'; .app-main { width: 100%; height: 100%; overflow: auto; } </style>
|