代码优化

This commit is contained in:
hooray 2022-07-06 00:37:42 +08:00
parent e046c7716f
commit 4c3baf3693
7 changed files with 57 additions and 41 deletions

View File

@ -1,5 +1,5 @@
<script setup name="Auth">
import { auth } from '@/util'
import { useAuth } from '@/util/composables'
const props = defineProps({
value: {
@ -9,7 +9,7 @@ const props = defineProps({
})
function check() {
return auth(props.value)
return useAuth().auth(props.value)
}
</script>

View File

@ -1,5 +1,5 @@
<script setup name="AuthAll">
import { authAll } from '@/util'
import { useAuth } from '@/util/composables'
const props = defineProps({
value: {
@ -9,7 +9,7 @@ const props = defineProps({
})
function check() {
return authAll(props.value)
return useAuth().authAll(props.value)
}
</script>

View File

@ -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

View File

@ -0,0 +1,5 @@
import useAuth from './auth'
export {
useAuth
}

View File

@ -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()
}
}

View File

@ -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')

View File

@ -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)
}