DataManager.h.autosave 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #pragma once
  2. #include <QObject>
  3. #include <QObject>
  4. #include <QHash>
  5. #include <QVariant>
  6. #include <QMutex>
  7. #include <QMutexLocker>
  8. #include <QReadWriteLock>
  9. #include <mutex>
  10. #include <QSettings>
  11. #include "json/json.h"
  12. #define READ_INI_STR(key, default_value) (settings.contains(key) ? settings.value(key).toString() : default_value)
  13. #define READ_INI_INT(key, default_value) (settings.contains(key) ? settings.value(key).toInt() : default_value)
  14. enum SignalDataType
  15. {
  16. CourseData = 0,
  17. HttpLogList = 1,
  18. };
  19. typedef struct tag_SaveJsonLog
  20. {
  21. std::string m_jsonRequest; //记录发送的json
  22. std::string m_jsonResponse; //记录返回的json
  23. bool m_bStatus;
  24. std::string m_nAPIID;
  25. bool m_serverData;
  26. tag_SaveJsonLog(std::string strRequest, std::string strResponse, std::string nAPIID, bool bStatus,bool bserverData=false)
  27. {
  28. m_jsonRequest = strRequest;
  29. m_jsonResponse = strResponse;
  30. m_nAPIID = nAPIID;
  31. m_bStatus = bStatus;
  32. m_serverData =bserverData;
  33. }
  34. }LogJson;
  35. class DataManager : public QObject
  36. {
  37. Q_OBJECT
  38. public:
  39. // 获取单例实例
  40. static DataManager& instance();
  41. // 清空所有数据
  42. static void destroyInstance();
  43. //公用操作函数
  44. //utf8与string互相转换
  45. static std::string string_To_UTF8(const std::string& str);
  46. static std::string UTF8_to_string(const std::string& str);
  47. //json格式化
  48. static bool JsonToStyled(const std::string& str, std::string& strRetrun);
  49. static bool JsonToStyled(Json::Value& jValue, std::string& strRetrun);
  50. //HTTP日志
  51. void AddHttpLog(LogJson& pNewLog);
  52. //清空HTTP日志
  53. void ClearHttpLog();
  54. QList<LogJson>* GetLogJson() {
  55. return &m_vectorLogJson;
  56. };
  57. void InitTestFileData();
  58. QString getTestFileData(){return strAllDataFile;};
  59. signals:
  60. // 数据改变信号
  61. void dataChanged(const int key);
  62. private:
  63. explicit DataManager(QObject* parent = nullptr);
  64. ~DataManager() = default;
  65. // 禁止拷贝和赋值
  66. DataManager(const DataManager&) = delete;
  67. DataManager& operator=(const DataManager&) = delete;
  68. static DataManager* g_GlobalSingle;
  69. static std::mutex m_Mutex;
  70. // 数据存储
  71. QList<LogJson> m_vectorLogJson;
  72. // 读写锁,保证线程安全
  73. mutable QReadWriteLock m_lock;
  74. QString strAllDataFile;
  75. };