diff --git a/src/components/Auth/index.vue b/src/components/Auth/index.vue index 9ea36ad9..762c34ff 100644 --- a/src/components/Auth/index.vue +++ b/src/components/Auth/index.vue @@ -1,5 +1,5 @@ diff --git a/src/components/AuthAll/index.vue b/src/components/AuthAll/index.vue index 382bac7d..b7b22ca8 100644 --- a/src/components/AuthAll/index.vue +++ b/src/components/AuthAll/index.vue @@ -1,5 +1,5 @@ diff --git a/src/util/composables/auth/index.js b/src/util/composables/auth/index.js new file mode 100644 index 00000000..4fce7a36 --- /dev/null +++ b/src/util/composables/auth/index.js @@ -0,0 +1,43 @@ +import useSettingsStore from '@/store/modules/settings' +import useUserStore from '@/store/modules/user' + +function useAuth() { + function hasPermission(permission) { + const settingsStore = useSettingsStore() + const userStore = useUserStore() + if (settingsStore.app.enablePermission) { + return userStore.permissions.some(v => { + return v === permission + }) + } else { + return true + } + } + + function auth(value) { + let auth + if (typeof value === 'string') { + auth = hasPermission(value) + } else { + auth = value.some(item => { + return hasPermission(item) + }) + } + return auth + } + + function authAll(value) { + const auth = value.every(item => { + return hasPermission(item) + }) + return auth + } + + return { + hasPermission, + auth, + authAll + } +} + +export default useAuth diff --git a/src/util/composables/index.js b/src/util/composables/index.js new file mode 100644 index 00000000..28c96ccb --- /dev/null +++ b/src/util/composables/index.js @@ -0,0 +1,5 @@ +import useAuth from './auth' + +export { + useAuth +} diff --git a/src/util/directive.js b/src/util/directive.js index 9a144882..a957a300 100644 --- a/src/util/directive.js +++ b/src/util/directive.js @@ -1,17 +1,17 @@ -import { auth, authAll } from '@/util' +import { useAuth } from '@/util/composables' export default function directive(app) { // 注册 v-auth 和 v-auth-all 指令 app.directive('auth', { mounted: (el, binding) => { - if (!auth(binding.value)) { + if (!useAuth().auth(binding.value)) { el.remove() } } }) app.directive('auth-all', { mounted: (el, binding) => { - if (!authAll(binding.value)) { + if (!useAuth().authAll(binding.value)) { el.remove() } } diff --git a/src/util/global.properties.js b/src/util/global.properties.js index cb30de53..8a9d9224 100644 --- a/src/util/global.properties.js +++ b/src/util/global.properties.js @@ -1,5 +1,5 @@ import api from '@/api' -import { auth, authAll } from '@/util' +import { useAuth } from '@/util/composables' import dayjs from 'dayjs' import 'dayjs/locale/zh-cn' import Cookies from 'js-cookie' @@ -10,6 +10,7 @@ export default function globalProperties(app) { // 请求 app.config.globalProperties.$api = api // 鉴权 + const { auth, authAll } = useAuth() app.config.globalProperties.$auth = auth app.config.globalProperties.$authAll = authAll dayjs.locale('zh-cn') diff --git a/src/util/index.js b/src/util/index.js index b14fa99e..29f9e511 100644 --- a/src/util/index.js +++ b/src/util/index.js @@ -1,6 +1,4 @@ import path from 'path-browserify' -import useSettingsStore from '@/store/modules/settings' -import useUserStore from '@/store/modules/user' export function deepClone(target) { // 定义一个变量 @@ -35,37 +33,6 @@ export function deepClone(target) { return result } -function hasPermission(permission) { - const settingsStore = useSettingsStore() - const userStore = useUserStore() - if (settingsStore.app.enablePermission) { - return userStore.permissions.some(v => { - return v === permission - }) - } else { - return true - } -} - -export function auth(value) { - let auth - if (typeof value === 'string') { - auth = hasPermission(value) - } else { - auth = value.some(item => { - return hasPermission(item) - }) - } - return auth -} - -export function authAll(value) { - const auth = value.every(item => { - return hasPermission(item) - }) - return auth -} - export function isExternalLink(path) { return /^(https?:|mailto:|tel:)/.test(path) }