login_dialog.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. """
  2. 登录对话框,进入主界面前进行身份认证
  3. """
  4. import os
  5. import time
  6. import logging
  7. from typing import Optional, Dict
  8. from PyQt5.QtWidgets import (
  9. QDialog, QVBoxLayout, QLabel, QLineEdit, QPushButton, QHBoxLayout,
  10. QFrame, QMessageBox
  11. )
  12. from PyQt5.QtGui import QFont, QIcon
  13. from PyQt5.QtCore import Qt
  14. from database import Database, get_base_path
  15. from .button_styles import apply_button_style
  16. startup_logger = logging.getLogger('startup')
  17. class LoginDialog(QDialog):
  18. """带背景图的登录对话框"""
  19. def __init__(self, db: Database, parent=None):
  20. dialog_start = time.time()
  21. super().__init__(parent)
  22. self.db = db
  23. self.logged_in_user: Optional[Dict] = None
  24. ui_start = time.time()
  25. self.init_ui()
  26. startup_logger.info(f" 登录对话框界面初始化 (耗时: {time.time() - ui_start:.3f}秒)")
  27. def init_ui(self):
  28. self.setWindowTitle("系统登录")
  29. self.setModal(True)
  30. self.setFixedSize(640, 420)
  31. self.setWindowFlags(self.windowFlags() & ~Qt.WindowContextHelpButtonHint)
  32. # 设置对话框图标
  33. icon_path = os.path.join(get_base_path(), "logo.ico")
  34. if os.path.exists(icon_path):
  35. self.setWindowIcon(QIcon(icon_path))
  36. # 设置背景图
  37. image_path = os.path.join(get_base_path(), "sx.jpg")
  38. if os.path.exists(image_path):
  39. image_url = image_path.replace("\\", "/")
  40. self.setStyleSheet(f"""
  41. QDialog {{
  42. border-image: url("{image_url}") 0 0 0 0 stretch stretch;
  43. }}
  44. """)
  45. else:
  46. self.setStyleSheet("background-color: #1f2933;")
  47. outer_layout = QVBoxLayout()
  48. outer_layout.setContentsMargins(30, 30, 30, 30)
  49. outer_layout.addStretch()
  50. container = QFrame()
  51. container.setObjectName("loginContainer")
  52. container.setStyleSheet("""
  53. QFrame#loginContainer {
  54. background-color: rgba(255, 255, 255, 0.9);
  55. border-radius: 16px;
  56. padding: 30px;
  57. }
  58. """)
  59. container_layout = QVBoxLayout()
  60. container_layout.setSpacing(18)
  61. title = QLabel("地磅称重打印软件 - 登录")
  62. title.setAlignment(Qt.AlignCenter)
  63. title.setFont(QFont("微软雅黑", 20, QFont.Bold))
  64. container_layout.addWidget(title)
  65. self.username_input = QLineEdit()
  66. self.username_input.setPlaceholderText("请输入账号")
  67. self.username_input.setMinimumHeight(48)
  68. self.username_input.setFont(QFont("微软雅黑", 16))
  69. # 默认用户名改为“过磅员”
  70. self.username_input.setText("过磅员")
  71. self.password_input = QLineEdit()
  72. self.password_input.setPlaceholderText("请输入密码")
  73. self.password_input.setMinimumHeight(48)
  74. self.password_input.setFont(QFont("微软雅黑", 16))
  75. self.password_input.setEchoMode(QLineEdit.Password)
  76. # 默认密码
  77. self.password_input.setText("sx123456")
  78. container_layout.addWidget(self.username_input)
  79. container_layout.addWidget(self.password_input)
  80. self.error_label = QLabel("")
  81. self.error_label.setStyleSheet("color: #d32f2f;")
  82. self.error_label.setAlignment(Qt.AlignCenter)
  83. self.error_label.setFont(QFont("微软雅黑", 14))
  84. container_layout.addWidget(self.error_label)
  85. button_layout = QHBoxLayout()
  86. button_layout.setSpacing(15)
  87. self.login_btn = QPushButton("登录")
  88. self.login_btn.setMinimumHeight(48)
  89. self.login_btn.setFont(QFont("微软雅黑", 16, QFont.Bold))
  90. apply_button_style(self.login_btn, 'green')
  91. self.login_btn.clicked.connect(self.handle_login)
  92. self.cancel_btn = QPushButton("退出")
  93. self.cancel_btn.setMinimumHeight(48)
  94. self.cancel_btn.setFont(QFont("微软雅黑", 16))
  95. apply_button_style(self.cancel_btn, 'normal')
  96. self.cancel_btn.clicked.connect(self.reject)
  97. button_layout.addWidget(self.login_btn)
  98. button_layout.addWidget(self.cancel_btn)
  99. container_layout.addLayout(button_layout)
  100. container.setLayout(container_layout)
  101. outer_layout.addWidget(container)
  102. outer_layout.addStretch()
  103. self.setLayout(outer_layout)
  104. self.password_input.returnPressed.connect(self.handle_login)
  105. self.username_input.returnPressed.connect(self.handle_login)
  106. self.password_input.setFocus()
  107. def handle_login(self):
  108. username = self.username_input.text().strip()
  109. password = self.password_input.text()
  110. if not username or not password:
  111. self.error_label.setText("账号和密码均不能为空")
  112. return
  113. try:
  114. user = self.db.authenticate_user(username, password)
  115. except Exception as exc:
  116. QMessageBox.critical(self, "错误", f"登录失败:{str(exc)}")
  117. return
  118. if user:
  119. self.logged_in_user = user
  120. self.accept()
  121. else:
  122. self.error_label.setText("账号或密码错误,请重试")