MiddleWares_YiHe 郑州颐和医院随访系统中间件

CellHttp.cpp 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include "stdafx.h"
  2. #include "CellHttp.h"
  3. #include "IvrFlow.h"
  4. #include "FlowDataProvider.h"
  5. #include "wininet.h"
  6. #include "afxinet.h"
  7. #include "json.h"
  8. CCellHttp::CCellHttp()
  9. {
  10. m_IsSaveRs = 0;
  11. m_SuccessPos = 0;
  12. m_FailPos = 0;
  13. m_Url = "";
  14. }
  15. CCellHttp::CCellHttp(CCellHttp & cellHttp) : CCellBase(cellHttp)
  16. {
  17. m_IsSaveRs = cellHttp.m_IsSaveRs;
  18. m_SuccessPos = cellHttp.m_SuccessPos;
  19. m_FailPos = cellHttp.m_FailPos;
  20. m_Url = cellHttp.m_Url;
  21. m_RespVar = cellHttp.m_RespVar;
  22. }
  23. CCellHttp::~CCellHttp(void)
  24. {
  25. }
  26. /*****************************************************************
  27. **【函数名称】 copy
  28. **【函数功能】 拷贝自身
  29. **【参数】
  30. **【返回值】 拷贝副本
  31. ****************************************************************/
  32. CCellBase * CCellHttp::copy(void)
  33. {
  34. CCellBase *pCellBase = new CCellHttp(*this);
  35. return pCellBase;
  36. }
  37. /*****************************************************************
  38. **【函数名称】 fillData
  39. **【函数功能】 节点解析,填充数据
  40. **【参数】 Provider:数据提供器
  41. **【返回值】 成功true,失败false
  42. ****************************************************************/
  43. bool CCellHttp::fillData(IFlowDataProvider& Provider)
  44. {
  45. CString Data;
  46. do
  47. {
  48. if (!Provider.getData(CELL_ATTRIBUTE_POS, Data))
  49. {
  50. Data = _T("节点号");
  51. break;
  52. }
  53. else
  54. {
  55. sscanf_s(Data, _T("%d"), &m_Pos);
  56. if (m_Pos < 1)
  57. {
  58. Data = _T("节点号");
  59. break;
  60. }
  61. }
  62. if (!Provider.getData(CELL_ATTRIBUTE_HTTP_RESP_VAR, m_RespVar))
  63. {
  64. Data = _T("HTTP响应存储变量");
  65. break;
  66. }
  67. if (!Provider.getData(CELL_ATTRIBUTE_URL_STR, m_Url))
  68. {
  69. Data = _T("SQL语句");
  70. break;
  71. }
  72. if (!Provider.getData(CELL_ATTRIBUTE_SAVE_FLAG, Data))
  73. {
  74. Data = _T("保存标志");
  75. break;
  76. }
  77. else
  78. {
  79. if (Data == DATA_BOOL_YES)
  80. m_IsSaveRs = true;
  81. else
  82. m_IsSaveRs = false;
  83. }
  84. if (!Provider.getData(CELL_ATTRIBUTE_SUCCESS_POS, Data))
  85. {
  86. Data = _T("成功跳转节点");
  87. break;
  88. }
  89. else
  90. {
  91. sscanf_s(Data, _T("%d"), &m_SuccessPos);
  92. if (m_SuccessPos < 0)
  93. {
  94. Data = _T("成功跳转节点");
  95. break;
  96. }
  97. }
  98. if (!Provider.getData(CELL_ATTRIBUTE_FAIL_POS, Data))
  99. {
  100. Data = _T("失败跳转节点");
  101. break;
  102. }
  103. else
  104. {
  105. sscanf_s(Data, _T("%d"), &m_FailPos);
  106. if (m_FailPos < 0)
  107. {
  108. Data = _T("失败跳转节点");
  109. break;
  110. }
  111. }
  112. Provider.getData(CELL_ATTRIBUTE_NOTE, m_Note);
  113. return true;
  114. } while (false);
  115. ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_ERROR, _T("{Cell}: 节点[%s]解析失败, '%s'错误"), CELL_NAME_SQL, Data);
  116. return false;
  117. }
  118. /*****************************************************************
  119. **【函数名称】 Operate
  120. **【函数功能】 节点执行函数
  121. **【参数】
  122. **【返回值】 下一个节点编号
  123. ****************************************************************/
  124. int CCellHttp::operate(void)
  125. {
  126. if (m_pIvrFlow == NULL)
  127. return CELL_OP_ERROR;
  128. CString Info;
  129. _getCellInfo(Info);
  130. ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_NORMAL, _T("{Cell}: 开始执行[%s]"), Info);
  131. CString t_Url;
  132. // 替换SQL语句中的变量
  133. if (!m_pIvrFlow->replaceVar(m_Url, t_Url))
  134. {
  135. ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_WARNING, _T("{Cell}: 执行[%s]出错, 替换URL语句中变量失败, URL = %s"), Info, t_Url);
  136. return m_FailPos;
  137. }
  138. CString t_resp;
  139. t_resp = doGet(t_Url);
  140. if (t_resp.GetLength() > 0) {
  141. //response cache
  142. CString strValue = _T("");
  143. CString strVarName = _T("RespVar");
  144. //m_RespVar
  145. m_pIvrFlow->addVar(m_RespVar, strValue);
  146. //m_pIvrFlow->findVarValue(m_RespVar, strValue);
  147. Json::Reader reader;
  148. Json::Value root;
  149. std::string strInput;
  150. strInput = t_resp.GetString();
  151. if (reader.parse(strInput, root)) // reader将Json字符串解析到root,root将包含Json里所有子元素
  152. {
  153. std::string TypeStr = root["Type"].asString(); // 访问节点,upload_id = "UP000000"
  154. if (0 == strcmp(TypeStr.c_str(), "Login"))
  155. {
  156. std::string AgentIDStr = root["AgentID"].asString();
  157. }
  158. }
  159. return m_SuccessPos;
  160. }
  161. // 返回执行结果
  162. return CELL_OP_ERROR;
  163. }
  164. // 以http Get方式请求URL
  165. CString CCellHttp::doGet(CString href)
  166. {
  167. CString httpsource = "";
  168. CInternetSession session(NULL, 0);
  169. CHttpFile* pFile = NULL;
  170. try {
  171. pFile = (CHttpFile*)session.OpenURL(href);
  172. }
  173. catch (CInternetException *e)
  174. {
  175. pFile = NULL;
  176. }
  177. if (pFile)
  178. {
  179. DWORD dwRet;
  180. pFile->QueryInfoStatusCode(dwRet);
  181. ILogger::getInstance().log(LOG_CLASS_SOCKET, LOG_LEVEL_NORMAL, "http_status[%d]", dwRet);
  182. if (dwRet == HTTP_STATUS_OK) {
  183. CString t_text;
  184. for (int i = 0; pFile->ReadString(t_text); i++)
  185. {
  186. httpsource = httpsource + t_text + "\r\n";
  187. }
  188. }
  189. else
  190. {
  191. ILogger::getInstance().log(LOG_CLASS_SOCKET, LOG_LEVEL_WARNING, "http_error,status[%d]", dwRet);
  192. }
  193. pFile->Close();
  194. delete pFile;
  195. }
  196. session.Close();
  197. if (httpsource.GetLength() > 0) {
  198. CString t_ansi_str = "";
  199. t_ansi_str = Convert(httpsource, CP_UTF8, CP_ACP);
  200. return t_ansi_str;
  201. }
  202. return httpsource;
  203. }
  204. CString CCellHttp::Convert(CString& str, int sourceCodePage, int targetCodePage)
  205. {
  206. int iUnicodeLen = MultiByteToWideChar(sourceCodePage, 0, (LPCSTR)str.GetBuffer(), -1, NULL, 0);
  207. wchar_t *pUnicode = NULL;
  208. pUnicode = new wchar_t[iUnicodeLen + 1];
  209. memset(pUnicode, 0, (iUnicodeLen + 1) * sizeof(wchar_t));
  210. MultiByteToWideChar(sourceCodePage, 0, (LPCSTR)str.GetBuffer(), -1, (LPWSTR)pUnicode, iUnicodeLen);
  211. BYTE *pTargetData = NULL;
  212. int iTargetLen = WideCharToMultiByte(targetCodePage, 0, (LPWSTR)pUnicode, -1, (char*)pTargetData, 0, NULL, NULL);
  213. pTargetData = new BYTE[iTargetLen + 1];
  214. memset(pTargetData, 0, iTargetLen + 1);
  215. WideCharToMultiByte(targetCodePage, 0, (LPWSTR)pUnicode, -1, (char*)pTargetData, iTargetLen, NULL, NULL);
  216. CString strFormatTargetData;
  217. strFormatTargetData.Format(_T("%s"), pTargetData);
  218. delete[] pTargetData;
  219. delete[] pUnicode;
  220. pTargetData = NULL;
  221. pUnicode = NULL;
  222. return strFormatTargetData;
  223. }