Older/WebApplication/Hello.cpp
luocai a9640bbda5
All checks were successful
Deploy Docker Images / Build dockerfile and Server deploy (push) Successful in 17s
Deploy / Build (push) Successful in 3m11s
remove blog example source.
2024-11-11 19:21:48 +08:00

87 lines
3.0 KiB
C++

#include "Hello.h"
#include "BoostLog.h"
#include "Dialog.h"
#include "Session.h"
#include "WebApplication.h"
#include <Wt/Auth/AuthService.h>
#include <Wt/Auth/AuthWidget.h>
#include <Wt/Auth/Identity.h>
#include <Wt/Auth/PasswordService.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) {
LOG(info) << "app root: " << appRoot();
m_session = std::make_unique<Session>(appRoot() + "auth.db");
m_session->login().changed().connect(this, &Hello::authEvent);
setTitle("Hello world");
setTheme(std::make_shared<Wt::WBootstrap2Theme>());
Wt::WContainerWidget *top = nullptr;
if (!embedded) {
top = root();
} else {
std::unique_ptr<Wt::WContainerWidget> topPtr = std::make_unique<Wt::WContainerWidget>();
top = topPtr.get();
const std::string *div = env.getParameter("div");
if (div) {
setJavaScriptClass(*div);
bindWidget(std::move(topPtr), *div);
} else {
LOG(error) << "Missing: parameter: 'div'";
}
LOG(info) << "url: " << url();
LOG(info) << "relative resources url: " << relativeResourcesUrl();
LOG(info) << "resources url: " << resourcesUrl();
url();
}
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>"));
}
top->addWidget(std::make_unique<Wt::WText>("Your name, please ? "));
m_nameEdit = top->addWidget(std::make_unique<Wt::WLineEdit>());
m_nameEdit->setFocus();
auto b = top->addWidget(std::make_unique<Wt::WPushButton>("点击我!"));
b->setMargin(5, Wt::Side::Left);
top->addWidget(std::make_unique<Wt::WBreak>());
m_greeting = top->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();
auto authWidget = std::make_unique<Wt::Auth::AuthWidget>(app->authService(), m_session->users(), m_session->login());
authWidget->model()->addPasswordAuth(&app->passwordService());
authWidget->setRegistrationEnabled(true);
authWidget->processEnvironment();
top->addWidget(std::move(authWidget));
top->addWidget(std::make_unique<Dialog>());
}
Hello::~Hello() {
}
void Hello::greet() {
m_greeting->setText("Hello there, " + m_nameEdit->text());
}
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.";
}
}