appevent.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. QJsonObject jsonConfig;
  13. QString examRoom;
  14. QString examNumber;
  15. };
  16. AppEvent::AppEvent(QObject *parent)
  17. : QObject{parent}
  18. , d(new AppEventPrivate)
  19. {
  20. d->jsonConfig.insert("serverIP", "Any");
  21. d->jsonConfig.insert("serverPort", 8080);
  22. d->jsonConfig.insert("webSocketIP", "Any");
  23. d->jsonConfig.insert("webSocketPort", 8081);
  24. d->jsonConfig.insert("answerDir", QDir().absolutePath());
  25. }
  26. AppEvent::~AppEvent()
  27. {
  28. delete d;
  29. }
  30. AppEvent *AppEvent::instance()
  31. {
  32. if (!g_instance) {
  33. g_instance = new AppEvent();
  34. }
  35. return g_instance;
  36. }
  37. qint64 AppEvent::time()
  38. {
  39. QDateTime currentDateTime = QDateTime::currentDateTime();
  40. qint64 timestamp = currentDateTime.toSecsSinceEpoch();
  41. timestamp /= 1000; // 转换为10位时间戳
  42. return timestamp;
  43. }
  44. void AppEvent::setExam(const QString &examRoom, const QString &examNumber)
  45. {
  46. d->examNumber = examNumber;
  47. d->examRoom = examRoom;
  48. }
  49. QString AppEvent::examRoom() const
  50. {
  51. return d->examRoom;
  52. }
  53. QString AppEvent::examNumber() const
  54. {
  55. return d->examNumber;
  56. }
  57. bool AppEvent::setConfigValue(const QString &key, const QVariant &value)
  58. {
  59. if (d->jsonConfig.contains(key)) {
  60. d->jsonConfig[key] = value.toJsonValue();
  61. } else {
  62. d->jsonConfig.insert(key, value.toJsonValue());
  63. }
  64. return true;
  65. }
  66. QVariant AppEvent::configValue(const QString &key)
  67. {
  68. return d->jsonConfig.value(key).toVariant();
  69. }
  70. bool AppEvent::configLoadJson(const QJsonObject &object)
  71. {
  72. for (auto it = object.constBegin(); it != object.constEnd(); ++it) {
  73. const QString key = it.key();
  74. if (d->jsonConfig.contains(key)) {
  75. d->jsonConfig[key] = object[key];
  76. } else {
  77. d->jsonConfig.insert(key, object[key]);
  78. }
  79. }
  80. return true;
  81. }
  82. bool AppEvent::configLoad()
  83. {
  84. QFile file("config.json");
  85. QByteArray data;
  86. if (file.open(QFile::ReadOnly | QFile::Text)) {
  87. data = file.readAll();
  88. file.close();
  89. }
  90. QJsonParseError jsonError;
  91. QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
  92. if (jsonError.error != QJsonParseError::NoError) {
  93. return false;
  94. }
  95. const QJsonObject object = jsonDoc.object();
  96. for (auto it = object.constBegin(); it != object.constEnd(); ++it) {
  97. const QString key = it.key();
  98. if (d->jsonConfig.contains(key)) {
  99. d->jsonConfig[key] = object[key];
  100. } else {
  101. d->jsonConfig.insert(key, object[key]);
  102. }
  103. }
  104. return true;
  105. }
  106. bool AppEvent::configSave()
  107. {
  108. QJsonDocument jsonDoc(d->jsonConfig);
  109. QString jsonString = jsonDoc.toJson(QJsonDocument::Compact);
  110. QFile file("config.json");
  111. QByteArray data;
  112. if (file.open(QFile::WriteOnly | QFile::Text)) {
  113. file.write(jsonString.toUtf8());
  114. file.close();
  115. }
  116. return true;
  117. }
  118. QJsonObject AppEvent::config() const
  119. {
  120. return d->jsonConfig;
  121. }
  122. void AppEvent::loginUser(qint64 id)
  123. {
  124. emit loginUserSignal(id);
  125. }
  126. void AppEvent::loginOutUser(qint64 id)
  127. {
  128. emit loginOutUserSignal(id);
  129. }
  130. void AppEvent::examsTestUpdate()
  131. {
  132. emit examsTestUpdateSignal();
  133. }
  134. void AppEvent::webSocketUpdata()
  135. {
  136. emit webSocketUpdataSignal();
  137. }