From a67f301e7959e7993499c4bff942392223a6ad7f Mon Sep 17 00:00:00 2001 From: dongl <2725096176@qq.com> Date: Sun, 18 Jun 2023 18:42:58 +0800 Subject: [PATCH] =?UTF-8?q?=E7=8E=B0=E5=9C=A8=E5=8F=AA=E8=83=BD=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E7=9B=B4=E6=8E=A5=E6=B7=BB=E5=8A=A0=E7=9A=84=20?= =?UTF-8?q?=E5=BE=85=E5=92=8Cserver=E4=BF=AE=E6=94=B9=20=E5=B7=B2=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=20=E8=AF=B7=E6=B1=82=E5=8C=85=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=E5=87=A0=E4=B8=AA=E5=87=BD=E6=95=B0=20add=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=20=E5=BE=85=E5=AE=8C=E5=96=84=20addfriendswindow=20?= =?UTF-8?q?=E7=95=8C=E9=9D=A2=20=E6=B7=BB=E5=8A=A0=E5=A5=BD=E5=8F=8B?= =?UTF-8?q?=E5=90=8E=E5=A5=BD=E5=8F=8B=E5=88=97=E8=A1=A8=E7=AB=8B=E9=A9=AC?= =?UTF-8?q?=E5=9B=9E=E6=98=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/MP/Body.h | 8 +++++ api/MP/Request.cpp | 7 +++++ api/MP/Request.h | 1 + api/user/UserOperation.cpp | 25 +++++++++++---- api/user/UserOperation.h | 10 +++--- src/Forms/addfrindwindow.ui | 15 ++++++++- src/Header/addfrindwindow.h | 10 +++++- src/Header/imwindow.h | 2 +- src/Source/addfrindwindow.cpp | 58 ++++++++++++++++++++++++++++++++--- src/Source/imwindow.cpp | 11 +++++-- 10 files changed, 126 insertions(+), 21 deletions(-) diff --git a/api/MP/Body.h b/api/MP/Body.h index 3bd681c..e84197a 100644 --- a/api/MP/Body.h +++ b/api/MP/Body.h @@ -44,6 +44,14 @@ public: body->set_subcommand(subType); } + Body(mp::MP_SUB_TYPE subType, uint64_t target, uint64_t source) { + body = new mp::body(); + + body->set_target(target); + body->set_source(source); + body->set_subcommand(subType); + } + Body() { body = new mp::body(); } diff --git a/api/MP/Request.cpp b/api/MP/Request.cpp index c55248d..f329b9a 100644 --- a/api/MP/Request.cpp +++ b/api/MP/Request.cpp @@ -16,6 +16,11 @@ Request::Request(mp::MP_TYPE type, mp::MP_SUB_TYPE subType, uint64_t target, uin } +Request::Request(mp::MP_TYPE type, mp::MP_SUB_TYPE subType, uint64_t target, uint64_t source) : + Mph(type), Body(subType, target, source), Cqi() { + +} + Request::Request(mp::MP_TYPE type, mp::MP_SUB_TYPE subType,const std::string &account, const std::string &password,const std::string &data) : Mph(type), Body(subType, account, password, data) { @@ -54,3 +59,5 @@ std::string Request::packet() { + + diff --git a/api/MP/Request.h b/api/MP/Request.h index dc827e3..16aacdf 100644 --- a/api/MP/Request.h +++ b/api/MP/Request.h @@ -16,6 +16,7 @@ public: Request(mp::MP_TYPE type, mp::MP_SUB_TYPE subType, const std::string& account, const std::string& password); Request(mp::MP_TYPE type, mp::MP_SUB_TYPE subType, const std::string& account, const std::string& password, const std::string& data); Request(mp::MP_TYPE type, mp::MP_SUB_TYPE subType, uint64_t target, uint64_t source, const std::string& data); + Request(mp::MP_TYPE type, mp::MP_SUB_TYPE subType, uint64_t target, uint64_t source); Request(mp::MP_TYPE type, mp::MP_SUB_TYPE subType, const std::string& account); ~Request() override; diff --git a/api/user/UserOperation.cpp b/api/user/UserOperation.cpp index d8916b7..627d587 100644 --- a/api/user/UserOperation.cpp +++ b/api/user/UserOperation.cpp @@ -39,6 +39,13 @@ std::string UserOperation::packet_base(mp::MP_TYPE type, mp::MP_SUB_TYPE subType return temp; } +std::string UserOperation::packet_base(mp::MP_TYPE type, mp::MP_SUB_TYPE subType, uint64_t source, uint64_t target) { + auto request = new Request(type,subType, target, source); + std::string temp = request->packet(); + delete request; + return temp; +} +//// packet end //// login @@ -127,12 +134,15 @@ std::optional UserOperation::fetch_friends(uint64_t account) } // 查询用户 -std::optional UserOperation::fetch_user(uint64_t account) { +std::optional UserOperation::fetch_user(uint64_t user, uint64_t account) { std::regex pattern_account("[1-9](\\d{5,11})"); std::string packet; - if (std::regex_match(std::to_string(account), pattern_account)) { - packet = packet_base(mp::MP_TYPE::MP_REQUEST_FRIENDS, mp::MP_SUB_TYPE::MP_SEARCH_FRIENDS_ACCOUNT, std::to_string(account)); + if (std::regex_match(std::to_string(user), pattern_account) + && std::regex_match(std::to_string(account), pattern_account)) { + + packet = packet_base(mp::MP_TYPE::MP_REQUEST_FRIENDS, mp::MP_SUB_TYPE::MP_SEARCH_FRIENDS_ACCOUNT, + std::to_string(account), "", std::to_string(user)); } else { return std::nullopt; } @@ -143,12 +153,14 @@ std::optional UserOperation::fetch_user(uint64_t account) { } // 添加用户 -std::optional UserOperation::add_user(uint64_t account) { +std::optional UserOperation::add_user(uint64_t account, uint64_t user) { std::regex pattern_account("[1-9](\\d{5,11})"); std::string packet; - if (std::regex_match(std::to_string(account), pattern_account)) { - packet = packet_base(mp::MP_TYPE::MP_REQUEST_FRIENDS, mp::MP_SUB_TYPE::MP_SEARCH_FRIENDS_ACCOUNT, std::to_string(account)); + if (std::regex_match(std::to_string(account), pattern_account) && + std::regex_match(std::to_string(user), pattern_account) ) { + packet = packet_base(mp::MP_TYPE::MP_REQUEST_FRIENDS, mp::MP_SUB_TYPE::MP_ADD_FRIENDS_ACCOUNT, + account, user); } else { return std::nullopt; } @@ -163,6 +175,7 @@ std::optional UserOperation::add_user(uint64_t account) { + //// login diff --git a/api/user/UserOperation.h b/api/user/UserOperation.h index 1f6cf03..8f630f2 100644 --- a/api/user/UserOperation.h +++ b/api/user/UserOperation.h @@ -36,16 +36,18 @@ public: std::optional get_code(const std::string& account); // 查询账户好友 std::optional fetch_friends(uint64_t account); - // 查询用户 - std::optional fetch_user(uint64_t account); - // 添加用户 - std::optional add_user(uint64_t account); + // 查询用户 并 查看是否已为好友 + std::optional fetch_user(uint64_t user, uint64_t account); + // 添加用户 account 本机账户 带添加账户 + std::optional add_user(uint64_t account, uint64_t user); private: // base 包 std::string packet_base(mp::MP_TYPE, mp::MP_SUB_TYPE, const std::string &account, const std::string &password); std::string packet_base(mp::MP_TYPE, mp::MP_SUB_TYPE, const std::string &account, const std::string &password, const std::string &data); std::string packet_base(mp::MP_TYPE type, mp::MP_SUB_TYPE subType, const std::string& account); + std::string packet_base(mp::MP_TYPE type, mp::MP_SUB_TYPE subType, uint64_t source, uint64_t target); + // 操作报 std::string login_packet(mp::MP_SUB_TYPE subType, const std::string &account, const std::string &password); diff --git a/src/Forms/addfrindwindow.ui b/src/Forms/addfrindwindow.ui index f088f79..f6a693a 100644 --- a/src/Forms/addfrindwindow.ui +++ b/src/Forms/addfrindwindow.ui @@ -15,7 +15,7 @@ - + @@ -33,6 +33,19 @@ + + + + check: + + + + + + + + + diff --git a/src/Header/addfrindwindow.h b/src/Header/addfrindwindow.h index 770a905..e4ae695 100644 --- a/src/Header/addfrindwindow.h +++ b/src/Header/addfrindwindow.h @@ -34,11 +34,19 @@ private slots: // 打招呼 void say_hello_action(); +private: + void add_ui_show(const std::string&); + void add_ui_hide(); + +public: + void setLocalUser(User* local); + private: Ui::addfrindwindow *ui; UserOperation* userOperation = nullptr; bool user_show_info = false; - User user; + User* user; + User* localUser; }; diff --git a/src/Header/imwindow.h b/src/Header/imwindow.h index 6daee75..d8a06a3 100644 --- a/src/Header/imwindow.h +++ b/src/Header/imwindow.h @@ -28,7 +28,7 @@ private slots: private: Ui::IMWindow *ui; - User user; + User* user; QList friendList; UserOperation* userOperation; }; diff --git a/src/Source/addfrindwindow.cpp b/src/Source/addfrindwindow.cpp index 85d0c49..e9fb7f0 100644 --- a/src/Source/addfrindwindow.cpp +++ b/src/Source/addfrindwindow.cpp @@ -6,6 +6,7 @@ #include #include +#include #include "ui_addfrindwindow.h" #include "addfrindwindow.h" #include "ClientExample.h" @@ -15,31 +16,48 @@ addfrindwindow::addfrindwindow(QWidget *parent) : ui(new Ui::addfrindwindow), userOperation(new UserOperation(ClientExample::run())) { ui->setupUi(this); + user = new User(); + localUser = new User(); + + // 添加验证界面 暂时隐藏 + add_ui_hide(); connect(ui->search, SIGNAL(returnPressed()), this, SLOT(get_search_input())); } addfrindwindow::~addfrindwindow() { + delete user; + delete localUser; + delete userOperation; delete ui; } +void addfrindwindow::setLocalUser(User * local) { + localUser = local; +} + // add 搜索框 void addfrindwindow::get_search_input() { // 取搜索框值 QString search_value = ui->search->text(); // 开始搜索 暂时只显示 - auto ret = userOperation->fetch_user(strtol(search_value.toStdString().c_str(), nullptr, 0)); + auto ret = userOperation->fetch_user(localUser->account, strtol(search_value.toStdString().c_str(), nullptr, 0)); + // 用户输入数据 是否合法 if (ret.has_value()) { + // 判断返回结果 if (ret.value().m_sri.subcommand() == mp::MP_SUB_TYPE::MP_SEARCH_SUCCESS) { search_value.append(" "); search_value.append(ret.value().m_sri.username().c_str()); search_value.append(" "); search_value.append(ret.value().m_sri.msg().c_str()); + + // 右键显示状态 user_show_info = true; - user.account = ret.value().m_sri.account(); - user.username = ret.value().m_sri.username(); - user.email = ret.value().m_sri.email(); + // 注册server来的user信息 以加载到 + user->account = ret.value().m_sri.account(); + user->username = ret.value().m_sri.username(); + user->email = ret.value().m_sri.email(); } else { search_value.append(" "); search_value.append(ret.value().m_sri.msg().c_str()); @@ -102,20 +120,50 @@ void addfrindwindow::contextMenuEvent(QContextMenuEvent *) { } + + +// add void addfrindwindow::add_friends_action() { -// userOperation; + // 先获取添加类型或者server 验证后返回 + auto ret = userOperation->add_user(localUser->account, user->account); + if (ret.value().m_sri.subcommand() == mp::MP_SUB_TYPE::MP_ADD_SUCCESS) { + add_ui_show(ret.value().m_sri.msg()); + } else { + add_ui_show(ret.value().m_sri.msg()); + } } +// show user info void addfrindwindow::user_home_action() { printf("222"); } +// temp im msg void addfrindwindow::say_hello_action() { printf("333"); } +//// ui show hide +void addfrindwindow::add_ui_show(const std::string& problem_value) { + ui->label->show(); + ui->problemPlainTextEdit->setPlainText(problem_value.c_str()); + ui->problemPlainTextEdit->show(); +// setfocuspolicy(qt::nofocus/clickfocus) + ui->problemPlainTextEdit->setFocusPolicy(Qt::NoFocus); + ui->AnswerPlainTextEdit->show(); +} + +void addfrindwindow::add_ui_hide() { + ui->label->hide(); + ui->problemPlainTextEdit->hide(); + ui->AnswerPlainTextEdit->hide(); +} +//// ui show hide + + + diff --git a/src/Source/imwindow.cpp b/src/Source/imwindow.cpp index c8d713f..fec41d1 100644 --- a/src/Source/imwindow.cpp +++ b/src/Source/imwindow.cpp @@ -13,16 +13,19 @@ IMWindow::IMWindow(QWidget *parent) : ui(new Ui::IMWindow) { ui->setupUi(this); + user = new User(); } IMWindow::~IMWindow() { + delete user; + delete userOperation; delete ui; } void IMWindow::setUser(uint64_t account, const std::string &username) { - user.account = account; - user.username = username; + user->account = account; + user->username = username; std::string temp = "在线:" + username; ui->userInfo->setText(temp.c_str()); @@ -32,7 +35,7 @@ void IMWindow::setUser(uint64_t account, const std::string &username) { void IMWindow::initFriendList() { // user.account; // 创建数据显示列表 - auto friends = userOperation->fetch_friends(user.account).value().m_sri.data(); + auto friends = userOperation->fetch_friends(user->account).value().m_sri.data(); rapidjson::Document document; document.Parse(friends.c_str()); @@ -68,7 +71,9 @@ void IMWindow::on_friendListView_clicked(const QModelIndex& model) { ui->chatBoxInfo->append(("username:" + temp_user->username).c_str()); } +// 显示 addfriendwindows 窗口 void IMWindow::on_addfriends_clicked() { auto add_win = new addfrindwindow(); + add_win->setLocalUser(user); add_win->show(); }