mirror of
https://gitee.com/Zhaoxin59/my-chat_-client.git
synced 2026-02-14 00:51:48 +08:00
Added the function of obtaining user information on the network
This commit is contained in:
@ -284,8 +284,14 @@ void MainWidget::initSignalSlot()
|
|||||||
/////////////////////////////////////
|
/////////////////////////////////////
|
||||||
// 获取个人的信息
|
// 获取个人的信息
|
||||||
/////////////////////////////////////
|
/////////////////////////////////////
|
||||||
|
//提供一个具体的方法,来获取到网络的数据
|
||||||
//dataCenter
|
connect(dataCenter, &DataCenter::getMyselfDone, this, [=]() {
|
||||||
|
//从DataCenter中拿到响应的结果myself,把里面的头像取出来,显示到界面上
|
||||||
|
//由于响应的逻辑已经执行完毕,说明从网络拿到了信息,则直接同步获取即可
|
||||||
|
auto myself = dataCenter->getMyselfsync();
|
||||||
|
userAvatar->setIcon(myself->avatar);
|
||||||
|
});
|
||||||
|
dataCenter->getMyselfAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWidget::switchTabToSession()
|
void MainWidget::switchTabToSession()
|
||||||
|
|||||||
@ -15,6 +15,7 @@
|
|||||||
#include "addfrienddialog.h"
|
#include "addfrienddialog.h"
|
||||||
#include "model/datacenter.h"
|
#include "model/datacenter.h"
|
||||||
|
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
namespace Ui {
|
namespace Ui {
|
||||||
class MainWidget;
|
class MainWidget;
|
||||||
|
|||||||
@ -149,4 +149,18 @@ namespace model
|
|||||||
netClient.getMyself(loginSessionId);
|
netClient.getMyself(loginSessionId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UserInfo* DataCenter::getMyselfsync()
|
||||||
|
{
|
||||||
|
return myself;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DataCenter::resetMyself(std::shared_ptr<bite_im::GetUserInfoRsp> resp)
|
||||||
|
{
|
||||||
|
if (myself == nullptr) {
|
||||||
|
myself = new UserInfo();
|
||||||
|
}
|
||||||
|
const bite_im::UserInfo& userInfo = resp->userInfo();
|
||||||
|
myself->load(userInfo);
|
||||||
|
}
|
||||||
|
|
||||||
} //end namespace model
|
} //end namespace model
|
||||||
|
|||||||
@ -97,6 +97,12 @@ namespace model
|
|||||||
//通过网络获取到用户的个人信息
|
//通过网络获取到用户的个人信息
|
||||||
void getMyselfAsync();
|
void getMyselfAsync();
|
||||||
|
|
||||||
|
UserInfo* getMyselfsync();
|
||||||
|
//
|
||||||
|
void resetMyself(std::shared_ptr<bite_im::GetUserInfoRsp> resp);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
|
//自定义信号
|
||||||
|
void getMyselfDone();
|
||||||
};
|
};
|
||||||
} //end namespace model
|
} //end namespace model
|
||||||
|
|||||||
@ -74,6 +74,19 @@ namespace network {
|
|||||||
return "R" + QUuid::createUuid().toString().sliced(25, 12);
|
return "R" + QUuid::createUuid().toString().sliced(25, 12);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//通过这个函数,把发送HTTP请求的操作封装一下
|
||||||
|
QNetworkReply* NetClient::sendHttpRequest(const QString& apiPath, const QByteArray& body)
|
||||||
|
{
|
||||||
|
//构造出HTTP请求
|
||||||
|
QNetworkRequest httpReq;
|
||||||
|
httpReq.setUrl(QUrl(HTTP_URL + apiPath));
|
||||||
|
httpReq.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-protobuf");
|
||||||
|
|
||||||
|
//发起HTTP请求
|
||||||
|
QNetworkReply* httpResp = httpClient.post(httpReq, body);
|
||||||
|
return httpResp;
|
||||||
|
}
|
||||||
|
|
||||||
//在这个函数内部,完成具体的网络通信即可
|
//在这个函数内部,完成具体的网络通信即可
|
||||||
void NetClient::getMyself(const QString& loginSessionId)
|
void NetClient::getMyself(const QString& loginSessionId)
|
||||||
{
|
{
|
||||||
@ -82,40 +95,52 @@ namespace network {
|
|||||||
req.setRequestId(makeRequestId());
|
req.setRequestId(makeRequestId());
|
||||||
req.setSessionId(loginSessionId);
|
req.setSessionId(loginSessionId);
|
||||||
QByteArray body = req.serialize(&serializer);
|
QByteArray body = req.serialize(&serializer);
|
||||||
LOG() << "获取个人信息: requestId=" << req.requestId() << ", loginSessionId=" << loginSessionId;
|
LOG() << "获取个人信息->发送请求: requestId=" << req.requestId() << ", loginSessionId=" << loginSessionId;
|
||||||
|
|
||||||
//构造出HTTP请求
|
//构造出HTTP请求
|
||||||
QNetworkRequest httpReq;
|
QNetworkReply* httpResp = sendHttpRequest("/service/user/get_user_info", body);
|
||||||
httpReq.setUrl(QUrl(HTTP_URL + "/service/user/get_user_info"));
|
|
||||||
httpReq.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-protobuf");
|
|
||||||
|
|
||||||
//发起HTTP请求
|
|
||||||
QNetworkReply* httpResp = httpClient.post(httpReq, body);
|
|
||||||
|
|
||||||
//通过信号槽,获取到当前的响应
|
//通过信号槽,获取到当前的响应
|
||||||
connect(httpResp, &QNetworkReply::finished, this, [=]() {
|
connect(httpResp, &QNetworkReply::finished, this, [=]() {
|
||||||
if (httpResp->error() != QNetworkReply::NoError) {
|
//if (httpResp->error() != QNetworkReply::NoError) {
|
||||||
//说明HTTP请求出错了,
|
// //说明HTTP请求出错了,
|
||||||
LOG() << "HTTP error: " << httpResp->errorString();
|
// LOG() << "HTTP error: " << httpResp->errorString();
|
||||||
httpResp->deleteLater();
|
// httpResp->deleteLater();
|
||||||
return;
|
// return;
|
||||||
}
|
//}
|
||||||
//说明拿到了body
|
////说明拿到了body
|
||||||
QByteArray respBody = httpResp->readAll();
|
//QByteArray respBody = httpResp->readAll();
|
||||||
|
|
||||||
//针对body进行反序列化,解析成对象
|
////针对body进行反序列化,解析成对象
|
||||||
bite_im::GetUserInfoRsp respObj;
|
//bite_im::GetUserInfoRsp respObj;
|
||||||
respObj.deserialize(&serializer, respBody);
|
//respObj.deserialize(&serializer, respBody);
|
||||||
|
|
||||||
//判定一下业务上是否出错
|
////判定一下业务上是否出错
|
||||||
if (!respObj.success()) {
|
//if (!respObj.success()) {
|
||||||
LOG() << "requestId= " << respObj.requestId() << ", errmsg=" << respObj.errmsg();
|
// LOG() << "requestId= " << respObj.requestId() << ", errmsg=" << respObj.errmsg();
|
||||||
httpResp->deleteLater();
|
// httpResp->deleteLater();
|
||||||
return;
|
// return;
|
||||||
}
|
//}
|
||||||
|
|
||||||
//继续处理后续的业务逻辑
|
//继续处理后续的业务逻辑
|
||||||
|
//获取http回复
|
||||||
|
bool ok = false;
|
||||||
|
QString reason;
|
||||||
|
auto resp = handleHttpResponse<bite_im::GetUserInfoRsp>(httpResp, &ok, &reason);
|
||||||
|
//判定响应是否正确
|
||||||
|
if (!ok) {
|
||||||
|
LOG() << "获取个人信息->出错 requestId= " << req.requestId() << ", reason= " << reason;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//把响应的数据保存到DataCenter
|
||||||
|
dataCenter->resetMyself(resp);
|
||||||
|
|
||||||
|
//通知调用逻辑,响应已经处理完成了,仍然通过信号槽通知
|
||||||
|
emit dataCenter->getMyselfDone();
|
||||||
|
|
||||||
|
//打印日志
|
||||||
|
LOG() << "获取个人信息->响应处理完毕 requestId" << req.requestId();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} //end namespace network
|
} //end namespace network
|
||||||
@ -42,6 +42,42 @@ namespace network {
|
|||||||
//生成请求的Id
|
//生成请求的Id
|
||||||
static QString makeRequestId();
|
static QString makeRequestId();
|
||||||
|
|
||||||
|
//封装发送请求的逻辑
|
||||||
|
QNetworkReply* sendHttpRequest(const QString& apiPath, const QByteArray& body);
|
||||||
|
|
||||||
|
//封装处理响应的逻辑(判定HTTP正确性,反序列化,判断业务的正确性)
|
||||||
|
//由于不同的api返回的pb对象结构不同,为了让一个函数能够处理多种不同的类型,需要使用模板
|
||||||
|
//后面两个是输出型参数,用于表示这次的操作是成功还是失败
|
||||||
|
template <typename T>
|
||||||
|
std::shared_ptr<T> handleHttpResponse(QNetworkReply* httpResp, bool* ok, QString* reason) {
|
||||||
|
//判定HTTP层面上
|
||||||
|
if (httpResp->error() != QNetworkReply::NoError) {
|
||||||
|
*ok = false;
|
||||||
|
*reason = httpResp->errorString();
|
||||||
|
httpResp->deleteLater();
|
||||||
|
return std::shared_ptr<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
//说明并没有出错, 那就获取到响应的body
|
||||||
|
QByteArray respBody = httpResp->readAll();
|
||||||
|
|
||||||
|
//针对body反序列化
|
||||||
|
std::shared_ptr<T> respObj = std::make_shared<T>();
|
||||||
|
respObj->deserialize(&serializer, respBody);
|
||||||
|
|
||||||
|
//判定业务的结构是否正确
|
||||||
|
if (!respObj->success()) {
|
||||||
|
*ok = false;
|
||||||
|
*reason = respObj->errmsg();
|
||||||
|
httpResp->deleteLater();
|
||||||
|
return std::shared_ptr<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
//释放httpResp对象
|
||||||
|
httpResp->deleteLater();
|
||||||
|
return respObj;
|
||||||
|
}
|
||||||
|
|
||||||
void getMyself(const QString& loginSessionId);
|
void getMyself(const QString& loginSessionId);
|
||||||
private:
|
private:
|
||||||
model::DataCenter* dataCenter;
|
model::DataCenter* dataCenter;
|
||||||
|
|||||||
Reference in New Issue
Block a user