NettyHttpResultMsg.h 3.8 KB

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