finish redirect.
All checks were successful
Deploy / Build (push) Successful in 6m12s

This commit is contained in:
amass 2025-01-05 14:23:02 +08:00
parent 1301e4bbc5
commit 38e6447885
3 changed files with 65 additions and 12 deletions

View File

@ -157,7 +157,7 @@ void Application::handlePathChange(const std::string &path) {
LOG(info) << "already logged in."; LOG(info) << "already logged in.";
m_root->clear(); m_root->clear();
auto p = m_root->addNew<RedirectPage>(); auto p = m_root->addNew<RedirectPage>();
p->setRedirect("/", "您已经登录..."); p->setMessage("您已经登录...");
} else { } else {
if (m_loginPage) { if (m_loginPage) {
m_root->clear(); m_root->clear();
@ -177,7 +177,8 @@ void Application::handlePathChange(const std::string &path) {
} else if (path.starts_with("/wt/redirect")) { } else if (path.starts_with("/wt/redirect")) {
m_root->clear(); m_root->clear();
auto p = m_root->addNew<RedirectPage>(); auto p = m_root->addNew<RedirectPage>();
p->setRedirect("/", "您已经登录..."); p->setMessage("这是一个跳转测试....");
p->setRedirect("https://amass.fun");
} else { } else {
m_root->clear(); m_root->clear();
m_root->addNew<HomePage>(); m_root->addNew<HomePage>();

View File

@ -1,33 +1,70 @@
#include "RedirectPage.h" #include "RedirectPage.h"
#include "BoostLog.h" #include "BoostLog.h"
#include <Wt/WAnchor.h>
#include <Wt/WTimer.h> #include <Wt/WTimer.h>
constexpr auto Template = R"( constexpr auto Template = R"(
<p>${message}</p> <p>${message}</p>
<div>${seconds}</div> <div>${seconds}${go}${anchor}</div>
)"; )";
RedirectPage::RedirectPage() : Wt::WTemplate(Template) { RedirectPage::RedirectPage() : Wt::WTemplate(Template), m_history(this, "history") {
m_timer = addChild(std::make_unique<Wt::WTimer>()); m_timer = addChild(std::make_unique<Wt::WTimer>());
m_timer->setInterval(std::chrono::milliseconds(1000)); m_timer->setInterval(std::chrono::milliseconds(1000));
m_timer->timeout().connect(this, &RedirectPage::onTimeout); m_timer->timeout().connect(this, &RedirectPage::onTimeout);
m_timer->start(); m_timer->start();
bindEmpty("message");
bindEmpty("anchor");
bindString("go", "返回");
bindInt("seconds", m_time / 1000); bindInt("seconds", m_time / 1000);
m_history.connect(this, [this](int history) {
LOG(info) << "history: " << history;
m_historySize = history;
m_anchor = bindWidget("anchor", std::make_unique<Wt::WAnchor>());
if (m_redirect.empty()) {
m_anchor->setText(history > 2 ? "上一页" : "首页");
bindString("go", history > 2 ? "返回" : "前往");
m_anchor->clicked().connect(this, [this]() { redirect(m_redirect); });
} else {
bindString("go", "前往");
m_anchor->setText(m_text.empty() ? m_redirect : m_text);
m_anchor->setLink(Wt::WLink(Wt::LinkType::Url, m_redirect));
}
});
doJavaScript(std::format("{}.emit({},'{}',window.history.length)", Wt::WApplication::instance()->javaScriptClass(), id(),
m_history.name()));
} }
void RedirectPage::setRedirect(const std::string &path, const std::string &message) { void RedirectPage::setRedirect(const std::string &path, const std::string &text) {
bindString("message", message);
if (m_redirect != path) { if (m_redirect != path) {
m_redirect = path; m_redirect = path;
} }
if (m_text != text) {
m_text = text;
}
if (m_anchor != nullptr) {
m_anchor->setText(text.empty() ? path : text);
m_anchor->setLink(Wt::WLink(Wt::LinkType::Url, path));
}
} }
void RedirectPage::onTimeout() { void RedirectPage::setMessage(const std::string &message) {
m_time -= 1000; bindString("message", message);
bindInt("seconds", m_time / 1000); }
if (m_time <= 0) {
void RedirectPage::redirect(const std::string &path) {
if (m_timer->isActive()) {
m_timer->stop(); m_timer->stop();
}
auto app = Wt::WApplication::instance(); auto app = Wt::WApplication::instance();
if (path.empty()) {
if (m_historySize > 2) {
doJavaScript("window.history.go(-1)");
} else {
app->redirect("/");
}
} else {
if (m_redirect.starts_with("/wt")) { if (m_redirect.starts_with("/wt")) {
app->setInternalPath(m_redirect, true); app->setInternalPath(m_redirect, true);
} else { } else {
@ -35,3 +72,12 @@ void RedirectPage::onTimeout() {
} }
} }
} }
void RedirectPage::onTimeout() {
m_time -= 1000;
if (m_time <= 0) {
redirect(m_redirect); // stop timer inside.
} else {
bindInt("seconds", m_time / 1000);
}
}

View File

@ -6,15 +6,21 @@
class RedirectPage : public Wt::WTemplate { class RedirectPage : public Wt::WTemplate {
public: public:
RedirectPage(); RedirectPage();
void setRedirect(const std::string &path, const std::string &message); void setRedirect(const std::string &path, const std::string &text = "");
void setMessage(const std::string &message);
void redirect(const std::string &path);
protected: protected:
void onTimeout(); void onTimeout();
private: private:
Wt::JSignal<int> m_history;
int m_historySize = 0;
Wt::WTimer *m_timer = nullptr; Wt::WTimer *m_timer = nullptr;
Wt::WAnchor *m_anchor = nullptr;
int m_time = 5000; int m_time = 5000;
std::string m_redirect; std::string m_redirect;
std::string m_text;
}; };
#endif // __REDIRECTPAGE_H__ #endif // __REDIRECTPAGE_H__