readFileSystem/ui/login_window.py

135 lines
4.7 KiB
Python
Raw Normal View History

from PySide6.QtWidgets import (
QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
QLabel, QLineEdit, QPushButton, QMessageBox
)
from PySide6.QtCore import Qt, Signal
from PySide6.QtGui import QFont
import hashlib
from src.db_manager import DatabaseManager
2026-02-28 14:05:55 +08:00
from utils.logger import get_logger, log_timing_context
logger = get_logger("login")
class LoginWindow(QMainWindow):
login_success = Signal(dict)
def __init__(self):
super().__init__()
self.setWindowTitle("检验管理系统 - 登录")
self.db = DatabaseManager()
self.init_ui()
def init_ui(self):
central_widget = QWidget()
self.setCentralWidget(central_widget)
main_layout = QVBoxLayout(central_widget)
main_layout.setContentsMargins(60, 80, 60, 80)
main_layout.setSpacing(40)
title_label = QLabel("检验管理系统")
title_label.setAlignment(Qt.AlignCenter)
title_label.setStyleSheet("""
font-family: 'Microsoft YaHei';
font-size: 32px;
font-weight: bold;
color: #0086fa;
padding: 20px;
""")
main_layout.addWidget(title_label)
form_layout = QVBoxLayout()
form_layout.setSpacing(15)
account_label = QLabel("账号")
account_label.setStyleSheet("font-family: 'Microsoft YaHei'; font-size: 18px; color: #333; padding: 5px;")
self.account_input = QLineEdit()
self.account_input.setPlaceholderText("请输入账号")
self.account_input.setMinimumHeight(50)
self.account_input.setStyleSheet("""
QLineEdit {
font-family: 'Microsoft YaHei';
font-size: 16px;
padding: 12px;
border: 2px solid #ccc;
border-radius: 5px;
}
QLineEdit:focus {
border: 2px solid #0086fa;
}
""")
password_label = QLabel("密码")
password_label.setStyleSheet("font-family: 'Microsoft YaHei'; font-size: 18px; color: #333; padding: 5px;")
self.password_input = QLineEdit()
self.password_input.setPlaceholderText("请输入密码")
self.password_input.setEchoMode(QLineEdit.Password)
self.password_input.setMinimumHeight(50)
self.password_input.setStyleSheet("""
QLineEdit {
font-family: 'Microsoft YaHei';
font-size: 16px;
padding: 12px;
border: 2px solid #ccc;
border-radius: 5px;
}
QLineEdit:focus {
border: 2px solid #0086fa;
}
""")
self.password_input.returnPressed.connect(self.on_login)
form_layout.addWidget(account_label)
form_layout.addWidget(self.account_input)
form_layout.addSpacing(10)
form_layout.addWidget(password_label)
form_layout.addWidget(self.password_input)
main_layout.addLayout(form_layout)
self.login_button = QPushButton("登录")
self.login_button.setMinimumHeight(55)
self.login_button.setStyleSheet("""
QPushButton {
background-color: #0086fa;
color: white;
border-radius: 5px;
padding: 15px;
font-family: 'Microsoft YaHei';
font-size: 18px;
font-weight: bold;
}
QPushButton:hover {
background-color: #0077e6;
}
QPushButton:pressed {
background-color: #006bbd;
}
""")
self.login_button.clicked.connect(self.on_login)
main_layout.addWidget(self.login_button)
main_layout.addStretch()
def on_login(self):
user_id = self.account_input.text().strip()
password = self.password_input.text().strip()
if not user_id or not password:
QMessageBox.warning(self, "登录失败", "请输入账号和密码")
return
2026-02-28 14:05:55 +08:00
logger.info(f"用户登录尝试: {user_id}")
password_md5 = hashlib.md5(password.encode()).hexdigest()
2026-02-28 14:05:55 +08:00
with log_timing_context(f"authenticate_user({user_id})", "login"):
success, user_info = self.db.authenticate_user(user_id, password_md5)
if success:
2026-02-28 14:05:55 +08:00
logger.info(f"登录成功: {user_id}")
self.login_success.emit(user_info)
self.close()
else:
2026-02-28 14:05:55 +08:00
logger.warning(f"登录失败: {user_id} - {user_info}")
QMessageBox.warning(self, "登录失败", user_info)
self.password_input.clear()
self.password_input.setFocus()