tlogin.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include "tlogin.h"
  2. #include "appevent.h"
  3. #include "qlabel.h"
  4. #include "qlineedit.h"
  5. #include <QGroupBox>
  6. #include <QMessageBox>
  7. #include <QMouseEvent>
  8. #include <QRegExpValidator>
  9. #include "qpushbutton.h"
  10. #include "qstyle.h"
  11. #include "qwidget.h"
  12. #include "utils/layoutbuilder.h"
  13. #include "widgets/captchalabel.h"
  14. #include "api/tloginapi.h"
  15. TLogin::TLogin(QWidget *parent)
  16. : QDialog(parent)
  17. {
  18. setObjectName("Login");
  19. setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
  20. setWindowTitle(tr("Login"));
  21. using namespace Layouting;
  22. info = new QLabel;
  23. info->setStyleSheet("color: #333333;");
  24. info->setText(tr("Welcome to the system"));
  25. // 创建登录组件
  26. usernameEdit = new QLineEdit();
  27. usernameEdit->setPlaceholderText(tr("Enter your user name"));
  28. usernameEdit->setMinimumWidth(200);
  29. passwordEdit = new QLineEdit();
  30. passwordEdit->setEchoMode(QLineEdit::Password);
  31. passwordEdit->setPlaceholderText(tr("Enter your ExamNo"));
  32. // 添加验证码输入框
  33. captchaEdit = new QLineEdit();
  34. captchaEdit->setPlaceholderText(tr("Enter captcha"));
  35. captchaEdit->setMaxLength(6);
  36. // 验证码图片显示
  37. captchaImage = new CaptchaLabel();
  38. captchaImage->setFixedSize(120, 40);
  39. captchaImage->setScaledContents(true);
  40. captchaImage->setCursor(Qt::PointingHandCursor); // 设置鼠标指针为手型,提示可点击
  41. captchaImage->setToolTip(tr("Click to refresh captcha")); // 添加提示文本
  42. // 连接 clicked 信号到刷新验证码的槽函数
  43. connect(captchaImage, &CaptchaLabel::clicked, this, &TLogin::getCaptcha);
  44. // 创建表单布局,将captchaEdit和captchaImage放在同一行
  45. QWidget *w = Group{Column{Row{st, tr("Login"), st},
  46. Form{tr("Username:"), usernameEdit,
  47. br, tr("Password:"), passwordEdit,
  48. br, tr("Captcha:"), Row{captchaEdit, captchaImage}},
  49. PushButton{bindTo(&loginPushButton),
  50. text(tr("Login")),
  51. onClicked(std::bind(&TLogin::on_login_clicked, this))}}}
  52. .emerge();
  53. QPushButton *close;
  54. auto h = Row{st, PushButton{bindTo(&close), ""}};
  55. Column{h, st, Row{st, w, st}, st, info, noMargin}.attachTo(this);
  56. // 设置美观的样式表,不使用背景图片
  57. QString styleSheet = R"(
  58. QDialog#Login {
  59. background-color: #2c3e50;
  60. border: 1px solid #34495e;
  61. border-radius: 5px;
  62. }
  63. QGroupBox {
  64. background-color: #ecf0f1;
  65. border-radius: 5px;
  66. border: 1px solid #bdc3c7;
  67. padding: 10px;
  68. }
  69. QLineEdit {
  70. border: 1px solid #bdc3c7;
  71. border-radius: 3px;
  72. padding: 5px;
  73. background-color: white;
  74. }
  75. QLineEdit:focus {
  76. border: 1px solid #3498db;
  77. background-color: #f8f9fa;
  78. }
  79. QPushButton {
  80. background-color: #3498db;
  81. color: white;
  82. border: none;
  83. border-radius: 3px;
  84. padding: 8px 16px;
  85. font-weight: bold;
  86. }
  87. QPushButton:hover {
  88. background-color: #2980b9;
  89. }
  90. QPushButton:disabled {
  91. background-color: #95a5a6;
  92. }
  93. QLabel {
  94. color: #333333;
  95. }
  96. )";
  97. setStyleSheet(styleSheet);
  98. QPixmap closePix = style()->standardPixmap(QStyle::SP_TitleBarCloseButton);
  99. close->setIcon(closePix);
  100. close->setStyleSheet(R"__(
  101. QPushButton {
  102. border: none;
  103. background-color: transparent;
  104. padding: 5px;
  105. }
  106. QPushButton:hover {
  107. background-color: #e74c3c;
  108. border-radius: 3px;
  109. }
  110. )__");
  111. resize({400, 450});
  112. loginPushButton->setEnabled(true);
  113. QObject::connect(close, &QPushButton::clicked, this, &TLogin::close);
  114. // 获取初始验证码
  115. getCaptcha();
  116. }
  117. void TLogin::getCaptcha()
  118. {
  119. bool ok = false;
  120. TC::Captcha::Data captchaData = TC::Captcha().get(&ok);
  121. if (ok) {
  122. captchaId = captchaData.captchaId;
  123. // 处理Base64图片数据
  124. QString imgData = captchaData.imgPath;
  125. if (imgData.startsWith("data:image/png;base64,")) {
  126. imgData = imgData.mid(22); // 移除前缀
  127. }
  128. QByteArray imageBytes = QByteArray::fromBase64(imgData.toLatin1());
  129. QPixmap pixmap;
  130. pixmap.loadFromData(imageBytes);
  131. captchaImage->setPixmap(pixmap);
  132. } else {
  133. info->setText(tr("Failed to get captcha"));
  134. }
  135. }
  136. void TLogin::on_refresh_captcha_clicked()
  137. {
  138. getCaptcha();
  139. }
  140. void TLogin::mousePressEvent(QMouseEvent *event)
  141. {
  142. if (event->button() == Qt::LeftButton) {
  143. m_dragPosition = event->globalPos() - frameGeometry().topLeft();
  144. event->accept();
  145. }
  146. }
  147. void TLogin::mouseMoveEvent(QMouseEvent *event)
  148. {
  149. if (event->buttons() & Qt::LeftButton) {
  150. move(event->globalPos() - m_dragPosition);
  151. event->accept();
  152. }
  153. }
  154. void TLogin::on_login_clicked()
  155. {
  156. const QString userName = usernameEdit->text();
  157. const QString password = passwordEdit->text();
  158. const QString captcha = captchaEdit->text();
  159. if (userName.isEmpty() || password.isEmpty()) {
  160. QMessageBox::warning(this,
  161. tr("Login failed"),
  162. tr("User name or password cannot be empty."));
  163. return;
  164. }
  165. if (captcha.isEmpty()) {
  166. QMessageBox::warning(this,
  167. tr("Login failed"),
  168. tr("Captcha cannot be empty."));
  169. return;
  170. }
  171. // 这里需要修改Login API,添加验证码参数
  172. // 由于我们没有看到Login类的完整实现,这里假设需要修改Login构造函数
  173. // 实际实现时需要根据后端API的要求进行调整
  174. // 修改Login类构造函数,添加captchaId和captcha参数
  175. // 这里只是示例,实际实现可能需要调整
  176. bool ok = TC::Login(userName, password, captchaId, captcha).post();
  177. if (ok && AppEvent::instance()->isLogin()) {
  178. // 登录成功,发出信号
  179. emit loginSuccessful();
  180. close();
  181. } else {
  182. // 登录失败,显示提示框并刷新验证码
  183. QMessageBox::warning(this,
  184. tr("Login failed"),
  185. tr("User name, password or captcha error, please try again."));
  186. getCaptcha(); // 刷新验证码
  187. captchaEdit->clear(); // 清空验证码输入框
  188. return;
  189. }
  190. }