diff --git a/WebApplication/Application.cpp b/WebApplication/Application.cpp index ab7e1ad..9f37c4c 100644 --- a/WebApplication/Application.cpp +++ b/WebApplication/Application.cpp @@ -24,7 +24,8 @@ #include 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("bulma", !embedded)); + std::string externalPath; if (!embedded) { m_navigationBar = root()->addNew(); 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::instance(); m_loginPage = std::make_unique(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(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();"); } diff --git a/WebApplication/Application.h b/WebApplication/Application.h index 362ea47..48f0684 100644 --- a/WebApplication/Application.h +++ b/WebApplication/Application.h @@ -36,12 +36,13 @@ protected: private: std::unique_ptr m_session; - std::string m_externalPath; + Wt::JSignal<> m_startup; Wt::WContainerWidget *m_root = nullptr; NavigationBar *m_navigationBar = nullptr; std::unique_ptr m_loginPage; LoginPage *m_loginPageRef = nullptr; + Wt::JSignal<> m_logout; }; class Server { diff --git a/WebApplication/HomePage.cpp b/WebApplication/HomePage.cpp index b26fd99..c13def8 100644 --- a/WebApplication/HomePage.cpp +++ b/WebApplication/HomePage.cpp @@ -11,10 +11,13 @@ HomePage::HomePage() { auto ul = addWidget(std::make_unique()); ul->setList(true); - auto li = ul->addWidget(std::make_unique(R"(登录页面)")); + auto li = ul->addNew(); li->setHtmlTagName("li"); - li = ul->addWidget(std::make_unique(R"(访客数据)")); + li->addNew(Wt::WLink(Wt::LinkType::InternalPath, "/wt/login"), "登录页面"); + + li = ul->addNew(); li->setHtmlTagName("li"); + li->addNew(Wt::WLink(Wt::LinkType::InternalPath, "/wt/visitor/analysis"), "访客数据"); addWidget(std::make_unique("Your name, please ? ")); m_nameEdit = addWidget(std::make_unique());