HttpResultMsg.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * @Description:
  3. * @Version: 1.0
  4. * @Autor: lishengyin
  5. * @Date: 2021-11-03 15:16:14
  6. * @LastEditors: lishengyin
  7. * @LastEditTime: 2022-09-28 14:20:07
  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. doc.Parse(json.c_str());
  49. if(!doc.IsObject()){
  50. return false;
  51. }
  52. // get members
  53. const auto end = doc.MemberEnd();
  54. if (end == doc.FindMember("code") || !doc["code"].IsString()) {
  55. return false;
  56. } else {
  57. code = doc["code"].GetString();
  58. }
  59. if (end == doc.FindMember("notOk") || !doc["notOk"].IsBool()) {
  60. return false;
  61. } else {
  62. notOk = doc["notOk"].GetBool();
  63. }
  64. if (end == doc.FindMember("ok") || !doc["ok"].IsBool()) {
  65. return false;
  66. } else {
  67. ok = doc["ok"].GetBool();
  68. }
  69. if (end != doc.FindMember("data")) {
  70. if(doc["data"].IsObject()){
  71. T1 data;
  72. const rapidjson::Value& object = doc["data"];
  73. if(!(data.jsonToObject(object))){
  74. return false;
  75. }
  76. datas.push_back(data);
  77. }else if(doc["data"].IsArray()){
  78. const rapidjson::Value& objs = doc["data"];
  79. for (size_t i = 0; i < objs.Size(); i++) {
  80. const rapidjson::Value& obj = objs[i];
  81. T1 data;
  82. if(!(data.jsonToObject(obj))){
  83. return false;
  84. }
  85. datas.push_back(data);
  86. }
  87. }
  88. }
  89. if (end != doc.FindMember("extendData")) {
  90. if(doc["extendData"].IsObject()){
  91. T2 extendData;
  92. const rapidjson::Value& object = doc["extendData"];
  93. if(!(extendData.jsonToObject(object))){
  94. return false;
  95. }
  96. extendDatas.push_back(extendData);
  97. }else if(doc["extendData"].IsArray()){
  98. const rapidjson::Value& objs = doc["extendData"];
  99. for (size_t i = 0; i < objs.Size(); ++i) {
  100. const rapidjson::Value& obj = objs[i];
  101. T2 extendData;
  102. if(!(extendData.jsonToObject(obj))){
  103. return false;
  104. }
  105. extendDatas.push_back(extendData);
  106. }
  107. }
  108. }
  109. if (end != doc.FindMember("attr1") && doc["attr1"].IsString()) {
  110. attr1 = doc["attr1"].GetString();
  111. }
  112. return true;
  113. }
  114. /**
  115. * @description: objectToJson
  116. * @param {string&} str
  117. * @return {*}
  118. */
  119. void objectToJson(std::string& str)
  120. {
  121. rapidjson::StringBuffer strBuf;
  122. rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf);
  123. writer.StartObject();
  124. writer.Key("code");
  125. writer.String(code.c_str());
  126. writer.Key("status");
  127. writer.String(status.c_str());
  128. writer.Key("msg");
  129. writer.String(msg.c_str());
  130. writer.Key("datas");
  131. writer.StartArray();
  132. for(auto iter = datas.begin(); iter != datas.end(); iter++){
  133. iter->ObjectToJson(writer);
  134. }
  135. writer.EndArray();
  136. writer.Key("extendDatas");
  137. writer.StartArray();
  138. for(auto iter = extendDatas.begin(); iter != extendDatas.end(); iter++){
  139. iter->ObjectToJson(writer);
  140. }
  141. writer.EndArray();
  142. writer.Key("attr1");
  143. writer.String(attr1.c_str());
  144. writer.Key("attr2");
  145. writer.String(attr2.c_str());
  146. writer.Key("attr3");
  147. writer.String(attr3.c_str());
  148. writer.Key("notOk");
  149. writer.Bool(notOk);
  150. writer.Key("ok");
  151. writer.Bool(ok);
  152. writer.EndObject();
  153. str = strBuf.GetString();
  154. }
  155. };