79 lines
3.0 KiB
C++
79 lines
3.0 KiB
C++
#include "AuthLoads.hpp"
|
||
|
||
#include "ApiChain.hpp"
|
||
#include "ApiClient.hpp" // geopro::net::ApiResponse
|
||
#include "IApiCall.hpp"
|
||
|
||
namespace geopro::net {
|
||
|
||
namespace {
|
||
|
||
// 统一的失败文案:优先服务端 msg,否则回退传输层 rawError,最后给通用文案。
|
||
QString reasonOf(const ApiResponse& resp, const QString& fallback) {
|
||
if (!resp.msg.isEmpty()) return resp.msg;
|
||
if (!resp.rawError.isEmpty()) return resp.rawError;
|
||
return fallback;
|
||
}
|
||
|
||
} // namespace
|
||
|
||
CaptchaLoad::CaptchaLoad(IApiCall* call, QObject* parent) : QObject(parent), call_(call) {
|
||
QObject::connect(call_, &IApiCall::finished, this, [this](const ApiResponse& resp) {
|
||
if (aborted_) return; // §5.0 入口守卫
|
||
if (resp.code != 200 || !resp.rawError.isEmpty()) {
|
||
aborted_ = true; // 终态置位:到达 failed 终态后 abort() 早退
|
||
emit failed(reasonOf(resp, QStringLiteral("获取验证码失败")));
|
||
deleteLater();
|
||
return;
|
||
}
|
||
AuthService::Captcha cap;
|
||
cap.codeId = resp.data.value(QStringLiteral("id")).toString();
|
||
cap.code = resp.data.value(QStringLiteral("code")).toString();
|
||
aborted_ = true; // 终态置位:到达 done 终态后 abort() 早退
|
||
emit done(cap);
|
||
deleteLater();
|
||
});
|
||
}
|
||
|
||
void CaptchaLoad::abort() {
|
||
if (aborted_) return;
|
||
aborted_ = true;
|
||
if (call_) call_->abort();
|
||
deleteLater();
|
||
}
|
||
|
||
LoginLoad::LoginLoad(ApiChain* chain, QObject* parent) : QObject(parent), chain_(chain) {
|
||
QObject::connect(chain_, &ApiChain::succeeded, this,
|
||
[this](const QList<ApiResponse>& responses) {
|
||
if (aborted_) return; // §5.0 入口守卫
|
||
const QString token =
|
||
responses.isEmpty()
|
||
? QString()
|
||
: responses.last().data.value(QStringLiteral("accessToken")).toString();
|
||
if (token.isEmpty()) {
|
||
aborted_ = true; // 终态置位:到达 failed 终态后 abort() 早退
|
||
emit failed(QStringLiteral("登录成功但缺少 accessToken"));
|
||
deleteLater();
|
||
return;
|
||
}
|
||
aborted_ = true; // 终态置位:到达 done 终态后 abort() 早退
|
||
emit done(token);
|
||
deleteLater();
|
||
});
|
||
QObject::connect(chain_, &ApiChain::failed, this, [this](int, const ApiResponse& resp) {
|
||
if (aborted_) return; // §5.0 入口守卫
|
||
aborted_ = true; // 终态置位:到达 failed 终态后 abort() 早退
|
||
emit failed(reasonOf(resp, QStringLiteral("登录失败")));
|
||
deleteLater();
|
||
});
|
||
}
|
||
|
||
void LoginLoad::abort() {
|
||
if (aborted_) return;
|
||
aborted_ = true;
|
||
if (chain_) chain_->abort();
|
||
deleteLater();
|
||
}
|
||
|
||
} // namespace geopro::net
|