HttpResultMsg.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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-09 14:46:09
  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. if (end != doc.FindMember("attr1") && doc["attr1"].IsString()) {
  109. attr1 = doc["attr1"].GetString();
  110. }
  111. return true;
  112. }
  113. /**
  114. * @description: objectToJson
  115. * @param {string&} str
  116. * @return {*}
  117. */
  118. void objectToJson(std::string& str)
  119. {
  120. rapidjson::StringBuffer strBuf;
  121. rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf);
  122. writer.StartObject();
  123. writer.Key("code");
  124. writer.String(code.c_str());
  125. writer.Key("status");
  126. writer.String(status.c_str());
  127. writer.Key("msg");
  128. writer.String(msg.c_str());
  129. writer.Key("datas");
  130. writer.StartArray();
  131. for(auto iter = datas.begin(); iter != datas.end(); iter++){
  132. iter->ObjectToJson(writer);
  133. }
  134. writer.EndArray();
  135. writer.Key("extendDatas");
  136. writer.StartArray();
  137. for(auto iter = extendDatas.begin(); iter != extendDatas.end(); iter++){
  138. iter->ObjectToJson(writer);
  139. }
  140. writer.EndArray();
  141. writer.Key("attr1");
  142. writer.String(attr1.c_str());
  143. writer.Key("attr2");
  144. writer.String(attr2.c_str());
  145. writer.Key("attr3");
  146. writer.String(attr3.c_str());
  147. writer.Key("notOk");
  148. writer.Bool(notOk);
  149. writer.Key("ok");
  150. writer.Bool(ok);
  151. writer.EndObject();
  152. str = strBuf.GetString();
  153. }
  154. };