appevent.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include "appevent.h"
  2. #include "qdir.h"
  3. #include <QDateTime>
  4. #include <QFile>
  5. #include <QJsonDocument>
  6. #include <QJsonObject>
  7. static AppEvent *g_instance = nullptr;
  8. class AppEventPrivate
  9. {
  10. public:
  11. AppEventPrivate()
  12. : token(QString())
  13. , accessExpire(0)
  14. {}
  15. QJsonObject jsonConfig;
  16. QString examRoom;
  17. QString examNumber;
  18. QString token;
  19. qint64 accessExpire;
  20. };
  21. AppEvent::AppEvent(QObject *parent)
  22. : QObject{parent}
  23. , d(new AppEventPrivate)
  24. {
  25. d->jsonConfig.insert("serverIP", "Any");
  26. d->jsonConfig.insert("serverPort", 8080);
  27. d->jsonConfig.insert("webSocketIP", "Any");
  28. d->jsonConfig.insert("webSocketPort", 8081);
  29. d->jsonConfig.insert("answerDir", QDir().absolutePath());
  30. }
  31. AppEvent::~AppEvent()
  32. {
  33. delete d;
  34. }
  35. AppEvent *AppEvent::instance()
  36. {
  37. if (!g_instance) {
  38. g_instance = new AppEvent();
  39. }
  40. return g_instance;
  41. }
  42. qint64 AppEvent::time()
  43. {
  44. QDateTime currentDateTime = QDateTime::currentDateTime();
  45. qint64 timestamp = currentDateTime.toSecsSinceEpoch();
  46. timestamp /= 1000; // 转换为10位时间戳
  47. return timestamp;
  48. }
  49. bool AppEvent::isLogin()
  50. {
  51. return !d->token.isEmpty();
  52. }
  53. void AppEvent::setJwtToken(const QString &token)
  54. {
  55. d->token = token;
  56. }
  57. QString AppEvent::jwtToken() const
  58. {
  59. return d->token;
  60. }
  61. bool AppEvent::isRefreshToken() const
  62. {
  63. if (time() > d->accessExpire && !d->token.isEmpty()) {
  64. return true;
  65. }
  66. return false;
  67. }
  68. void AppEvent::setRefreshTime(qint64 time) const
  69. {
  70. d->accessExpire = time;
  71. }
  72. void AppEvent::setExam(const QString &examRoom, const QString &examNumber)
  73. {
  74. d->examNumber = examNumber;
  75. d->examRoom = examRoom;
  76. }
  77. QString AppEvent::examRoom() const
  78. {
  79. return d->examRoom;
  80. }
  81. QString AppEvent::examNumber() const
  82. {
  83. return d->examNumber;
  84. }
  85. bool AppEvent::setConfigValue(const QString &key, const QVariant &value)
  86. {
  87. if (d->jsonConfig.contains(key)) {
  88. d->jsonConfig[key] = value.toJsonValue();
  89. } else {
  90. d->jsonConfig.insert(key, value.toJsonValue());
  91. }
  92. return true;
  93. }
  94. QVariant AppEvent::configValue(const QString &key)
  95. {
  96. return d->jsonConfig.value(key).toVariant();
  97. }
  98. bool AppEvent::configLoadJson(const QJsonObject &object)
  99. {
  100. for (auto it = object.constBegin(); it != object.constEnd(); ++it) {
  101. const QString key = it.key();
  102. if (d->jsonConfig.contains(key)) {
  103. d->jsonConfig[key] = object[key];
  104. } else {
  105. d->jsonConfig.insert(key, object[key]);
  106. }
  107. }
  108. return true;
  109. }
  110. bool AppEvent::configLoad()
  111. {
  112. QFile file("config.json");
  113. QByteArray data;
  114. if (file.open(QFile::ReadOnly | QFile::Text)) {
  115. data = file.readAll();
  116. file.close();
  117. }
  118. QJsonParseError jsonError;
  119. QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
  120. if (jsonError.error != QJsonParseError::NoError) {
  121. return false;
  122. }
  123. const QJsonObject object = jsonDoc.object();
  124. for (auto it = object.constBegin(); it != object.constEnd(); ++it) {
  125. const QString key = it.key();
  126. if (d->jsonConfig.contains(key)) {
  127. d->jsonConfig[key] = object[key];
  128. } else {
  129. d->jsonConfig.insert(key, object[key]);
  130. }
  131. }
  132. return true;
  133. }
  134. bool AppEvent::configSave()
  135. {
  136. QJsonDocument jsonDoc(d->jsonConfig);
  137. QString jsonString = jsonDoc.toJson(QJsonDocument::Compact);
  138. QFile file("config.json");
  139. QByteArray data;
  140. if (file.open(QFile::WriteOnly | QFile::Text)) {
  141. file.write(jsonString.toUtf8());
  142. file.close();
  143. }
  144. return true;
  145. }
  146. QJsonObject AppEvent::config() const
  147. {
  148. return d->jsonConfig;
  149. }
  150. QStringList AppEvent::configFilter() const
  151. {
  152. return {"serverIP",
  153. "serverPort",
  154. "webSocketIP",
  155. "webSocketPort",
  156. "savedUsername",
  157. "savedPassword"};
  158. }
  159. void AppEvent::loginUser(qint64 id)
  160. {
  161. emit loginUserSignal(id);
  162. }
  163. void AppEvent::loginOutUser(qint64 id)
  164. {
  165. emit loginOutUserSignal(id);
  166. }
  167. void AppEvent::examsTestUpdate()
  168. {
  169. emit examsTestUpdateSignal();
  170. }
  171. void AppEvent::webSocketUpdata()
  172. {
  173. emit webSocketUpdataSignal();
  174. }