useAuth.ts 861 B

1234567891011121314151617181920212223242526272829303132333435
  1. import useSettingsStore from '@/store/modules/settings'
  2. import useUserStore from '@/store/modules/user'
  3. export default function useAuth() {
  4. function hasPermission(permission: string) {
  5. const settingsStore = useSettingsStore()
  6. const userStore = useUserStore()
  7. if (settingsStore.settings.app.enablePermission) {
  8. return userStore.permissions.includes(permission)
  9. }
  10. else {
  11. return true
  12. }
  13. }
  14. function auth(value: string | string[]) {
  15. let auth
  16. if (typeof value === 'string') {
  17. auth = value !== '' ? hasPermission(value) : true
  18. }
  19. else {
  20. auth = value.length > 0 ? value.some(item => hasPermission(item)) : true
  21. }
  22. return auth
  23. }
  24. function authAll(value: string[]) {
  25. return value.length > 0 ? value.every(item => hasPermission(item)) : true
  26. }
  27. return {
  28. auth,
  29. authAll,
  30. }
  31. }