diff --git a/WebApplication/Application.cpp b/WebApplication/Application.cpp index 4502e28..fbb5a0a 100644 --- a/WebApplication/Application.cpp +++ b/WebApplication/Application.cpp @@ -157,7 +157,7 @@ void Application::handlePathChange(const std::string &path) { LOG(info) << "already logged in."; m_root->clear(); auto p = m_root->addNew(); - p->setRedirect("/", "您已经登录..."); + p->setMessage("您已经登录..."); } else { if (m_loginPage) { m_root->clear(); @@ -177,7 +177,8 @@ void Application::handlePathChange(const std::string &path) { } else if (path.starts_with("/wt/redirect")) { m_root->clear(); auto p = m_root->addNew(); - p->setRedirect("/", "您已经登录..."); + p->setMessage("这是一个跳转测试...."); + p->setRedirect("https://amass.fun"); } else { m_root->clear(); m_root->addNew(); diff --git a/WebApplication/RedirectPage.cpp b/WebApplication/RedirectPage.cpp index 25c173a..f79a33f 100644 --- a/WebApplication/RedirectPage.cpp +++ b/WebApplication/RedirectPage.cpp @@ -1,33 +1,70 @@ #include "RedirectPage.h" #include "BoostLog.h" +#include #include constexpr auto Template = R"(

${message}

-
您将在${seconds}秒后返回上一页。
+
您将在${seconds}秒后${go}${anchor}。
)"; -RedirectPage::RedirectPage() : Wt::WTemplate(Template) { +RedirectPage::RedirectPage() : Wt::WTemplate(Template), m_history(this, "history") { m_timer = addChild(std::make_unique()); m_timer->setInterval(std::chrono::milliseconds(1000)); m_timer->timeout().connect(this, &RedirectPage::onTimeout); m_timer->start(); + bindEmpty("message"); + bindEmpty("anchor"); + bindString("go", "返回"); 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()); + 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) { - bindString("message", message); +void RedirectPage::setRedirect(const std::string &path, const std::string &text) { if (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() { - m_time -= 1000; - bindInt("seconds", m_time / 1000); - if (m_time <= 0) { +void RedirectPage::setMessage(const std::string &message) { + bindString("message", message); +} + +void RedirectPage::redirect(const std::string &path) { + if (m_timer->isActive()) { 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")) { app->setInternalPath(m_redirect, true); } 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); + } +} diff --git a/WebApplication/RedirectPage.h b/WebApplication/RedirectPage.h index 864b7ef..c3194cc 100644 --- a/WebApplication/RedirectPage.h +++ b/WebApplication/RedirectPage.h @@ -6,15 +6,21 @@ class RedirectPage : public Wt::WTemplate { public: 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: void onTimeout(); private: + Wt::JSignal m_history; + int m_historySize = 0; Wt::WTimer *m_timer = nullptr; + Wt::WAnchor *m_anchor = nullptr; int m_time = 5000; std::string m_redirect; + std::string m_text; }; #endif // __REDIRECTPAGE_H__ \ No newline at end of file