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>
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() + "auth_strings");
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->login().changed().connect(this, &Application::authEvent);
setTheme(std::make_shared<BulmaTheme>("bulma", !embedded));
std::string externalPath;
if (!embedded) {
m_navigationBar = root()->addNew<NavigationBar>();
m_navigationBar->registerClicked.connect([this]() {
@ -54,10 +56,10 @@ Application::Application(const Wt::WEnvironment &env, bool embedded) : Wt::WAppl
LOG(error) << "Missing: parameter: 'div'";
m_root = nullptr;
}
auto externalPath = env.getParameter("path");
if (externalPath != nullptr) {
m_externalPath = *externalPath;
LOG(info) << "external path: " << m_externalPath;
auto path = env.getParameter("path");
if (path != nullptr) {
externalPath = *path;
LOG(info) << "external path: " << externalPath;
} else {
auto parameters = env.getParameterMap();
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();
m_loginPage = std::make_unique<LoginPage>(app->authService(), m_session->users(), m_session->login());
if (m_externalPath.empty()) {
if (externalPath.empty()) {
m_loginPage->processEnvironment();
} else {
m_loginPage->processExternalEnvironment(m_externalPath, app->authService());
m_loginPage->processExternalEnvironment(externalPath, app->authService());
}
handlePathChange(m_externalPath.empty() ? internalPath() : m_externalPath);
m_logout.connect([this]() {
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);
doJavaScript(m_startup.createCall({}));
}
Application::~Application() {
@ -119,8 +134,13 @@ void Application::authEvent() {
setCookie(cookie);
}
} else {
m_loginPage = m_navigationBar->removeLoginItem();
LOG(info) << "User logged out.";
if (m_navigationBar != nullptr) {
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();");
}

View File

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

View File

@ -11,10 +11,13 @@ HomePage::HomePage() {
auto ul = addWidget(std::make_unique<Wt::WContainerWidget>());
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 = 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->addNew<Wt::WAnchor>(Wt::WLink(Wt::LinkType::InternalPath, "/wt/visitor/analysis"), "访客数据");
addWidget(std::make_unique<Wt::WText>("Your name, please ? "));
m_nameEdit = addWidget(std::make_unique<Wt::WLineEdit>());