From 83f3f4f74e49bf4a03c872452ae87a01521f7d10 Mon Sep 17 00:00:00 2001 From: xyz <2050965275@qq.com> Date: Wed, 30 Jul 2025 11:38:07 +0800 Subject: [PATCH] add button functionality --- SelfInfoWidget.cpp | 246 +++++++++++++++++++++++++++++++++++++++--- SelfInfoWidget.h | 24 +++++ mainwidget.cpp | 8 ++ messageshowarea.cpp | 13 +++ model/datacenter.cpp | 82 ++++++++++++++ model/datacenter.h | 30 ++++++ network/netclient.cpp | 186 ++++++++++++++++++++++++++++++++ network/netclient.h | 7 +- userinfowidget.cpp | 51 ++++++--- userinfowidget.h | 2 + 10 files changed, 623 insertions(+), 26 deletions(-) diff --git a/SelfInfoWidget.cpp b/SelfInfoWidget.cpp index d9bfbc9..47064d8 100644 --- a/SelfInfoWidget.cpp +++ b/SelfInfoWidget.cpp @@ -1,11 +1,8 @@ #include "selfinfowidget.h" -#include -#include - #include "model/datacenter.h" - +using namespace model; SelfInfoWidget::SelfInfoWidget(QWidget *parent) : QDialog(parent) @@ -20,7 +17,7 @@ SelfInfoWidget::SelfInfoWidget(QWidget *parent) this->move(QCursor::pos()); //创建布局管理器 - QGridLayout* layout = new QGridLayout(); + layout = new QGridLayout(); //layout->setSpacing(0); layout->setHorizontalSpacing(10); layout->setVerticalSpacing(3); @@ -32,14 +29,12 @@ SelfInfoWidget::SelfInfoWidget(QWidget *parent) avatarBtn = new QPushButton(); avatarBtn->setFixedSize(75, 75); avatarBtn->setIconSize(QSize(75, 75)); - //avatarBtn->setIcon(QIcon(":/resource/image/defaultAvatar.png")); - avatarBtn->setIcon(QIcon(":/resource/image/defaultAvatar.png")); avatarBtn->setStyleSheet("QPushButton { border: none; background-color: transparent; }"); QString labelStyle = "QLabel { font-size: 14px; font-weight: 800; }"; QString btnStyle = "QPushButton {border: none; background-color: transparent; }"; btnStyle += "QPushButton:pressed { background-color: rgb(210, 210, 210); }"; - QString editStyle = "QLineEdit { border: none; background-color: rgb(255 ,255, 255); border-radius: 5px; padding-left: 2px; }"; + QString editStyle = "QLineEdit { border: none; background-color: rgb(250 ,250, 250); color: rgb(0, 0, 0); border-radius: 5px; padding-left: 2px; }"; int height = 30; @@ -118,12 +113,13 @@ SelfInfoWidget::SelfInfoWidget(QWidget *parent) phoneTag = new QLabel(); phoneTag->setFixedSize(50, height); phoneTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); - phoneTag->setText("电话"); + phoneTag->setText("邮箱"); phoneTag->setStyleSheet(labelStyle); phoneLabel = new QLabel(); phoneLabel->setFixedHeight(height); phoneLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); + //phoneLabel->setStyleSheet("QLabel {background-color: rgb(255, 255, 255); }"); phoneModifyBtn = new QPushButton(); phoneModifyBtn->setFixedSize(70, 25); @@ -134,7 +130,8 @@ SelfInfoWidget::SelfInfoWidget(QWidget *parent) phoneEdit = new QLineEdit(); phoneEdit->setFixedHeight(height); phoneEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); - phoneEdit->setStyleSheet(btnStyle); + phoneEdit->setStyleSheet(editStyle); + //phoneEdit->setStyleSheet("QLineEdit {background-color: rgb(245, 245, 245); }"); phoneEdit->hide(); phoneSubmitBtn = new QPushButton(); @@ -196,8 +193,34 @@ SelfInfoWidget::SelfInfoWidget(QWidget *parent) nameLabel->setText("xyz"); descLabel->setText("It didn't matter that i lived another day."); phoneLabel->setText("12345678900"); + //此处只做测试,不要让界面显示默认头像 + avatarBtn->setIcon(QIcon(":/resource/image/defaultAvatar.png")); #endif + initSingnalSlots(); + + //加载数据到界面上 + DataCenter* dataCenter = DataCenter::getInstance(); + UserInfo* myself = dataCenter->getMyselfsync(); + if (myself != nullptr) { + //把个人信息加载到界面上 + avatarBtn->setIcon(myself->avatar); + idLabel->setText(myself->userId); + nameLabel->setText(myself->nickname); + descLabel->setText(myself->description); + phoneLabel->setText(myself->phone); + } + + /*connect(nameSubmitBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickNameSubmitBtn); + connect(descSubmitBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickDescSubmitBtn); + connect(getVerifyCodeBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickGetVerifyCodeBtn); + connect(phoneSubmitBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickPhoneSubmitBtn); + connect(avatarBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickAvatarBtn);*/ + +} + +void SelfInfoWidget::initSingnalSlots() +{ //添加连接的槽函数 connect(nameModifyBtn, &QPushButton::clicked, this, [=]() { //把当前的nameLabel和nameModifyBtn隐藏起来 @@ -249,10 +272,209 @@ SelfInfoWidget::SelfInfoWidget(QWidget *parent) phoneEdit->setText(phoneLabel->text()); }); - /*connect(nameSubmitBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickNameSubmitBtn); + connect(nameSubmitBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickNameSubmitBtn); connect(descSubmitBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickDescSubmitBtn); connect(getVerifyCodeBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickGetVerifyCodeBtn); connect(phoneSubmitBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickPhoneSubmitBtn); - connect(avatarBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickAvatarBtn);*/ + connect(avatarBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickAvatarBtn); +} + +void SelfInfoWidget::clickNameSubmitBtn() +{ + //从输入框中拿到修改后的呢称 + const QString& nickName = nameEdit->text(); + if (nickName.isEmpty()) { + return; + } + + //发送网络请求 + DataCenter* dataCenter = DataCenter::getInstance(); + connect(dataCenter, &DataCenter::changeNicknameDone, this, &SelfInfoWidget::clickNameSubmitBtnDone, Qt::UniqueConnection); + dataCenter->changeNicknameAsync(nickName); +} + +void SelfInfoWidget::clickNameSubmitBtnDone() +{ + //对界面控件进行切换, 把输入框切换为label, 把提交按钮切换为编辑按钮 + //同时要把输入框的内容设置为修改后的呢称 + layout->removeWidget(nameEdit); + nameEdit->hide(); + layout->addWidget(nameLabel, 1, 2); + nameLabel->show(); + nameLabel->setText(nameEdit->text()); + + layout->removeWidget(nameSubmitBtn); + nameSubmitBtn->hide(); + layout->addWidget(nameModifyBtn, 1, 3); + nameModifyBtn->show(); +} + +void SelfInfoWidget::clickDescSubmitBtn() +{ + //从输入框拿到输入的签名内容 + const QString& desc = descEdit->text(); + if (desc.isEmpty()) { + return; + } + + //发送网络请求 + DataCenter* dataCenter = DataCenter::getInstance(); + connect(dataCenter, &DataCenter::changeDescriptionDone, this, &SelfInfoWidget::clickDescSubmitBtnDone, Qt::UniqueConnection); + dataCenter->changeDescriptionAsync(desc); +} + +void SelfInfoWidget::clickDescSubmitBtnDone() +{ + //切换界面 + //把label替换为输入框,把提交按钮替换为编辑按钮 + layout->removeWidget(descEdit); + descEdit->hide(); + + layout->addWidget(descLabel, 2, 2); + descLabel->show(); + descLabel->setText(descEdit->text()); + + layout->removeWidget(descSubmitBtn); + descSubmitBtn->hide(); + layout->addWidget(descModifyBtn, 2, 3); + descModifyBtn->show(); +} + +void SelfInfoWidget::clickGetVerifyCodeBtn() +{ + //获取到输入框的邮箱号 + const QString& email = phoneEdit->text(); + if (email.isEmpty()) { + + QTimer* timer = new QTimer(this); + int* leftTime = new int(2); + phoneEdit->setPlaceholderText("请输入邮箱"); + phoneEdit->setStyleSheet("QLineEdit { border: 1px solid red; background-color: rgb(255, 220, 220); color: rgb(0, 0, 0); border-radius: 5px; padding-left: 2px; }"); + connect(timer, &QTimer::timeout, this, [=]() { + if (*leftTime <= 0) { + phoneEdit->setPlaceholderText(""); + phoneEdit->setStyleSheet("QLineEdit { border: none; background-color: rgb(250 ,250, 250); color: rgb(0, 0, 0); border-radius: 5px; padding-left: 2px; }"); + timer->stop(); + timer->deleteLater(); + delete timer; + delete leftTime; + return; + } + (*leftTime)--; + }); + timer->start(1000); // 1秒后清除提示 + return; + } + + //给服务器发送请求 + DataCenter* dataCenter = DataCenter::getInstance(); + connect(dataCenter, &DataCenter::getVerifyCodeDone, this, [=]() { + //不需要做其他的处理,只需要提示一下,验证码已经发送出去了 + Toast::showMessage("验证码已发送,请注意查收"); + }); + dataCenter->getVerifyCodeAsync(email); + + //把刚才发送请求的邮箱号码保存起来 + this->emailToChange = email; + + //禁用发送验证码的按钮,给出倒计时 + this->getVerifyCodeBtn->setEnabled(false); + + leftTime = 30; // 倒计时30秒 + QTimer* timer = new QTimer(this); + connect(timer, &QTimer::timeout, this, [=]() { + if (leftTime <= 0) { + //倒计时结束 + getVerifyCodeBtn->setEnabled(true); + getVerifyCodeBtn->setText("获取验证码"); + timer->stop(); + timer->deleteLater(); + return; + } + (leftTime)--; + getVerifyCodeBtn->setText(QString::number(leftTime) + "s"); + }); + timer->start(1000); // 每秒更新一次 +} + +void SelfInfoWidget::clickPhoneSubmitBtn() +{ + //判定,当前的验证码是否已经收到 + DataCenter* dataCenter = DataCenter::getInstance(); + QString verifyCodeId = dataCenter->getVerifyCodeId(); + if (verifyCodeId.isEmpty()) { + //服务器还没有返回验证码响应 + Toast::showMessage("服务器未返回响应,请稍后重试"); + return; + } + + //如果当前已经拿到了verifyCodeId,就可以清空DataCenter中存储的值了,确保下次点击提交按钮的时候,不会影响当次的逻辑 + dataCenter->resetVerifyCodeId(""); + + //获取到用户输入的验证码 + QString verifyCode = verifyCodeEdit->text(); + if (verifyCode.isEmpty()) { + Toast::showMessage("验证码不能为空"); + return; + } + verifyCodeEdit->setText(""); + + //发送请求,把当前验证码信息,发送给服务器 + connect(dataCenter, &DataCenter::changePhoneDone, this, &SelfInfoWidget::clickPhoneSubmitBtnDone, Qt::UniqueConnection); + dataCenter->changePhoneAsync(this->emailToChange, verifyCodeId, verifyCode); + + leftTime = 1; } + +void SelfInfoWidget::clickPhoneSubmitBtnDone() +{ + layout->removeWidget(verifyCodeTag); + layout->removeWidget(verifyCodeEdit); + layout->removeWidget(getVerifyCodeBtn); + layout->removeWidget(phoneEdit); + layout->removeWidget(phoneSubmitBtn); + verifyCodeTag->hide(); + verifyCodeEdit->hide(); + getVerifyCodeBtn->hide(); + phoneEdit->hide(); + phoneSubmitBtn->hide(); + + layout->addWidget(phoneLabel, 3, 2); + phoneLabel->show(); + layout->addWidget(phoneModifyBtn, 3, 3); + phoneModifyBtn->show(); + + phoneLabel->setText(this->emailToChange); + +} + +void SelfInfoWidget::clickAvatarBtn() +{ + //弹出对话框,选择文件 + QString filter = "Image Files (*.png *.jpg *.jpeg)"; + QString imagePath = QFileDialog::getOpenFileName(this, "选择头像", QDir::homePath(), filter); + if (imagePath.isEmpty()) { + //用户取消了 + LOG() << "用户没有选择任何头像文件"; + return; + } + + //根据路径,读取到图片的内容 + QByteArray imageBytes = loadFileToByteArray(imagePath); + + //发送请求,修改头像 + DataCenter* dataCenter = DataCenter::getInstance(); + connect(dataCenter, &DataCenter::changeAvatarDone, this, &SelfInfoWidget::clickAvatarBtnDone, Qt::UniqueConnection); + dataCenter->changeAvatarAsync(imageBytes); +} + +void SelfInfoWidget::clickAvatarBtnDone() +{ + //设置头像,更新到界面上 + DataCenter* dataCenter = DataCenter::getInstance(); + avatarBtn->setIcon(dataCenter->getMyselfsync()->avatar); + +} + + diff --git a/SelfInfoWidget.h b/SelfInfoWidget.h index c1dc263..19fbaca 100644 --- a/SelfInfoWidget.h +++ b/SelfInfoWidget.h @@ -5,7 +5,12 @@ #include #include #include +#include +#include +#include +#include #include "debug.h" +#include "toast.h" class SelfInfoWidget : public QDialog { @@ -15,7 +20,22 @@ public: SelfInfoWidget(QWidget *parent); //~SelfInfoWidget(); + void initSingnalSlots(); + + void clickNameSubmitBtn(); + void clickNameSubmitBtnDone(); + + void clickDescSubmitBtn(); + void clickDescSubmitBtnDone(); + void clickGetVerifyCodeBtn(); + void clickPhoneSubmitBtn(); + void clickPhoneSubmitBtnDone(); + void clickAvatarBtn(); + void clickAvatarBtnDone(); + private: + QGridLayout* layout; + QPushButton* avatarBtn; QLabel* idTag; //序号标签 QLabel* idLabel; //序号 @@ -41,4 +61,8 @@ private: QLabel* verifyCodeTag;//显示验证码 QLineEdit* verifyCodeEdit;//输入验证码 QPushButton* getVerifyCodeBtn;//获取验证码按钮 + + //要修改新的手机号码 + QString emailToChange; + int leftTime = 30; }; diff --git a/mainwidget.cpp b/mainwidget.cpp index acc6ee7..cce311a 100644 --- a/mainwidget.cpp +++ b/mainwidget.cpp @@ -308,6 +308,14 @@ void MainWidget::initSignalSlot() // 获取好友申请的列表 ///////////////////////////////////// loadApplyList(); + + ///////////////////////////////////// + // 处理修改头像 + ///////////////////////////////////// + connect(dataCenter, &DataCenter::changeAvatarDone, this, [=]() { + UserInfo* myself = dataCenter->getMyselfsync(); + userAvatar->setIcon(myself->avatar); + }); } void MainWidget::switchTabToSession() diff --git a/messageshowarea.cpp b/messageshowarea.cpp index da62bf6..c405d20 100644 --- a/messageshowarea.cpp +++ b/messageshowarea.cpp @@ -3,6 +3,7 @@ #include "mainwidget.h" #include "userinfowidget.h" #include +#include "model/datacenter.h" MessageShowArea::MessageShowArea() { //初始化基本属性 @@ -175,6 +176,18 @@ MessageItem *MessageItem::makeMessageItem(bool isLeft, const Message &message) userInfoWidget->exec(); }); + //当用户修改了昵称的时候,更新名字的显示 + if (!isLeft) { + DataCenter* dataCenter = DataCenter::getInstance(); + connect(dataCenter, &DataCenter::changeNicknameDone, messageItem, [=]() { + nameLabel->setText(dataCenter->getMyselfsync()->nickname + " " + message.time); + }); + + connect(dataCenter, &DataCenter::changeAvatarDone, messageItem, [=]() { + UserInfo* myself = dataCenter->getMyselfsync(); + avatarBtn->setIcon(myself->avatar); + }); + } return messageItem; } diff --git a/model/datacenter.cpp b/model/datacenter.cpp index 0f70921..7fd75d3 100644 --- a/model/datacenter.cpp +++ b/model/datacenter.cpp @@ -294,6 +294,75 @@ namespace model netClient.sendMessage(loginSessionId, chatSessionId, MessageType::TEXT_TYPE, content.toUtf8(), ""); } + void DataCenter::changeNicknameAsync(const QString& nickname) + { + netClient.changeNickname(loginSessionId, nickname); + } + + void DataCenter::resetNickname(const QString& nickname) + { + if (myself == nullptr) { + return; + } + myself->nickname = nickname; + } + + void DataCenter::changeDescriptionAsync(const QString& desc) + { + netClient.changeDescription(loginSessionId, desc); + } + + void DataCenter::resetDescription(const QString& desc) + { + if (myself == nullptr) { + return; + } + myself->description = desc; + } + + void DataCenter::getVerifyCodeAsync(const QString& email) + { + //这个操作,不需要loginSessionId + // + netClient.getVerifyCode(email); + } + + void DataCenter::resetVerifyCodeId(const QString& verifyCodeId) + { + this->currentVerifyCodeId = verifyCodeId; + } + + const QString& DataCenter::getVerifyCodeId() const + { + return currentVerifyCodeId; + } + + void DataCenter::changePhoneAsync(const QString& email, const QString& verifyCodeId, const QString& verifyCode) + { + netClient.changeEmail(loginSessionId, email, verifyCodeId, verifyCode); + } + + void DataCenter::resetPhone(const QString& email) + { + if (myself == nullptr) { + return; + } + myself->phone = email; + } + + void DataCenter::changeAvatarAsync(const QByteArray& imageBytes) + { + netClient.changeAvatar(loginSessionId, imageBytes); + } + + void DataCenter::resetAvatar(const QByteArray& avatar) + { + if (myself == nullptr) { + return; + } + myself->avatar = makeIcon(avatar); + } + ChatSessionInfo* DataCenter::findChatSessionById(const QString& chatSessionId) { if (chatSessionList == nullptr) { @@ -347,6 +416,19 @@ namespace model chatSessionList->push_front(backup); } + UserInfo* DataCenter::findFriendById(const QString& userId) + { + if (this->friendList == nullptr) { + return nullptr; + } + for (auto& f : *friendList) { + if (f.userId == userId) { + return &f; + } + } + return nullptr; + } + void DataCenter::setCurrentChatSessionId(const QString& chatSessionId) { this->currentChatSessionId = chatSessionId; diff --git a/model/datacenter.h b/model/datacenter.h index 8afd343..43f26eb 100644 --- a/model/datacenter.h +++ b/model/datacenter.h @@ -134,6 +134,29 @@ namespace model //发送消息给服务器 void sendTextMessageAsync(const QString& chatSessionId, const QString& content); + //修改用户昵称 + void changeNicknameAsync(const QString& nickname); + void resetNickname(const QString& nickname); + + //修改用户签名 + void changeDescriptionAsync(const QString& desc); + void resetDescription(const QString& desc); + + //获取邮箱验证码 + void getVerifyCodeAsync(const QString& email); + void resetVerifyCodeId(const QString& verifyCodeId); + + //获取verifyCodeId + const QString& getVerifyCodeId() const; + + //修改邮箱号码 + void changePhoneAsync(const QString& email, const QString& verifyCodeId, const QString& verifyCode); + void resetPhone(const QString& email); + + //修改头像 + void changeAvatarAsync(const QByteArray& imageBytes); + void resetAvatar(const QByteArray& avatar); + /////////////////////////////////////////////////////////////////////////////////// ///辅助函数 /////////////////////////////////////////////////////////////////////////////// @@ -144,6 +167,8 @@ namespace model ChatSessionInfo* findChatSessionByUserId(const QString& userId); //把指定的会话信息,放到列表头部 void topCurrentChatSessionId(const ChatSessionInfo& chatSessionInfo); + //根据用户id查询好友信息 + UserInfo* findFriendById(const QString& userId); //设置/获取当前选中的会话 void setCurrentChatSessionId(const QString& chatSessionId); @@ -162,5 +187,10 @@ namespace model void sendMessageDone(MessageType messageType, const QByteArray& content, const QString& extraInfo); void updateLastMessage(const QString& chatSessionId); void receiveMessageDone(const Message& lastMessage); + void changeNicknameDone(); + void changeDescriptionDone(); + void getVerifyCodeDone(); + void changePhoneDone(); + void changeAvatarDone(); }; } //end namespace model diff --git a/network/netclient.cpp b/network/netclient.cpp index d42486d..1347b54 100644 --- a/network/netclient.cpp +++ b/network/netclient.cpp @@ -433,4 +433,190 @@ namespace network { //统一更新会话列表的消息预览 emit dataCenter->updateLastMessage(chatSessionId); } + void NetClient::changeNickname(const QString& loginSessionId, const QString& nickname) + { + //通过 protobuf 构造请求body + bite_im::SetUserNicknameReq pbReq; + pbReq.setRequestId(makeRequestId());; + pbReq.setSessionId(loginSessionId); + pbReq.setNickname(nickname); + + QByteArray body = pbReq.serialize(&serializer); + LOG() << "修改用户昵称->发送请求 requestId= " << pbReq.requestId() << ", loginSessionId= " << pbReq.sessionId() + << ", nickname= " << pbReq.nickname(); + + //发送HTTP请求 + QNetworkReply* resp = this->sendHttpRequest("/service/user/set_nickname", body); + + //处理HTTP响应 + connect(resp, & QNetworkReply::finished, this, [=]() { + //解析响应 + bool ok = false; + QString reason; + auto pbResp = this->handleHttpResponse(resp, &ok, &reason); + //判定响应是否正确 + if (!ok) { + LOG() << "修改用户昵称->处理出错 reason= " << reason; + return; + } + //把结果写入到DataCenter中 + dataCenter->resetNickname(nickname); + + //通知调用者,响应已经处理完毕 + emit dataCenter->changeNicknameDone(); + LOG() << "修改用户昵称->响应处理完毕 requestId= " << pbResp->requestId(); + }); + + } + + void NetClient::changeDescription(const QString& loginSessionId, const QString& desc) + { + //通过 protobuf 构造请求body + bite_im::SetUserDescriptionReq pbReq; + pbReq.setRequestId(makeRequestId()); + pbReq.setSessionId(loginSessionId); + pbReq.setDescription(desc); + QByteArray body = pbReq.serialize(&serializer); + LOG() << "修改用户描述->发送请求 requestId= " << pbReq.requestId() << ", loginSessionId= " << pbReq.sessionId() + << ", description= " << pbReq.description(); + + //发送HTTP请求 + QNetworkReply* resp = this->sendHttpRequest("/service/user/set_description", body); + + //处理HTTP响应 + connect(resp, &QNetworkReply::finished, this, [=]() { + //解析响应 + bool ok = false; + QString reason; + auto pbResp = this->handleHttpResponse(resp, &ok, &reason); + + //判定响应是否正确 + if (!ok) { + LOG() << "修改用户描述->处理出错 reason= " << reason; + return; + } + + //把得到的结果写入到DataCenter中 + dataCenter->resetDescription(desc); + + //发送信号,通知修改完成 + emit dataCenter->changeDescriptionDone(); + + //打印日志 + LOG() << "修改用户描述->响应处理完毕 requestId= " << pbResp->requestId(); + }); + } + + void NetClient::getVerifyCode(const QString& email) + { + //构造请求body + bite_im::PhoneVerifyCodeReq pbReq; + pbReq.setRequestId(makeRequestId()); + pbReq.setPhoneNumber(email); + QByteArray body = pbReq.serialize(&serializer); + LOG() << "获取验证码->发送请求 requestId= " << pbReq.requestId() << ", email= " << pbReq.phoneNumber(); + + //发送HTTP请求 + QNetworkReply* resp = this->sendHttpRequest("/service/user/get_phone_verify_code", body); + + //处理HTTP响应 + connect(resp, &QNetworkReply::finished, this, [=]() { + //解析响应 + bool ok = false; + QString reason; + auto pbResp = this->handleHttpResponse(resp, &ok, &reason); + //判定响应是否正确 + if (!ok) { + LOG() << "获取验证码->处理出错 reason= " << reason; + return; + } + + //把得到的结果写入到DataCenter中 + dataCenter->resetVerifyCodeId(pbResp->verifyCodeId()); + + //发送信号给调用者 + emit dataCenter->getVerifyCodeDone(); + + //打印日志 + LOG() << "获取验证码->响应处理完毕 requestId= " << pbResp->requestId() << "Id= " << dataCenter->getVerifyCodeId(); + }); + } + + void NetClient::changeEmail(const QString& loginSessionId, const QString& email, const QString& verifyCodeId, const QString& verifyCode) + { + //构造请求body + bite_im::SetUserPhoneNumberReq pbReq; + pbReq.setRequestId(makeRequestId()); + pbReq.setSessionId(loginSessionId); + pbReq.setPhoneNumber(email); + pbReq.setPhoneVerifyCodeId(verifyCodeId); + pbReq.setPhoneVerifyCode(verifyCode); + QByteArray body = pbReq.serialize(&serializer); + LOG() << "修改用户邮箱->发送请求 requestId= " << pbReq.requestId() << ", loginSessionId= " << pbReq.sessionId() + << ", email= " << pbReq.phoneNumber() << ", verifyCodeId= " << pbReq.phoneVerifyCodeId() + << ", verifyCode= " << pbReq.phoneVerifyCode(); + + //发送HTTP请求 + QNetworkReply* resp = this->sendHttpRequest("/service/user/set_phone", body); + + //处理HTTP响应 + connect(resp, &QNetworkReply::finished, this, [=]() { + bool ok = false; + QString reason; + auto pbResp = this->handleHttpResponse(resp, &ok, &reason); + + //判定响应是否正确 + if (!ok) { + LOG() << "修改用户邮箱->处理出错 reason= " << reason; + return; + } + + //把结果记录到DataCenter中 + dataCenter->resetPhone(email); + + //打印日志 + LOG() << "修改用户邮箱->响应处理完毕 requestId= " << pbResp->requestId(); + }); + + //发送信号,通知调用者完成 + emit dataCenter->changePhoneDone(); + } + + void NetClient::changeAvatar(const QString& loginSessionId, const QByteArray& avatar) + { + //构造请求body + bite_im::SetUserAvatarReq pbReq; + pbReq.setRequestId(makeRequestId()); + pbReq.setSessionId(loginSessionId); + pbReq.setAvatar(avatar); + QByteArray body = pbReq.serialize(&serializer); + LOG() << "修改用户头像->发送请求 requestId= " << pbReq.requestId() << ", loginSessionId= " << pbReq.sessionId() + << ", avatar size= " << pbReq.avatar().size(); + + //发送HTTP请求 + QNetworkReply* resp = this->sendHttpRequest("/service/user/set_avatar", body); + + //处理HTTP响应 + connect(resp, &QNetworkReply::finished, this, [=]() { + //解析响应 + bool ok = false; + QString reason; + auto pbResp = this->handleHttpResponse(resp, &ok, &reason); + + //判定响应是否正确 + if (!ok) { + LOG() << "修改用户头像->处理出错 reason= " << reason; + return; + } + + //把数据写入到DataCenter + dataCenter->resetAvatar(avatar); + + //发送信号 + emit dataCenter->changeAvatarDone(); + + //打印日志 + LOG() << "修改用户头像->响应处理完成 requestId= " << pbResp->requestId(); + }); + } } //end namespace network \ No newline at end of file diff --git a/network/netclient.h b/network/netclient.h index 0198a51..9861de7 100644 --- a/network/netclient.h +++ b/network/netclient.h @@ -89,8 +89,13 @@ namespace network { void getRecentMessageList(const QString& loginSessionId, const QString& chatSessionId, bool updateUI); void sendMessage(const QString& loginSessionId, const QString& chatSessionId, model::MessageType messageType, const QByteArray& content, const QString& extraInfo); - void receiveMessage(const QString& chatSessionId); + void changeNickname(const QString& loginSessionId, const QString& nickname); + void changeDescription(const QString& loginSessionId, const QString& desc); + void getVerifyCode(const QString& email); + void changeEmail(const QString& loginSessionId, const QString& email, const QString& verifyCodeId, const QString& verifyCode); + void changeAvatar(const QString& loginSessionId, const QByteArray& avatar); + private: model::DataCenter* dataCenter; diff --git a/userinfowidget.cpp b/userinfowidget.cpp index 9247135..0018e6d 100644 --- a/userinfowidget.cpp +++ b/userinfowidget.cpp @@ -26,9 +26,11 @@ UserInfoWidget::UserInfoWidget(const UserInfo& userInfo, QWidget *parent) avatarBtn->setFixedSize(75, 75); avatarBtn->setIconSize(QSize(75, 75)); avatarBtn->setIcon(userInfo.avatar); + avatarBtn->setStyleSheet("QPushButton { border: none; background-color: transparent; }"); QString labelStyle = "QLabel { font-weight: 800; padding-left: 20px;}"; - QString btnStyle = "QPushButton { border: 1px solid rgb(100, 100, 100); border-radius: 5px; background-color: rgb(240, 240, 240); color: rgb(0, 0, 0); }"; + QString btnStyle = "QPushButton { border: 1px solid rgb(100, 100, 100); border-radius: 5px; background-color: rgb(240, 240, 240); color: rgb(0, 0, 0);}"; + //QString btnStyle = ""; btnStyle += "QPushButton:pressed { background-color: rgb(205, 205, 205); }"; int width = 80; @@ -107,16 +109,39 @@ UserInfoWidget::UserInfoWidget(const UserInfo& userInfo, QWidget *parent) // 9. 初始化按钮的禁用关系 // 判定依据就是拿着当前用户的 userId, 在 DataCenter 的好友列表中, 查询即可. - //DataCenter* dataCenter = DataCenter::getInstance(); - //auto* myFriend = dataCenter->findFriendById(this->userInfo.userId); - //if (myFriend == nullptr) { - // // 不是好友 - // sendMessageBtn->setEnabled(false); - // deleteFriendBtn->setEnabled(false); - //} - //else { - // // 是好友 - // applyBtn->setEnabled(false); - //} + DataCenter* dataCenter = DataCenter::getInstance(); + auto* myFriend = dataCenter->findFriendById(this->userInfo.userId); + if (myFriend == nullptr) { + // 不是好友 + sendMessageBtn->setEnabled(false); + deleteFriendBtn->setEnabled(false); -} \ No newline at end of file + /*sendMessageBtn->setObjectName("sendMessageBtn"); + deleteFriendBtn->setObjectName("deleteFriendBtn");*/ + + sendMessageBtn->setStyleSheet("QPushButton { border: 1px solid rgb(200, 200, 200); border-radius: 5px; background-color: rgb(240, 240, 240); color: rgb(150, 150, 150); }"); + deleteFriendBtn->setStyleSheet("QPushButton { border: 1px solid rgb(200, 200, 200); border-radius: 5px; background-color: rgb(240, 240, 240); color: rgb(150, 150, 150); }"); + + } + else { + // 是好友 + applyBtn->setEnabled(false); + applyBtn->setStyleSheet("QPushButton { border: 1px solid rgb(200, 200, 200); border-radius: 5px; background-color: rgb(240, 240, 240); color: rgb(150, 150, 150); }"); + + } + + //初始化信号槽 + initSingleSlot(); +} + +void UserInfoWidget::initSingleSlot() +{ + connect(sendMessageBtn, &QPushButton::clicked, this, [=]() { + //拿到主窗口的指针,通过主窗口,前面实现的切换到会话这样的功能,直接调用即可 + MainWidget* mainWidget = MainWidget::getInstance(); + mainWidget->switchSession(userInfo.userId); + + //把当前模态窗口关闭 + this->close(); + }); +} diff --git a/userinfowidget.h b/userinfowidget.h index 51817f4..086d629 100644 --- a/userinfowidget.h +++ b/userinfowidget.h @@ -9,6 +9,7 @@ #include "mainwidget.h" #include "model/data.h" +#include "model/datacenter.h" //using model::Message; using namespace model; @@ -19,6 +20,7 @@ class UserInfoWidget : public QDialog public: UserInfoWidget(const UserInfo& userInfo, QWidget *parent); + void initSingleSlot(); private: ////保存对应的Message对象,暂时先放在这里