auth.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // 用户认证相关功能
  2. // 登录函数
  3. async function login() {
  4. const username = document.getElementById('username').value;
  5. const password = document.getElementById('password').value;
  6. const captcha = document.getElementById('captcha').value;
  7. try {
  8. core.showLoading();
  9. const response = await fetch('/api/login', {
  10. method: 'POST',
  11. headers: { 'Content-Type': 'application/json' },
  12. body: JSON.stringify({ username, password, captcha })
  13. });
  14. if (response.ok) {
  15. const data = await response.json();
  16. window.isLoggedIn = true;
  17. localStorage.setItem('isLoggedIn', 'true');
  18. persistSession();
  19. document.getElementById('currentUsername').textContent = username;
  20. document.getElementById('welcomeUsername').textContent = username;
  21. document.getElementById('loginModal').style.display = 'none';
  22. document.getElementById('adminContainer').style.display = 'flex';
  23. // 确保加载完成后初始化事件监听器
  24. await core.loadSystemConfig();
  25. core.initEventListeners();
  26. core.showSection('dashboard');
  27. userCenter.getUserInfo();
  28. systemStatus.refreshSystemStatus();
  29. } else {
  30. const errorData = await response.json();
  31. core.showAlert(errorData.error || '登录失败', 'error');
  32. refreshCaptcha();
  33. }
  34. } catch (error) {
  35. core.showAlert('登录失败: ' + error.message, 'error');
  36. refreshCaptcha();
  37. } finally {
  38. core.hideLoading();
  39. }
  40. }
  41. // 注销函数
  42. async function logout() {
  43. console.log("注销操作被触发");
  44. try {
  45. core.showLoading();
  46. const response = await fetch('/api/logout', { method: 'POST' });
  47. if (response.ok) {
  48. // 清除所有登录状态
  49. localStorage.removeItem('isLoggedIn');
  50. sessionStorage.removeItem('sessionActive');
  51. window.isLoggedIn = false;
  52. // 清除cookie
  53. document.cookie = 'connect.sid=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
  54. window.location.reload();
  55. } else {
  56. throw new Error('退出登录失败');
  57. }
  58. } catch (error) {
  59. console.error('退出登录失败:', error);
  60. core.showAlert('退出登录失败: ' + error.message, 'error');
  61. // 即使API失败也清除本地状态
  62. localStorage.removeItem('isLoggedIn');
  63. sessionStorage.removeItem('sessionActive');
  64. window.isLoggedIn = false;
  65. window.location.reload();
  66. } finally {
  67. core.hideLoading();
  68. }
  69. }
  70. // 验证码刷新函数
  71. async function refreshCaptcha() {
  72. try {
  73. const response = await fetch('/api/captcha');
  74. if (!response.ok) {
  75. throw new Error(`验证码获取失败: ${response.status}`);
  76. }
  77. const data = await response.json();
  78. document.getElementById('captchaText').textContent = data.captcha;
  79. } catch (error) {
  80. console.error('刷新验证码失败:', error);
  81. document.getElementById('captchaText').textContent = '验证码加载失败,点击重试';
  82. }
  83. }
  84. // 持久化会话
  85. function persistSession() {
  86. if (document.cookie.includes('connect.sid')) {
  87. sessionStorage.setItem('sessionActive', 'true');
  88. }
  89. }
  90. // 显示登录模态框
  91. function showLoginModal() {
  92. // 确保先隐藏加载指示器
  93. if (core && typeof core.hideLoadingIndicator === 'function') {
  94. core.hideLoadingIndicator();
  95. }
  96. document.getElementById('loginModal').style.display = 'flex';
  97. refreshCaptcha();
  98. }
  99. // 导出模块
  100. const auth = {
  101. init: function() {
  102. console.log('初始化认证模块...');
  103. // 在这里可以添加认证模块初始化的相关代码
  104. return Promise.resolve(); // 返回一个已解决的 Promise,保持与其他模块一致
  105. },
  106. login,
  107. logout,
  108. refreshCaptcha,
  109. showLoginModal
  110. };
  111. // 全局公开认证模块
  112. window.auth = auth;