dma_handbook/docs/utils/auth.js

40 lines
1.2 KiB
JavaScript
Raw Normal View History

2026-02-05 10:24:40 +08:00
import { PERMISSIONS } from '../.vuepress/roles';
2025-09-29 16:51:36 +08:00
import axios from 'axios'
export function getCurrentUserRole() {
console.log('32-')
2026-02-06 10:12:08 +08:00
if (typeof window !== 'undefined') {
// 从store或localStorage获取用户角色
return localStorage.getItem('userRole') || 'coach';
}
2025-09-29 16:51:36 +08:00
}
export function checkPermission(currentRole, requiredPath) {
console.log(currentRole,PERMISSIONS)
// const rolePermissions = PERMISSIONS[currentRole] || [];
// console.log(rolePermissions,'rolePermissions=')
// return rolePermissions.includes('*') || rolePermissions.includes(requiredPath);
const allowedPaths = PERMISSIONS[currentRole] || [];
2026-02-05 10:24:40 +08:00
return allowedPaths.some(path =>
2025-09-29 16:51:36 +08:00
requiredPath.startsWith(path) || path === '*'
);
}
export function isLoggedIn() {
2026-02-06 10:12:08 +08:00
if (typeof window !== 'undefined') {
return !!localStorage.getItem('authToken');
}
2025-09-29 16:51:36 +08:00
}
export function showLoginModal() {
// 实现弹窗逻辑建议使用ElementUI等UI库
}
// 添加登录API调用方法
export async function login(credentials) {
2026-02-06 10:12:08 +08:00
if (typeof window !== 'undefined') {
const response = await axios.post('/api/login', credentials);
localStorage.setItem('authToken', response.data.token);
localStorage.setItem('userRole', response.data.role);
}
2025-09-29 16:51:36 +08:00
}