improve wt code.
All checks were successful
Deploy / Build (push) Successful in 7m25s

This commit is contained in:
amass 2024-12-21 23:08:38 +08:00
parent ab2fe4f492
commit 6ab3e2b22b
4 changed files with 85 additions and 24 deletions

View File

@ -59,12 +59,13 @@ Application::Application(const Wt::WEnvironment &env, bool embedded) : Wt::WAppl
LOG(info) << "relative resources url: " << relativeResourcesUrl(); LOG(info) << "relative resources url: " << relativeResourcesUrl();
LOG(info) << "resources url: " << resourcesUrl(); LOG(info) << "resources url: " << resourcesUrl();
LOG(info) << "internal path: " << internalPath(); LOG(info) << "internal path: " << internalPath();
internalPathChanged().connect(this, &Application::handlePathChange);
handlePathChange(m_externalPath.empty() ? internalPath() : m_externalPath);
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());
m_loginPage->processEnvironment(); m_loginPage->processEnvironment();
handlePathChange(m_externalPath.empty() ? internalPath() : m_externalPath);
internalPathChanged().connect(this, &Application::handlePathChange);
} }
Application::~Application() { Application::~Application() {
@ -75,16 +76,36 @@ void Application::authEvent() {
const Wt::Auth::User &u = m_session->login().user(); const Wt::Auth::User &u = m_session->login().user();
LOG(info) << "User " << u.id() << " (" << u.identity(Wt::Auth::Identity::LoginName) << ")" LOG(info) << "User " << u.id() << " (" << u.identity(Wt::Auth::Identity::LoginName) << ")"
<< " logged in."; << " logged in.";
if (m_loginPage) {
m_loginPageRef = m_navigationBar->addLoginItem(std::move(m_loginPage));
} else if (m_loginPageRef != nullptr && m_loginPageRef->parent() == m_root) {
m_loginPage = m_loginPageRef->parent()->removeWidget(m_loginPageRef);
m_loginPageRef = m_navigationBar->addLoginItem(std::move(m_loginPage));
}
setInternalPath("/", true);
} else { } else {
m_loginPage = m_navigationBar->removeLoginItem();
LOG(info) << "User logged out."; LOG(info) << "User logged out.";
} }
} }
void Application::handlePathChange(const std::string &path) { void Application::handlePathChange(const std::string &path) {
LOG(info) << "handlePathChange: " << path; LOG(info) << "handlePathChange: " << path;
if (path.starts_with("/wt/login") || path.starts_with("/wt/register")) { if (path.starts_with("/wt/login")) {
if (m_session->login().loggedIn()) {
std::exit(0);
} else {
if (m_loginPage) {
m_root->clear(); m_root->clear();
// m_loginPageRef = m_root->addWidget(std::move(m_loginPage)); m_loginPageRef = m_root->addWidget(std::move(m_loginPage));
}
}
} else {
if (m_loginPageRef != nullptr && m_loginPageRef->parent() == m_root) {
m_loginPage = m_root->removeWidget(m_loginPageRef);
}
if (path.starts_with("/wt/register")) {
} 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);
@ -92,6 +113,7 @@ void Application::handlePathChange(const std::string &path) {
m_root->clear(); m_root->clear();
m_root->addNew<HomePage>(); m_root->addNew<HomePage>();
} }
}
} }
Server::Server(uint16_t port, const std::string &applicationRoot, const std::string &documentRoot) { Server::Server(uint16_t port, const std::string &applicationRoot, const std::string &documentRoot) {

View File

@ -1,4 +1,7 @@
#include "NavigationBar.h" #include "NavigationBar.h"
#include "LoginPage.h"
#include <Wt/WApplication.h>
#include <Wt/WPushButton.h>
#include <format> #include <format>
constexpr auto Burger = R"( constexpr auto Burger = R"(
@ -8,19 +11,17 @@ constexpr auto Burger = R"(
<span aria-hidden="true"></span> <span aria-hidden="true"></span>
)"; )";
constexpr auto Login = R"(
${register-button class="bulma-button bulma-is-primary"}
${login-button class="bulma-button bulma-is-light"}
)";
constexpr auto Menu = R"( constexpr auto Menu = R"(
<div class="bulma-navbar-start"> <div class="bulma-navbar-start">
<a class="bulma-navbar-item">Home</a> <a class="bulma-navbar-item">Home</a>
</div> </div>
<div class="bulma-navbar-end"> <div class="bulma-navbar-end">
<div class="bulma-navbar-item"> ${login-item}
<div class="bulma-buttons">
<a class="bulma-button bulma-is-primary">
<strong></strong>
</a>
<a class="bulma-button bulma-is-light">登录</a>
</div>
</div>
</div> </div>
)"; )";
@ -46,7 +47,35 @@ NavigationBar::NavigationBar() : Wt::WTemplate(Template) {
setAttributeValue("aria-label", "main navigation"); setAttributeValue("aria-label", "main navigation");
auto navbarBurger = bindWidget("navbar-burger", std::make_unique<Wt::WTemplate>(Burger)); auto navbarBurger = bindWidget("navbar-burger", std::make_unique<Wt::WTemplate>(Burger));
navbarBurger->setHtmlTagName("a"); navbarBurger->setHtmlTagName("a");
auto navbarMenu = bindWidget("navbar-menu", std::make_unique<Wt::WTemplate>(Menu));
navbarBurger->clicked().connect(std::format(JS, navbarBurger->id(), navbarMenu->id())); m_menu = bindWidget("navbar-menu", std::make_unique<Wt::WTemplate>(Menu));
createLoginButtons();
navbarBurger->clicked().connect(std::format(JS, navbarBurger->id(), m_menu->id()));
}
NavigationBar::~NavigationBar() {
}
LoginPage *NavigationBar::addLoginItem(std::unique_ptr<LoginPage> item) {
auto ret = m_menu->bindWidget("login-item", std::move(item));
ret->addStyleClass("bulma-navbar-item");
return ret;
}
std::unique_ptr<LoginPage> NavigationBar::removeLoginItem() {
auto ret = m_menu->removeWidget("login-item");
createLoginButtons();
return std::unique_ptr<LoginPage>(dynamic_cast<LoginPage *>(ret.release()));
}
void NavigationBar::createLoginButtons() {
auto loginItem = m_menu->bindWidget("login-item", std::make_unique<Wt::WTemplate>(Login));
loginItem->addStyleClass("bulma-buttons");
auto loginButton = loginItem->bindWidget("login-button", std::make_unique<Wt::WPushButton>("登录"));
loginButton->clicked().connect([]() { Wt::WApplication::instance()->setInternalPath("/wt/login", true); });
auto registerButton = loginItem->bindWidget("register-button", std::make_unique<Wt::WPushButton>("<strong>注册</strong>"));
registerButton->setTextFormat(Wt::TextFormat::XHTML);
registerButton->clicked().connect([]() { Wt::WApplication::instance()->setInternalPath("/wt/register", true); });
} }

View File

@ -3,9 +3,20 @@
#include <Wt/WTemplate.h> #include <Wt/WTemplate.h>
class NavigationBar: public Wt::WTemplate { class LoginPage;
class NavigationBar : public Wt::WTemplate {
public: public:
NavigationBar(); NavigationBar();
~NavigationBar();
LoginPage *addLoginItem(std::unique_ptr<LoginPage> item);
std::unique_ptr<LoginPage> removeLoginItem();
protected:
void createLoginButtons();
private:
Wt::WTemplate *m_menu = nullptr;
}; };
#endif // __NAVIGATIONBAR_H__ #endif // __NAVIGATIONBAR_H__

View File

@ -218,9 +218,8 @@
</message> </message>
<message id="Wt.Auth.template.logged-in"> <message id="Wt.Auth.template.logged-in">
<div class="Wt-auth-logged-in"> <span class="is-size-5 has-text-justified">${user-name}</span>
<b>${user-name}</b> ${logout} ${logout}
</div>
</message> </message>
<message id="Wt.Auth.template.totp"> <message id="Wt.Auth.template.totp">