geopro/src/net/ApiClient.hpp

47 lines
1.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <QJsonObject>
#include <QString>
#include <memory>
class QNetworkAccessManager;
namespace geopro::net {
// 服务端统一响应信封:{code, data, msg}。
// httpStatus 为 HTTP 状态码(如 200code/data/msg 解析自响应体 JSON。
// 网络层错误连接失败、超时、JSON 解析失败)写入 rawError此时 httpStatus 可能为 0。
struct ApiResponse {
int httpStatus = 0;
int code = 0;
QJsonObject data;
QString msg;
QString rawError;
};
// QtNetwork 的同步 HTTP 封装。
// 内部持有【唯一一个】QNetworkAccessManager 成员,默认共享 cookie jar
// 因此同一 ApiClient 实例发出的多次请求处于同一会话(共享 JSESSIONID
// 这是登录流程 getImageCode -> verifyCodeCheck -> login2 串联的关键。
class ApiClient {
public:
explicit ApiClient(QString baseUrl);
~ApiClient();
ApiClient(const ApiClient&) = delete;
ApiClient& operator=(const ApiClient&) = delete;
// 设置令牌;注入请求头 geomativeauthorization。token 值本身已含 "Geomative " 前缀。
void setToken(const QString& token);
// 同步 GET / POST(JSON)。用 QNetworkReply + QEventLoop 阻塞等待响应。
ApiResponse get(const QString& path);
ApiResponse postJson(const QString& path, const QJsonObject& body);
private:
struct Impl;
std::unique_ptr<Impl> impl_;
};
} // namespace geopro::net