This commit is contained in:
vipg
2025-11-12 17:24:55 +08:00
parent 8159f03e8c
commit 6dd4a9a41a
16 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,87 @@
/**
* 权限验证核心模块
*/
window.Auth = {
/**
* 获取Token
*/
getToken() {
return localStorage.getItem(SystemConfig.tokenKey);
},
/**
* 设置Token
* @param {string} token - 认证令牌
*/
setToken(token) {
localStorage.setItem(SystemConfig.tokenKey, token);
// 设置过期时间(可选)
const expireTime = new Date().getTime() + SystemConfig.tokenExpire * 60 * 60 * 1000;
localStorage.setItem(`${SystemConfig.tokenKey}_expire`, expireTime);
},
/**
* 移除Token
*/
removeToken() {
localStorage.removeItem(SystemConfig.tokenKey);
localStorage.removeItem(`${SystemConfig.tokenKey}_expire`);
},
/**
* 验证Token是否有效
*/
isValidToken() {
const token = this.getToken();
if (!token) return false;
// 验证过期时间
const expireTime = localStorage.getItem(`${SystemConfig.tokenKey}_expire`);
if (expireTime && new Date().getTime() > expireTime) {
this.removeToken();
return false;
}
return true;
},
/**
* 登录验证拦截
* 未登录自动跳转到登录页
*/
checkLogin() {
if (!this.isValidToken()) {
window.location.href = '../pages/login.html';
}
},
/**
* 登录请求
* @param {string} username - 用户名
* @param {string} password - 密码
* @returns {Promise} - 登录结果
*/
login(username, password) {
return new Promise((resolve, reject) => {
// 模拟接口请求(实际项目替换为真实接口)
setTimeout(() => {
// 简单验证(实际项目需要后端验证)
if (username && password) {
const token = `TOKEN_${new Date().getTime()}_${username}`;
this.setToken(token);
resolve({ success: true, token, message: '登录成功' });
} else {
reject({ success: false, message: '账号或密码不能为空' });
}
}, 800);
});
},
/**
* 退出登录
*/
logout() {
this.removeToken();
window.location.href = '../pages/login.html';
}
};

View File

@@ -0,0 +1,25 @@
/* 加载器 */
export const Loader = {
show() {
const loader = document.createElement('div');
loader.id = 'app-loader';
loader.style.cssText = `
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(255,255,255,0.8);
display: flex;
align-items: center;
justify-content: center;
z-index: 9999;
`;
loader.innerHTML = '<div>加载中...</div>';
document.body.appendChild(loader);
},
hide() {
const loader = document.getElementById('app-loader');
if (loader) loader.remove();
}
};

View File

@@ -0,0 +1,121 @@
/**
* 全局消息提示组件
*/
window.Message = {
/**
* 消息容器初始化
*/
init() {
let container = $('#message-container');
if (!container.length) {
container = $('<div id="message-container" class="message-container"></div>');
$('body').append(container);
}
return container;
},
/**
* 创建消息元素(修复 CSS 变量引用问题)
* @param {string} content - 消息内容
* @param {string} type - 消息类型success/error/info
* @returns {jQuery} - 消息元素
*/
createMessage(content, type) {
// 存储 CSS 变量名,而非直接使用 var()
const typeMap = {
success: { icon: '✓', cssVar: '--success-color' },
error: { icon: '✗', cssVar: '--error-color' },
info: { icon: 'i', cssVar: '--info-color' }
};
const config = typeMap[type] || typeMap.info;
// 关键:通过 JS 获取 CSS 变量的实际值
const rootElement = document.documentElement;
const computedStyle = getComputedStyle(rootElement);
const themeColor = computedStyle.getPropertyValue(config.cssVar).trim(); // 解析 CSS 变量
const message = $(`
<div class="message message-${type}">
<span class="message-icon">${config.icon}</span>
<span class="message-content">${content}</span>
</div>
`);
// 设置样式(使用解析后的 CSS 变量值)
message.css({
backgroundColor: computedStyle.getPropertyValue('--bg-light-color').trim(),
borderLeft: `4px solid ${themeColor}`,
color: computedStyle.getPropertyValue('--text-primary').trim(),
padding: '12px 16px',
borderRadius: '4px',
boxShadow: '0 2px 8px rgba(0,0,0,0.3)',
marginBottom: '8px',
display: 'flex',
alignItems: 'center',
animation: 'messageFadeIn 0.3s ease'
});
message.find('.message-icon').css({
color: themeColor,
marginRight: '8px',
fontWeight: 'bold'
});
return message;
},
/**
* 显示消息
* @param {string} content - 消息内容
* @param {string} type - 消息类型success/error/info
* @param {number} duration - 显示时长(毫秒)
*/
show(content, type = 'info', duration = SystemConfig.messageDuration) {
const container = this.init();
const message = this.createMessage(content, type);
container.append(message);
// 自动关闭
setTimeout(() => {
message.css({ animation: 'messageFadeOut 0.3s ease' });
setTimeout(() => {
message.remove();
}, 300);
}, duration);
},
// 快捷方法
success(content, duration) {
this.show(content, 'success', duration);
},
error(content, duration) {
this.show(content, 'error', duration);
},
info(content, duration) {
this.show(content, 'info', duration);
}
};
// 添加动画样式
$('head').append(`
<style>
@keyframes messageFadeIn {
from { opacity: 0; transform: translateY(-10px); }
to { opacity: 1; transform: translateY(0); }
}
@keyframes messageFadeOut {
from { opacity: 1; transform: translateY(0); }
to { opacity: 0; transform: translateY(-10px); }
}
.message-container {
position: fixed;
top: 20px;
right: 20px;
z-index: 9999;
max-width: 300px;
}
</style>
`);

View File

@@ -0,0 +1,19 @@
/* 路由管理 */
import { Auth } from './auth.js';
import { systemConfig } from '../config/system.js';
export const Router = {
// 跳转页面
push(path) {
// 验证权限
if (path !== '/login' && !Auth.isLogin()) {
window.location.href = `${systemConfig.baseUrl}login.html`;
return;
}
window.location.href = `${systemConfig.baseUrl}${path.startsWith('/') ? path.slice(1) : path}`;
},
// 获取当前路径
getCurrentPath() {
return window.location.pathname.replace(systemConfig.baseUrl, '');
}
};