diff --git a/WebApplication/Application.cpp b/WebApplication/Application.cpp index 9f37c4c..4502e28 100644 --- a/WebApplication/Application.cpp +++ b/WebApplication/Application.cpp @@ -5,6 +5,7 @@ #include "HomePage.h" #include "LoginPage.h" #include "NavigationBar.h" +#include "RedirectPage.h" #include "Restful.h" #include "VisitorRecordsPage.h" #include "model/AuthModel.h" @@ -154,6 +155,9 @@ void Application::handlePathChange(const std::string &path) { if (path.starts_with("/wt/login")) { if (m_session->login().loggedIn()) { LOG(info) << "already logged in."; + m_root->clear(); + auto p = m_root->addNew(); + p->setRedirect("/", "您已经登录..."); } else { if (m_loginPage) { m_root->clear(); @@ -170,6 +174,10 @@ void Application::handlePathChange(const std::string &path) { } else if (path.starts_with("/wt/visitor/analysis")) { m_root->clear(); m_root->addNew(*m_session); + } else if (path.starts_with("/wt/redirect")) { + m_root->clear(); + auto p = m_root->addNew(); + p->setRedirect("/", "您已经登录..."); } else { m_root->clear(); m_root->addNew(); diff --git a/WebApplication/CMakeLists.txt b/WebApplication/CMakeLists.txt index 68b2041..42627c2 100644 --- a/WebApplication/CMakeLists.txt +++ b/WebApplication/CMakeLists.txt @@ -6,6 +6,7 @@ add_library(WebApplication HomePage.h HomePage.cpp LoginPage.h LoginPage.cpp NavigationBar.h NavigationBar.cpp + RedirectPage.h RedirectPage.cpp VisitorRecordsPage.h VisitorRecordsPage.cpp Restful.h Restful.cpp Dialog.h Dialog.cpp diff --git a/WebApplication/RedirectPage.cpp b/WebApplication/RedirectPage.cpp new file mode 100644 index 0000000..25c173a --- /dev/null +++ b/WebApplication/RedirectPage.cpp @@ -0,0 +1,37 @@ +#include "RedirectPage.h" +#include "BoostLog.h" +#include + +constexpr auto Template = R"( +

${message}

+
您将在${seconds}秒后返回上一页。
+)"; + +RedirectPage::RedirectPage() : Wt::WTemplate(Template) { + m_timer = addChild(std::make_unique()); + m_timer->setInterval(std::chrono::milliseconds(1000)); + m_timer->timeout().connect(this, &RedirectPage::onTimeout); + m_timer->start(); + bindInt("seconds", m_time / 1000); +} + +void RedirectPage::setRedirect(const std::string &path, const std::string &message) { + bindString("message", message); + if (m_redirect != path) { + m_redirect = path; + } +} + +void RedirectPage::onTimeout() { + m_time -= 1000; + bindInt("seconds", m_time / 1000); + if (m_time <= 0) { + m_timer->stop(); + auto app = Wt::WApplication::instance(); + if (m_redirect.starts_with("/wt")) { + app->setInternalPath(m_redirect, true); + } else { + app->redirect(m_redirect); + } + } +} diff --git a/WebApplication/RedirectPage.h b/WebApplication/RedirectPage.h new file mode 100644 index 0000000..864b7ef --- /dev/null +++ b/WebApplication/RedirectPage.h @@ -0,0 +1,20 @@ +#ifndef __REDIRECTPAGE_H__ +#define __REDIRECTPAGE_H__ + +#include + +class RedirectPage : public Wt::WTemplate { +public: + RedirectPage(); + void setRedirect(const std::string &path, const std::string &message); + +protected: + void onTimeout(); + +private: + Wt::WTimer *m_timer = nullptr; + int m_time = 5000; + std::string m_redirect; +}; + +#endif // __REDIRECTPAGE_H__ \ No newline at end of file