初始提交
This commit is contained in:
174
rg-09112127/js/userManager.js
Normal file
174
rg-09112127/js/userManager.js
Normal file
@@ -0,0 +1,174 @@
|
||||
/**
|
||||
* 用户管理器 - 管理用户登录和状态
|
||||
*/
|
||||
class UserManager {
|
||||
constructor() {
|
||||
this.currentUser = null;
|
||||
this.isLoggedIn = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化用户管理器
|
||||
*/
|
||||
async init() {
|
||||
// 检查本地存储的用户信息
|
||||
const userStr = localStorage.getItem('currentUser');
|
||||
if (userStr) {
|
||||
try {
|
||||
this.currentUser = JSON.parse(userStr);
|
||||
this.isLoggedIn = true;
|
||||
console.log('用户已登录:', this.currentUser);
|
||||
} catch (error) {
|
||||
console.error('解析用户信息失败:', error);
|
||||
this.clearUser();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
*/
|
||||
async login(username, password) {
|
||||
try {
|
||||
const result = await window.apiService.login(username, password);
|
||||
if (result.data) {
|
||||
this.currentUser = result.data;
|
||||
this.isLoggedIn = true;
|
||||
console.log('登录成功:', this.currentUser);
|
||||
return { success: true, user: this.currentUser };
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('登录失败:', error);
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户登出
|
||||
*/
|
||||
logout() {
|
||||
this.clearUser();
|
||||
console.log('用户已登出');
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除用户信息
|
||||
*/
|
||||
clearUser() {
|
||||
this.currentUser = null;
|
||||
this.isLoggedIn = false;
|
||||
localStorage.removeItem('currentUser');
|
||||
window.apiService.clearCurrentUser();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户
|
||||
*/
|
||||
getCurrentUser() {
|
||||
return this.currentUser;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否已登录
|
||||
*/
|
||||
isUserLoggedIn() {
|
||||
return this.isLoggedIn && this.currentUser !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户ID
|
||||
*/
|
||||
getUserId() {
|
||||
return this.currentUser ? this.currentUser.id : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户名
|
||||
*/
|
||||
getUsername() {
|
||||
return this.currentUser ? this.currentUser.username : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户真实姓名
|
||||
*/
|
||||
getRealName() {
|
||||
return this.currentUser ? this.currentUser.realName : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取班级名称
|
||||
*/
|
||||
getClassName() {
|
||||
return this.currentUser ? this.currentUser.className : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取年级
|
||||
*/
|
||||
getGradeLevel() {
|
||||
return this.currentUser ? this.currentUser.gradeLevel : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示登录对话框
|
||||
*/
|
||||
showLoginDialog() {
|
||||
const username = prompt('请输入用户名:');
|
||||
if (username) {
|
||||
const password = prompt('请输入密码:');
|
||||
if (password) {
|
||||
this.login(username, password).then(result => {
|
||||
if (result.success) {
|
||||
alert('登录成功!');
|
||||
this.updateUserDisplay();
|
||||
} else {
|
||||
alert('登录失败: ' + result.error);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户显示
|
||||
*/
|
||||
updateUserDisplay() {
|
||||
const userDisplay = document.getElementById('userDisplay');
|
||||
if (userDisplay) {
|
||||
if (this.isUserLoggedIn()) {
|
||||
const displayName = this.getRealName() || this.getUsername();
|
||||
userDisplay.innerHTML = `
|
||||
<div class="user-info">
|
||||
<span>欢迎, ${displayName}</span>
|
||||
<button onclick="userManager.logout()" class="logout-btn">登出</button>
|
||||
</div>
|
||||
`;
|
||||
} else {
|
||||
userDisplay.innerHTML = `
|
||||
<button onclick="userManager.showLoginDialog()" class="login-btn">登录</button>
|
||||
`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建用户显示元素
|
||||
*/
|
||||
createUserDisplay() {
|
||||
const userDisplay = document.createElement('div');
|
||||
userDisplay.id = 'userDisplay';
|
||||
userDisplay.className = 'user-display';
|
||||
|
||||
// 添加到页面
|
||||
const header = document.querySelector('header') || document.body;
|
||||
if (header) {
|
||||
header.appendChild(userDisplay);
|
||||
}
|
||||
|
||||
this.updateUserDisplay();
|
||||
}
|
||||
}
|
||||
|
||||
// 创建全局用户管理器实例
|
||||
window.userManager = new UserManager();
|
||||
Reference in New Issue
Block a user