lhhttpapi.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #pragma once
  2. #include <QString>
  3. #include <QObject>
  4. #include <QLibrary>
  5. #include <QDateTime>
  6. #include "threadcontroller.h"
  7. #define LH_HTTPAPI_SUCC 0
  8. const int LHHTTPAPI_BUFFER_SIZE = 1024*1024;
  9. // 用New
  10. #define LHAPI_BUFFER_SIZE_NEW 2048000
  11. #define LHAPI_BUFFER_SIZE_ARY 4096
  12. // token 超时
  13. #define TOKENTIME 20*60
  14. extern int g_nHttpTimeOut;
  15. // 操作类型【1查询 2更新 3删除 4插入 5存储过程】
  16. enum enDBOperatorType
  17. {
  18. EDBOT_Select = 1,
  19. EDBOT_Update,
  20. EDBOT_Delete,
  21. EDBOT_Insert,
  22. EDBOT_Procedure,
  23. EDBOT_Batch, /* 单动作批量操作,失败全部回滚 */
  24. EDBOT_BatchTransAction /* 多动作批量操作,失败回滚,请求[{op1}, {op2}, ...],
  25. 回复
  26. {
  27. "code":0,
  28. "data":[{
  29. "key":"1",
  30. "value":{
  31. "code":0,
  32. "result":1
  33. }
  34. },
  35. {
  36. "key":"2",
  37. "value":{
  38. "code":0,
  39. "result":[{
  40. "counT(*)":0
  41. }]
  42. }
  43. }]
  44. }" */
  45. };
  46. /**
  47. * 单个查询语句多条不同的变量,适合批量查询,paraName是变量名
  48. EDBOT_Batch
  49. {
  50. "opName":"TMS_GetExecPlanOneTemplateList",
  51. "paraName":[
  52. 1,
  53. 2,
  54. 3
  55. ]
  56. }
  57. 多个不同语句集合,适合插入数据,Key表示的是这一个语句的唯一值,用于区分插入操作的
  58. EDBOT_BatchTransAction
  59. [
  60. {
  61. "opName":"TMS_InsertExecPlanOneTemplateList",
  62. "Key":"1",
  63. "paramList":
  64. {
  65. "channelID":1,
  66. "templateName":"测试模版1",
  67. "templateType":0
  68. }
  69. },
  70. {
  71. "opName":"TMS_InsertExecPlanOneTemplateList",
  72. "paramList":
  73. {
  74. "channelID":2,
  75. "templateName":"测试模版2",
  76. "templateType":0
  77. }
  78. }
  79. ]
  80. */
  81. class lhhttpapi : public QObject
  82. {
  83. Q_OBJECT
  84. public:
  85. typedef void* (*FunDBInit)(const char* lpUrl,bool bismulti);
  86. typedef int (*FunDBLogin)(const char *plocalip,const char *pSerid, const char* CType, char *pszUserToken, int nSize,bool bismulti, void* phttpip);
  87. typedef int (*FunDBGetServerList)(char *pszList, int nSize,bool bismulti, void* phttpip);
  88. typedef int (*FunDBGetChannelList)(const char* pSerid,char *pszList, int nSize,bool bismulti, void* phttpip);
  89. typedef int (*FunDBDoInterface)(const char* pSerid,const char* CType,const char* strtoken,int nOperatorType, const char * strParamXml, char *strRetXml, int nSize,bool bismulti, void* phttpip);
  90. typedef int64_t (*FunDoGetHttpFileSize)(const char* pHttAddr);
  91. typedef int64_t (*FunDoGetHttpFileContent)(const char* lpHttpAddr, char *pszContent, int nSize);
  92. typedef int (*FunDoUploadFtpFile)(const char* pszLocalFilePath, const char* pszFtpFilePath);
  93. typedef int (*FunDoUploadFtpFileContent)(const char * szFileContent, int nSize, const char* strFtpFilePath);
  94. typedef int (*FunDoCurlDeleteFtpFile)(const char* user, const char* pwd, const char* pszFtpFile);
  95. typedef int (*FunDoCurlUploadFtpFile)(const char* user, const char* pwd, const char* pszLocalFilePath, const char* pszFtpFile);
  96. typedef int (*FunDoCurlUploadFtpFileContent)(const char* user, const char* pwd, const char* pszFileContent, int nSize, const char* pszFtpFile);
  97. // typedef int (*FunUnInit)();
  98. typedef int (*FunDoGetLastError)(char *pError, int nLen, int *nErrorCode);
  99. typedef int (*FunDoRelease)(bool bismulti, void* phttpip);
  100. public:
  101. explicit lhhttpapi(QObject *parent = nullptr);
  102. ~lhhttpapi();
  103. public:
  104. QLibrary *m_pQLib;
  105. FunDoRelease fnDoRelease;
  106. FunDBInit fnDBInit;
  107. FunDBLogin fnDBLogin;
  108. FunDBGetServerList fnDBGettServerList;
  109. FunDBGetChannelList fnDBGetChannelList;
  110. FunDBDoInterface fnDBDoInterface;
  111. FunDoGetHttpFileSize fnDoGetHttpFileSize;
  112. FunDoGetHttpFileContent fnDoGetHttpFileContent;
  113. FunDoUploadFtpFile fnDoUploadFtpFile;
  114. FunDoUploadFtpFileContent fnDoUploadFtpFileContent;
  115. FunDoCurlDeleteFtpFile fnDoCurlDeleteFtpFile;
  116. FunDoCurlUploadFtpFile fnDoCurlUploadFtpFile;
  117. FunDoCurlUploadFtpFileContent fnDoCurlUploadFtpFileContent;
  118. // FunUnInit fnUnInit;
  119. FunDoGetLastError fnGetLastError;
  120. QMutex m_mutexWorkerDestroy;
  121. QWaitCondition m_Condition;
  122. public:
  123. bool Load(QString file);
  124. bool Load();
  125. bool UnLoad();
  126. int DoRelease(bool bismulti = false, void* phttpip=nullptr);
  127. void* DBInit (const char *lpUrl,bool bismulti = false);
  128. int DBLogin(const QString& pLocalip,const QString &pSerid,const QString &appType, QString &pszUserToken,bool bismulti = false, void* phttpip=nullptr);
  129. int DoGetToken(QString &szToken,bool bismulti = false, void* phttpip=nullptr);
  130. // void DoGetTokenAgain();
  131. int DBGetServerList(char *pszList, int nSize,bool bismulti = false, void* phttpip=nullptr);
  132. int DBGetChannelList(const char* pSerid,char *pszList, int nSize,bool bismulti = false, void* phttpip=nullptr);
  133. // int DBDoInterface(int nOperatorType, const QString &strParamXml, QString &strRetXml, bool wait = false, bool bismulti = false, void* phttpip=nullptr);
  134. int DBDoInterface(int nOperatorType, const QString &strParamXml, QString &strRetXml, bool bismulti = false);
  135. int DoGetLastError(char *pError, int nLen, int *nErrorCode);
  136. QString DoGetLastError(int *nErrorCode);
  137. int64_t DoGetHttpFileSize(const QString &url);
  138. int64_t DoGetHttpFileContent(const QString &url, char *pszContent, int nSize);
  139. // int64_t DoGetHttpPNGFile(const QString &url, QPixmap &outPixmap);
  140. bool DoUploadFtpFile(const QString &localFilePath, const QString &ftpFilePath);
  141. bool DoUploadFtpFileContent(const char * szFileContent, int nSize, const QString &ftpFilePath);
  142. bool DoCurlDeleteFtpFile(const QString &targetFilePath);
  143. bool DoCurlDeleteFtpFile(const QString &user, const QString &pwd, const QString &targetFilePath);
  144. bool DoCurlUploadFtpFile(const QString &localFilePath, const QString &destFilePath);
  145. bool DoCurlUploadFtpFile(const QString &user, const QString &pwd, const QString &localFilePath, const QString &destFilePath);
  146. bool DoCurlUploadFtpFileContent(const char * szFileContent, int nSize, const QString &destFilePath);
  147. bool DoCurlUploadFtpFileContent(const QString &user, const QString &pwd, const char * szFileContent, int nSize, const QString &destFilePath);
  148. private slots:
  149. void OnWorkerFinished();
  150. private:
  151. QString m_Localip; // localip
  152. QString m_Serid; // 当前服务数据库id
  153. QString m_appType; // 设备类型
  154. QString m_strtoken;// token
  155. QDateTime m_lasttimer; // 最后一个获取token的时间
  156. void* m_phttpip = nullptr;
  157. };
  158. class DoInterfaceObject : public ThreadWorker
  159. {
  160. Q_OBJECT
  161. public:
  162. explicit DoInterfaceObject(lhhttpapi *pApi, const QString &strToken, int nOperatorType, const QString &strParamXml,bool bismulti = false,void* phttpip=nullptr);
  163. virtual ~DoInterfaceObject() override {}
  164. QString GetRetXML() const {return m_strRetXml;}
  165. int GetResult() const {return m_nResult;}
  166. public slots:
  167. virtual void DoInit() override {}
  168. virtual void DoWork() override;
  169. signals:
  170. void sig_WorkFinished();
  171. public:
  172. lhhttpapi *m_pApi;
  173. QString m_strToken;
  174. int m_nOperatorType;
  175. QString m_strParamXml;
  176. QString m_strRetXml;
  177. int m_nResult;
  178. bool m_bismulti;
  179. void* m_phttpip;
  180. };