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:
@ -42,6 +42,42 @@ namespace network {
|
||||
//生成请求的Id
|
||||
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);
|
||||
private:
|
||||
model::DataCenter* dataCenter;
|
||||
|
||||
Reference in New Issue
Block a user