LoginWindow.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. #include "LoginWindow.h"
  2. #include "RegisterDialog.h"
  3. #include "ForgotPasswordDialog.h"
  4. #include "ConfirmDialog.h"
  5. #include "UiHelpers.h"
  6. #include "TitleBar.h"
  7. #include "FramelessHelper.h"
  8. #include "UserSettings.h"
  9. #include "AppConfig.h"
  10. #include "ImClient.h"
  11. #include "Protocol.h"
  12. #include "Updater.h"
  13. #include <QApplication>
  14. #include <QCheckBox>
  15. #include <QCloseEvent>
  16. #include <QEasingCurve>
  17. #include <QJsonObject>
  18. #include <QLabel>
  19. #include <QLineEdit>
  20. #include <QPointer>
  21. #include <QPropertyAnimation>
  22. #include <QPushButton>
  23. #include <QShowEvent>
  24. #include <QStackedWidget>
  25. #include <QTimer>
  26. #include <QVBoxLayout>
  27. #include <QHBoxLayout>
  28. LoginWindow::LoginWindow(QWidget *parent)
  29. : QWidget(parent)
  30. {
  31. setObjectName(QStringLiteral("ChromeWindow"));
  32. setWindowTitle(QStringLiteral("NIM"));
  33. setFixedSize(350, 400);
  34. setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
  35. setWindowIcon(UiHelpers::appIcon());
  36. auto *root = new QVBoxLayout(this);
  37. root->setContentsMargins(0, 0, 0, 0);
  38. root->setSpacing(0);
  39. m_titleBar = new TitleBar(this);
  40. m_titleBar->setTitle(QStringLiteral("NIM"));
  41. m_titleBar->setTheme(TitleBar::Light);
  42. m_titleBar->setMaximizable(false);
  43. m_titleBar->bind(this, false);
  44. root->addWidget(m_titleBar);
  45. m_header = UiHelpers::authHeader(QStringLiteral("即时通讯"), this);
  46. root->addWidget(m_header);
  47. m_stack = new QStackedWidget(this);
  48. m_registerPage = new RegisterDialog(m_stack);
  49. m_resetPage = new ForgotPasswordDialog(m_stack);
  50. m_stack->addWidget(buildLoginPage());
  51. m_stack->addWidget(m_registerPage);
  52. m_stack->addWidget(m_resetPage);
  53. root->addWidget(m_stack, 1);
  54. connect(m_registerPage, &RegisterDialog::submitted, this, &LoginWindow::onRegisterSubmitted);
  55. connect(m_registerPage, &RegisterDialog::backRequested, this, [this]() { showPage(PageLogin); });
  56. connect(m_resetPage, &ForgotPasswordDialog::submitted, this, &LoginWindow::onResetSubmitted);
  57. connect(m_resetPage, &ForgotPasswordDialog::backRequested, this, [this]() { showPage(PageLogin); });
  58. applySavedLogin();
  59. connect(&ImClient::instance(), &ImClient::limitsUpdated, this, &LoginWindow::applyServerLimits);
  60. applyServerLimits();
  61. ImClient::instance().connectToServer();
  62. tryAutoLogin();
  63. }
  64. QWidget *LoginWindow::buildLoginPage()
  65. {
  66. auto *form = new QWidget(this);
  67. auto *lay = new QVBoxLayout(form);
  68. lay->setContentsMargins(36, 28, 36, 28);
  69. lay->setSpacing(12);
  70. m_account = UiHelpers::lineEdit(QStringLiteral("账号"), form);
  71. m_password = UiHelpers::passwordEdit(QStringLiteral("密码"), form);
  72. auto *opts = new QHBoxLayout();
  73. opts->setContentsMargins(0, 2, 0, 0);
  74. m_remember = new QCheckBox(QStringLiteral("记住密码"), form);
  75. m_autoLogin = new QCheckBox(QStringLiteral("自动登录"), form);
  76. opts->addWidget(m_remember);
  77. opts->addStretch();
  78. opts->addWidget(m_autoLogin);
  79. m_error = UiHelpers::errorLabel(form);
  80. m_loginBtn = UiHelpers::primaryButton(QStringLiteral("登 录"), form);
  81. m_loginBtn->setMinimumWidth(0);
  82. auto *links = new QHBoxLayout();
  83. auto *reg = UiHelpers::linkButton(QStringLiteral("注册账号"), form);
  84. auto *forgot = UiHelpers::linkButton(QStringLiteral("忘记密码"), form);
  85. links->addWidget(reg);
  86. links->addStretch();
  87. links->addWidget(forgot);
  88. lay->addWidget(m_account);
  89. lay->addWidget(m_password);
  90. lay->addLayout(opts);
  91. lay->addWidget(m_error);
  92. lay->addSpacing(6);
  93. lay->addWidget(m_loginBtn);
  94. lay->addLayout(links);
  95. lay->addStretch();
  96. connect(m_loginBtn, &QPushButton::clicked, this, &LoginWindow::onLogin);
  97. connect(m_password, &QLineEdit::returnPressed, this, &LoginWindow::onLogin);
  98. connect(m_account, &QLineEdit::returnPressed, this, [this]() { m_password->setFocus(); });
  99. connect(reg, &QPushButton::clicked, this, [this]() { showPage(PageRegister); });
  100. connect(forgot, &QPushButton::clicked, this, [this]() { showPage(PageReset); });
  101. connect(m_autoLogin, &QCheckBox::toggled, this, [this](bool on) {
  102. if (on)
  103. m_remember->setChecked(true);
  104. });
  105. connect(m_remember, &QCheckBox::toggled, this, [this](bool on) {
  106. if (!on)
  107. m_autoLogin->setChecked(false);
  108. });
  109. return form;
  110. }
  111. void LoginWindow::showPage(Page page)
  112. {
  113. m_stack->setCurrentIndex(page);
  114. switch (page)
  115. {
  116. case PageRegister:
  117. m_titleBar->setTitle(QStringLiteral("注册账号"));
  118. UiHelpers::setAuthHeaderSubtitle(m_header, QStringLiteral("创建你的账号"));
  119. m_registerPage->resetForm();
  120. animateToHeight(580);
  121. break;
  122. case PageReset:
  123. m_titleBar->setTitle(QStringLiteral("找回密码"));
  124. UiHelpers::setAuthHeaderSubtitle(m_header, QStringLiteral("重置登录密码"));
  125. m_resetPage->resetForm();
  126. animateToHeight(540);
  127. break;
  128. case PageLogin:
  129. default:
  130. m_titleBar->setTitle(QStringLiteral("NIM"));
  131. UiHelpers::setAuthHeaderSubtitle(m_header, QStringLiteral("即时通讯"));
  132. m_account->setFocus();
  133. animateToHeight(400);
  134. break;
  135. }
  136. }
  137. void LoginWindow::animateToHeight(int h)
  138. {
  139. if (height() == h && minimumHeight() == h && maximumHeight() == h)
  140. return;
  141. if (m_heightAnim)
  142. {
  143. m_heightAnim->stop();
  144. m_heightAnim = nullptr;
  145. }
  146. setMinimumWidth(350);
  147. setMaximumWidth(350);
  148. setMinimumHeight(qMin(height(), h));
  149. setMaximumHeight(qMax(height(), h));
  150. auto *anim = new QPropertyAnimation(this, "geometry", this);
  151. anim->setDuration(220);
  152. anim->setEasingCurve(QEasingCurve::OutCubic);
  153. const QRect from = geometry();
  154. QRect to = from;
  155. to.setHeight(h);
  156. anim->setStartValue(from);
  157. anim->setEndValue(to);
  158. connect(anim, &QPropertyAnimation::finished, this, [this, h]() {
  159. setFixedSize(350, h);
  160. m_heightAnim = nullptr;
  161. });
  162. m_heightAnim = anim;
  163. anim->start(QAbstractAnimation::DeleteWhenStopped);
  164. }
  165. void LoginWindow::showEvent(QShowEvent *event)
  166. {
  167. QWidget::showEvent(event);
  168. const int h = (m_stack && m_stack->currentIndex() == PageRegister) ? 580
  169. : (m_stack && m_stack->currentIndex() == PageReset) ? 540
  170. : 400;
  171. setFixedSize(350, h);
  172. if (m_titleBar && m_titleBar->framelessHelper())
  173. m_titleBar->framelessHelper()->fixupSize();
  174. QTimer::singleShot(0, this, [this, h]() {
  175. if (m_heightAnim)
  176. return;
  177. setFixedSize(350, h);
  178. if (m_titleBar && m_titleBar->framelessHelper())
  179. m_titleBar->framelessHelper()->fixupSize();
  180. });
  181. }
  182. void LoginWindow::closeEvent(QCloseEvent *event)
  183. {
  184. QApplication::quit();
  185. QWidget::closeEvent(event);
  186. }
  187. QString LoginWindow::lastAccount() const
  188. {
  189. return m_account ? m_account->text().trimmed() : QString();
  190. }
  191. void LoginWindow::prepareAfterLogout()
  192. {
  193. m_skipAutoLogin = true;
  194. showPage(PageLogin);
  195. }
  196. void LoginWindow::showLogoutNotice(const QString &reason)
  197. {
  198. if (reason.trimmed().isEmpty())
  199. return;
  200. const QString text = reason;
  201. QTimer::singleShot(280, this, [this, text]() {
  202. if (!isVisible())
  203. return;
  204. ConfirmDialog::notice(this, QStringLiteral("下线通知"), text);
  205. });
  206. }
  207. void LoginWindow::applySavedLogin()
  208. {
  209. const auto &u = UserSettings::instance();
  210. m_account->setText(u.lastAccount());
  211. m_remember->setChecked(u.rememberPassword());
  212. m_autoLogin->setChecked(u.autoLogin());
  213. if (u.rememberPassword())
  214. m_password->setText(u.lastPassword());
  215. else
  216. m_password->clear();
  217. if (u.lastAccount().isEmpty())
  218. m_account->setFocus();
  219. else if (!u.rememberPassword() || u.lastPassword().isEmpty())
  220. m_password->setFocus();
  221. }
  222. void LoginWindow::applyServerLimits()
  223. {
  224. const auto &cfg = AppConfig::instance();
  225. if (m_account && cfg.accountMax() > 0)
  226. m_account->setMaxLength(cfg.accountMax());
  227. if (m_password && cfg.passwordMax() > 0)
  228. m_password->setMaxLength(cfg.passwordMax());
  229. if (m_registerPage)
  230. m_registerPage->applyLimits();
  231. if (m_resetPage)
  232. m_resetPage->applyLimits();
  233. Updater::promptIfNeeded(this);
  234. }
  235. void LoginWindow::tryAutoLogin()
  236. {
  237. if (m_skipAutoLogin)
  238. return;
  239. if (!willAutoLogin())
  240. return;
  241. QTimer::singleShot(0, this, &LoginWindow::onLogin);
  242. }
  243. bool LoginWindow::willAutoLogin() const
  244. {
  245. if (m_skipAutoLogin)
  246. return false;
  247. const auto &u = UserSettings::instance();
  248. if (!u.autoLogin() || !u.rememberPassword())
  249. return false;
  250. if (!m_account || !m_password)
  251. return false;
  252. return !m_account->text().trimmed().isEmpty() && !m_password->text().isEmpty();
  253. }
  254. void LoginWindow::onLogin()
  255. {
  256. if (m_loggingIn)
  257. return;
  258. UiHelpers::setError(m_error, QString());
  259. const QString account = m_account->text().trimmed();
  260. const QString password = m_password->text();
  261. if (account.isEmpty() || password.isEmpty())
  262. {
  263. UiHelpers::setError(m_error, QStringLiteral("请输入账号和密码"));
  264. return;
  265. }
  266. m_loggingIn = true;
  267. m_loginBtn->setEnabled(false);
  268. QPointer<LoginWindow> self(this);
  269. ImClient::instance().login(account, password, [self, account, password](const QJsonObject &reply) {
  270. if (!self)
  271. return;
  272. self->m_loggingIn = false;
  273. self->m_loginBtn->setEnabled(true);
  274. if (!ImClient::ok(reply))
  275. {
  276. UiHelpers::setError(self->m_error, ImClient::messageOf(reply));
  277. if (!self->isVisible())
  278. {
  279. self->show();
  280. self->raise();
  281. self->activateWindow();
  282. }
  283. return;
  284. }
  285. const QString nickname = reply.value(QStringLiteral("nickname")).toString();
  286. const QString savedAccount = reply.value(QStringLiteral("account")).toString();
  287. const QString acc = savedAccount.isEmpty() ? account : savedAccount;
  288. UserSettings::instance().saveLogin(acc,
  289. nickname.isEmpty() ? acc : nickname,
  290. password,
  291. self->m_remember->isChecked(),
  292. self->m_autoLogin->isChecked());
  293. emit self->loginSucceeded(acc, nickname.isEmpty() ? acc : nickname);
  294. });
  295. }
  296. void LoginWindow::onRegisterSubmitted(qint64 /*uid*/)
  297. {
  298. m_account->setText(m_registerPage->account());
  299. m_password->clear();
  300. UiHelpers::setError(m_error, QString());
  301. showPage(PageLogin);
  302. m_password->setFocus();
  303. UiHelpers::showToast(this, QStringLiteral("注册成功,请使用账号登录"));
  304. }
  305. void LoginWindow::onResetSubmitted()
  306. {
  307. m_password->clear();
  308. UiHelpers::setError(m_error, QString());
  309. showPage(PageLogin);
  310. m_password->setFocus();
  311. UiHelpers::showToast(this, QStringLiteral("密码已重置,请登录"));
  312. }