Older/WebApplication/Hello.cpp
luocai 889faa4404
All checks were successful
Deploy / Build (push) Successful in 6m54s
use dbo, not test.
2024-11-27 19:23:06 +08:00

105 lines
3.7 KiB
C++

#include "Hello.h"
#include "BoostLog.h"
#include "Database/Session.h"
#include "Dialog.h"
#include "LoginWidget.h"
#include "WebApplication.h"
#include <Wt/Auth/Identity.h>
#include <Wt/WBootstrap2Theme.h>
#include <Wt/WContainerWidget.h>
#include <Wt/WEnvironment.h>
#include <Wt/WLineEdit.h>
#include <Wt/WPushButton.h>
#include <Wt/WText.h>
Hello::Hello(const Wt::WEnvironment &env, bool embedded) : Wt::WApplication(env) {
messageResourceBundle().use(appRoot() + "wt");
messageResourceBundle().use(appRoot() + "auth_strings");
messageResourceBundle().use(appRoot() + "auth_css_theme");
useStyleSheet("/resources/app.css");
LOG(info) << "app root: " << appRoot();
m_session = Database::session();
m_session->login().changed().connect(this, &Hello::authEvent);
setTitle("Hello world");
if (!embedded) {
setTheme(std::make_shared<Wt::WBootstrap2Theme>());
m_root = root();
} else {
std::unique_ptr<Wt::WContainerWidget> topPtr = std::make_unique<Wt::WContainerWidget>();
m_root = topPtr.get();
const std::string *div = env.getParameter("div");
if (div) {
setJavaScriptClass(*div);
bindWidget(std::move(topPtr), *div);
} else {
LOG(error) << "Missing: parameter: 'div'";
}
auto externalPath = env.getParameter("path");
if (externalPath != nullptr) {
m_externalPath = *externalPath;
LOG(info) << "external path: " << m_externalPath;
} else {
auto parameters = env.getParameterMap();
for (auto &p : parameters) {
LOG(info) << p.first;
}
}
LOG(info) << "url: " << url();
LOG(info) << "relative resources url: " << relativeResourcesUrl();
LOG(info) << "resources url: " << resourcesUrl();
}
if (!embedded) {
root()->addWidget(std::make_unique<Wt::WText>("<p><emph>Note: you can also run this application "
"from within <a href=\"hello.html\">a web page</a>.</emph></p>"));
}
LOG(info) << "internal path: " << internalPath();
m_root->addWidget(std::make_unique<Wt::WText>("Your name, please ? "));
m_nameEdit = m_root->addWidget(std::make_unique<Wt::WLineEdit>());
m_nameEdit->setFocus();
auto b = m_root->addWidget(std::make_unique<Wt::WPushButton>("点击我!"));
b->setMargin(5, Wt::Side::Left);
m_root->addWidget(std::make_unique<Wt::WBreak>());
m_greeting = m_root->addWidget(std::make_unique<Wt::WText>());
b->clicked().connect(this, &Hello::greet);
m_nameEdit->enterPressed().connect(this, &Hello::greet);
auto app = Amass::Singleton<WebApplication>::instance();
m_root->addWidget(std::make_unique<Dialog>());
internalPathChanged().connect(this, &Hello::handlePathChange);
handlePathChange(m_externalPath.empty() ? internalPath() : m_externalPath);
}
Hello::~Hello() {
}
void Hello::greet() {
m_greeting->setText("Hello there, " + m_nameEdit->text());
setInternalPath(m_externalPath);
}
void Hello::authEvent() {
if (m_session->login().loggedIn()) {
const Wt::Auth::User &u = m_session->login().user();
LOG(info) << "User " << u.id() << " (" << u.identity(Wt::Auth::Identity::LoginName) << ")"
<< " logged in.";
} else {
LOG(info) << "User logged out.";
}
}
void Hello::handlePathChange(const std::string &path) {
LOG(info) << "handlePathChange: " << path;
if (path.starts_with("/wt/login") || path.starts_with("/wt/register")) {
m_root->clear();
m_root->setStyleClass("WtCenterContainer");
m_root->addNew<LoginWidget>(m_session->users(), m_session->login());
}
}