index.vue 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <template>
  2. <div class="auth" v-if="isAuth">
  3. <slot></slot>
  4. </div>
  5. </template>
  6. <script>
  7. import store from '@/store'
  8. export default {
  9. name: 'auth',
  10. props: {
  11. auths: {
  12. type: String | Array
  13. },
  14. router: {
  15. type: Array
  16. },
  17. mulit: {
  18. type: Boolean,
  19. default() {
  20. return false
  21. }
  22. }
  23. },
  24. data() {
  25. return {
  26. isAuth: false
  27. }
  28. },
  29. async mounted() {
  30. let permission = window.localStorage.getItem('permission')
  31. permission = permission.split(',')
  32. this.isAuth = this.hasAuths(permission) && this.hasRouters() ? true : false
  33. },
  34. methods: {
  35. hasAuths(menus) {
  36. const auths = this.auths
  37. if(!auths) { // 判断是否有权限
  38. return false
  39. }
  40. // console.log(auths)
  41. if(typeof auths === 'object') {
  42. let sum = 0
  43. for(const auth of auths) {
  44. if(this.mulit) {
  45. if(menus.includes(auth)) {
  46. sum ++
  47. }
  48. } else {
  49. return menus.includes(auth)
  50. }
  51. }
  52. if(sum === auths.length) {
  53. return true
  54. }
  55. } else {
  56. return menus.includes(auths)
  57. }
  58. },
  59. hasRouters() {
  60. const path = this.$route.path
  61. if(this.router) {
  62. return this.router.includes(path)
  63. }
  64. return true
  65. }
  66. }
  67. }
  68. </script>
  69. <style scoped lang="less">
  70. .auth {
  71. display: inline-block;
  72. // & + .auth {
  73. // margin-left: 10px;
  74. // }
  75. }
  76. </style>