现在只能添加直接添加的 待和server修改

已修改
请求包添加了几个函数
add修改
待完善
addfriendswindow 界面
添加好友后好友列表立马回显
This commit is contained in:
dongl 2023-06-18 18:42:58 +08:00
parent 65b659948a
commit a67f301e79
10 changed files with 126 additions and 21 deletions

View File

@ -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();
}

View File

@ -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() {

View File

@ -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;

View File

@ -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<agreement_response> UserOperation::fetch_friends(uint64_t account)
}
// 查询用户
std::optional<agreement_response> UserOperation::fetch_user(uint64_t account) {
std::optional<agreement_response> 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<agreement_response> UserOperation::fetch_user(uint64_t account) {
}
// 添加用户
std::optional<agreement_response> UserOperation::add_user(uint64_t account) {
std::optional<agreement_response> 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<agreement_response> UserOperation::add_user(uint64_t account) {
//// login

View File

@ -36,16 +36,18 @@ public:
std::optional<agreement_response> get_code(const std::string& account);
// 查询账户好友
std::optional<agreement_response> fetch_friends(uint64_t account);
// 查询用户
std::optional<agreement_response> fetch_user(uint64_t account);
// 添加用户
std::optional<agreement_response> add_user(uint64_t account);
// 查询用户 并 查看是否已为好友
std::optional<agreement_response> fetch_user(uint64_t user, uint64_t account);
// 添加用户 account 本机账户 带添加账户
std::optional<agreement_response> 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);

View File

@ -15,7 +15,7 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QVBoxLayout" name="verticalLayout" stretch="1,1,15">
<layout class="QVBoxLayout" name="verticalLayout" stretch="1,1,15,0,0,0">
<item>
<widget class="QLabel" name="title">
<property name="text">
@ -33,6 +33,19 @@
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>check:</string>
</property>
</widget>
</item>
<item>
<widget class="QPlainTextEdit" name="problemPlainTextEdit"/>
</item>
<item>
<widget class="QPlainTextEdit" name="AnswerPlainTextEdit"/>
</item>
</layout>
</item>
</layout>

View File

@ -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;
};

View File

@ -28,7 +28,7 @@ private slots:
private:
Ui::IMWindow *ui;
User user;
User* user;
QList<User*> friendList;
UserOperation* userOperation;
};

View File

@ -6,6 +6,7 @@
#include <QStringListModel>
#include <QMenuBar>
#include <QMessageBox>
#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();
// setfocuspolicyqt::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

View File

@ -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();
}