has been completed.

This commit is contained in:
xyz
2025-09-09 15:37:57 +08:00
parent 83f3f4f74e
commit 89ff4fbac0
38 changed files with 2679 additions and 161 deletions

View File

@ -1,5 +1,11 @@
#include "phoneloginwidget.h"
#include "mainwidget.h"
#include "model/datacenter.h"
#include "toast.h"
using namespace model;
PhoneLoginWidget::PhoneLoginWidget(QWidget *parent)
: QWidget(parent)
{
@ -27,7 +33,7 @@ PhoneLoginWidget::PhoneLoginWidget(QWidget *parent)
// 4. 创建手机号输入框
QString editStyle = "QLineEdit { border: none; background-color: rgb(240, 240, 240); font-size: 20px; border-radius: 10px; padding-left: 5px;}";
phoneEdit = new QLineEdit();
phoneEdit->setPlaceholderText("输入手机");
phoneEdit->setPlaceholderText("输入邮箱");
phoneEdit->setFixedHeight(40);
phoneEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
phoneEdit->setStyleSheet(editStyle);
@ -81,24 +87,24 @@ PhoneLoginWidget::PhoneLoginWidget(QWidget *parent)
layout->addWidget(switchModeBtn, 4, 4, 1, 1);
// 11. 添加信号槽
//connect(switchModeBtn, &QPushButton::clicked, this, &PhoneLoginWidget::switchMode);
connect(switchModeBtn, &QPushButton::clicked, this, [=]() {
if (isLoginMode) {
//切换到注册模式
this->setWindowTitle("注册");
titleLabel->setText("注册");
submitBtn->setText("注册");
switchModeBtn->setText("登录");
}
else {
//切换到登录模式
this->setWindowTitle("登录");
titleLabel->setText("登录");
submitBtn->setText("登录");
switchModeBtn->setText("注册");
}
isLoginMode = !isLoginMode;
});
connect(switchModeBtn, &QPushButton::clicked, this, &PhoneLoginWidget::switchMode);
//connect(switchModeBtn, &QPushButton::clicked, this, [=]() {
// if (isLoginMode) {
// //切换到注册模式
// this->setWindowTitle("注册");
// titleLabel->setText("注册");
// submitBtn->setText("注册");
// switchModeBtn->setText("登录");
// }
// else {
// //切换到登录模式
// this->setWindowTitle("登录");
// titleLabel->setText("登录");
// submitBtn->setText("登录");
// switchModeBtn->setText("注册");
// }
// isLoginMode = !isLoginMode;
// });
connect(userModeBtn, &QPushButton::clicked, this, [=]() {
LoginWidget* loginWidget = new LoginWidget(nullptr);
@ -106,11 +112,139 @@ PhoneLoginWidget::PhoneLoginWidget(QWidget *parent)
this->close();
});
//connect(sendVerifyCodeBtn, &QPushButton::clicked, this, &PhoneLoginWidget::sendVerifyCode);
connect(sendVerifyCodeBtn, &QPushButton::clicked, this, &PhoneLoginWidget::sendVerifyCode);
//timer = new QTimer(this);
//connect(timer, &QTimer::timeout, this, &PhoneLoginWidget::countDown);
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &PhoneLoginWidget::countDown);
//connect(submitBtn, &QPushButton::clicked, this, &PhoneLoginWidget::clickSubmitBtn);
connect(submitBtn, &QPushButton::clicked, this, &PhoneLoginWidget::clickSubmitBtn);
}
void PhoneLoginWidget::sendVerifyCode()
{
sendVerifyCodeBtn->setEnabled(false);
sendVerifyCodeBtn->setStyleSheet("QPushButton { color: rgb(200, 200, 200); }");
//获取到手机验证码
const QString& phone = this->phoneEdit->text();
if (phone.isEmpty()) {
return;
}
this->currentPhone = phone;
//发送网络请求,获取验证吗
DataCenter* dataCenter = DataCenter::getInstance();
connect(dataCenter, &DataCenter::getVerifyCodeDone, this, &PhoneLoginWidget::sendVerifyCodeDone, Qt::AutoConnection);
dataCenter->getVerifyCodeAsync(phone);
//开启定时器,开始倒计时
timer->start(1000);
}
void PhoneLoginWidget::sendVerifyCodeDone(bool ok)
{
if (!ok) {
Toast::showMessage("邮箱格式错误");
sendVerifyCodeBtn->setEnabled(true);
sendVerifyCodeBtn->setText("发送验证码");
timer->stop();
sendVerifyCodeBtn->setStyleSheet("QPushButton { color: rgb(0, 0, 0); }");
}
else {
Toast::showMessage("验证码已发送");
}
}
void PhoneLoginWidget::clickSubmitBtn()
{
//从输入框中拿到必要的内容
const QString& phone = phoneEdit->text();
const QString& verifyCode = verifyCodeEdit->text();
if (phone.isEmpty() || verifyCode.isEmpty()) {
Toast::showMessage("电话或验证码不应该为空");
return;
}
//发送请求
DataCenter* dataCenter = DataCenter::getInstance();
if (isLoginMode) {
//登录
connect(dataCenter, &DataCenter::phoneLoginDone, this, &PhoneLoginWidget::phoneLoginDone, Qt::AutoConnection);
dataCenter->phoneLoginAsync(phone, verifyCode);
}
else {
//注册
connect(dataCenter, &DataCenter::phoneRegisterDone, this, &PhoneLoginWidget::phoneRegisterDone, Qt::UniqueConnection);
dataCenter->phoneRegisterAsync(phone, verifyCode);
}
}
void PhoneLoginWidget::phoneLoginDone(bool ok, const QString& reason)
{
if (!ok) {
Toast::showMessage("登录失败:" + reason);
return;
}
//跳转到主窗口
MainWidget* mainWidget = MainWidget::getInstance();
mainWidget->show();
//关闭自己
this->close();
}
void PhoneLoginWidget::phoneRegisterDone(bool ok, const QString& reason)
{
if (!ok) {
Toast::showMessage("注册失败:" + reason);
return;
}
Toast::showMessage("注册成功");
//跳转到登录页面
switchMode();
//清空一下输入框
verifyCodeEdit->clear();
//处理一下倒计时按钮
leftTime = 1;
}
void PhoneLoginWidget::countDown()
{
if (leftTime <= 1) {
//时间到了,发送按钮设为可用,并停止定时器
sendVerifyCodeBtn->setEnabled(true);
sendVerifyCodeBtn->setText("发送验证码");
timer->stop();
sendVerifyCodeBtn->setStyleSheet("QPushButton { color: rgb(0, 0, 0); }");
return;
}
leftTime -= 1;
sendVerifyCodeBtn->setText(QString::number(leftTime) + " s");
if (sendVerifyCodeBtn->isEnabled()) {
sendVerifyCodeBtn->setEnabled(false);
}
}
void PhoneLoginWidget::switchMode()
{
if (isLoginMode) {
//切换到注册模式
this->setWindowTitle("注册");
titleLabel->setText("注册");
submitBtn->setText("注册");
switchModeBtn->setText("登录");
}
else {
//切换到登录模式
this->setWindowTitle("登录");
titleLabel->setText("登录");
submitBtn->setText("登录");
switchModeBtn->setText("注册");
}
isLoginMode = !isLoginMode;
}