first commit
This commit is contained in:
commit
db15eb3ee0
42 files changed
+4620
No files matched your search
@@ -0,0 +1,22 @@
|
||||
import { computed, ref } from 'vue'
|
||||
|
||||
const authenticated = ref(localStorage.getItem('reelshort-auth') === '1')
|
||||
const currentRole = ref('超级管理员')
|
||||
|
||||
export function useAuth() {
|
||||
const isAuthenticated = computed(() => authenticated.value)
|
||||
function login(username, password) {
|
||||
if (username !== 'admin' || password !== 'admin123') return false
|
||||
localStorage.setItem('reelshort-auth', '1')
|
||||
currentRole.value = '超级管理员'
|
||||
authenticated.value = true
|
||||
return true
|
||||
}
|
||||
function logout() {
|
||||
localStorage.removeItem('reelshort-auth')
|
||||
currentRole.value = '超级管理员'
|
||||
authenticated.value = false
|
||||
}
|
||||
function setRole(role) { currentRole.value = role }
|
||||
return { isAuthenticated, currentRole, login, logout, setRole }
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { reactive, computed } from 'vue'
|
||||
import { defaultRolePermissions } from '../permissions/permissions'
|
||||
import { useAuth } from './auth'
|
||||
import { customMenus } from '../data/customMenus'
|
||||
|
||||
const rolePermissions = reactive(Object.fromEntries(Object.entries(defaultRolePermissions).map(([role, list]) => [role, [...list]])))
|
||||
rolePermissions['超级管理员'].push(...customMenus.map(menu => menu.permission))
|
||||
|
||||
export function useRbac() {
|
||||
const { currentRole } = useAuth()
|
||||
const permissions = computed(() => rolePermissions[currentRole.value] || [])
|
||||
const can = permission => permissions.value.includes(permission)
|
||||
function updateRolePermission(role, permission, enabled) {
|
||||
const current = rolePermissions[role] || (rolePermissions[role] = [])
|
||||
rolePermissions[role] = enabled ? [...new Set([...current, permission])] : current.filter(item => item !== permission)
|
||||
}
|
||||
function createRole(name) { if (!rolePermissions[name]) rolePermissions[name] = [] }
|
||||
function createPermission(permission) {
|
||||
rolePermissions['超级管理员'] = [...new Set([...(rolePermissions['超级管理员'] || []), permission])]
|
||||
}
|
||||
function removePermission(permission) {
|
||||
for (const role of Object.keys(rolePermissions)) rolePermissions[role] = rolePermissions[role].filter(item => item !== permission)
|
||||
}
|
||||
return { permissions, can, rolePermissions, updateRolePermission, createRole, createPermission, removePermission }
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { ref } from 'vue'
|
||||
|
||||
const storageKey = 'reelshort-theme'
|
||||
const systemTheme = window.matchMedia('(prefers-color-scheme: dark)')
|
||||
const storedTheme = localStorage.getItem(storageKey)
|
||||
const isDark = ref(storedTheme ? storedTheme === 'dark' : systemTheme.matches)
|
||||
|
||||
function applyTheme(value) {
|
||||
isDark.value = value
|
||||
document.documentElement.classList.toggle('dark', value)
|
||||
}
|
||||
|
||||
applyTheme(isDark.value)
|
||||
systemTheme.addEventListener?.('change', event => {
|
||||
if (!localStorage.getItem(storageKey)) applyTheme(event.matches)
|
||||
})
|
||||
|
||||
export function useTheme() {
|
||||
function toggleTheme() {
|
||||
const next = !isDark.value
|
||||
localStorage.setItem(storageKey, next ? 'dark' : 'light')
|
||||
applyTheme(next)
|
||||
}
|
||||
return { isDark, toggleTheme }
|
||||
}
|
||||
Reference in new issue
Block a user