add redirect page.
All checks were successful
Deploy / Build (push) Successful in 6m48s

This commit is contained in:
amass 2025-01-05 00:33:19 +08:00
parent 6f7238ea47
commit 1301e4bbc5
4 changed files with 66 additions and 0 deletions

View File

@ -5,6 +5,7 @@
#include "HomePage.h" #include "HomePage.h"
#include "LoginPage.h" #include "LoginPage.h"
#include "NavigationBar.h" #include "NavigationBar.h"
#include "RedirectPage.h"
#include "Restful.h" #include "Restful.h"
#include "VisitorRecordsPage.h" #include "VisitorRecordsPage.h"
#include "model/AuthModel.h" #include "model/AuthModel.h"
@ -154,6 +155,9 @@ void Application::handlePathChange(const std::string &path) {
if (path.starts_with("/wt/login")) { if (path.starts_with("/wt/login")) {
if (m_session->login().loggedIn()) { if (m_session->login().loggedIn()) {
LOG(info) << "already logged in."; LOG(info) << "already logged in.";
m_root->clear();
auto p = m_root->addNew<RedirectPage>();
p->setRedirect("/", "您已经登录...");
} else { } else {
if (m_loginPage) { if (m_loginPage) {
m_root->clear(); m_root->clear();
@ -170,6 +174,10 @@ void Application::handlePathChange(const std::string &path) {
} else if (path.starts_with("/wt/visitor/analysis")) { } else if (path.starts_with("/wt/visitor/analysis")) {
m_root->clear(); m_root->clear();
m_root->addNew<VisitorRecordsPage>(*m_session); m_root->addNew<VisitorRecordsPage>(*m_session);
} else if (path.starts_with("/wt/redirect")) {
m_root->clear();
auto p = m_root->addNew<RedirectPage>();
p->setRedirect("/", "您已经登录...");
} else { } else {
m_root->clear(); m_root->clear();
m_root->addNew<HomePage>(); m_root->addNew<HomePage>();

View File

@ -6,6 +6,7 @@ add_library(WebApplication
HomePage.h HomePage.cpp HomePage.h HomePage.cpp
LoginPage.h LoginPage.cpp LoginPage.h LoginPage.cpp
NavigationBar.h NavigationBar.cpp NavigationBar.h NavigationBar.cpp
RedirectPage.h RedirectPage.cpp
VisitorRecordsPage.h VisitorRecordsPage.cpp VisitorRecordsPage.h VisitorRecordsPage.cpp
Restful.h Restful.cpp Restful.h Restful.cpp
Dialog.h Dialog.cpp Dialog.h Dialog.cpp

View File

@ -0,0 +1,37 @@
#include "RedirectPage.h"
#include "BoostLog.h"
#include <Wt/WTimer.h>
constexpr auto Template = R"(
<p>${message}</p>
<div>${seconds}</div>
)";
RedirectPage::RedirectPage() : Wt::WTemplate(Template) {
m_timer = addChild(std::make_unique<Wt::WTimer>());
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);
}
}
}

View File

@ -0,0 +1,20 @@
#ifndef __REDIRECTPAGE_H__
#define __REDIRECTPAGE_H__
#include <Wt/WTemplate.h>
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__