lhhttpapi.cpp 14 KB

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