NettyHttpResultMsg.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * @Description:
  3. * @Version: 1.0
  4. * @Autor: lishengyin
  5. * @Date: 2021-11-03 15:16:14
  6. * @LastEditors: lishengyin
  7. * @LastEditTime: 2022-10-09 09:14:53
  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. #include "Notices.h"
  23. using namespace std;
  24. using namespace toolkit;
  25. template <class T1, class T2>
  26. class NettyHttpResultMsg
  27. {
  28. public:
  29. std::string code;
  30. std::string msg;
  31. vector<T1> datas;
  32. vector<T2> extendDatas;
  33. bool notOk;
  34. bool ok;
  35. public:
  36. NettyHttpResultMsg(){}
  37. ~NettyHttpResultMsg(){}
  38. /**
  39. * @description: json转码为对象
  40. * @param {const std::string& json} json数据
  41. * @return {*}
  42. */
  43. bool jsonToObject(const std::string& json)
  44. {
  45. rapidjson::Document doc;
  46. if (doc.Parse<rapidjson::kParseCommentsFlag>(json.c_str()).HasParseError()) {
  47. return false;
  48. }
  49. // get members
  50. const auto end = doc.MemberEnd();
  51. if (end == doc.FindMember("code") || !doc["code"].IsString()) {
  52. return false;
  53. } else {
  54. code = doc["code"].GetString();
  55. }
  56. if (end == doc.FindMember("notOk") || !doc["notOk"].IsBool()) {
  57. return false;
  58. } else {
  59. notOk = doc["notOk"].GetBool();
  60. }
  61. if (end == doc.FindMember("ok") || !doc["ok"].IsBool()) {
  62. return false;
  63. } else {
  64. ok = doc["ok"].GetBool();
  65. }
  66. if (end != doc.FindMember("data")) {
  67. if(doc["data"].IsObject()){
  68. T1 data;
  69. const rapidjson::Value& object = doc["data"];
  70. if(!(data.jsonToObject(object))){
  71. return false;
  72. }
  73. datas.push_back(data);
  74. }else if(doc["data"].IsArray()){
  75. const rapidjson::Value& objs = doc["data"];
  76. for (size_t i = 0; i < objs.Size(); i++) {
  77. const rapidjson::Value& obj = objs[i];
  78. T1 data;
  79. if(!(data.jsonToObject(obj))){
  80. return false;
  81. }
  82. datas.push_back(data);
  83. }
  84. }
  85. }
  86. if (end != doc.FindMember("extendData")) {
  87. if(doc["extendData"].IsObject()){
  88. T2 extendData;
  89. const rapidjson::Value& object = doc["extendData"];
  90. if(!(extendData.jsonToObject(object))){
  91. return false;
  92. }
  93. extendDatas.push_back(extendData);
  94. }else if(doc["extendData"].IsArray()){
  95. const rapidjson::Value& objs = doc["extendData"];
  96. for (size_t i = 0; i < objs.Size(); ++i) {
  97. const rapidjson::Value& obj = objs[i];
  98. T2 extendData;
  99. if(!(extendData.jsonToObject(obj))){
  100. return false;
  101. }
  102. extendDatas.push_back(extendData);
  103. }
  104. }
  105. }
  106. if (end == doc.FindMember("ok")) {
  107. return false;
  108. } else {
  109. ok = doc["ok"].GetBool();
  110. }
  111. return true;
  112. }
  113. int32_t ClearTable(std::string tableName)
  114. {
  115. vector<vector<string>> sqlRet;
  116. std::string sql = "truncate table MIVA_DB." + tableName;
  117. SqlWriter sqlTruncate(sql.c_str());
  118. sqlTruncate << sqlRet;
  119. return OK;
  120. }
  121. };