appevent.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #include "appevent.h"
  2. #include "qdir.h"
  3. #include "qglobal.h"
  4. #include "qjsonobject.h"
  5. #include "qobject.h"
  6. #include <QByteArray>
  7. #include <QCryptographicHash>
  8. #include <QDateTime>
  9. #include <QFile>
  10. #include <QGuiApplication>
  11. #include <QJsonDocument>
  12. #include <QPainter>
  13. #include <QPixmap>
  14. #include <QScreen>
  15. #include "thardware/thardware.h"
  16. static AppEvent *g_instance = nullptr;
  17. static QString computeMD5(const QString &input)
  18. {
  19. // Convert the input string to a QByteArray
  20. QByteArray byteArray = input.toUtf8();
  21. // Compute the MD5 hash using QCryptographicHash
  22. QByteArray hash = QCryptographicHash::hash(byteArray, QCryptographicHash::Md5);
  23. // Convert the QByteArray hash to a hexadecimal string
  24. QString hexHash = hash.toHex();
  25. return hexHash;
  26. }
  27. class AppEventPrivate
  28. {
  29. public:
  30. AppEventPrivate() {}
  31. QString token;
  32. qint64 accessExpire;
  33. QJsonObject jsonConfig;
  34. qint64 maxExamTime;
  35. QString machineCode;
  36. };
  37. AppEvent::AppEvent(QObject *parent)
  38. : QObject{parent}
  39. , d(new AppEventPrivate)
  40. {
  41. const std::string code = TC::THardWare::machineCode();
  42. d->machineCode = computeMD5(QString::fromStdString(code));
  43. // 考题存放路径
  44. d->jsonConfig.insert("serverIP", "127.0.0.1");
  45. d->jsonConfig.insert("serverPort", "8080");
  46. d->jsonConfig.insert("webSocketIP", "127.0.0.1");
  47. d->jsonConfig.insert("webSocketPort", "8081");
  48. }
  49. AppEvent::~AppEvent()
  50. {
  51. delete d;
  52. }
  53. AppEvent *AppEvent::instance()
  54. {
  55. if (!g_instance) {
  56. g_instance = new AppEvent();
  57. }
  58. return g_instance;
  59. }
  60. qint64 AppEvent::time()
  61. {
  62. QDateTime currentDateTime = QDateTime::currentDateTime();
  63. qint64 timestamp = currentDateTime.toSecsSinceEpoch();
  64. timestamp /= 1000; // 转换为10位时间戳
  65. return timestamp;
  66. }
  67. QString AppEvent::formatElapsedTime(qint64 ms)
  68. {
  69. // 构建QTime对象
  70. QTime time(0, 0, 0);
  71. time = time.addMSecs(ms);
  72. // 格式化成时:分:秒:毫秒格式
  73. return time.toString("hh:mm:ss");
  74. }
  75. void AppEvent::captureDesktop(const QString &path)
  76. {
  77. QList<QScreen *> screens = QGuiApplication::screens();
  78. if (screens.isEmpty()) {
  79. qWarning("No screens found.");
  80. return;
  81. }
  82. // 计算整个桌面的大小
  83. QRect fullDesktopRect;
  84. for (QScreen *screen : screens) {
  85. fullDesktopRect = fullDesktopRect.united(screen->geometry());
  86. }
  87. // 创建一个空白的 QPixmap,大小为整个桌面的大小
  88. QPixmap fullDesktop(fullDesktopRect.size());
  89. fullDesktop.fill(Qt::transparent);
  90. // 使用 QPainter 将每个屏幕的内容绘制到 fullDesktop 上
  91. QPainter painter(&fullDesktop);
  92. for (QScreen *screen : screens) {
  93. // 计算当前屏幕在 fullDesktop 中的位置
  94. QPoint screenPos = screen->geometry().topLeft() - fullDesktopRect.topLeft();
  95. painter.drawPixmap(screenPos, screen->grabWindow(0));
  96. }
  97. painter.end();
  98. // 保存截图到文件(可选)
  99. QString fileName = "/desktop_screenshot_"
  100. + QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss") + ".png";
  101. fullDesktop.save(path + fileName, "PNG");
  102. }
  103. void AppEvent::saveTimeFile(qint64 elapsed, const QString &path)
  104. {
  105. QFile file(path + "/time.txt");
  106. if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  107. QTextStream stream(&file);
  108. QString time = formatElapsedTime(elapsed); // Assuming formatElapsedTime is defined somewhere
  109. stream << "time: " << time << Qt::endl;
  110. stream << "elapsed(ns): " << elapsed << Qt::endl;
  111. file.close();
  112. }
  113. }
  114. QString AppEvent::machineCode() const
  115. {
  116. return d->machineCode;
  117. }
  118. bool AppEvent::isLogin()
  119. {
  120. return !d->token.isEmpty();
  121. }
  122. void AppEvent::setJwtToken(const QString &token)
  123. {
  124. d->token = token;
  125. }
  126. QString AppEvent::jwtToken() const
  127. {
  128. return d->token;
  129. }
  130. bool AppEvent::isRefreshToken() const
  131. {
  132. if (time() > d->accessExpire && !d->token.isEmpty()) {
  133. return true;
  134. }
  135. return false;
  136. }
  137. void AppEvent::setRefreshTime(qint64 time) const
  138. {
  139. d->accessExpire = time;
  140. }
  141. void AppEvent::startExam(int time)
  142. {
  143. emit startExamSignals(time);
  144. }
  145. qint64 AppEvent::maxExamTime()
  146. {
  147. return d->maxExamTime;
  148. }
  149. void AppEvent::setMaxExamTime(qint64 time)
  150. {
  151. d->maxExamTime = time;
  152. }
  153. bool AppEvent::setConfigValue(const QString &key, const QVariant &value)
  154. {
  155. if (d->jsonConfig.contains(key)) {
  156. d->jsonConfig[key] = value.toJsonValue();
  157. } else {
  158. d->jsonConfig.insert(key, value.toJsonValue());
  159. }
  160. return true;
  161. }
  162. QVariant AppEvent::configValue(const QString &key)
  163. {
  164. return d->jsonConfig.value(key).toVariant();
  165. }
  166. QVariant AppEvent::configReadValue(const QString &key, bool *isok)
  167. {
  168. QFile file("config.json");
  169. QByteArray data;
  170. if (file.open(QFile::ReadOnly | QFile::Text)) {
  171. data = file.readAll();
  172. file.close();
  173. }
  174. QJsonParseError jsonError;
  175. QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
  176. if (jsonError.error != QJsonParseError::NoError) {
  177. if (isok) {
  178. *isok = false;
  179. }
  180. return QVariant();
  181. }
  182. const QJsonObject object = jsonDoc.object();
  183. for (auto it = object.constBegin(); it != object.constEnd(); ++it) {
  184. if (it.key() == key) {
  185. if (isok) {
  186. *isok = true;
  187. }
  188. return object[key];
  189. }
  190. }
  191. if (isok) {
  192. *isok = false;
  193. }
  194. return QVariant();
  195. }
  196. bool AppEvent::configLoadJson(const QJsonObject &object)
  197. {
  198. for (auto it = object.constBegin(); it != object.constEnd(); ++it) {
  199. const QString key = it.key();
  200. if (d->jsonConfig.contains(key)) {
  201. d->jsonConfig[key] = object[key];
  202. } else {
  203. d->jsonConfig.insert(key, object[key]);
  204. }
  205. }
  206. qDebug() << d->jsonConfig;
  207. return true;
  208. }
  209. bool AppEvent::configLoad()
  210. {
  211. //
  212. QFile file("config.json");
  213. QByteArray data;
  214. if (file.open(QFile::ReadOnly | QFile::Text)) {
  215. data = file.readAll();
  216. file.close();
  217. }
  218. QJsonParseError jsonError;
  219. QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
  220. if (jsonError.error != QJsonParseError::NoError) {
  221. return false;
  222. }
  223. const QJsonObject object = jsonDoc.object();
  224. for (auto it = object.constBegin(); it != object.constEnd(); ++it) {
  225. const QString key = it.key();
  226. if (d->jsonConfig.contains(key)) {
  227. d->jsonConfig[key] = object[key];
  228. } else {
  229. d->jsonConfig.insert(key, object[key]);
  230. }
  231. }
  232. return true;
  233. }
  234. bool AppEvent::configSave()
  235. {
  236. QJsonDocument jsonDoc(d->jsonConfig);
  237. QString jsonString = jsonDoc.toJson(QJsonDocument::Indented);
  238. QFile file("config.json");
  239. QByteArray data;
  240. if (file.open(QFile::WriteOnly | QFile::Text)) {
  241. file.write(jsonString.toUtf8());
  242. file.close();
  243. }
  244. return true;
  245. }
  246. QJsonObject AppEvent::config() const
  247. {
  248. return d->jsonConfig;
  249. }
  250. void AppEvent::serverLink(bool b)
  251. {
  252. emit serverLinkSignals(b);
  253. }
  254. void AppEvent::unLockScreen()
  255. {
  256. emit unLockScreenSignals();
  257. }