from loguru import logger from PyQt6.QtCore import QRegularExpression, Qt from PyQt6.QtGui import ( QRegularExpressionValidator, ) from PyQt6.QtWidgets import QHBoxLayout, QLabel, QVBoxLayout, QWidget from qfluentwidgets import ( LineEdit, PasswordLineEdit, PrimaryPushButton, ) from app.core import CaptchaThread class RegisterWidget(QWidget): def __init__(self, parent=None): logger.debug("初始化注册组件") super().__init__(parent) self.setObjectName("RegisterWidget") self.emailLineEdit = LineEdit(self) self.emailLineEdit.setPlaceholderText("请输入注册邮箱") self.passwordLineEdit = PasswordLineEdit(self) self.passwordLineEdit.setPlaceholderText("请输入密码") self.confirmPasswordLineEdit = PasswordLineEdit(self) self.confirmPasswordLineEdit.setPlaceholderText("请确认您的密码") self.verificationCodeLabel = QLabel(self) self.verificationCodeLabel.setFixedSize(120, 35) self.verificationCodeLabel.setScaledContents(True) # 设置图片自适应 self.verificationCodeLabel.mousePressEvent = ( self.refreshVerificationCode ) # 绑定点击事件 self.verificationCodeLineEdit = LineEdit(self) self.verificationCodeLineEdit.setPlaceholderText("请输入验证码") self.verificationLayout = QHBoxLayout() self.verificationLayout.setAlignment(Qt.AlignmentFlag.AlignVCenter) self.verificationLayout.addWidget(self.verificationCodeLineEdit) self.verificationLayout.addWidget(self.verificationCodeLabel) self.registerButton = PrimaryPushButton("注册", self) self.vBoxLayout = QVBoxLayout(self) self.vBoxLayout.setAlignment(Qt.AlignmentFlag.AlignTop) self.vBoxLayout.setSpacing(15) self.vBoxLayout.addSpacing(15) self.vBoxLayout.addWidget(self.emailLineEdit) self.vBoxLayout.addWidget(self.passwordLineEdit) self.vBoxLayout.addWidget(self.confirmPasswordLineEdit) self.vBoxLayout.addLayout(self.verificationLayout) self.vBoxLayout.addWidget(self.registerButton) email_regex = QRegularExpression( r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$" ) validator = QRegularExpressionValidator(email_regex, self) self.emailLineEdit.setValidator(validator) self.emailLineEdit.textChanged.connect(self.checkeEmail) logger.debug("注册组件初始化完成") self.refreshVerificationCode() def checkeEmail(self, text): # 检查当前输入是否通过验证器 state, _, _ = self.emailLineEdit.validator().validate(text, 0) if state == QRegularExpressionValidator.Acceptable: logger.debug("注册邮箱格式验证通过") self.registerButton.setDisabled(False) else: self.registerButton.setDisabled(True) def refreshVerificationCode(self, event=None): logger.debug("刷新验证码") self.captchaThread = CaptchaThread() self.captchaThread.captchaReady.connect(self._showVerificationCode) self.captchaThread.captchaFailed.connect(self._showCaptchaFailed) self.captchaThread.start() def _showVerificationCode(self, pixmap): logger.debug("显示验证码") self.verificationCodeLabel.setPixmap(pixmap) def _showCaptchaFailed(self, message): logger.debug(f"验证码刷新失败:{message}") self.verificationCodeLineEdit.clear()