mirror of
https://gitee.com/Zhaoxin59/my-chat_-client.git
synced 2026-02-14 09:01:50 +08:00
has been completed.
This commit is contained in:
@ -6,7 +6,8 @@ namespace network {
|
||||
NetClient::NetClient(model::DataCenter* dataCenter)
|
||||
:dataCenter(dataCenter)
|
||||
{
|
||||
initWebSocket();
|
||||
//不应该在此处初始化websocket
|
||||
//initWebSocket();
|
||||
}
|
||||
|
||||
void NetClient::ping()
|
||||
@ -70,16 +71,27 @@ namespace network {
|
||||
handleWsMessage(message);
|
||||
}
|
||||
else if (notifyMessage.notifyType() == bite_im::NotifyTypeGadget::NotifyType::CHAT_SESSION_CREATE_NOTIFY) {
|
||||
|
||||
//创建新的会话通知
|
||||
ChatSessionInfo chatSessionInfo;
|
||||
chatSessionInfo.load(notifyMessage.newChatSessionInfo().chatSessionInfo());
|
||||
handleWsSessionCreate(chatSessionInfo);
|
||||
}
|
||||
else if (notifyMessage.notifyType() == bite_im::NotifyTypeGadget::NotifyType::FRIEND_ADD_APPLY_NOTIFY) {
|
||||
|
||||
//添加好友申请通知
|
||||
UserInfo userInfo;
|
||||
userInfo.load(notifyMessage.friendAddApply().userInfo());
|
||||
handleWsAddfriendApply(userInfo);
|
||||
}
|
||||
else if (notifyMessage.notifyType() == bite_im::NotifyTypeGadget::NotifyType::FRIEND_ADD_PROCESS_NOTIFY) {
|
||||
|
||||
//添加好友申请的处理结果通知
|
||||
UserInfo userInfo;
|
||||
userInfo.load(notifyMessage.friendProcessResult().userInfo());
|
||||
bool agree = notifyMessage.friendProcessResult().agree();
|
||||
handleWsAddFriendProcess(userInfo, agree);
|
||||
}
|
||||
else if (notifyMessage.notifyType() == bite_im::NotifyTypeGadget::NotifyType::FRIEND_REMOVE_NOTIFY) {
|
||||
|
||||
const QString& userId = notifyMessage.friendRemove().userId();
|
||||
handleWsRemoveFriend(userId);
|
||||
}
|
||||
}
|
||||
|
||||
@ -99,6 +111,63 @@ namespace network {
|
||||
}
|
||||
}
|
||||
|
||||
void NetClient::handleWsRemoveFriend(const QString& userId)
|
||||
{
|
||||
//删除数据 DataCenter 好友列表的数据
|
||||
dataCenter->removeFriend(userId);
|
||||
//通知界面变化,更新好友列表/会话列表
|
||||
emit dataCenter->deleteFriendDone();
|
||||
}
|
||||
|
||||
void NetClient::handleWsAddfriendApply(const model::UserInfo& userInfo)
|
||||
{
|
||||
//DataCenter中有一个好友申请列表,需要把这个数据添加到好友申请的列表中
|
||||
QList<UserInfo>* applyList = dataCenter->getApplyList();
|
||||
if (applyList == nullptr) {
|
||||
LOG() << "客户端没有加载到好友申请的列表";
|
||||
return;
|
||||
}
|
||||
//把新的元素放到列表前面
|
||||
applyList->push_front(userInfo);
|
||||
|
||||
//通知界面进行更新
|
||||
emit dataCenter->receiveFriendApplyDone();
|
||||
}
|
||||
|
||||
void NetClient::handleWsAddFriendProcess(const model::UserInfo& userInfo, bool agree)
|
||||
{
|
||||
if (agree) {
|
||||
//对方同意了你的好友申请
|
||||
QList<UserInfo>* friendList = dataCenter->getFriendList();
|
||||
if (friendList == nullptr) {
|
||||
LOG() << "客户端没有加载好友列表";
|
||||
return;
|
||||
}
|
||||
friendList->push_front(userInfo);
|
||||
|
||||
//通知更新一下界面
|
||||
emit dataCenter->receiveFriendProcessDone(userInfo.nickname, agree);
|
||||
}
|
||||
else {
|
||||
//对方未同意你的好友申请
|
||||
emit dataCenter->receiveFriendProcessDone(userInfo.nickname, agree);
|
||||
}
|
||||
}
|
||||
|
||||
void NetClient::handleWsSessionCreate(const model::ChatSessionInfo& chatSessionInfo)
|
||||
{
|
||||
//把这个ChatSessionInfo添加到会话列表中即可
|
||||
QList<ChatSessionInfo>* chatSessionList = dataCenter->getChatSessionList();
|
||||
if (chatSessionList == nullptr) {
|
||||
LOG() << "客户端没有加载会话列表";
|
||||
return;
|
||||
}
|
||||
//新的元素添加到列表头部
|
||||
chatSessionList->push_front(chatSessionInfo);
|
||||
//发送一个信号,通知界面更新
|
||||
emit dataCenter->receiveSessionCreateDone();
|
||||
}
|
||||
|
||||
void NetClient::sendAuth()
|
||||
{
|
||||
bite_im::ClientAuthenticationReq req;
|
||||
@ -107,7 +176,6 @@ namespace network {
|
||||
QByteArray body = req.serialize(&serializer);
|
||||
websocketClient.sendBinaryMessage(body);
|
||||
LOG() << "[WS身份认证] requestId= " << req.requestId() << ",loginSessionId= " << req.sessionId();
|
||||
|
||||
}
|
||||
|
||||
QString NetClient::makeRequestId()
|
||||
@ -525,6 +593,8 @@ namespace network {
|
||||
bool ok = false;
|
||||
QString reason;
|
||||
auto pbResp = this->handleHttpResponse<bite_im::PhoneVerifyCodeRsp>(resp, &ok, &reason);
|
||||
//发送信号给调用者
|
||||
emit dataCenter->getVerifyCodeDone(ok);
|
||||
//判定响应是否正确
|
||||
if (!ok) {
|
||||
LOG() << "获取验证码->处理出错 reason= " << reason;
|
||||
@ -534,9 +604,6 @@ namespace network {
|
||||
//把得到的结果写入到DataCenter中
|
||||
dataCenter->resetVerifyCodeId(pbResp->verifyCodeId());
|
||||
|
||||
//发送信号给调用者
|
||||
emit dataCenter->getVerifyCodeDone();
|
||||
|
||||
//打印日志
|
||||
LOG() << "获取验证码->响应处理完毕 requestId= " << pbResp->requestId() << "Id= " << dataCenter->getVerifyCodeId();
|
||||
});
|
||||
@ -619,4 +686,592 @@ namespace network {
|
||||
LOG() << "修改用户头像->响应处理完成 requestId= " << pbResp->requestId();
|
||||
});
|
||||
}
|
||||
|
||||
void NetClient::deleteFriend(const QString& loginSessionId, const QString& userId)
|
||||
{
|
||||
//构造请求body
|
||||
bite_im::FriendRemoveReq pbReq;
|
||||
pbReq.setRequestId(makeRequestId());
|
||||
pbReq.setSessionId(loginSessionId);
|
||||
pbReq.setPeerId(userId);
|
||||
QByteArray body = pbReq.serialize(&serializer);
|
||||
LOG() << "删除好友->发送请求 requestId= " << pbReq.requestId() << ", loginSessionId= " << pbReq.sessionId()
|
||||
<< ", userId= " << pbReq.peerId();
|
||||
|
||||
//发送HTTP请求
|
||||
QNetworkReply* resp = this->sendHttpRequest("/service/friend/remove_friend", body);
|
||||
|
||||
//处理HTTP响应
|
||||
connect(resp, &QNetworkReply::finished, this, [=]() {
|
||||
//解析响应
|
||||
bool ok = false;
|
||||
QString reason;
|
||||
auto pbResp = this->handleHttpResponse<bite_im::FriendRemoveRsp>(resp, &ok, &reason);
|
||||
|
||||
//判定响应结果是否正确
|
||||
if (!ok) {
|
||||
LOG() << "删除好友->处理出错 reason= " << reason;
|
||||
return;
|
||||
}
|
||||
|
||||
//把结果写入到DataCenter中,把该用户从好友列表中删除掉
|
||||
dataCenter->removeFriend(userId);
|
||||
|
||||
//发送信号,通知调用者当前好友删除完毕
|
||||
emit dataCenter->deleteFriendDone();
|
||||
|
||||
//打印日志
|
||||
LOG() << "删除好友->响应完成 requestId= " << pbResp->requestId();
|
||||
});
|
||||
}
|
||||
|
||||
void NetClient::addFriendApply(const QString& loginSessionId, const QString& userId)
|
||||
{
|
||||
//构造请求body
|
||||
bite_im::FriendAddReq pbReq;
|
||||
pbReq.setRequestId(makeRequestId());
|
||||
pbReq.setSessionId(loginSessionId);
|
||||
pbReq.setRespondentId(userId);
|
||||
QByteArray body = pbReq.serialize(&serializer);
|
||||
LOG() << "添加好友申请->发送请求 requestId= " << pbReq.requestId() << ", loginSessionId= " << pbReq.sessionId()
|
||||
<< ", userId= " << userId;
|
||||
|
||||
//发送HTTP请求
|
||||
QNetworkReply* resp = this->sendHttpRequest("/service/friend/add_friend_apply", body);
|
||||
|
||||
//处理响应
|
||||
connect(resp, &QNetworkReply::finished, this, [=]() {
|
||||
//解析响应
|
||||
bool ok = false;
|
||||
QString reason;
|
||||
auto pbResp = this->handleHttpResponse<bite_im::FriendAddRsp>(resp, &ok, &reason);
|
||||
|
||||
//判定响应是否正确
|
||||
if (!ok) {
|
||||
LOG() << "添加好友申请->响应失败 reason= " << reason;
|
||||
return;
|
||||
}
|
||||
|
||||
//记录结果到DataCenter,此处不需要记录任何数据
|
||||
|
||||
//发送信号,通知调用者
|
||||
emit dataCenter->addFriendApplyDone();
|
||||
|
||||
//打印日志
|
||||
LOG() << "添加好友申请->响应完毕 requestId= " << pbResp->requestId();
|
||||
});
|
||||
}
|
||||
|
||||
void NetClient::acceptFriendApply(const QString& loginSessionId, const QString& userId)
|
||||
{
|
||||
//构造请求body
|
||||
bite_im::FriendAddProcessReq pbReq;
|
||||
pbReq.setRequestId(makeRequestId());
|
||||
pbReq.setSessionId(loginSessionId);
|
||||
pbReq.setAgree(true);
|
||||
pbReq.setApplyUserId(userId);
|
||||
QByteArray body = pbReq.serialize(&serializer);
|
||||
LOG() << "同意好友申请->发送请求 requestId= " << pbReq.requestId() << ", loginSessionId= " << pbReq.sessionId()
|
||||
<< ", userId= " << pbReq.applyUserId();
|
||||
|
||||
//发送HTTP请求
|
||||
QNetworkReply* resp = this->sendHttpRequest("/service/friend/add_friend_process", body);
|
||||
|
||||
//处理HTTP响应
|
||||
connect(resp, &QNetworkReply::finished, this, [=]() {
|
||||
//解析响应
|
||||
bool ok = false;
|
||||
QString reason;
|
||||
auto pbResp = this->handleHttpResponse<bite_im::FriendAddRsp>(resp, &ok, &reason);
|
||||
|
||||
//判定响应结果是否正确
|
||||
if (!ok) {
|
||||
LOG() << "同意好友申请->处理出错 reason= " << reason;
|
||||
return;
|
||||
}
|
||||
|
||||
//此处做一个好友列表的更新
|
||||
//把好友从申请列表中删除掉
|
||||
//并将该好友添加到好友列表中
|
||||
UserInfo applyUser = dataCenter->removeFromApplyList(userId);
|
||||
QList<UserInfo>* friendList = dataCenter->getFriendList();
|
||||
friendList->push_front(applyUser);
|
||||
|
||||
//发送信号,通知界面进行更新
|
||||
emit dataCenter->acceptFriendApplyDone();
|
||||
|
||||
//打印日志
|
||||
LOG() << "同意好友申请->相应完成 requestId= " << pbResp->requestId();
|
||||
});
|
||||
}
|
||||
|
||||
void NetClient::rejectFriendApply(const QString& loginSessionId, const QString& userId)
|
||||
{
|
||||
//构造请求body
|
||||
bite_im::FriendAddProcessReq pbReq;
|
||||
pbReq.setRequestId(makeRequestId());
|
||||
pbReq.setSessionId(loginSessionId);
|
||||
pbReq.setAgree(false);
|
||||
pbReq.setApplyUserId(userId);
|
||||
QByteArray body = pbReq.serialize(&serializer);
|
||||
LOG() << "拒绝好友申请->发送请求 requestId= " << pbReq.requestId() << ", loginSessionId= " << pbReq.sessionId()
|
||||
<< ", userId= " << pbReq.applyUserId();
|
||||
|
||||
//发送HTTP请求
|
||||
QNetworkReply* resp = this->sendHttpRequest("/service/friend/add_friend_process", body);
|
||||
|
||||
//处理HTTP响应
|
||||
connect(resp, &QNetworkReply::finished, this, [=]() {
|
||||
//解析响应
|
||||
bool ok = false;
|
||||
QString reason;
|
||||
auto pbResp = this->handleHttpResponse<bite_im::FriendAddRsp>(resp, &ok, &reason);
|
||||
|
||||
//判定响应结果是否正确
|
||||
if (!ok) {
|
||||
LOG() << "拒绝好友申请->处理出错 reason= " << reason;
|
||||
return;
|
||||
}
|
||||
|
||||
//把好友从申请列表中删除掉
|
||||
dataCenter->removeFromApplyList(userId);
|
||||
|
||||
//发送信号,通知界面进行更新
|
||||
emit dataCenter->rejectFriendApplyDone();
|
||||
|
||||
//打印日志
|
||||
LOG() << "拒绝好友申请->相应完成 requestId= " << pbResp->requestId();
|
||||
});
|
||||
}
|
||||
|
||||
void NetClient::createGroupChatSession(const QString& loginSessionId, const QList<QString>& userIdList)
|
||||
{
|
||||
//构造请求body
|
||||
bite_im::ChatSessionCreateReq pbReq;
|
||||
pbReq.setRequestId(makeRequestId());
|
||||
pbReq.setSessionId(loginSessionId);
|
||||
pbReq.setChatSessionName("新的群聊");
|
||||
pbReq.setMemberIdList(userIdList);
|
||||
QByteArray body = pbReq.serialize(&serializer);
|
||||
LOG() << "创建群聊会话->发送请求 requestId= " << pbReq.requestId() << ", loginSessionId= " << loginSessionId
|
||||
<< ", userIdList= " << userIdList;
|
||||
|
||||
//发送HTTP请求
|
||||
QNetworkReply* resp = this->sendHttpRequest("/service/friend/create_chat_session", body);
|
||||
|
||||
//处理响应
|
||||
connect(resp, &QNetworkReply::finished, this, [=]() {
|
||||
//解析响应
|
||||
bool ok = false;
|
||||
QString reason;
|
||||
auto pbResp = this->handleHttpResponse<bite_im::ChatSessionCreateRsp>(resp, &ok, &reason);
|
||||
|
||||
//判断结果是否正确
|
||||
if (!ok) {
|
||||
LOG() << "创建群聊会话->响应失败 reason= " << reason;
|
||||
return;
|
||||
}
|
||||
|
||||
//这里无需更新DataCenter, 后续通过websocket的逻辑来更新即可
|
||||
|
||||
//通知调用者
|
||||
emit dataCenter->createGroupChatSessionDone();
|
||||
|
||||
//打印日志
|
||||
LOG() << "创建群聊会话->响应完成 requestId= " << pbResp->requestId();
|
||||
});
|
||||
}
|
||||
|
||||
void NetClient::getMemberList(const QString& loginSessionId, const QString& chatSessionId)
|
||||
{
|
||||
//构造请求body
|
||||
bite_im::GetChatSessionMemberReq pbReq;
|
||||
pbReq.setRequestId(makeRequestId());
|
||||
pbReq.setSessionId(loginSessionId);
|
||||
pbReq.setChatSessionId(chatSessionId);
|
||||
QByteArray body = pbReq.serialize(&serializer);
|
||||
LOG() << "获取会话成员列表->发送请求 requestId= " << pbReq.requestId() << ", loginSessionId= " << pbReq.sessionId()
|
||||
<< ", chatSessionId= " << pbReq.chatSessionId();
|
||||
|
||||
//发送HTTP请求
|
||||
QNetworkReply* resp = this->sendHttpRequest("/service/friend/get_chat_session_member", body);
|
||||
|
||||
//处理响应
|
||||
connect(resp, &QNetworkReply::finished, this, [=]() {
|
||||
//解析响应
|
||||
bool ok = false;
|
||||
QString reason;
|
||||
auto pbResp = this->handleHttpResponse<bite_im::GetChatSessionMemberRsp>(resp, &ok, &reason);
|
||||
|
||||
//判定响应结果是否正确
|
||||
if (!ok) {
|
||||
LOG() << "获取会话成员列表->响应失败 reason= " << reason;
|
||||
return;
|
||||
}
|
||||
|
||||
//把结果记录到DataCenter
|
||||
dataCenter->resetMemberList(chatSessionId, pbResp->memberInfoList());
|
||||
|
||||
//发送信号
|
||||
emit dataCenter->getMemberListDone(chatSessionId);
|
||||
|
||||
//打印日志
|
||||
LOG() << "获取会话成员列表->响应完成 requestId= " << pbResp->requestId();
|
||||
});
|
||||
}
|
||||
|
||||
void NetClient::searchUser(const QString& loginSessionId, const QString& searchKey)
|
||||
{
|
||||
// 1. 构造请求 body
|
||||
bite_im::FriendSearchReq pbReq;
|
||||
pbReq.setRequestId(makeRequestId());
|
||||
pbReq.setSessionId(loginSessionId);
|
||||
pbReq.setSearchKey(searchKey);
|
||||
QByteArray body = pbReq.serialize(&serializer);
|
||||
LOG() << "搜索用户->发送请求 requestId=" << pbReq.requestId() << ", loginSessionId=" << loginSessionId
|
||||
<< ", searchKey=" << searchKey;
|
||||
|
||||
// 2. 发送 HTTP 请求
|
||||
QNetworkReply* resp = this->sendHttpRequest("/service/friend/search_friend", body);
|
||||
|
||||
// 3. 处理响应
|
||||
connect(resp, &QNetworkReply::finished, this, [=]() {
|
||||
// a) 解析响应
|
||||
bool ok = false;
|
||||
QString reason;
|
||||
auto pbResp = this->handleHttpResponse<bite_im::FriendSearchRsp>(resp, &ok, &reason);
|
||||
|
||||
// b) 判定响应成功
|
||||
if (!ok) {
|
||||
LOG() << "搜索用户->响应失败 reason=" << reason;
|
||||
return;
|
||||
}
|
||||
|
||||
// c) 把得到的结果, 记录到 DataCenter
|
||||
dataCenter->resetSearchUserResult(pbResp->userInfo());
|
||||
|
||||
// d) 发送信号, 通知调用者
|
||||
emit dataCenter->searchUserDone();
|
||||
|
||||
// e) 打印日志
|
||||
LOG() << "搜索用户->响应完成 requestId=" << pbResp->requestId();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
void NetClient::searchMessage(const QString& loginSessionId, const QString& chatSessionId, const QString& searchKey)
|
||||
{
|
||||
// 1. 构造请求 body
|
||||
bite_im::MsgSearchReq pbReq;
|
||||
pbReq.setRequestId(makeRequestId());
|
||||
pbReq.setSessionId(loginSessionId);
|
||||
pbReq.setChatSessionId(chatSessionId);
|
||||
pbReq.setSearchKey(searchKey);
|
||||
QByteArray body = pbReq.serialize(&serializer);
|
||||
LOG() << "按关键词搜索历史消息->发送请求 requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId()
|
||||
<< ", chatSessionId=" << pbReq.chatSessionId() << ", searchKey=" << searchKey;
|
||||
|
||||
// 2. 发送 HTTP 请求
|
||||
QNetworkReply* resp = this->sendHttpRequest("/service/message_storage/search_history", body);
|
||||
|
||||
// 3. 处理响应
|
||||
connect(resp, &QNetworkReply::finished, this, [=]() {
|
||||
// a) 解析响应
|
||||
bool ok = false;
|
||||
QString reason;
|
||||
auto pbResp = this->handleHttpResponse<bite_im::MsgSearchRsp>(resp, &ok, &reason);
|
||||
|
||||
// b) 判定响应是否正确
|
||||
if (!ok) {
|
||||
LOG() << "按关键词搜索历史消息->响应失败! reason=" << reason;
|
||||
return;
|
||||
}
|
||||
|
||||
// c) 把响应结果写入到 DataCenter
|
||||
dataCenter->resetSearchMessageResult(pbResp->msgList());
|
||||
|
||||
// d) 发送信号
|
||||
emit dataCenter->searchMessageDone();
|
||||
|
||||
// e) 打印日志
|
||||
LOG() << "按关键词搜索历史消息->响应完成 requestId=" << pbResp->requestId();
|
||||
});
|
||||
}
|
||||
|
||||
void NetClient::searchMessageByTime(const QString& loginSessionId, const QString& chatSessionId, const QDateTime& begTime, const QDateTime& endTime)
|
||||
{
|
||||
// 1. 构造请求 body
|
||||
bite_im::GetHistoryMsgReq pbReq;
|
||||
pbReq.setRequestId(makeRequestId());
|
||||
pbReq.setSessionId(loginSessionId);
|
||||
pbReq.setChatSessionId(chatSessionId);
|
||||
pbReq.setStartTime(begTime.toSecsSinceEpoch());
|
||||
pbReq.setOverTime(endTime.toSecsSinceEpoch());
|
||||
QByteArray body = pbReq.serialize(&serializer);
|
||||
LOG() << "按时间搜索历史消息->发送请求 requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId()
|
||||
<< ", chatSessionId=" << pbReq.chatSessionId() << ", begTime= " << begTime << ", endTime= " << endTime;
|
||||
|
||||
// 2. 发送 HTTP 请求
|
||||
QNetworkReply* resp = this->sendHttpRequest("/service/message_storage/get_history", body);
|
||||
|
||||
// 3. 处理响应
|
||||
connect(resp, &QNetworkReply::finished, this, [=]() {
|
||||
// a) 解析响应
|
||||
bool ok = false;
|
||||
QString reason;
|
||||
auto pbResp = this->handleHttpResponse<bite_im::GetHistoryMsgRsp>(resp, &ok, &reason);
|
||||
|
||||
// b) 判定响应是否正确
|
||||
if (!ok) {
|
||||
LOG() << "按时间搜索历史消息->响应失败! reason=" << reason;
|
||||
return;
|
||||
}
|
||||
|
||||
// c) 把响应结果写入到 DataCenter
|
||||
dataCenter->resetSearchMessageResult(pbResp->msgList());
|
||||
|
||||
// d) 发送信号
|
||||
emit dataCenter->searchMessageDone();
|
||||
|
||||
// e) 打印日志
|
||||
LOG() << "按时间搜索历史消息->响应完成 requestId=" << pbResp->requestId();
|
||||
});
|
||||
}
|
||||
|
||||
void NetClient::userLogin(const QString& username, const QString& password)
|
||||
{
|
||||
// 1. 构造请求 body
|
||||
bite_im::UserLoginReq pbReq;
|
||||
pbReq.setRequestId(makeRequestId());
|
||||
pbReq.setNickname(username);
|
||||
pbReq.setPassword(password);
|
||||
pbReq.setVerifyCodeId("");
|
||||
pbReq.setVerifyCode("");
|
||||
QByteArray body = pbReq.serialize(&serializer);
|
||||
LOG() << "用户名登录->发送请求 requestId=" << pbReq.requestId() << ", username=" << pbReq.nickname() << ", password=" << pbReq.password();
|
||||
|
||||
// 2. 发送 HTTP 请求
|
||||
QNetworkReply* resp = this->sendHttpRequest("/service/user/username_login", body);
|
||||
|
||||
// 3. 处理响应
|
||||
connect(resp, &QNetworkReply::finished, this, [=]() {
|
||||
// a) 解析响应内容
|
||||
bool ok = false;
|
||||
QString reason;
|
||||
auto pbResp = this->handleHttpResponse<bite_im::UserLoginRsp>(resp, &ok, &reason);
|
||||
|
||||
// b) 判定响应结果是否正确
|
||||
if (!ok) {
|
||||
LOG() << "用户名登录->处理失败 reason=" << reason;
|
||||
emit dataCenter->userLoginDone(false, reason);
|
||||
return;
|
||||
}
|
||||
|
||||
// c) 记录一下当前返回的数据
|
||||
dataCenter->resetLoginSessionId(pbResp->loginSessionId());
|
||||
|
||||
// d) 发送信号, 通知调用者, 处理完毕了.
|
||||
emit dataCenter->userLoginDone(true, "");
|
||||
|
||||
// e) 打印日志
|
||||
LOG() << "用户名登录->处理响应 requestId=" << pbResp->requestId();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
void NetClient::userRegister(const QString& username, const QString& password)
|
||||
{
|
||||
// 1. 构造请求 body
|
||||
bite_im::UserRegisterReq pbReq;
|
||||
pbReq.setRequestId(makeRequestId());
|
||||
pbReq.setNickname(username);
|
||||
pbReq.setPassword(password);
|
||||
pbReq.setVerifyCodeId("");
|
||||
pbReq.setVerifyCode("");
|
||||
QByteArray body = pbReq.serialize(&serializer);
|
||||
LOG() << "用户名注册->发送请求 requestId=" << pbReq.requestId() << ", username=" << pbReq.nickname() << ", password=" << pbReq.password();
|
||||
|
||||
// 2. 发送 HTTP 请求
|
||||
QNetworkReply* resp = this->sendHttpRequest("/service/user/username_register", body);
|
||||
|
||||
// 3. 处理响应
|
||||
connect(resp, &QNetworkReply::finished, this, [=]() {
|
||||
// a) 解析响应 body
|
||||
bool ok = false;
|
||||
QString reason;
|
||||
auto pbResp = this->handleHttpResponse<bite_im::UserRegisterRsp>(resp, &ok, &reason);
|
||||
|
||||
// b) 判定响应结果是否正确
|
||||
if (!ok) {
|
||||
LOG() << "用户名注册->响应失败! reason=" << reason;
|
||||
emit dataCenter->userRegisterDone(false, reason);
|
||||
return;
|
||||
}
|
||||
|
||||
// c) 把返回的数据保存到 DataCenter 中
|
||||
// 对于注册来说, 不需要保存任何信息, 直接跳过这个环节.
|
||||
|
||||
// d) 通知调用者响应处理完成
|
||||
emit dataCenter->userRegisterDone(true, "");
|
||||
|
||||
// e) 打印日志
|
||||
LOG() << "用户名注册->响应完成 requestId=" << pbResp->requestId();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
void NetClient::phoneLogin(const QString& phone, const QString& verifyCodeId, const QString& verifyCode)
|
||||
{
|
||||
// 1. 构造请求 body
|
||||
bite_im::PhoneLoginReq pbReq;
|
||||
pbReq.setRequestId(makeRequestId());
|
||||
pbReq.setPhoneNumber(phone);
|
||||
pbReq.setVerifyCodeId(verifyCodeId);
|
||||
pbReq.setVerifyCode(verifyCode);
|
||||
QByteArray body = pbReq.serialize(&serializer);
|
||||
LOG() << "手机号登录->发送请求 requestId=" << pbReq.requestId() << ", phone=" << pbReq.phoneNumber()
|
||||
<< ", verifyCodeId=" << pbReq.verifyCodeId() << ", verifyCode=" << pbReq.verifyCode();
|
||||
|
||||
// 2. 发送 HTTP 请求
|
||||
QNetworkReply* resp = this->sendHttpRequest("/service/user/phone_login", body);
|
||||
|
||||
// 3. 处理响应
|
||||
connect(resp, &QNetworkReply::finished, this, [=]() {
|
||||
// a) 解析响应
|
||||
bool ok = false;
|
||||
QString reason;
|
||||
auto pbResp = this->handleHttpResponse<bite_im::PhoneLoginRsp>(resp, &ok, &reason);
|
||||
|
||||
// b) 判定响应是否成功
|
||||
if (!ok) {
|
||||
LOG() << "手机号登录->响应出错! reason=" << reason;
|
||||
emit dataCenter->phoneLoginDone(false, reason);
|
||||
return;
|
||||
}
|
||||
|
||||
// c) 把响应结果记录到 DataCenter
|
||||
dataCenter->resetLoginSessionId(pbResp->loginSessionId());
|
||||
|
||||
// d) 发送信号
|
||||
emit dataCenter->phoneLoginDone(true, "");
|
||||
|
||||
// e) 打印日志
|
||||
LOG() << "手机号登录->响应完毕 requestId=" << pbResp->requestId();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
void NetClient::phoneRegister(const QString& phone, const QString& verifyCodeId, const QString& verifyCode)
|
||||
{
|
||||
// 1. 构造请求 body
|
||||
bite_im::PhoneRegisterReq pbReq;
|
||||
pbReq.setRequestId(makeRequestId());
|
||||
pbReq.setPhoneNumber(phone);
|
||||
pbReq.setVerifyCodeId(verifyCodeId);
|
||||
pbReq.setVerifyCode(verifyCode);
|
||||
QByteArray body = pbReq.serialize(&serializer);
|
||||
LOG() << "手机号注册->发送请求 requestId=" << pbReq.requestId() << ", phone=" << pbReq.phoneNumber()
|
||||
<< ", verifyCodeId=" << pbReq.verifyCodeId() << ", verifyCode=" << pbReq.verifyCode();
|
||||
|
||||
// 2. 发送 HTTP 请求
|
||||
QNetworkReply* resp = this->sendHttpRequest("/service/user/phone_register", body);
|
||||
|
||||
// 3. 处理响应
|
||||
connect(resp, &QNetworkReply::finished, this, [=]() {
|
||||
// a) 解析响应
|
||||
bool ok = false;
|
||||
QString reason;
|
||||
auto pbResp = this->handleHttpResponse<bite_im::PhoneRegisterRsp>(resp, &ok, &reason);
|
||||
|
||||
// b) 判定响应是否成功
|
||||
if (!ok) {
|
||||
LOG() << "手机号注册->响应失败! reason=" << reason;
|
||||
emit dataCenter->phoneRegisterDone(false, reason);
|
||||
return;
|
||||
}
|
||||
|
||||
// c) 让 DataCenter 记录结果, 注册操作不需要记录
|
||||
|
||||
// d) 发送信号
|
||||
emit dataCenter->phoneRegisterDone(true, "");
|
||||
|
||||
// e) 打印日志
|
||||
LOG() << "手机号注册->响应完成 requestId=" << pbResp->requestId();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
void NetClient::getSingleFile(const QString& loginSessionId, const QString& fileId)
|
||||
{
|
||||
//构造请求body
|
||||
bite_im::GetSingleFileReq pbReq;
|
||||
pbReq.setRequestId(makeRequestId());
|
||||
pbReq.setSessionId(loginSessionId);
|
||||
pbReq.setFileId(fileId);
|
||||
QByteArray body = pbReq.serialize(&serializer);
|
||||
LOG() << "获取文件内容->发送请求 requestId= " << pbReq.requestId() << ", fileId= " << fileId;
|
||||
|
||||
//发送HTTP请求
|
||||
QNetworkReply* resp = this->sendHttpRequest("/service/file/get_single_file", body);
|
||||
|
||||
//处理响应
|
||||
connect(resp, &QNetworkReply::finished, this, [=]() {
|
||||
//解析响应
|
||||
bool ok = false;
|
||||
QString reason;
|
||||
auto pbResp = this->handleHttpResponse<bite_im::GetSingleFileRsp>(resp, &ok, &reason);
|
||||
|
||||
//判定响应结果是否正确
|
||||
if (!ok) {
|
||||
LOG() << "获取文件内容->响应失败 reason= " << reason;
|
||||
return;
|
||||
}
|
||||
|
||||
//这里涉及的文件可能会很多,不使用DataCenter保存
|
||||
//直接通过信号把文件数据,投送到调用者位置
|
||||
|
||||
//发送信号
|
||||
emit dataCenter->getSingleFileDone(fileId, pbResp->fileData().fileContent());
|
||||
|
||||
//打印日志
|
||||
LOG() << "获取文件内容->响应完成 requestId= " << pbResp->requestId();
|
||||
});
|
||||
}
|
||||
|
||||
void NetClient::speechConvertText(const QString& loginSessionId, const QString& fileId, const QByteArray& content)
|
||||
{
|
||||
// 1. 构造请求 body
|
||||
bite_im::SpeechRecognitionReq pbReq;
|
||||
pbReq.setRequestId(makeRequestId());
|
||||
pbReq.setSessionId(loginSessionId);
|
||||
pbReq.setSpeechContent(content);
|
||||
QByteArray body = pbReq.serialize(&serializer);
|
||||
LOG() << "[语音转文字] 发送请求 requestId=" << pbReq.requestId() << ", loginSessonId=" << pbReq.sessionId();
|
||||
|
||||
// 2. 发送 HTTP 请求
|
||||
QNetworkReply* resp = this->sendHttpRequest("/service/speech/recognition", body);
|
||||
|
||||
// 3. 处理响应
|
||||
connect(resp, &QNetworkReply::finished, this, [=]() {
|
||||
// a) 解析响应
|
||||
bool ok = false;
|
||||
QString reason;
|
||||
auto pbResp = this->handleHttpResponse<bite_im::SpeechRecognitionRsp>(resp, &ok, &reason);
|
||||
|
||||
// b) 判定响应结果
|
||||
if (!ok) {
|
||||
LOG() << "[语音转文字] 响应错误! reason=" << reason;
|
||||
return;
|
||||
}
|
||||
|
||||
// c) 把结果写入到 DataCenter 中. 此处不打算通过 DataCenter 表示这里的语音识别结果. 直接通过 信号 通知结果即可.
|
||||
|
||||
// d) 发送信号, 通知调用者
|
||||
emit dataCenter->speechConvertTextDone(fileId, pbResp->recognitionResult());
|
||||
|
||||
// e) 打印日志
|
||||
LOG() << "[语音转文字] 响应完成 requestId=" << pbResp->requestId();
|
||||
});
|
||||
|
||||
}
|
||||
} //end namespace network
|
||||
@ -36,8 +36,13 @@ namespace network {
|
||||
//初始化websocket
|
||||
void initWebSocket();
|
||||
|
||||
//针对websocket的处理
|
||||
void handleWsResponse(const bite_im::NotifyMessage& notifyMessage);
|
||||
void handleWsMessage(const model::Message& message);
|
||||
void handleWsRemoveFriend(const QString& userId);
|
||||
void handleWsAddfriendApply(const model::UserInfo& userInfo);
|
||||
void handleWsAddFriendProcess(const model::UserInfo& userInfo, bool agree);
|
||||
void handleWsSessionCreate(const model::ChatSessionInfo& chatSessionInfo);
|
||||
|
||||
//发送身份认证请求
|
||||
void sendAuth();
|
||||
@ -95,6 +100,21 @@ namespace network {
|
||||
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);
|
||||
void deleteFriend(const QString& loginSessionId, const QString& userId);
|
||||
void addFriendApply(const QString& loginSessionId, const QString& userId);
|
||||
void acceptFriendApply(const QString& loginSessionId, const QString& userId);
|
||||
void rejectFriendApply(const QString& loginSessionId, const QString& userId);
|
||||
void createGroupChatSession(const QString& loginSessionId, const QList<QString>& userIdList);
|
||||
void getMemberList(const QString& loginSessionId, const QString& chatSessionId);
|
||||
void searchUser(const QString& loginSessionId, const QString& searchKey);
|
||||
void searchMessage(const QString& loginSessionId, const QString& chatSessionId, const QString& searchKey);
|
||||
void searchMessageByTime(const QString& loginSessionId, const QString& chatSessionId, const QDateTime& begTime, const QDateTime& endTime);
|
||||
void userLogin(const QString& username, const QString& password);
|
||||
void userRegister(const QString& username, const QString& password);
|
||||
void phoneLogin(const QString& phone, const QString& verifyCodeId, const QString& verifyCode);
|
||||
void phoneRegister(const QString& phone, const QString& verifyCodeId, const QString& verifyCode);
|
||||
void getSingleFile(const QString& loginSessionId, const QString& fileId);
|
||||
void speechConvertText(const QString& loginSessionId, const QString& fileId, const QByteArray& content);
|
||||
|
||||
private:
|
||||
model::DataCenter* dataCenter;
|
||||
|
||||
Reference in New Issue
Block a user