| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- #include "appevent.h"
- #include "qdir.h"
- #include <QDateTime>
- #include <QFile>
- #include <QJsonDocument>
- #include <QJsonObject>
- static AppEvent *g_instance = nullptr;
- class AppEventPrivate
- {
- public:
- AppEventPrivate()
- : token(QString())
- , accessExpire(0)
- {}
- QJsonObject jsonConfig;
- QString examRoom;
- QString examNumber;
- QString token;
- qint64 accessExpire;
- };
- AppEvent::AppEvent(QObject *parent)
- : QObject{parent}
- , d(new AppEventPrivate)
- {
- d->jsonConfig.insert("serverIP", "Any");
- d->jsonConfig.insert("serverPort", 8080);
- d->jsonConfig.insert("webSocketIP", "Any");
- d->jsonConfig.insert("webSocketPort", 8081);
- d->jsonConfig.insert("answerDir", QDir().absolutePath());
- }
- AppEvent::~AppEvent()
- {
- delete d;
- }
- AppEvent *AppEvent::instance()
- {
- if (!g_instance) {
- g_instance = new AppEvent();
- }
- return g_instance;
- }
- qint64 AppEvent::time()
- {
- QDateTime currentDateTime = QDateTime::currentDateTime();
- qint64 timestamp = currentDateTime.toSecsSinceEpoch();
- timestamp /= 1000; // 转换为10位时间戳
- return timestamp;
- }
- bool AppEvent::isLogin()
- {
- return !d->token.isEmpty();
- }
- void AppEvent::setJwtToken(const QString &token)
- {
- d->token = token;
- }
- QString AppEvent::jwtToken() const
- {
- return d->token;
- }
- bool AppEvent::isRefreshToken() const
- {
- if (time() > d->accessExpire && !d->token.isEmpty()) {
- return true;
- }
- return false;
- }
- void AppEvent::setRefreshTime(qint64 time) const
- {
- d->accessExpire = time;
- }
- void AppEvent::setExam(const QString &examRoom, const QString &examNumber)
- {
- d->examNumber = examNumber;
- d->examRoom = examRoom;
- }
- QString AppEvent::examRoom() const
- {
- return d->examRoom;
- }
- QString AppEvent::examNumber() const
- {
- return d->examNumber;
- }
- bool AppEvent::setConfigValue(const QString &key, const QVariant &value)
- {
- if (d->jsonConfig.contains(key)) {
- d->jsonConfig[key] = value.toJsonValue();
- } else {
- d->jsonConfig.insert(key, value.toJsonValue());
- }
- return true;
- }
- QVariant AppEvent::configValue(const QString &key)
- {
- return d->jsonConfig.value(key).toVariant();
- }
- bool AppEvent::configLoadJson(const QJsonObject &object)
- {
- for (auto it = object.constBegin(); it != object.constEnd(); ++it) {
- const QString key = it.key();
- if (d->jsonConfig.contains(key)) {
- d->jsonConfig[key] = object[key];
- } else {
- d->jsonConfig.insert(key, object[key]);
- }
- }
- return true;
- }
- bool AppEvent::configLoad()
- {
- QFile file("config.json");
- QByteArray data;
- if (file.open(QFile::ReadOnly | QFile::Text)) {
- data = file.readAll();
- file.close();
- }
- QJsonParseError jsonError;
- QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
- if (jsonError.error != QJsonParseError::NoError) {
- return false;
- }
- const QJsonObject object = jsonDoc.object();
- for (auto it = object.constBegin(); it != object.constEnd(); ++it) {
- const QString key = it.key();
- if (d->jsonConfig.contains(key)) {
- d->jsonConfig[key] = object[key];
- } else {
- d->jsonConfig.insert(key, object[key]);
- }
- }
- return true;
- }
- bool AppEvent::configSave()
- {
- QJsonDocument jsonDoc(d->jsonConfig);
- QString jsonString = jsonDoc.toJson(QJsonDocument::Compact);
- QFile file("config.json");
- QByteArray data;
- if (file.open(QFile::WriteOnly | QFile::Text)) {
- file.write(jsonString.toUtf8());
- file.close();
- }
- return true;
- }
- QJsonObject AppEvent::config() const
- {
- return d->jsonConfig;
- }
- QStringList AppEvent::configFilter() const
- {
- return {"serverIP",
- "serverPort",
- "webSocketIP",
- "webSocketPort",
- "savedUsername",
- "savedPassword"};
- }
- void AppEvent::loginUser(qint64 id)
- {
- emit loginUserSignal(id);
- }
- void AppEvent::loginOutUser(qint64 id)
- {
- emit loginOutUserSignal(id);
- }
- void AppEvent::examsTestUpdate()
- {
- emit examsTestUpdateSignal();
- }
- void AppEvent::webSocketUpdata()
- {
- emit webSocketUpdataSignal();
- }
|