add js signal handle.
All checks were successful
Deploy / Build (push) Successful in 5m57s

This commit is contained in:
amass 2025-01-04 14:04:27 +08:00
parent c11eca4340
commit 6f7238ea47
3 changed files with 38 additions and 14 deletions

View File

@ -24,7 +24,8 @@
#include <format> #include <format>
namespace WebToolkit { namespace WebToolkit {
Application::Application(const Wt::WEnvironment &env, bool embedded) : Wt::WApplication(env) { Application::Application(const Wt::WEnvironment &env, bool embedded)
: Wt::WApplication(env), m_startup(this, "startup"), m_logout(this, "logout") {
messageResourceBundle().use(appRoot() + "wt"); messageResourceBundle().use(appRoot() + "wt");
messageResourceBundle().use(appRoot() + "auth_strings"); messageResourceBundle().use(appRoot() + "auth_strings");
messageResourceBundle().use(appRoot() + "auth_css_theme"); messageResourceBundle().use(appRoot() + "auth_css_theme");
@ -33,6 +34,7 @@ Application::Application(const Wt::WEnvironment &env, bool embedded) : Wt::WAppl
m_session = Database::session(); m_session = Database::session();
m_session->login().changed().connect(this, &Application::authEvent); m_session->login().changed().connect(this, &Application::authEvent);
setTheme(std::make_shared<BulmaTheme>("bulma", !embedded)); setTheme(std::make_shared<BulmaTheme>("bulma", !embedded));
std::string externalPath;
if (!embedded) { if (!embedded) {
m_navigationBar = root()->addNew<NavigationBar>(); m_navigationBar = root()->addNew<NavigationBar>();
m_navigationBar->registerClicked.connect([this]() { m_navigationBar->registerClicked.connect([this]() {
@ -54,10 +56,10 @@ Application::Application(const Wt::WEnvironment &env, bool embedded) : Wt::WAppl
LOG(error) << "Missing: parameter: 'div'"; LOG(error) << "Missing: parameter: 'div'";
m_root = nullptr; m_root = nullptr;
} }
auto externalPath = env.getParameter("path"); auto path = env.getParameter("path");
if (externalPath != nullptr) { if (path != nullptr) {
m_externalPath = *externalPath; externalPath = *path;
LOG(info) << "external path: " << m_externalPath; LOG(info) << "external path: " << externalPath;
} else { } else {
auto parameters = env.getParameterMap(); auto parameters = env.getParameterMap();
for (auto &p : parameters) { for (auto &p : parameters) {
@ -74,14 +76,27 @@ Application::Application(const Wt::WEnvironment &env, bool embedded) : Wt::WAppl
auto app = Amass::Singleton<WebToolkit::Server>::instance(); auto app = Amass::Singleton<WebToolkit::Server>::instance();
m_loginPage = std::make_unique<LoginPage>(app->authService(), m_session->users(), m_session->login()); m_loginPage = std::make_unique<LoginPage>(app->authService(), m_session->users(), m_session->login());
if (m_externalPath.empty()) { if (externalPath.empty()) {
m_loginPage->processEnvironment(); m_loginPage->processEnvironment();
} else { } else {
m_loginPage->processExternalEnvironment(m_externalPath, app->authService()); m_loginPage->processExternalEnvironment(externalPath, app->authService());
} }
m_logout.connect([this]() {
handlePathChange(m_externalPath.empty() ? internalPath() : m_externalPath); LOG(info) << "logout from external callbak.";
m_session->login().logout();
});
m_startup.connect(this, [externalPath]() {
LOG(info) << "wtapp started.";
auto app = Wt::WApplication::instance();
auto path = externalPath.empty() ? app->internalPath() : externalPath;
if (path != app->internalPath()) {
app->setInternalPath(externalPath.empty() ? app->internalPath() : externalPath, true);
} else {
dynamic_cast<Application *>(app)->handlePathChange(path);
}
});
internalPathChanged().connect(this, &Application::handlePathChange); internalPathChanged().connect(this, &Application::handlePathChange);
doJavaScript(m_startup.createCall({}));
} }
Application::~Application() { Application::~Application() {
@ -119,8 +134,13 @@ void Application::authEvent() {
setCookie(cookie); setCookie(cookie);
} }
} else { } else {
m_loginPage = m_navigationBar->removeLoginItem(); if (m_navigationBar != nullptr) {
LOG(info) << "User logged out."; m_loginPage = m_navigationBar->removeLoginItem();
}
LOG(info) << "user logged out, internal path: " << internalPath();
if (internalPath() == "/wt/login") {
handlePathChange(internalPath());
}
} }
doJavaScript("if (window.updateAuthStatus) window.updateAuthStatus();"); doJavaScript("if (window.updateAuthStatus) window.updateAuthStatus();");
} }

View File

@ -36,12 +36,13 @@ protected:
private: private:
std::unique_ptr<Session> m_session; std::unique_ptr<Session> m_session;
std::string m_externalPath; Wt::JSignal<> m_startup;
Wt::WContainerWidget *m_root = nullptr; Wt::WContainerWidget *m_root = nullptr;
NavigationBar *m_navigationBar = nullptr; NavigationBar *m_navigationBar = nullptr;
std::unique_ptr<LoginPage> m_loginPage; std::unique_ptr<LoginPage> m_loginPage;
LoginPage *m_loginPageRef = nullptr; LoginPage *m_loginPageRef = nullptr;
Wt::JSignal<> m_logout;
}; };
class Server { class Server {

View File

@ -11,10 +11,13 @@ HomePage::HomePage() {
auto ul = addWidget(std::make_unique<Wt::WContainerWidget>()); auto ul = addWidget(std::make_unique<Wt::WContainerWidget>());
ul->setList(true); ul->setList(true);
auto li = ul->addWidget(std::make_unique<Wt::WText>(R"(<a href="/wt/login">登录页面</a>)")); auto li = ul->addNew<Wt::WContainerWidget>();
li->setHtmlTagName("li"); li->setHtmlTagName("li");
li = ul->addWidget(std::make_unique<Wt::WText>(R"(<a href="/wt/visitor/analysis">访客数据</a>)")); li->addNew<Wt::WAnchor>(Wt::WLink(Wt::LinkType::InternalPath, "/wt/login"), "登录页面");
li = ul->addNew<Wt::WContainerWidget>();
li->setHtmlTagName("li"); li->setHtmlTagName("li");
li->addNew<Wt::WAnchor>(Wt::WLink(Wt::LinkType::InternalPath, "/wt/visitor/analysis"), "访客数据");
addWidget(std::make_unique<Wt::WText>("Your name, please ? ")); addWidget(std::make_unique<Wt::WText>("Your name, please ? "));
m_nameEdit = addWidget(std::make_unique<Wt::WLineEdit>()); m_nameEdit = addWidget(std::make_unique<Wt::WLineEdit>());