user_handbook/docs/utils/auth.js

40 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2026-03-30 10:23:03 +08:00
import { PERMISSIONS } from '../.vuepress/roles';
import axios from 'axios'
export function getCurrentUserRole() {
console.log('32-')
if (typeof window !== 'undefined') {
// 从store或localStorage获取用户角色
return localStorage.getItem('userRole') || 'coach';
}
}
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] || [];
return allowedPaths.some(path =>
requiredPath.startsWith(path) || path === '*'
);
}
export function isLoggedIn() {
if (typeof window !== 'undefined') {
return !!localStorage.getItem('authToken');
}
}
export function showLoginModal() {
// 实现弹窗逻辑建议使用ElementUI等UI库
}
// 添加登录API调用方法
export async function login(credentials) {
if (typeof window !== 'undefined') {
const response = await axios.post('/api/login', credentials);
localStorage.setItem('authToken', response.data.token);
localStorage.setItem('userRole', response.data.role);
}
}