HttpResultMsg.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * @Description:
  3. * @Version: 1.0
  4. * @Autor: lishengyin
  5. * @Date: 2021-11-03 15:16:14
  6. * @LastEditors: lishengyin
  7. * @LastEditTime: 2022-06-22 11:35:11
  8. */
  9. #pragma once
  10. #include <iostream>
  11. #include <rapidjson/document.h>
  12. #include <rapidjson/rapidjson.h>
  13. #include <rapidjson/stringbuffer.h>
  14. #include <rapidjson/writer.h>
  15. #include <vector>
  16. #include "Util/logger.h"
  17. #include "Util/NoticeCenter.h"
  18. #include "Poller/EventPoller.h"
  19. #include "Util/SqlPool.h"
  20. #include "Network/TcpClient.h"
  21. #include "Poller/Timer.h"
  22. using namespace std;
  23. template <class T1, class T2>
  24. class HttpResultMsg
  25. {
  26. public:
  27. std::string code;
  28. std::string status;
  29. std::string msg;
  30. vector<T1> datas;
  31. vector<T2> extendDatas;
  32. std::string attr1;
  33. std::string attr2;
  34. std::string attr3;
  35. bool notOk;
  36. bool ok;
  37. public:
  38. HttpResultMsg(){}
  39. ~HttpResultMsg(){}
  40. /**
  41. * @description: json转码为对象
  42. * @param {const std::string& json} json数据
  43. * @return {*}
  44. */
  45. bool jsonToObject(const std::string& json)
  46. {
  47. rapidjson::Document doc;
  48. if (doc.Parse<rapidjson::kParseCommentsFlag>(json.c_str()).HasParseError()) {
  49. return false;
  50. }
  51. // get members
  52. const auto end = doc.MemberEnd();
  53. if (end == doc.FindMember("code") || !doc["code"].IsString()) {
  54. return false;
  55. } else {
  56. code = doc["code"].GetString();
  57. }
  58. if (end == doc.FindMember("notOk") || !doc["notOk"].IsBool()) {
  59. return false;
  60. } else {
  61. notOk = doc["notOk"].GetBool();
  62. }
  63. if (end == doc.FindMember("ok") || !doc["ok"].IsBool()) {
  64. return false;
  65. } else {
  66. ok = doc["ok"].GetBool();
  67. }
  68. if (end != doc.FindMember("data")) {
  69. if(doc["data"].IsObject()){
  70. T1 data;
  71. const rapidjson::Value& object = doc["data"];
  72. if(!(data.jsonToObject(object))){
  73. return false;
  74. }
  75. datas.push_back(data);
  76. }else if(doc["data"].IsArray()){
  77. const rapidjson::Value& objs = doc["data"];
  78. for (size_t i = 0; i < objs.Size(); i++) {
  79. const rapidjson::Value& obj = objs[i];
  80. T1 data;
  81. if(!(data.jsonToObject(obj))){
  82. return false;
  83. }
  84. datas.push_back(data);
  85. }
  86. }
  87. }
  88. if (end != doc.FindMember("extendData")) {
  89. if(doc["extendData"].IsObject()){
  90. T2 extendData;
  91. const rapidjson::Value& object = doc["extendData"];
  92. if(!(extendData.jsonToObject(object))){
  93. return false;
  94. }
  95. extendDatas.push_back(extendData);
  96. }else if(doc["extendData"].IsArray()){
  97. const rapidjson::Value& objs = doc["extendData"];
  98. for (size_t i = 0; i < objs.Size(); ++i) {
  99. const rapidjson::Value& obj = objs[i];
  100. T2 extendData;
  101. if(!(extendData.jsonToObject(obj))){
  102. return false;
  103. }
  104. extendDatas.push_back(extendData);
  105. }
  106. }
  107. }
  108. return true;
  109. }
  110. /**
  111. * @description: objectToJson
  112. * @param {string&} str
  113. * @return {*}
  114. */
  115. void objectToJson(std::string& str)
  116. {
  117. rapidjson::StringBuffer strBuf;
  118. rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf);
  119. writer.StartObject();
  120. writer.Key("code");
  121. writer.String(code.c_str());
  122. writer.Key("status");
  123. writer.String(status.c_str());
  124. writer.Key("msg");
  125. writer.String(msg.c_str());
  126. writer.Key("datas");
  127. writer.StartArray();
  128. for(auto iter = datas.begin(); iter != datas.end(); iter++){
  129. iter->ObjectToJson(writer);
  130. }
  131. writer.EndArray();
  132. writer.Key("extendDatas");
  133. writer.StartArray();
  134. for(auto iter = extendDatas.begin(); iter != extendDatas.end(); iter++){
  135. iter->ObjectToJson(writer);
  136. }
  137. writer.EndArray();
  138. writer.Key("attr1");
  139. writer.String(attr1.c_str());
  140. writer.Key("attr2");
  141. writer.String(attr2.c_str());
  142. writer.Key("attr3");
  143. writer.String(attr3.c_str());
  144. writer.Key("notOk");
  145. writer.Bool(notOk);
  146. writer.Key("ok");
  147. writer.Bool(ok);
  148. writer.EndObject();
  149. str = strBuf.GetString();
  150. }
  151. };