lhhttpapi.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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,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. return ret;
  138. }
  139. int lhhttpapi::DBGetServerList(char *pszList, int nSize,bool bismulti, void* phttpip)
  140. {
  141. if(fnDBGettServerList == nullptr) return -1;
  142. return fnDBGettServerList(pszList, nSize,bismulti,phttpip);
  143. }
  144. int lhhttpapi::DBGetChannelList(const char* pSerid,char *pszList, int nSize,bool bismulti, void* phttpip)
  145. {
  146. if(fnDBGetChannelList == nullptr) return -1;
  147. return fnDBGetChannelList(pSerid,pszList, nSize,bismulti,phttpip);
  148. }
  149. void lhhttpapi::OnWorkerFinished()
  150. {
  151. //LH_WRITE_COMMON("DoInterfaceObject::DoWork End1");
  152. m_Condition.wakeAll();
  153. // LH_WRITE_COMMON("DoInterfaceObject::DoWork End2");
  154. }
  155. int lhhttpapi::DBDoInterface( int nOperatorType, const QString &strParamXml, QString &strRetXml, bool wait,bool bismulti, void* phttpip)
  156. {
  157. QString strToken="";
  158. DoGetToken(strToken,bismulti,phttpip);
  159. if(wait)
  160. {
  161. DoInterfaceObject *pWorker = new DoInterfaceObject(this, strToken, nOperatorType, strParamXml,bismulti,phttpip);
  162. connect(pWorker, &DoInterfaceObject::sig_WorkFinished, this, &lhhttpapi::OnWorkerFinished, Qt::DirectConnection);
  163. pWorker->Start(100000);
  164. QMutexLocker locker(&m_mutexWorkerDestroy);
  165. m_Condition.wait(&m_mutexWorkerDestroy);
  166. strRetXml = pWorker->GetRetXML();
  167. int ret = pWorker->GetResult();
  168. pWorker->BlockStop();
  169. if(ret != 0)
  170. {
  171. int nerror = 0;
  172. QString strerror = QString("访问DBDoInterface 接口失败: token:%1,optype:%2,param:%3,ret:%4,error:%5")
  173. .arg(strToken).
  174. arg(nOperatorType).
  175. arg(strParamXml).
  176. arg(strRetXml).
  177. arg(DoGetLastError(&nerror));
  178. //if(strerr)
  179. LH_WRITE_ERROR(strerror);
  180. if(strerror.contains("票据"))
  181. {
  182. LH_WRITE_ERROR("重新获取票据,并再次执行命令");
  183. QString szToken;
  184. int nret1 = DBLogin(m_Localip,m_Serid,m_appType,szToken,bismulti,phttpip);
  185. m_strtoken = szToken;
  186. DoInterfaceObject *pWorker = new DoInterfaceObject(this, szToken, nOperatorType, strParamXml,bismulti,phttpip);
  187. connect(pWorker, &DoInterfaceObject::sig_WorkFinished, this, &lhhttpapi::OnWorkerFinished, Qt::DirectConnection);
  188. pWorker->Start(20000);
  189. QMutexLocker locker(&m_mutexWorkerDestroy);
  190. m_Condition.wait(&m_mutexWorkerDestroy);
  191. strRetXml = pWorker->GetRetXML();
  192. int ret = pWorker->GetResult();
  193. pWorker->BlockStop();
  194. if(ret != 0)
  195. {
  196. int nerror = 0;
  197. QString strerror = QString("再次访问DBDoInterface 接口失败: token:%1,optype:%2,param:%3,ret:%4,error:%5")
  198. .arg(strToken).
  199. arg(nOperatorType).
  200. arg(strParamXml).
  201. arg(strRetXml).
  202. arg(DoGetLastError(&nerror));
  203. LH_WRITE_ERROR(strerror);
  204. }
  205. }
  206. }
  207. return ret;
  208. }
  209. else
  210. {
  211. if(fnDBDoInterface == nullptr) return -1;
  212. QString serid = m_Serid;
  213. QString appType = m_appType;
  214. //char buffer[LHHTTPAPI_BUFFER_SIZE] = {0};
  215. char *buffer = new char[LHHTTPAPI_BUFFER_SIZE]{0};
  216. // memset(buffer, 0, LHHTTPAPI_BUFFER_SIZE);
  217. int ret = fnDBDoInterface(serid.toUtf8().data(), appType.toUtf8().data(), strToken.toUtf8().data(), nOperatorType, strParamXml.toUtf8().data(), buffer, LHHTTPAPI_BUFFER_SIZE - 1,bismulti,phttpip);
  218. strRetXml = QString::fromUtf8(buffer);
  219. delete [] buffer;
  220. return ret;
  221. }
  222. }
  223. int64_t lhhttpapi::DoGetHttpFileContent(const QString &url, char *pszContent, int nSize)
  224. {
  225. if(fnDoGetHttpFileContent == nullptr) return 0;
  226. return fnDoGetHttpFileContent(url.toUtf8().data(), pszContent, nSize);
  227. }
  228. bool lhhttpapi::DoUploadFtpFile(const QString &localFilePath, const QString &ftpFilePath)
  229. {
  230. if(fnDoUploadFtpFile == nullptr) return -1;
  231. return (fnDoUploadFtpFile(localFilePath.toUtf8(), ftpFilePath.toUtf8()) == 0);
  232. }
  233. bool lhhttpapi::DoUploadFtpFileContent(const char * szFileContent, int nSize, const QString &ftpFilePath)
  234. {
  235. if(fnDoUploadFtpFileContent == nullptr) return -1;
  236. return (fnDoUploadFtpFileContent(szFileContent, nSize, ftpFilePath.toUtf8()) == 0);
  237. }
  238. bool lhhttpapi::DoCurlDeleteFtpFile(const QString &targetFilePath)
  239. {
  240. return DoCurlDeleteFtpFile("","",targetFilePath);
  241. }
  242. bool lhhttpapi::DoCurlDeleteFtpFile(const QString &user, const QString &pwd, const QString &targetFilePath)
  243. {
  244. if(fnDoCurlDeleteFtpFile == nullptr) return -1;
  245. return (fnDoCurlDeleteFtpFile(user.toUtf8(), pwd.toUtf8(), targetFilePath.toUtf8()) == 0);
  246. }
  247. bool lhhttpapi::DoCurlUploadFtpFile(const QString &localFilePath, const QString &destFilePath)
  248. {
  249. return DoCurlUploadFtpFile("", "", localFilePath, destFilePath);
  250. }
  251. bool lhhttpapi::DoCurlUploadFtpFile(const QString &user, const QString &pwd, const QString &localFilePath, const QString &destFilePath)
  252. {
  253. if(fnDoCurlUploadFtpFile == nullptr) return -1;
  254. return (fnDoCurlUploadFtpFile(user.toUtf8(), pwd.toUtf8(), localFilePath.toUtf8(), destFilePath.toUtf8()) == 0);
  255. }
  256. bool lhhttpapi::DoCurlUploadFtpFileContent(const char *szFileContent, int nSize, const QString &destFilePath)
  257. {
  258. return DoCurlUploadFtpFileContent("", "", szFileContent, nSize, destFilePath);
  259. }
  260. bool lhhttpapi::DoCurlUploadFtpFileContent(const QString &user, const QString &pwd, const char *szFileContent, int nSize, const QString &destFilePath)
  261. {
  262. if(fnDoCurlUploadFtpFileContent == nullptr) return -1;
  263. return (fnDoCurlUploadFtpFileContent(user.toUtf8(), pwd.toUtf8(), szFileContent, nSize, destFilePath.toUtf8()) == 0);
  264. }
  265. int64_t lhhttpapi::DoGetHttpFileSize(const QString &url)
  266. {
  267. if(fnDoGetHttpFileSize == nullptr) return 0;
  268. return fnDoGetHttpFileSize(url.toUtf8().data());
  269. }
  270. int lhhttpapi::DoGetLastError(char *pError, int nLen, int *nErrorCode)
  271. {
  272. if(fnGetLastError == nullptr) return -1;
  273. return fnGetLastError(pError, nLen, nErrorCode);
  274. }
  275. QString lhhttpapi::DoGetLastError(int *nErrorCode)
  276. {
  277. if(fnGetLastError == nullptr) return QString();
  278. int nLen = 1024;
  279. char pError[1024] = {0};
  280. if(fnGetLastError(pError, nLen, nErrorCode) != 0) return QString();
  281. return QString::fromLocal8Bit(pError).toLocal8Bit();
  282. }
  283. DoInterfaceObject::DoInterfaceObject(lhhttpapi *pApi, const QString &strToken, int nOperatorType, const QString &strParamXml,bool bismulti,void* phttpip)
  284. : ThreadWorker()
  285. , m_pApi(pApi)
  286. , m_strToken(strToken)
  287. , m_nOperatorType(nOperatorType)
  288. , m_strParamXml(strParamXml)
  289. , m_nResult(-1)
  290. ,m_bismulti(bismulti)
  291. ,m_phttpip(phttpip)
  292. {
  293. }
  294. void DoInterfaceObject::DoWork()
  295. {
  296. //LH_WRITE_COMMON("DoInterfaceObject::DoWork Start");
  297. if(m_pApi != nullptr)
  298. {
  299. //QString serid = singletonWork::GetServerDBID();
  300. // QString serid = SingletonSet::GetServerID();
  301. // QString appType = APPTYPE;
  302. // char *buffer = new char[LHHTTPAPI_BUFFER_SIZE];
  303. // 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);
  304. // m_strRetXml = QString::fromUtf8(buffer);
  305. // delete [] buffer;
  306. }
  307. emit sig_WorkFinished();
  308. }