lhhttpapi.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. #include "lhhttpapi.h"
  2. #include <QDebug>
  3. #include "LHQLogAPI.h"
  4. #include <QCoreApplication>
  5. // #include "singletonset.h"
  6. //#include <QMessageBox>
  7. lhhttpapi::lhhttpapi(QObject *parent)
  8. : QObject(parent)
  9. , m_pQLib(nullptr)
  10. , fnDBInit(nullptr)
  11. , fnDBLogin(nullptr)
  12. , fnDBGettServerList(nullptr)
  13. , fnDBGetChannelList(nullptr)
  14. , fnDBDoInterface(nullptr)
  15. , fnDoGetHttpFileSize(nullptr)
  16. , fnDoGetHttpFileContent(nullptr)
  17. , fnDoUploadFtpFile(nullptr)
  18. , fnDoUploadFtpFileContent(nullptr)
  19. , fnDoCurlDeleteFtpFile(nullptr)
  20. , fnDoCurlUploadFtpFile(nullptr)
  21. , fnDoCurlUploadFtpFileContent(nullptr)
  22. , fnDoRelease(nullptr)
  23. , fnGetLastError(nullptr)
  24. , m_strtoken("")
  25. {
  26. m_lasttimer = QDateTime::currentDateTime();
  27. }
  28. lhhttpapi::~lhhttpapi()
  29. {
  30. //qDebug()<<"destroy LHMPJmdInfoModuleApi";
  31. //DoUnInit();
  32. UnLoad();
  33. }
  34. bool lhhttpapi::UnLoad()
  35. {
  36. if(m_pQLib == nullptr) return false;
  37. int ret = m_pQLib->unload();
  38. m_pQLib = nullptr;
  39. return ret;
  40. }
  41. bool lhhttpapi::Load(QString file)
  42. {
  43. m_pQLib = new QLibrary(file);
  44. if (!m_pQLib->load())
  45. {
  46. // QMessageBox::critical(nullptr, "警告", QString("模块%1加载失败-1").arg(file));
  47. qDebug() << QString("模块%1加载失败-1").arg(file);
  48. return false;
  49. }
  50. fnDBInit = reinterpret_cast<FunDBInit>(m_pQLib->resolve("DBInitex"));
  51. fnDBLogin = reinterpret_cast<FunDBLogin>(m_pQLib->resolve("DBLogin"));
  52. fnDBGettServerList = reinterpret_cast<FunDBGetServerList>(m_pQLib->resolve("DBGetServerList"));
  53. fnDBGetChannelList = reinterpret_cast<FunDBGetChannelList>(m_pQLib->resolve("DBGetChannelList"));
  54. fnDBDoInterface = reinterpret_cast<FunDBDoInterface>(m_pQLib->resolve("DBDoInterface"));
  55. fnDoGetHttpFileSize = reinterpret_cast<FunDoGetHttpFileSize>(m_pQLib->resolve("DoGetHttpFileSize"));
  56. fnDoGetHttpFileContent = reinterpret_cast<FunDoGetHttpFileContent>(m_pQLib->resolve("DoGetHttpFileContent"));
  57. fnDoUploadFtpFile = reinterpret_cast<FunDoUploadFtpFile>(m_pQLib->resolve("DoUploadFtpFile"));
  58. fnDoUploadFtpFileContent = reinterpret_cast<FunDoUploadFtpFileContent>(m_pQLib->resolve("DoUploadFtpFileContent"));
  59. fnDoCurlDeleteFtpFile = reinterpret_cast<FunDoCurlDeleteFtpFile>(m_pQLib->resolve("DoCurlDeleteFtpFile"));
  60. fnDoCurlUploadFtpFile = reinterpret_cast<FunDoCurlUploadFtpFile>(m_pQLib->resolve("DoCurlUploadFtpFile"));
  61. fnDoCurlUploadFtpFileContent = reinterpret_cast<FunDoCurlUploadFtpFileContent>(m_pQLib->resolve("DoCurlUploadFtpFileContent"));
  62. fnGetLastError = reinterpret_cast<FunDoGetLastError>(m_pQLib->resolve("DBGetLastError"));
  63. fnDoRelease= reinterpret_cast<FunDoRelease>(m_pQLib->resolve("DoRelease"));
  64. if(fnDBInit == nullptr||fnDBLogin == nullptr||fnDBGetChannelList == nullptr||
  65. fnDBDoInterface == nullptr|| fnDBGettServerList == nullptr || fnGetLastError == nullptr
  66. || fnDoGetHttpFileSize == nullptr || fnDoGetHttpFileContent == nullptr
  67. || fnDoUploadFtpFile == nullptr || fnDoUploadFtpFileContent == nullptr
  68. || fnDoCurlDeleteFtpFile == nullptr || fnDoCurlUploadFtpFile == nullptr || fnDoCurlUploadFtpFileContent == nullptr
  69. || fnDoRelease == nullptr)
  70. {
  71. // QMessageBox::critical(nullptr, "警告", QString("模块%1加载失败-2").arg(file));
  72. return false;
  73. }
  74. return true;
  75. }
  76. bool lhhttpapi::Load()
  77. {
  78. #if defined(Q_OS_WIN)
  79. QString libFile = QString("%1/LHSqlWebInterface.dll").arg(QCoreApplication::applicationDirPath());
  80. #elif defined(Q_OS_LINUX)
  81. QString libFile = QString("%1/libLHSqlWebInterface.so").arg(QCoreApplication::applicationDirPath());
  82. #elif defined(Q_OS_MAC)
  83. QString libFile = QString("%1/libLHSqlWebInterface.dylib").arg(QCoreApplication::applicationDirPath());
  84. #endif
  85. return Load(libFile);
  86. }
  87. void* lhhttpapi::DBInit(const char* lpUrl,bool bismulti )
  88. {
  89. if(fnDBInit == nullptr) return nullptr;
  90. return fnDBInit(lpUrl,bismulti);
  91. }
  92. int lhhttpapi::DoRelease(bool bismulti, void *phttpip)
  93. {
  94. if(fnDoRelease == nullptr) return -1;
  95. return fnDoRelease(bismulti,phttpip);
  96. }
  97. int lhhttpapi::DoGetToken(QString &szToken,bool bismulti,void* phttpip)
  98. {
  99. int nret = 0;
  100. if(m_strtoken=="")
  101. {
  102. qDebug() << "in1-DoGetToken";
  103. nret = DBLogin(m_Localip, m_appType, m_appType, szToken,bismulti, phttpip);
  104. qDebug() << "in1-DoGetToken";
  105. m_strtoken = szToken ;
  106. }
  107. else
  108. {
  109. //判断时间
  110. QDateTime tspan = QDateTime::currentDateTime();
  111. qint64 intervalTime = m_lasttimer.secsTo(tspan); //求时间差
  112. if(intervalTime>TOKENTIME)
  113. {
  114. qDebug() << "in2-DoGetToken";
  115. nret = DBLogin(m_Localip,m_Serid,m_appType,szToken);
  116. // nret = DBLogin(m_Localip, SingletonSet::GetServerID(),APPTYPE,szToken);
  117. qDebug() << "out2-DoGetToken";
  118. m_strtoken = szToken ;
  119. m_lasttimer = QDateTime::currentDateTime();
  120. }
  121. }
  122. szToken = m_strtoken;
  123. return nret;
  124. }
  125. int lhhttpapi::DBLogin(const QString& pLocalip,const QString &pSerid, const QString &appType, QString &pszUserToken,bool bismulti, void* phttpip)
  126. {
  127. if(fnDBLogin == nullptr) return -1;
  128. //char buffer[LHHTTPAPI_BUFFER_SIZE] = {0};
  129. char *buffer = new char[LHHTTPAPI_BUFFER_SIZE];
  130. int ret = fnDBLogin(pLocalip.toUtf8().data(),pSerid.toUtf8().data(),appType.toUtf8().data(), buffer, LHHTTPAPI_BUFFER_SIZE - 1,bismulti,phttpip);
  131. pszUserToken = QString::fromUtf8(buffer);
  132. m_strtoken = pszUserToken;
  133. delete [] buffer;
  134. m_Localip = pLocalip;
  135. m_Serid = pSerid;
  136. m_appType = appType;
  137. if(bismulti)
  138. {
  139. m_phttpip = phttpip;
  140. }
  141. return ret;
  142. }
  143. int lhhttpapi::DBGetServerList(char *pszList, int nSize,bool bismulti, void* phttpip)
  144. {
  145. if(fnDBGettServerList == nullptr) return -1;
  146. return fnDBGettServerList(pszList, nSize,bismulti,phttpip);
  147. }
  148. int lhhttpapi::DBGetChannelList(const char* pSerid,char *pszList, int nSize,bool bismulti, void* phttpip)
  149. {
  150. if(fnDBGetChannelList == nullptr) return -1;
  151. return fnDBGetChannelList(pSerid,pszList, nSize,bismulti,phttpip);
  152. }
  153. void lhhttpapi::OnWorkerFinished()
  154. {
  155. //LH_WRITE_COMMON("DoInterfaceObject::DoWork End1");
  156. m_Condition.wakeAll();
  157. // LH_WRITE_COMMON("DoInterfaceObject::DoWork End2");
  158. }
  159. // int lhhttpapi::DBDoInterface( int nOperatorType, const QString &strParamXml, QString &strRetXml, bool wait,bool bismulti, void* phttpip)
  160. // {
  161. // QString strToken="";
  162. // DoGetToken(strToken, bismulti, phttpip);
  163. // if(wait)
  164. // {
  165. // DoInterfaceObject *pWorker = new DoInterfaceObject(this, strToken, nOperatorType, strParamXml,bismulti,phttpip);
  166. // connect(pWorker, &DoInterfaceObject::sig_WorkFinished, this, &lhhttpapi::OnWorkerFinished, Qt::DirectConnection);
  167. // pWorker->Start(100000);
  168. // QMutexLocker locker(&m_mutexWorkerDestroy);
  169. // m_Condition.wait(&m_mutexWorkerDestroy);
  170. // strRetXml = pWorker->GetRetXML();
  171. // int ret = pWorker->GetResult();
  172. // pWorker->BlockStop();
  173. // if(ret != 0)
  174. // {
  175. // int nerror = 0;
  176. // QString strerror = QString("访问DBDoInterface 接口失败: token:%1,optype:%2,param:%3,ret:%4,error:%5")
  177. // .arg(strToken).
  178. // arg(nOperatorType).
  179. // arg(strParamXml).
  180. // arg(strRetXml).
  181. // arg(DoGetLastError(&nerror));
  182. // //if(strerr)
  183. // LH_WRITE_ERROR(strerror);
  184. // if(strerror.contains("票据"))
  185. // {
  186. // LH_WRITE_ERROR("重新获取票据,并再次执行命令");
  187. // QString szToken;
  188. // int nret1 = DBLogin(m_Localip,m_Serid,m_appType,szToken,bismulti,phttpip);
  189. // m_strtoken = szToken;
  190. // DoInterfaceObject *pWorker = new DoInterfaceObject(this, szToken, nOperatorType, strParamXml,bismulti,phttpip);
  191. // connect(pWorker, &DoInterfaceObject::sig_WorkFinished, this, &lhhttpapi::OnWorkerFinished, Qt::DirectConnection);
  192. // pWorker->Start(20000);
  193. // QMutexLocker locker(&m_mutexWorkerDestroy);
  194. // m_Condition.wait(&m_mutexWorkerDestroy);
  195. // strRetXml = pWorker->GetRetXML();
  196. // int ret = pWorker->GetResult();
  197. // pWorker->BlockStop();
  198. // if(ret != 0)
  199. // {
  200. // int nerror = 0;
  201. // QString strerror = QString("再次访问DBDoInterface 接口失败: token:%1,optype:%2,param:%3,ret:%4,error:%5")
  202. // .arg(strToken).
  203. // arg(nOperatorType).
  204. // arg(strParamXml).
  205. // arg(strRetXml).
  206. // arg(DoGetLastError(&nerror));
  207. // LH_WRITE_ERROR(strerror);
  208. // }
  209. // }
  210. // }
  211. // return ret;
  212. // }
  213. // else
  214. // {
  215. // if(fnDBDoInterface == nullptr) return -1;
  216. // QString serid = m_Serid;
  217. // QString appType = m_appType;
  218. // //char buffer[LHHTTPAPI_BUFFER_SIZE] = {0};
  219. // char *buffer = new char[LHHTTPAPI_BUFFER_SIZE]{0};
  220. // // memset(buffer, 0, LHHTTPAPI_BUFFER_SIZE);
  221. // int ret = fnDBDoInterface(serid.toUtf8().data(), appType.toUtf8().data(), strToken.toUtf8().data(), nOperatorType, strParamXml.toUtf8().data(), buffer, LHHTTPAPI_BUFFER_SIZE - 1, bismulti, phttpip);
  222. // strRetXml = QString::fromUtf8(buffer);
  223. // delete [] buffer;
  224. // return ret;
  225. // }
  226. // }
  227. int lhhttpapi::DBDoInterface(int nOperatorType, const QString &strParamXml, QString &strRetXml, bool bismulti)
  228. {
  229. if(fnDBDoInterface == nullptr) return -1;
  230. if(m_phttpip == nullptr) return -1;
  231. int ret = 0;
  232. if(bismulti)
  233. {
  234. QString strToken="";
  235. DoGetToken(strToken, bismulti, m_phttpip);
  236. QString serid = m_Serid;
  237. QString appType = m_appType;
  238. char *buffer = new char[LHHTTPAPI_BUFFER_SIZE]{0};
  239. ret = fnDBDoInterface(serid.toUtf8().data(), appType.toUtf8().data(), strToken.toUtf8().data(), nOperatorType, strParamXml.toUtf8().data(), buffer, LHHTTPAPI_BUFFER_SIZE - 1, bismulti, m_phttpip);
  240. strRetXml = QString::fromUtf8(buffer);
  241. delete [] buffer;
  242. }
  243. else
  244. {
  245. QString strToken="";
  246. DoGetToken(strToken, bismulti, nullptr);
  247. QString serid = m_Serid;
  248. QString appType = m_appType;
  249. char *buffer = new char[LHHTTPAPI_BUFFER_SIZE]{0};
  250. ret = fnDBDoInterface(serid.toUtf8().data(), appType.toUtf8().data(), strToken.toUtf8().data(), nOperatorType, strParamXml.toUtf8().data(), buffer, LHHTTPAPI_BUFFER_SIZE - 1, bismulti, nullptr);
  251. strRetXml = QString::fromUtf8(buffer);
  252. delete [] buffer;
  253. }
  254. return ret;
  255. }
  256. int64_t lhhttpapi::DoGetHttpFileContent(const QString &url, char *pszContent, int nSize)
  257. {
  258. if(fnDoGetHttpFileContent == nullptr) return 0;
  259. return fnDoGetHttpFileContent(url.toUtf8().data(), pszContent, nSize);
  260. }
  261. bool lhhttpapi::DoUploadFtpFile(const QString &localFilePath, const QString &ftpFilePath)
  262. {
  263. if(fnDoUploadFtpFile == nullptr) return -1;
  264. return (fnDoUploadFtpFile(localFilePath.toUtf8(), ftpFilePath.toUtf8()) == 0);
  265. }
  266. bool lhhttpapi::DoUploadFtpFileContent(const char * szFileContent, int nSize, const QString &ftpFilePath)
  267. {
  268. if(fnDoUploadFtpFileContent == nullptr) return -1;
  269. return (fnDoUploadFtpFileContent(szFileContent, nSize, ftpFilePath.toUtf8()) == 0);
  270. }
  271. bool lhhttpapi::DoCurlDeleteFtpFile(const QString &targetFilePath)
  272. {
  273. return DoCurlDeleteFtpFile("","",targetFilePath);
  274. }
  275. bool lhhttpapi::DoCurlDeleteFtpFile(const QString &user, const QString &pwd, const QString &targetFilePath)
  276. {
  277. if(fnDoCurlDeleteFtpFile == nullptr) return -1;
  278. return (fnDoCurlDeleteFtpFile(user.toUtf8(), pwd.toUtf8(), targetFilePath.toUtf8()) == 0);
  279. }
  280. bool lhhttpapi::DoCurlUploadFtpFile(const QString &localFilePath, const QString &destFilePath)
  281. {
  282. return DoCurlUploadFtpFile("", "", localFilePath, destFilePath);
  283. }
  284. bool lhhttpapi::DoCurlUploadFtpFile(const QString &user, const QString &pwd, const QString &localFilePath, const QString &destFilePath)
  285. {
  286. if(fnDoCurlUploadFtpFile == nullptr) return -1;
  287. return (fnDoCurlUploadFtpFile(user.toUtf8(), pwd.toUtf8(), localFilePath.toUtf8(), destFilePath.toUtf8()) == 0);
  288. }
  289. bool lhhttpapi::DoCurlUploadFtpFileContent(const char *szFileContent, int nSize, const QString &destFilePath)
  290. {
  291. return DoCurlUploadFtpFileContent("", "", szFileContent, nSize, destFilePath);
  292. }
  293. bool lhhttpapi::DoCurlUploadFtpFileContent(const QString &user, const QString &pwd, const char *szFileContent, int nSize, const QString &destFilePath)
  294. {
  295. if(fnDoCurlUploadFtpFileContent == nullptr) return -1;
  296. return (fnDoCurlUploadFtpFileContent(user.toUtf8(), pwd.toUtf8(), szFileContent, nSize, destFilePath.toUtf8()) == 0);
  297. }
  298. int64_t lhhttpapi::DoGetHttpFileSize(const QString &url)
  299. {
  300. if(fnDoGetHttpFileSize == nullptr) return 0;
  301. return fnDoGetHttpFileSize(url.toUtf8().data());
  302. }
  303. int lhhttpapi::DoGetLastError(char *pError, int nLen, int *nErrorCode)
  304. {
  305. if(fnGetLastError == nullptr) return -1;
  306. return fnGetLastError(pError, nLen, nErrorCode);
  307. }
  308. QString lhhttpapi::DoGetLastError(int *nErrorCode)
  309. {
  310. if(fnGetLastError == nullptr) return QString();
  311. int nLen = 1024;
  312. char pError[1024] = {0};
  313. if(fnGetLastError(pError, nLen, nErrorCode) != 0) return QString();
  314. return QString::fromLocal8Bit(pError).toLocal8Bit();
  315. }
  316. DoInterfaceObject::DoInterfaceObject(lhhttpapi *pApi, const QString &strToken, int nOperatorType, const QString &strParamXml,bool bismulti,void* phttpip)
  317. : ThreadWorker()
  318. , m_pApi(pApi)
  319. , m_strToken(strToken)
  320. , m_nOperatorType(nOperatorType)
  321. , m_strParamXml(strParamXml)
  322. , m_nResult(-1)
  323. ,m_bismulti(bismulti)
  324. ,m_phttpip(phttpip)
  325. {
  326. }
  327. void DoInterfaceObject::DoWork()
  328. {
  329. //LH_WRITE_COMMON("DoInterfaceObject::DoWork Start");
  330. if(m_pApi != nullptr)
  331. {
  332. //QString serid = singletonWork::GetServerDBID();
  333. // QString serid = SingletonSet::GetServerID();
  334. // QString appType = APPTYPE;
  335. // char *buffer = new char[LHHTTPAPI_BUFFER_SIZE];
  336. // m_nResult = m_pApi->fnDBDoInterface(serid.toUtf8().data(), appType.toUtf8().data(), m_strToken.toUtf8().data(), m_nOperatorType, m_strParamXml.toUtf8().data(), buffer, LHHTTPAPI_BUFFER_SIZE,m_bismulti,m_phttpip);
  337. // m_strRetXml = QString::fromUtf8(buffer);
  338. // delete [] buffer;
  339. }
  340. emit sig_WorkFinished();
  341. }