parseCreditXml.py1013 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. 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. shutil.move(xml_path+".txt", descXmlPath.replace("execed_new","execed_xml")+".txt")
  188. except:
  189. logger.info("移动文件 from " + xml_path + " to " + descXmlPath)
  190. def invokePboc(businessInfo,txtPath):
  191. # ===================================
  192. try:
  193. businessNum = businessInfo["businessNum"]
  194. coopBusinessNum = businessInfo["coopBusinessNum"]
  195. pboc = PBOC()
  196. jarTxt = pboc.calc(txtPath,coopBusinessNum);
  197. result = json.loads(jarTxt)
  198. logger.info(result)
  199. if result.get("errcode")== None:
  200. uploadAudit(result,businessNum,businessInfo["productNum"])
  201. else:
  202. logger.error(result["errmsg"])
  203. except:
  204. info = sys.exc_info()
  205. logger.error(info[0])
  206. logger.error(info[1])
  207. # logging.log(logging.ERROR, info[2])
  208. logger.error(traceback.extract_tb(info[2], 1))
  209. #调用xxwjar包
  210. def invokeXxw(businessInfo,txtPath):
  211. try:
  212. businessNum = businessInfo["businessNum"]
  213. coopBusinessNum = businessInfo["coopBusinessNum"]
  214. customerNum = businessInfo["customerNum"]
  215. certificateNum = businessInfo["certificateNum"]
  216. pboc = PBOC()
  217. jarTxt = pboc.calcXxw(coopBusinessNum,customerNum,txtPath);
  218. logger.info("jarTxt:"+jarTxt)
  219. result = json.loads(jarTxt)
  220. jsonPath = basePath+certificateNum+".txt"
  221. # logger.info(jsonPath)
  222. with open(jsonPath, 'w', encoding='utf-8') as fp:
  223. fp.write(jarTxt)
  224. uploadJsonFile(businessInfo,jsonPath)
  225. if not os.path.exists(basePath + "execed_txt/"):
  226. os.mkdir(basePath + "execed_txt/")
  227. descJsonPath = basePath + "execed_txt/" + os.path.basename(jsonPath)
  228. shutil.move(jsonPath, descJsonPath)
  229. except:
  230. info = sys.exc_info()
  231. logger.error(info[0])
  232. logger.error(info[1])
  233. logger.error(traceback.extract_tb(info[2], 1))
  234. #上传审批结果
  235. def uploadAudit(result,businessNum,productNum):
  236. approvalType = result["approveResult"]
  237. if productNum == productNumJz:
  238. if approvalType=="1":
  239. approvalOpinion = "征信通过"
  240. approvalType = "4"
  241. else:
  242. approvalOpinion = "征信拒绝"
  243. approvalType = "3"
  244. elif productNum == productNumKn:
  245. if approvalType=="1":
  246. approvalOpinion = "征信通过"
  247. approvalType = "4"
  248. else:#快牛 需要添加拒绝原因
  249. approvalOpinion = "征信拒绝" + "#" + result["rule"]
  250. approvalType = "3"
  251. taskKey = config.get("baseconf","taskKey")
  252. appoveApiUrl = config.get("baseconf","appoveApiUrl")
  253. key = config.get("baseconf", "AESKey")
  254. data = {"header":{
  255. "ticket": "2938123198320412343",
  256. "timestamp": int(int(round(time.time() * 1000+60*1000))),
  257. "nonce": config.get("baseconf", "nonce")
  258. },
  259. "body":{"approvalType": approvalType, "businessNum": businessNum,"taskKey":taskKey,"approvalOpinion":approvalOpinion}}
  260. access_token = dbController.getToken()
  261. appoveApiUrl = appoveApiUrl+"?access_token="+access_token
  262. headers = {"Content-Type": "application/json"}
  263. jsonStr = json.dumps(data);
  264. jsonStr = jsonStr.replace('"',"\\\"")#必须替换才行
  265. logger.info(jsonStr)
  266. pboc = PBOC();
  267. encryData = pboc.encrypt(jsonStr,key)
  268. encryData = encryData[0:len(encryData)-2]
  269. logger.info(encryData)
  270. response = requests.post(appoveApiUrl, data=encryData,headers=headers)
  271. text = response.text
  272. pboc = PBOC();
  273. resultText = pboc.decrypt(text, config.get("baseconf", "AESKey"))
  274. logger.info(businessNum + "#" + "uploadAudit upload_result:" + resultText)
  275. #上传解析json
  276. def uploadJsonFile(businessInfo,json_path):
  277. # ===================================
  278. try:
  279. fileName = os.path.basename(json_path)
  280. #上传文件逻辑
  281. logger.info(json_path+"#"+"准备上传文件")
  282. uploadApiUrl = config.get("baseconf", "uploadApiUrl");
  283. uploadApiUrl = uploadApiUrl + "?access_token=" + dbController.getToken()
  284. files = {'file': open(json_path, 'rb')}
  285. logger.info(fileName+"#"+"businessNum:"+businessInfo["businessNum"])
  286. data = {'docType': "23", 'businessNum': businessInfo["businessNum"]}
  287. response = requests.post(uploadApiUrl, files=files, data=data)
  288. text = response.text
  289. logger.info("上传结果:"+text)
  290. pboc = PBOC();
  291. resultText = pboc.decrypt(text,config.get("baseconf", "AESKey"))
  292. logger.info(fileName+"#"+"uploadJsonFile:" + resultText)
  293. except:
  294. info = sys.exc_info()
  295. logger.error(info[0])
  296. logger.error(info[1])
  297. # logging.log(logging.ERROR, info[2])
  298. logger.error(traceback.extract_tb(info[2], 1))
  299. if __name__ == '__main__':
  300. file_name = ""
  301. # basePath = "D:/mydocument/myproject/git/parse/"
  302. basePath = "D:/mydocument/myprojects/creditreport/parse/"
  303. file_name = "黄振武_420115199306180091_230034699446131714.xml"
  304. xml_path = basePath + file_name
  305. start = timeit.default_timer();
  306. if len(sys.argv) > 1:
  307. basePath = sys.argv[1]
  308. xml_path = basePath + sys.argv[2]
  309. file_name = sys.argv[2]
  310. logger.info(xml_path+" 解析开始")
  311. businessInfo = getBusinessInfo(xml_path)
  312. # logger.info(businessInfo)
  313. process(businessInfo,basePath,xml_path)
  314. s = timeit.default_timer() - start;
  315. logger.info(str(s) + " 秒")
  316. logger.info(xml_path+" 解析完成")