lhhttpapi.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include "lhhttpapi.h"
  2. lhhttpapi::lhhttpapi(QObject *parent)
  3. : QObject(parent)
  4. , m_pQLib(nullptr)
  5. , fnDBInit(nullptr)
  6. , fnDBLogin(nullptr)
  7. , fnDBGettServerList(nullptr)
  8. , fnDBGetChannelList(nullptr)
  9. , fnDBDoInterface(nullptr)
  10. , fnDoGetHttpFileSize(nullptr)
  11. , fnDoGetHttpFileContent(nullptr)
  12. , fnDoUploadFtpFile(nullptr)
  13. , fnDoUploadFtpFileContent(nullptr)
  14. , fnDoCurlDeleteFtpFile(nullptr)
  15. , fnDoCurlUploadFtpFile(nullptr)
  16. , fnDoCurlUploadFtpFileContent(nullptr)
  17. , fnGetLastError(nullptr)
  18. {
  19. }
  20. lhhttpapi::~lhhttpapi()
  21. {
  22. UnLoad();
  23. }
  24. bool lhhttpapi::UnLoad()
  25. {
  26. if(m_pQLib == nullptr)
  27. {
  28. return false;
  29. }
  30. bool bRet = m_pQLib->unload();
  31. delete m_pQLib;
  32. m_pQLib = nullptr;
  33. return bRet;
  34. }
  35. bool lhhttpapi::Load(QString file)
  36. {
  37. m_pQLib = new QLibrary(file);
  38. if (!m_pQLib->load())
  39. {
  40. qDebug() << QString("模块%1加载失败-1").arg(file);
  41. return false;
  42. }
  43. fnDBInit = reinterpret_cast<FunDBInit>(m_pQLib->resolve("DBInit"));
  44. fnDBLogin = reinterpret_cast<FunDBLogin>(m_pQLib->resolve("DBLogin"));
  45. fnDBGettServerList = reinterpret_cast<FunDBGetServerList>(m_pQLib->resolve("DBGetServerList"));
  46. fnDBGetChannelList = reinterpret_cast<FunDBGetChannelList>(m_pQLib->resolve("DBGetChannelList"));
  47. fnDBDoInterface = reinterpret_cast<FunDBDoInterface>(m_pQLib->resolve("DBDoInterface"));
  48. fnDoGetHttpFileSize = reinterpret_cast<FunDoGetHttpFileSize>(m_pQLib->resolve("DoGetHttpFileSize"));
  49. fnDoGetHttpFileContent = reinterpret_cast<FunDoGetHttpFileContent>(m_pQLib->resolve("DoGetHttpFileContent"));
  50. fnDoUploadFtpFile = reinterpret_cast<FunDoUploadFtpFile>(m_pQLib->resolve("DoUploadFtpFile"));
  51. fnDoUploadFtpFileContent = reinterpret_cast<FunDoUploadFtpFileContent>(m_pQLib->resolve("DoUploadFtpFileContent"));
  52. fnDoCurlDeleteFtpFile = reinterpret_cast<FunDoCurlDeleteFtpFile>(m_pQLib->resolve("DoCurlDeleteFtpFile"));
  53. fnDoCurlUploadFtpFile = reinterpret_cast<FunDoCurlUploadFtpFile>(m_pQLib->resolve("DoCurlUploadFtpFile"));
  54. fnDoCurlUploadFtpFileContent = reinterpret_cast<FunDoCurlUploadFtpFileContent>(m_pQLib->resolve("DoCurlUploadFtpFileContent"));
  55. fnGetLastError = reinterpret_cast<FunDoGetLastError>(m_pQLib->resolve("DBGetLastError"));
  56. if(fnDBInit == nullptr||fnDBLogin == nullptr||fnDBGetChannelList == nullptr||
  57. fnDBDoInterface == nullptr|| fnDBGettServerList == nullptr || fnGetLastError == nullptr
  58. || fnDoGetHttpFileSize == nullptr || fnDoGetHttpFileContent == nullptr
  59. || fnDoUploadFtpFile == nullptr || fnDoUploadFtpFileContent == nullptr
  60. || fnDoCurlDeleteFtpFile == nullptr || fnDoCurlUploadFtpFile == nullptr || fnDoCurlUploadFtpFileContent == nullptr )
  61. {
  62. return false;
  63. }
  64. return true;
  65. }
  66. int lhhttpapi::DBInit(const char* lpUrl)
  67. {
  68. if(fnDBInit == nullptr) return -1;
  69. return fnDBInit(lpUrl);
  70. }
  71. int lhhttpapi::DoGetToken(QString &szToken)
  72. {
  73. return DBLogin(m_Localip,m_Serid,m_appType,szToken);
  74. }
  75. int lhhttpapi::DBLogin(const QString& pLocalip,const QString &pSerid, const QString &appType, QString &pszUserToken)
  76. {
  77. if(fnDBLogin == nullptr) return -1;
  78. //char buffer[LHHTTPAPI_BUFFER_SIZE] = {0};
  79. char *buffer = (char*)malloc(LHHTTPAPI_BUFFER_SIZE);
  80. memset(buffer, 0, LHHTTPAPI_BUFFER_SIZE);
  81. int ret = fnDBLogin(pLocalip.toUtf8().data(),pSerid.toUtf8().data(),appType.toUtf8().data(), buffer, LHHTTPAPI_BUFFER_SIZE);
  82. pszUserToken = QString::fromUtf8(buffer);
  83. free(buffer);
  84. buffer = nullptr;
  85. m_Localip = pLocalip;
  86. m_Serid = pSerid;
  87. m_appType = appType;
  88. return ret;
  89. }
  90. int lhhttpapi::DBGetServerList(char *pszList, int nSize)
  91. {
  92. if(fnDBGettServerList == nullptr) return -1;
  93. return fnDBGettServerList(pszList, nSize);
  94. }
  95. int lhhttpapi::DBGetChannelList(const char* pSerid,char *pszList, int nSize)
  96. {
  97. if(fnDBGetChannelList == nullptr) return -1;
  98. return fnDBGetChannelList(pSerid,pszList, nSize);
  99. }
  100. int lhhttpapi::DBDoInterface(const QString &strToken, int nOperatorType, const QString &strParamXml, QString &strRetXml)
  101. {
  102. if(fnDBDoInterface == nullptr) return -1;
  103. QString serid = m_Serid;
  104. //QString appType = APPTYPE;//singletonWork::skm_type;
  105. QString appType = WEBAPPTYPE;//singletonWork::skm_type;
  106. char *buffer = (char*)malloc(LHHTTPAPI_BUFFER_SIZE);
  107. memset(buffer, 0, LHHTTPAPI_BUFFER_SIZE);
  108. int ret = fnDBDoInterface(serid.toUtf8().data(), appType.toUtf8().data(), strToken.toUtf8().data(), nOperatorType, strParamXml.toUtf8().data(), buffer, LHHTTPAPI_BUFFER_SIZE);
  109. strRetXml = QString::fromUtf8(buffer);
  110. free(buffer);
  111. buffer = nullptr;
  112. return ret;
  113. }
  114. int64_t lhhttpapi::DoGetHttpFileContent(const QString &url, char *pszContent, int nSize)
  115. {
  116. if(fnDoGetHttpFileContent == nullptr) return 0;
  117. return fnDoGetHttpFileContent(url.toUtf8().data(), pszContent, nSize);
  118. }
  119. bool lhhttpapi::DoUploadFtpFile(const QString &localFilePath, const QString &ftpFilePath)
  120. {
  121. if(fnDoUploadFtpFile == nullptr) return -1;
  122. return (fnDoUploadFtpFile(localFilePath.toUtf8(), ftpFilePath.toUtf8()) == 0);
  123. }
  124. bool lhhttpapi::DoUploadFtpFileContent(const char * szFileContent, int nSize, const QString &ftpFilePath)
  125. {
  126. if(fnDoUploadFtpFileContent == nullptr) return -1;
  127. return (fnDoUploadFtpFileContent(szFileContent, nSize, ftpFilePath.toUtf8()) == 0);
  128. }
  129. bool lhhttpapi::DoCurlDeleteFtpFile(const QString &targetFilePath)
  130. {
  131. return DoCurlDeleteFtpFile("","",targetFilePath);
  132. }
  133. bool lhhttpapi::DoCurlDeleteFtpFile(const QString &user, const QString &pwd, const QString &targetFilePath)
  134. {
  135. if(fnDoCurlDeleteFtpFile == nullptr) return -1;
  136. return (fnDoCurlDeleteFtpFile(user.toUtf8(), pwd.toUtf8(), targetFilePath.toUtf8()) == 0);
  137. }
  138. bool lhhttpapi::DoCurlUploadFtpFile(const QString &localFilePath, const QString &destFilePath)
  139. {
  140. return DoCurlUploadFtpFile("", "", localFilePath, destFilePath);
  141. }
  142. bool lhhttpapi::DoCurlUploadFtpFile(const QString &user, const QString &pwd, const QString &localFilePath, const QString &destFilePath)
  143. {
  144. if(fnDoCurlUploadFtpFile == nullptr) return -1;
  145. return (fnDoCurlUploadFtpFile(user.toUtf8(), pwd.toUtf8(), localFilePath.toUtf8(), destFilePath.toUtf8()) == 0);
  146. }
  147. bool lhhttpapi::DoCurlUploadFtpFileContent(const char *szFileContent, int nSize, const QString &destFilePath)
  148. {
  149. return DoCurlUploadFtpFileContent("", "", szFileContent, nSize, destFilePath);
  150. }
  151. bool lhhttpapi::DoCurlUploadFtpFileContent(const QString &user, const QString &pwd, const char *szFileContent, int nSize, const QString &destFilePath)
  152. {
  153. if(fnDoCurlUploadFtpFileContent == nullptr) return -1;
  154. return (fnDoCurlUploadFtpFileContent(user.toUtf8(), pwd.toUtf8(), szFileContent, nSize, destFilePath.toUtf8()) == 0);
  155. }
  156. int64_t lhhttpapi::DoGetHttpFileSize(const QString &url)
  157. {
  158. if(fnDoGetHttpFileSize == nullptr) return 0;
  159. return fnDoGetHttpFileSize(url.toUtf8().data());
  160. }
  161. int lhhttpapi::DoGetLastError(char *pError, int nLen, int *nErrorCode)
  162. {
  163. if(fnGetLastError == nullptr) return -1;
  164. return fnGetLastError(pError, nLen, nErrorCode);
  165. }
  166. QString lhhttpapi::DoGetLastError(int *nErrorCode)
  167. {
  168. if(fnGetLastError == nullptr) return QString();
  169. int nLen = 1024;
  170. char pError[1024] = {0};
  171. if(fnGetLastError(pError, nLen, nErrorCode) != 0) return QString();
  172. return QString::fromLocal8Bit(pError).toLocal8Bit();
  173. }