75 lines
3.2 KiB
C++
75 lines
3.2 KiB
C++
#include "api/ApiProjectRepository.hpp"
|
||
|
||
#include <QJsonArray>
|
||
#include <QJsonObject>
|
||
#include <QString>
|
||
#include <QUrl>
|
||
|
||
#include "ApiClient.hpp"
|
||
#include "dto/NavDto.hpp"
|
||
|
||
namespace geopro::data {
|
||
|
||
namespace {
|
||
constexpr int kCodeSuccess = 200;
|
||
|
||
bool ok(const net::ApiResponse& r) { return r.code == kCodeSuccess; }
|
||
|
||
std::string errorOf(const net::ApiResponse& r, const char* fallback) {
|
||
if (!r.msg.isEmpty()) return r.msg.toStdString();
|
||
if (!r.rawError.isEmpty()) return r.rawError.toStdString();
|
||
return fallback;
|
||
}
|
||
|
||
// 后端 id 进 URL 前做百分号编码(不可信外部数据:防 ? # & / 空格 破坏路径/查询)。
|
||
QString enc(const std::string& s) {
|
||
return QString::fromUtf8(QUrl::toPercentEncoding(QString::fromStdString(s)));
|
||
}
|
||
} // namespace
|
||
|
||
ApiProjectRepository::ApiProjectRepository(net::ApiClient& api) : api_(api) {}
|
||
|
||
RepoResult<std::vector<Workspace>> ApiProjectRepository::listWorkspaces() {
|
||
const net::ApiResponse r =
|
||
api_.get(QStringLiteral("/business/system/tenant/enterprise/joined/list"));
|
||
if (!ok(r)) return {false, {}, errorOf(r, "listWorkspaces failed")};
|
||
return {true, dto::parseWorkspaces(r.data.value(QStringLiteral("value")).toArray()), {}};
|
||
}
|
||
|
||
RepoResult<bool> ApiProjectRepository::switchWorkspace(const std::string& tenantId) {
|
||
const QString path =
|
||
QStringLiteral("/business/system/tenant/enterprise/switch/%1").arg(enc(tenantId));
|
||
const net::ApiResponse r = api_.postJson(path, QJsonObject{});
|
||
if (!ok(r)) return {false, false, errorOf(r, "switchWorkspace failed")};
|
||
// 切换空间返回新 accessToken:必须重新注入,后续请求才落到新空间。
|
||
const QString token = r.data.value(QStringLiteral("accessToken")).toString();
|
||
if (!token.isEmpty()) api_.setToken(token);
|
||
return {true, true, {}};
|
||
}
|
||
|
||
RepoResult<std::vector<ProjectSummary>> ApiProjectRepository::listProjects(const std::string&) {
|
||
// 我的工作台项目列表(当前空间全部项目)。queryByUser 实测为空,故用此接口。
|
||
const net::ApiResponse r = api_.get(QStringLiteral("/business/my/profile/queryProject"));
|
||
if (!ok(r)) return {false, {}, errorOf(r, "listProjects failed")};
|
||
return {true, dto::parseProjectList(r.data.value(QStringLiteral("value")).toArray()), {}};
|
||
}
|
||
|
||
RepoResult<std::vector<StructNode>> ApiProjectRepository::loadStructure(const std::string& projectId) {
|
||
// 项目结构(项目根 + GS + TM;不含 DS)。比 projectWorkbench 干净。
|
||
const QString path =
|
||
QStringLiteral("/business/projectStruct/queryProjectStruct/%1").arg(enc(projectId));
|
||
const net::ApiResponse r = api_.get(path);
|
||
if (!ok(r)) return {false, {}, errorOf(r, "loadStructure failed")};
|
||
return {true, dto::parseStructNodes(r.data.value(QStringLiteral("value")).toArray()), {}};
|
||
}
|
||
|
||
RepoResult<std::vector<DsNode>> ApiProjectRepository::loadDatasetsOfTm(const std::string& tmObjectId) {
|
||
const QString path = QStringLiteral("/business/projectWorkbench/queryDsByTmObjectId/%1")
|
||
.arg(enc(tmObjectId));
|
||
const net::ApiResponse r = api_.get(path);
|
||
if (!ok(r)) return {false, {}, errorOf(r, "loadDatasetsOfTm failed")};
|
||
return {true, dto::parseDatasets(r.data.value(QStringLiteral("value")).toArray()), {}};
|
||
}
|
||
|
||
} // namespace geopro::data
|