HttpClient.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * @Description:
  3. * @Version: 1.0
  4. * @Autor: lishengyin
  5. * @Date: 2022-02-23 09:32:08
  6. * @LastEditors: lishengyin
  7. * @LastEditTime: 2022-07-07 08:48:08
  8. */
  9. #include "HttpClient.h"
  10. namespace gsd{
  11. std::shared_ptr<httplib::Client> m_httpClient = nullptr;
  12. /**
  13. * @description: 创建对象
  14. * @param {*}
  15. * @return {*}
  16. */
  17. std::shared_ptr<HttpClient> HttpClient::CreateNew(){
  18. static shared_ptr<HttpClient> httpClient = nullptr;
  19. if(httpClient == nullptr) httpClient = std::make_shared<HttpClient>();
  20. return httpClient;
  21. }
  22. /**
  23. * @description: 初始化
  24. * @param {*}
  25. * @return {*}
  26. */
  27. int32_t HttpClient::Init(std::string account,std::string url, int port){
  28. this->account = account;
  29. m_httpClient = std::make_shared<httplib::Client>(url, port);
  30. if(m_httpClient == NULL){
  31. ErrorL << "HttpClient initialization failed";
  32. return ERR;
  33. }
  34. if(this->LoginNetty() != OK){
  35. ErrorL << "Failed to log in to netty";
  36. return ERR;
  37. }
  38. return OK;
  39. }
  40. /**
  41. * @description: 登录Netty获取token
  42. * @param {*}
  43. * @return {*}
  44. */
  45. int32_t HttpClient::LoginNetty(){
  46. if(m_httpClient == nullptr) return ERR;
  47. // 获取时间
  48. char ctime[80];
  49. getDataTime(ctime);
  50. string reqTime = ctime;
  51. std::string reqSign = md5(this->privateKey + this->account + this->pwd + reqTime);
  52. httplib::Params params;
  53. params.emplace("account", this->account);
  54. params.emplace("reqSign", reqSign);
  55. params.emplace("reqTime", reqTime);
  56. if (auto res = m_httpClient->Post("/BMP-WEB/user/pass/loginByNetty",params)){
  57. if (res->status == 200) {
  58. NettyHttpResultMsg<NettyHttpNull,NettyHttpToken> nettyHttpResultMsg;
  59. // 解析数据
  60. if(!nettyHttpResultMsg.jsonToObject(res->body)){
  61. return ERR;
  62. }
  63. // 获取token
  64. if(nettyHttpResultMsg.extendDatas.size()){
  65. this->authToken = nettyHttpResultMsg.extendDatas[0].authToken;
  66. this->refreshToken = nettyHttpResultMsg.extendDatas[0].refreshToken;
  67. return OK;
  68. }
  69. return ERR;
  70. }
  71. return ERR;
  72. }else{
  73. auto err = res.error();
  74. ErrorL << "The service is not available," << err << endl;
  75. return ERR;
  76. }
  77. return OK;
  78. }
  79. /**
  80. * @description: 获取时间
  81. * @param {char} *ctime
  82. * @return {*}
  83. */
  84. void HttpClient::getDataTime(char *ctime)
  85. {
  86. time_t rawtime;
  87. struct tm *info;
  88. time(&rawtime);
  89. info = localtime(&rawtime);
  90. strftime(ctime, 80, "%Y-%m-%d %H:%M:%S", info);
  91. }
  92. /**
  93. * @description: 判断是否获取到Token
  94. * @param {*}
  95. * @return {*}
  96. */
  97. bool HttpClient::alive(){
  98. return this->authToken == "" ? false : true;
  99. }
  100. /**
  101. * @description: 获取驱鸟设备信息
  102. * @param {*}
  103. * @return {*}
  104. */
  105. int32_t HttpClient::getExpelInfo(std::string simCode, NettyHttpResultMsg<NettyExpelDevInfo,NettyHttpNull>& nettyHttpResultMsg){
  106. if(m_httpClient == nullptr) return ERR;
  107. httplib::Headers headers = {
  108. { "authToken", this->authToken},
  109. { "authUid", this->account},
  110. };
  111. httplib::Params params;
  112. params.emplace("simCode", simCode);
  113. if (auto res = m_httpClient->Post("/BMP-WEB/device/findBySimCode",headers, params)){
  114. if (res->status == 200) {
  115. if(nettyHttpResultMsg.jsonToObject(res->body)){
  116. return OK;
  117. }
  118. return ERR;
  119. }
  120. return ERR;
  121. }else{
  122. auto err = res.error();
  123. ErrorL << "The service is not available," << err << endl;
  124. return ERR;
  125. }
  126. return OK;
  127. }
  128. /**
  129. * @description: 获取数字字典
  130. * @param {*}
  131. * @return {*}
  132. */
  133. int32_t HttpClient::getHttpDicCode(std::string dictCode,NettyHttpResultMsg<NettyHttpDicCode,NettyHttpNull>& nettyHttpResultMsg){
  134. if(m_httpClient == nullptr) return ERR;
  135. httplib::Headers headers = {
  136. { "authToken", this->authToken},
  137. { "authUid", this->account},
  138. };
  139. httplib::Params params;
  140. params.emplace("appId", "1");
  141. params.emplace("dictCode", dictCode);
  142. params.emplace("dictType", "2");
  143. if (auto res = m_httpClient->Post("/BMP-WEB/dict/selectByDictCodeAndDictType",headers, params)){
  144. if (res->status == 200) {
  145. if(nettyHttpResultMsg.jsonToObject(res->body)){
  146. return OK;
  147. }
  148. }
  149. ErrorL << res->body << endl;
  150. return ERR;
  151. }else{
  152. auto err = res.error();
  153. ErrorL << "The service is not available," << err << endl;
  154. return ERR;
  155. }
  156. return OK;
  157. }
  158. };