parseCreditXml.py0923 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #coding=utf-8
  2. from xml.dom import minidom
  3. import base64
  4. import sys
  5. import os
  6. from ini_op import Config;
  7. base_dir = os.path.dirname(os.path.abspath(__file__))
  8. config = Config(base_dir+"/config.ini");
  9. productNumXy = config.get("baseconf", "productNumXy")
  10. productNumJz = config.get("baseconf","productNumJz")
  11. productNumXxw = config.get("baseconf","productNumXxw")
  12. productNumFb = config.get("baseconf", "productNumFb")
  13. productNumKcd = config.get("baseconf", "productNumKcd")
  14. productNumFd = config.get("baseconf", "productNumFd")
  15. productNumKn = config.get("baseconf", "productNumKn")
  16. productNum500 = config.get("baseconf", "productNum500")
  17. isPlt = config.get("baseconf", "isPlt");
  18. import xyHttp
  19. import log
  20. import shutil
  21. logger = log.logger
  22. import time
  23. from dbController import DbController
  24. dbController = DbController();
  25. import json
  26. import requests
  27. import timeit
  28. import traceback
  29. import gzip
  30. import io
  31. from xmlParser import XmlParser;
  32. from pboc.invokePboc import PBOC
  33. from mailUtil import MailUtil
  34. def gzip_str(string_):
  35. out = io.BytesIO()
  36. with gzip.GzipFile(fileobj=out, mode='w') as fo:
  37. fo.write(string_.encode())
  38. bytes_obj = out.getvalue()
  39. return bytes_obj
  40. def gunzip_bytes_obj(bytes_obj):
  41. in_ = io.BytesIO()
  42. in_.write(bytes_obj)
  43. in_.seek(0)
  44. with gzip.GzipFile(fileobj=in_, mode='rb') as fo:
  45. gunzipped_bytes_obj = fo.read()
  46. return gunzipped_bytes_obj.decode()
  47. #解析xml数据
  48. def getBusinessInfo(xmlFile):
  49. doc = minidom.parse(xmlFile)
  50. request = doc.documentElement.getElementsByTagName("request")[0]
  51. responseBody = doc.documentElement.getElementsByTagName("responseBody")[0]
  52. if len(responseBody.childNodes)==0:
  53. mailUtil = MailUtil();
  54. webhook = 'https://oapi.dingtalk.com/robot/send?access_token=64d8b2c7fed4949e9433b807c7c5559939f1517af8f77c1dacb4de19c6910b56'
  55. mailUtil.dingtalk("号码:" +xmlFile.split("_")[0] + " 查询失败 ", webhook)
  56. responseBodyText = responseBody.childNodes[0].data
  57. decrpyt_bytes = base64.b64decode(responseBodyText)
  58. xmlData = gunzip_bytes_obj(decrpyt_bytes)
  59. # print(xmlData)
  60. xmlPath = xmlFile+".txt"
  61. with open(xmlPath, 'w', encoding='utf-8') as fp:
  62. fp.write(xmlData)
  63. # docXml = minidom.parseString(xmlData)
  64. # print(docXml.documentElement.getElementsByTagName("RPTTIME")[0].childNodes[0].data)
  65. #非小赢需要解析
  66. result = ""
  67. productNum = getNodeData(request, "productNum")
  68. if productNum != productNumXy:
  69. result = parse(xmlData)
  70. #输出文件
  71. # print(result)
  72. data = {
  73. "businessNum":getNodeData(request,"businessNum"),
  74. "coopBusinessNum": getNodeData(request, "coopBusinessNum"),
  75. "customerNum":getNodeData(request,"customerNum"),
  76. "certificateNum": getNodeData(request, "certificateNum"),
  77. "productNum": productNum,
  78. "creditXml":xmlData,
  79. "extend": getNodeData(request, "extend"),
  80. "result":result,
  81. }
  82. return data
  83. #解析xml报文
  84. def parse(xmlData):
  85. xmlParse = XmlParser()
  86. result = ""
  87. try:
  88. result = xmlParse.parse(xmlData)
  89. except:
  90. info = sys.exc_info()
  91. logger.error(info[0])
  92. logger.error(info[1])
  93. logger.error(traceback.extract_tb(info[2], 1))
  94. return result
  95. def getNodeData(request,key):
  96. data = request.getElementsByTagName(key)[0].childNodes[0].data
  97. return data;
  98. # if __name__ == '__main__':
  99. # businessInfo = getBusinessInfo('./test.xml')
  100. # print(businessInfo)
  101. # xml = ""
  102. # data = base64.b64encode(xml.encode("UTF-8"))
  103. # print(str(data))
  104. #调用http
  105. def process(businessInfo,basePath,xml_path):
  106. productNum = businessInfo["productNum"]
  107. if productNumXy.find(productNum) >= 0:
  108. result = xyHttp.call_credit(businessInfo)
  109. # result = json.loads(result);
  110. logger.info(result)
  111. #上传审批结果
  112. jsonPath = basePath + businessInfo["certificateNum"] + ".txt"
  113. logger.info(jsonPath)
  114. with open(jsonPath, 'w', encoding='utf-8') as fp:
  115. fp.write(result)
  116. uploadJsonFile(businessInfo,jsonPath)
  117. #移动xml文件
  118. descXmlPath = basePath + "execed_new/" + os.path.basename(xml_path)
  119. descJsonPath = basePath + "execed_txt/" + os.path.basename(jsonPath)
  120. if not os.path.exists(basePath + "execed_new/"):
  121. os.mkdir(basePath + "execed_new/")
  122. logger.info("移动文件 from " + xml_path + " to " + descXmlPath)
  123. if isPlt == "1":
  124. shutil.move(xml_path, descXmlPath)
  125. shutil.move(jsonPath, descJsonPath)
  126. else:
  127. #走jar包逻辑
  128. # print(businessInfo["result"])
  129. txtPath = xml_path.replace("xml","txt")
  130. with open(txtPath, 'w', encoding='utf-8') as fp:
  131. fp.write(businessInfo["result"])
  132. if productNum == productNumJz:#桔子
  133. invokePboc(businessInfo, txtPath)
  134. #移动xml文件
  135. descXmlPath = basePath + "execed_new/" + os.path.basename(xml_path)
  136. if isPlt == "1":
  137. move(xml_path, descXmlPath)
  138. elif productNum == productNumXxw:#新希望
  139. invokeXxw(businessInfo, txtPath);
  140. jsonPath = basePath + businessInfo["certificateNum"] + ".txt"
  141. #移动xml文件
  142. descXmlPath = basePath + "execed_new/" + os.path.basename(xml_path)
  143. if not os.path.exists(basePath + "execed_new/"):
  144. os.mkdir(basePath + "execed_new/")
  145. logger.info("移动文件 from " + xml_path + " to " + descXmlPath)
  146. if isPlt == "1":
  147. move(xml_path, descXmlPath)
  148. elif productNum == productNumFb or productNum== productNumKcd or productNum500.find(productNum)>=0:#没有java包 #快车道和风暴 上传500个字段
  149. logger.info(businessInfo["productNum"])
  150. uploadJsonFile(businessInfo,txtPath)
  151. # 移动xml文件
  152. descXmlPath = basePath + "execed_new/" + os.path.basename(xml_path)
  153. if not os.path.exists(basePath + "execed_new/"):
  154. os.mkdir(basePath + "execed_new/")
  155. logger.info("移动文件 from " + xml_path + " to " + descXmlPath)
  156. if isPlt == "1":
  157. move(xml_path, descXmlPath)
  158. elif productNum == productNumFd:#分蛋,上传解析后的xml
  159. uploadJsonFile(businessInfo, xml_path+".txt")
  160. # 移动xml文件
  161. descXmlPath = basePath + "execed_new/" + os.path.basename(xml_path)
  162. if not os.path.exists(basePath + "execed_new/"):
  163. os.mkdir(basePath + "execed_new/")
  164. logger.info("移动文件 from " + xml_path + " to " + descXmlPath)
  165. if isPlt == "1":
  166. move(xml_path, descXmlPath)
  167. elif productNum == productNumKn:#快牛本地jar包
  168. result = xyHttp.callLocal(businessInfo)
  169. uploadAudit(result,businessInfo["businessNum"],productNum)
  170. # 移动xml文件
  171. descXmlPath = basePath + "execed_new/" + os.path.basename(xml_path)
  172. if not os.path.exists(basePath + "execed_new/"):
  173. os.mkdir(basePath + "execed_new/")
  174. logger.info("移动文件 from " + xml_path + " to " + descXmlPath)
  175. if isPlt == "1":
  176. move(xml_path, descXmlPath)
  177. else:
  178. i=0
  179. #本地jar
  180. if isPlt == "1":
  181. descTxtPath = basePath + "execed_txt/" + os.path.basename(txtPath)
  182. shutil.move(txtPath, descTxtPath)
  183. #调用jar包
  184. def move(xml_path, descXmlPath):
  185. try:
  186. shutil.move(xml_path, descXmlPath)
  187. except:
  188. logger.info("移动文件 from " + xml_path + " to " + descXmlPath)
  189. def invokePboc(businessInfo,txtPath):
  190. # ===================================
  191. try:
  192. businessNum = businessInfo["businessNum"]
  193. coopBusinessNum = businessInfo["coopBusinessNum"]
  194. pboc = PBOC()
  195. jarTxt = pboc.calc(txtPath,coopBusinessNum);
  196. result = json.loads(jarTxt)
  197. logger.info(result)
  198. if result.get("errcode")== None:
  199. uploadAudit(result,businessNum,businessInfo["productNum"])
  200. else:
  201. logger.error(result["errmsg"])
  202. except:
  203. info = sys.exc_info()
  204. logger.error(info[0])
  205. logger.error(info[1])
  206. # logging.log(logging.ERROR, info[2])
  207. logger.error(traceback.extract_tb(info[2], 1))
  208. #调用xxwjar包
  209. def invokeXxw(businessInfo,txtPath):
  210. try:
  211. businessNum = businessInfo["businessNum"]
  212. coopBusinessNum = businessInfo["coopBusinessNum"]
  213. customerNum = businessInfo["customerNum"]
  214. certificateNum = businessInfo["certificateNum"]
  215. pboc = PBOC()
  216. jarTxt = pboc.calcXxw(coopBusinessNum,customerNum,txtPath);
  217. logger.info("jarTxt:"+jarTxt)
  218. result = json.loads(jarTxt)
  219. jsonPath = basePath+certificateNum+".txt"
  220. # logger.info(jsonPath)
  221. with open(jsonPath, 'w', encoding='utf-8') as fp:
  222. fp.write(jarTxt)
  223. uploadJsonFile(businessInfo,jsonPath)
  224. if not os.path.exists(basePath + "execed_txt/"):
  225. os.mkdir(basePath + "execed_txt/")
  226. descJsonPath = basePath + "execed_txt/" + os.path.basename(jsonPath)
  227. shutil.move(jsonPath, descJsonPath)
  228. except:
  229. info = sys.exc_info()
  230. logger.error(info[0])
  231. logger.error(info[1])
  232. logger.error(traceback.extract_tb(info[2], 1))
  233. #上传审批结果
  234. def uploadAudit(result,businessNum,productNum):
  235. approvalType = result["approveResult"]
  236. if productNum == productNumJz:
  237. if approvalType=="1":
  238. approvalOpinion = "征信通过"
  239. approvalType = "4"
  240. else:
  241. approvalOpinion = "征信拒绝"
  242. approvalType = "3"
  243. elif productNum == productNumKn:
  244. if approvalType=="1":
  245. approvalOpinion = "征信通过"
  246. approvalType = "4"
  247. else:#快牛 需要添加拒绝原因
  248. approvalOpinion = "征信拒绝" + "#" + result["rule"]
  249. approvalType = "3"
  250. taskKey = config.get("baseconf","taskKey")
  251. appoveApiUrl = config.get("baseconf","appoveApiUrl")
  252. key = config.get("baseconf", "AESKey")
  253. data = {"header":{
  254. "ticket": "2938123198320412343",
  255. "timestamp": int(int(round(time.time() * 1000+60*1000))),
  256. "nonce": config.get("baseconf", "nonce")
  257. },
  258. "body":{"approvalType": approvalType, "businessNum": businessNum,"taskKey":taskKey,"approvalOpinion":approvalOpinion}}
  259. access_token = dbController.getToken()
  260. appoveApiUrl = appoveApiUrl+"?access_token="+access_token
  261. headers = {"Content-Type": "application/json"}
  262. jsonStr = json.dumps(data);
  263. jsonStr = jsonStr.replace('"',"\\\"")#必须替换才行
  264. logger.info(jsonStr)
  265. pboc = PBOC();
  266. encryData = pboc.encrypt(jsonStr,key)
  267. encryData = encryData[0:len(encryData)-2]
  268. logger.info(encryData)
  269. response = requests.post(appoveApiUrl, data=encryData,headers=headers)
  270. text = response.text
  271. pboc = PBOC();
  272. resultText = pboc.decrypt(text, config.get("baseconf", "AESKey"))
  273. logger.info(businessNum + "#" + "uploadAudit upload_result:" + resultText)
  274. #上传解析json
  275. def uploadJsonFile(businessInfo,json_path):
  276. # ===================================
  277. try:
  278. fileName = os.path.basename(json_path)
  279. #上传文件逻辑
  280. logger.info(json_path+"#"+"准备上传文件")
  281. uploadApiUrl = config.get("baseconf", "uploadApiUrl");
  282. uploadApiUrl = uploadApiUrl + "?access_token=" + dbController.getToken()
  283. files = {'file': open(json_path, 'rb')}
  284. logger.info(fileName+"#"+"businessNum:"+businessInfo["businessNum"])
  285. data = {'docType': "23", 'businessNum': businessInfo["businessNum"]}
  286. response = requests.post(uploadApiUrl, files=files, data=data)
  287. text = response.text
  288. logger.info("上传结果:"+text)
  289. pboc = PBOC();
  290. resultText = pboc.decrypt(text,config.get("baseconf", "AESKey"))
  291. logger.info(fileName+"#"+"uploadJsonFile:" + resultText)
  292. except:
  293. info = sys.exc_info()
  294. logger.error(info[0])
  295. logger.error(info[1])
  296. # logging.log(logging.ERROR, info[2])
  297. logger.error(traceback.extract_tb(info[2], 1))
  298. if __name__ == '__main__':
  299. file_name = ""
  300. # basePath = "D:/mydocument/myproject/git/parse/"
  301. basePath = "D:/mydocument/myprojects/creditreport/parse/"
  302. file_name = "黄振武_420115199306180091_230034699446131714.xml"
  303. xml_path = basePath + file_name
  304. start = timeit.default_timer();
  305. if len(sys.argv) > 1:
  306. basePath = sys.argv[1]
  307. xml_path = basePath + sys.argv[2]
  308. file_name = sys.argv[2]
  309. logger.info(xml_path+" 解析开始")
  310. businessInfo = getBusinessInfo(xml_path)
  311. # logger.info(businessInfo)
  312. process(businessInfo,basePath,xml_path)
  313. s = timeit.default_timer() - start;
  314. logger.info(str(s) + " 秒")
  315. logger.info(xml_path+" 解析完成")