Skip to content

Commit

Permalink
重构回复逻辑,全都放service中调用
Browse files Browse the repository at this point in the history
  • Loading branch information
iwxyi committed May 23, 2021
1 parent 86cccf8 commit 9b522a6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 59 deletions.
47 changes: 6 additions & 41 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,7 @@ void MainWindow::initService()

connect(service, SIGNAL(signalMessage(const MsgBean&)), this, SLOT(autoReplyMessage(const MsgBean&)));

connect(sig, &SignalTransfer::loadGroupMembers, service, [=](qint64 groupId) {
MyJson json;
json.insert("action", "get_group_member_list");
MyJson params;
params.insert("group_id", groupId);
json.insert("params", params);
json.insert("echo", "get_group_member_list:" + snum(groupId));
service->sendMessage(json.toBa());
});
connect(sig, &SignalTransfer::loadGroupMembers, service, &CqhttpService::refreshGroupMembers);
}

void MainWindow::initKey()
Expand Down Expand Up @@ -373,28 +365,10 @@ void MainWindow::createNotificationBanner(const MsgBean &msg)
adjustUnderCardsTop(index, -(card->height() + us->bannerSpacing));
notificationCards.removeOne(card);
});
connect(card, &NotificationCard::signalReplyPrivate, this, [=](qint64 userId, const QString& message) {
MyJson json;
json.insert("action", "send_private_msg");
MyJson params;
params.insert("user_id", userId);
params.insert("message", message);
json.insert("params", params);
json.insert("echo", "send_private_msg");
service->sendMessage(json.toBa());
emit sig->myReplyUser(userId, message);
});
connect(card, &NotificationCard::signalReplyGroup, this, [=](qint64 groupId, const QString& message) {
MyJson json;
json.insert("action", "send_group_msg");
MyJson params;
params.insert("group_id", groupId);
params.insert("message", message);
json.insert("params", params);
json.insert("echo", "send_group_msg");
service->sendMessage(json.toBa());
emit sig->myReplyGroup(groupId, message);
});
connect(card, &NotificationCard::signalReplyPrivate, service, &CqhttpService::sendUserMsg);

connect(card, &NotificationCard::signalReplyGroup, service, &CqhttpService::sendGroupMsg);

connect(card, &NotificationCard::signalCancelReply, this, [=]{
returnToPrevWindow();
});
Expand Down Expand Up @@ -614,15 +588,6 @@ void MainWindow::triggerAiReply(const MsgBean &msg, int retry)
answer = us->aiReplyPrefix + answer + us->aiReplySuffix;
}

json = MyJson();
json.insert("action", "send_private_msg");
MyJson params;
params.insert("user_id", msg.senderId);
params.insert("message", answer);
json.insert("params", params);
json.insert("echo", "send_private_msg");
service->sendMessage(json.toBa());

emit sig->myReplyUser(msg.senderId, answer);
service->sendUserMsg(msg.senderId, answer);
});
}
67 changes: 55 additions & 12 deletions web_service/cqhttpservice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,10 @@ void CqhttpService::loopStarted()
socket->sendTextMessage(json.toBa());
}
// 获取好友列表
{
MyJson json;
json.insert("action", "get_friend_list");
json.insert("echo", "get_friend_list");
socket->sendTextMessage(json.toBa());
}
refreshFriends();

// 获取群列表
{
MyJson json;
json.insert("action", "get_group_list");
json.insert("echo", "get_group_list");
socket->sendTextMessage(json.toBa());
}
refreshGroups();
}

void CqhttpService::openHost(QString host, QString token)
Expand Down Expand Up @@ -305,3 +295,56 @@ void CqhttpService::parseGroupUpload(const MyJson &json)
emit signalMessage(msg);
qInfo() << "收到群文件消息:" << group_id << ac->groupNames.value(group_id) << user_id << ac->friendNames.value(user_id) << name << size << id;
}

void CqhttpService::refreshFriends()
{
MyJson json;
json.insert("action", "get_friend_list");
json.insert("echo", "get_friend_list");
socket->sendTextMessage(json.toBa());
}

void CqhttpService::refreshGroups()
{
MyJson json;
json.insert("action", "get_group_list");
json.insert("echo", "get_group_list");
socket->sendTextMessage(json.toBa());
}

void CqhttpService::refreshGroupMembers(qint64 groupId)
{
MyJson json;
json.insert("action", "get_group_member_list");
MyJson params;
params.insert("group_id", groupId);
json.insert("params", params);
json.insert("echo", "get_group_member_list:" + snum(groupId));
sendMessage(json.toBa());
}

void CqhttpService::sendUserMsg(qint64 userId, const QString& message)
{
MyJson json;
json.insert("action", "send_private_msg");
MyJson params;
params.insert("user_id", userId);
params.insert("message", message);
json.insert("params", params);
json.insert("echo", "send_private_msg");
sendMessage(json.toBa());
emit sig->myReplyUser(userId, message);
}

void CqhttpService::sendGroupMsg(qint64 groupId, const QString& message)
{
MyJson json;
json.insert("action", "send_group_msg");
MyJson params;
params.insert("group_id", groupId);
params.insert("message", message);
json.insert("params", params);
json.insert("echo", "send_group_msg");
sendMessage(json.toBa());
emit sig->myReplyGroup(groupId, message);
}
6 changes: 6 additions & 0 deletions web_service/cqhttpservice.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public slots:
void sendMessage(const QString& text);
void messageReceived(const QString &message);

void refreshFriends();
void refreshGroups();
void refreshGroupMembers(qint64 groupId);
void sendUserMsg(qint64 userId, const QString &message);
void sendGroupMsg(qint64 groupId, const QString &message);

private:
void parseEchoMessage(const MyJson& json);
void parsePrivateMessage(const MyJson& json);
Expand Down
6 changes: 0 additions & 6 deletions widgets/settings/accountwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ AccountWidget::~AccountWidget()
void AccountWidget::resotreSettings()
{
QString host = us->host;
// 加密
QRegularExpressionMatch match;
if (host.indexOf(QRegularExpression(":(\\d+)"), 0, &match) > -1)
host.replace(match.captured(1), repeatString("*", match.captured(1).length()));
if (host.indexOf(QRegularExpression("://(\\w+)"), 0, &match) > -1)
host.replace(match.captured(1), repeatString("*", match.captured(1).length()));
ui->hostEdit->setText(host);

QString token = us->accessToken;
Expand Down

0 comments on commit 9b522a6

Please sign in to comment.