| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- """
- 登录对话框,进入主界面前进行身份认证
- """
- import os
- import time
- import logging
- from typing import Optional, Dict
- from PyQt5.QtWidgets import (
- QDialog, QVBoxLayout, QLabel, QLineEdit, QPushButton, QHBoxLayout,
- QFrame, QMessageBox
- )
- from PyQt5.QtGui import QFont, QIcon
- from PyQt5.QtCore import Qt
- from database import Database, get_base_path
- from .button_styles import apply_button_style
- startup_logger = logging.getLogger('startup')
- class LoginDialog(QDialog):
- """带背景图的登录对话框"""
-
- def __init__(self, db: Database, parent=None):
- dialog_start = time.time()
- super().__init__(parent)
- self.db = db
- self.logged_in_user: Optional[Dict] = None
-
- ui_start = time.time()
- self.init_ui()
- startup_logger.info(f" 登录对话框界面初始化 (耗时: {time.time() - ui_start:.3f}秒)")
-
- def init_ui(self):
- self.setWindowTitle("系统登录")
- self.setModal(True)
- self.setFixedSize(640, 420)
- self.setWindowFlags(self.windowFlags() & ~Qt.WindowContextHelpButtonHint)
-
- # 设置对话框图标
- icon_path = os.path.join(get_base_path(), "logo.ico")
- if os.path.exists(icon_path):
- self.setWindowIcon(QIcon(icon_path))
-
- # 设置背景图
- image_path = os.path.join(get_base_path(), "sx.jpg")
- if os.path.exists(image_path):
- image_url = image_path.replace("\\", "/")
- self.setStyleSheet(f"""
- QDialog {{
- border-image: url("{image_url}") 0 0 0 0 stretch stretch;
- }}
- """)
- else:
- self.setStyleSheet("background-color: #1f2933;")
-
- outer_layout = QVBoxLayout()
- outer_layout.setContentsMargins(30, 30, 30, 30)
- outer_layout.addStretch()
-
- container = QFrame()
- container.setObjectName("loginContainer")
- container.setStyleSheet("""
- QFrame#loginContainer {
- background-color: rgba(255, 255, 255, 0.9);
- border-radius: 16px;
- padding: 30px;
- }
- """)
-
- container_layout = QVBoxLayout()
- container_layout.setSpacing(18)
-
- title = QLabel("地磅称重打印软件 - 登录")
- title.setAlignment(Qt.AlignCenter)
- title.setFont(QFont("微软雅黑", 20, QFont.Bold))
- container_layout.addWidget(title)
-
- self.username_input = QLineEdit()
- self.username_input.setPlaceholderText("请输入账号")
- self.username_input.setMinimumHeight(48)
- self.username_input.setFont(QFont("微软雅黑", 16))
- # 默认用户名改为“过磅员”
- self.username_input.setText("过磅员")
-
- self.password_input = QLineEdit()
- self.password_input.setPlaceholderText("请输入密码")
- self.password_input.setMinimumHeight(48)
- self.password_input.setFont(QFont("微软雅黑", 16))
- self.password_input.setEchoMode(QLineEdit.Password)
- # 默认密码
- self.password_input.setText("sx123456")
-
- container_layout.addWidget(self.username_input)
- container_layout.addWidget(self.password_input)
-
- self.error_label = QLabel("")
- self.error_label.setStyleSheet("color: #d32f2f;")
- self.error_label.setAlignment(Qt.AlignCenter)
- self.error_label.setFont(QFont("微软雅黑", 14))
- container_layout.addWidget(self.error_label)
-
- button_layout = QHBoxLayout()
- button_layout.setSpacing(15)
-
- self.login_btn = QPushButton("登录")
- self.login_btn.setMinimumHeight(48)
- self.login_btn.setFont(QFont("微软雅黑", 16, QFont.Bold))
- apply_button_style(self.login_btn, 'green')
- self.login_btn.clicked.connect(self.handle_login)
-
- self.cancel_btn = QPushButton("退出")
- self.cancel_btn.setMinimumHeight(48)
- self.cancel_btn.setFont(QFont("微软雅黑", 16))
- apply_button_style(self.cancel_btn, 'normal')
- self.cancel_btn.clicked.connect(self.reject)
-
- button_layout.addWidget(self.login_btn)
- button_layout.addWidget(self.cancel_btn)
-
- container_layout.addLayout(button_layout)
- container.setLayout(container_layout)
-
- outer_layout.addWidget(container)
- outer_layout.addStretch()
- self.setLayout(outer_layout)
-
- self.password_input.returnPressed.connect(self.handle_login)
- self.username_input.returnPressed.connect(self.handle_login)
- self.password_input.setFocus()
-
- def handle_login(self):
- username = self.username_input.text().strip()
- password = self.password_input.text()
-
- if not username or not password:
- self.error_label.setText("账号和密码均不能为空")
- return
-
- try:
- user = self.db.authenticate_user(username, password)
- except Exception as exc:
- QMessageBox.critical(self, "错误", f"登录失败:{str(exc)}")
- return
-
- if user:
- self.logged_in_user = user
- self.accept()
- else:
- self.error_label.setText("账号或密码错误,请重试")
|