#coding=utf-8 from xml.dom import minidom import base64 import sys import os from ini_op import Config; base_dir = os.path.dirname(os.path.abspath(__file__)) config = Config(base_dir+"/config.ini"); productNumXy = config.get("baseconf", "productNumXy") productNumJz = config.get("baseconf","productNumJz") productNumXxw = config.get("baseconf","productNumXxw") productNumFb = config.get("baseconf", "productNumFb") productNumKcd = config.get("baseconf", "productNumKcd") productNumFd = config.get("baseconf", "productNumFd") productNumKn = config.get("baseconf", "productNumKn") productNum500 = config.get("baseconf", "productNum500") isPlt = config.get("baseconf", "isPlt"); import xyHttp import log import shutil logger = log.logger import time from dbController import DbController dbController = DbController(); import json import requests import timeit import traceback import gzip import io from xmlParser import XmlParser; from pboc.invokePboc import PBOC from mailUtil import MailUtil def gzip_str(string_): out = io.BytesIO() with gzip.GzipFile(fileobj=out, mode='w') as fo: fo.write(string_.encode()) bytes_obj = out.getvalue() return bytes_obj def gunzip_bytes_obj(bytes_obj): in_ = io.BytesIO() in_.write(bytes_obj) in_.seek(0) with gzip.GzipFile(fileobj=in_, mode='rb') as fo: gunzipped_bytes_obj = fo.read() return gunzipped_bytes_obj.decode() #解析xml数据 def getBusinessInfo(xmlFile): doc = minidom.parse(xmlFile) request = doc.documentElement.getElementsByTagName("request")[0] responseBody = doc.documentElement.getElementsByTagName("responseBody")[0] responseHeader = doc.documentElement.getElementsByTagName("responseHeader")[0] result = "" xmlData = "" isBaihu = "0"; # 非小赢需要解析 productNum = getNodeData(request, "productNum") if productNum != productNumXy: if xmlData !="": result = parse(xmlData) if len(responseBody.childNodes)==0: mailUtil = MailUtil(); webhook = 'https://oapi.dingtalk.com/robot/send?access_token=64d8b2c7fed4949e9433b807c7c5559939f1517af8f77c1dacb4de19c6910b56' mailUtil.dingtalk("号码:" +xmlFile.split("_")[0] + " 查询失败 ", webhook) if getNodeData(responseHeader,"resultMsg")=="查询成功,无报告": isBaihu = "1" else: responseBodyText = responseBody.childNodes[0].data decrpyt_bytes = base64.b64decode(responseBodyText) xmlData = gunzip_bytes_obj(decrpyt_bytes) # print(xmlData) xmlPath = xmlFile+".txt" with open(xmlPath, 'w', encoding='utf-8') as fp: fp.write(xmlData) # docXml = minidom.parseString(xmlData) # print(docXml.documentElement.getElementsByTagName("RPTTIME")[0].childNodes[0].data) #输出文件 # print(result) data = { "businessNum":getNodeData(request,"businessNum"), "coopBusinessNum": getNodeData(request, "coopBusinessNum"), "customerNum":getNodeData(request,"customerNum"), "certificateNum": getNodeData(request, "certificateNum"), "productNum": productNum, "creditXml":xmlData, "extend": getNodeData(request, "extend"), "result":result, "isBaihu":isBaihu } return data #解析xml报文 def parse(xmlData): xmlParse = XmlParser() result = "" try: result = xmlParse.parse(xmlData) except: info = sys.exc_info() logger.error(info[0]) logger.error(info[1]) logger.error(traceback.extract_tb(info[2], 1)) return result def getNodeData(request,key): data = request.getElementsByTagName(key)[0].childNodes[0].data return data; # if __name__ == '__main__': # businessInfo = getBusinessInfo('./test.xml') # print(businessInfo) # xml = "" # data = base64.b64encode(xml.encode("UTF-8")) # print(str(data)) #调用http def process(businessInfo,basePath,xml_path): productNum = businessInfo["productNum"] if productNumXy.find(productNum) >= 0: result = xyHttp.call_credit(businessInfo) # result = json.loads(result); logger.info(result) #上传审批结果 jsonPath = basePath + businessInfo["certificateNum"] + ".txt" logger.info(jsonPath) with open(jsonPath, 'w', encoding='utf-8') as fp: fp.write(result) uploadJsonFile(businessInfo,jsonPath) #移动xml文件 descXmlPath = basePath + "execed_new/" + os.path.basename(xml_path) descJsonPath = basePath + "execed_txt/" + os.path.basename(jsonPath) if not os.path.exists(basePath + "execed_new/"): os.mkdir(basePath + "execed_new/") logger.info("移动文件 from " + xml_path + " to " + descXmlPath) if isPlt == "1": move(xml_path, descXmlPath) shutil.move(jsonPath, descJsonPath) else: #走jar包逻辑 # print(businessInfo["result"]) txtPath = xml_path.replace("xml","txt") with open(txtPath, 'w', encoding='utf-8') as fp: fp.write(businessInfo["result"]) if productNum == productNumJz:#桔子 invokePboc(businessInfo, txtPath) #移动xml文件 descXmlPath = basePath + "execed_new/" + os.path.basename(xml_path) if isPlt == "1": move(xml_path, descXmlPath) elif productNum == productNumXxw:#新希望 invokeXxw(businessInfo, txtPath); jsonPath = basePath + businessInfo["certificateNum"] + ".txt" #移动xml文件 descXmlPath = basePath + "execed_new/" + os.path.basename(xml_path) if not os.path.exists(basePath + "execed_new/"): os.mkdir(basePath + "execed_new/") logger.info("移动文件 from " + xml_path + " to " + descXmlPath) if isPlt == "1": move(xml_path, descXmlPath) elif productNum == productNumFb or productNum== productNumKcd or productNum500.find(productNum)>=0:#没有java包 #快车道和风暴 上传500个字段 logger.info(businessInfo["productNum"]) uploadJsonFile(businessInfo,txtPath) # 移动xml文件 descXmlPath = basePath + "execed_new/" + os.path.basename(xml_path) if not os.path.exists(basePath + "execed_new/"): os.mkdir(basePath + "execed_new/") logger.info("移动文件 from " + xml_path + " to " + descXmlPath) if isPlt == "1": move(xml_path, descXmlPath) elif productNum == productNumFd:#分蛋,上传解析后的xml uploadJsonFile(businessInfo, xml_path+".txt") # 移动xml文件 descXmlPath = basePath + "execed_new/" + os.path.basename(xml_path) if not os.path.exists(basePath + "execed_new/"): os.mkdir(basePath + "execed_new/") logger.info("移动文件 from " + xml_path + " to " + descXmlPath) if isPlt == "1": move(xml_path, descXmlPath) elif productNum == productNumKn:#快牛本地jar包 if businessInfo["result"]!="": result = xyHttp.callLocal(businessInfo) uploadAudit(result,businessInfo["businessNum"],productNum) else: result = {"approveResult":"0","rule":"白户"} uploadAudit(result, businessInfo["businessNum"], productNum) # 移动xml文件 descXmlPath = basePath + "execed_new/" + os.path.basename(xml_path) if not os.path.exists(basePath + "execed_new/"): os.mkdir(basePath + "execed_new/") logger.info("移动文件 from " + xml_path + " to " + descXmlPath) if isPlt == "1": move(xml_path, descXmlPath) else: i=0 #本地jar if isPlt == "1": descTxtPath = basePath + "execed_txt/" + os.path.basename(txtPath) shutil.move(txtPath, descTxtPath) #调用jar包 def move(xml_path, descXmlPath): try: shutil.move(xml_path, descXmlPath) shutil.move(xml_path+".txt", descXmlPath.replace("execed_new","execed_xml")+".txt") except: logger.info("移动文件 from " + xml_path + " to " + descXmlPath) def invokePboc(businessInfo,txtPath): # =================================== try: businessNum = businessInfo["businessNum"] coopBusinessNum = businessInfo["coopBusinessNum"] pboc = PBOC() jarTxt = pboc.calc(txtPath,coopBusinessNum); result = json.loads(jarTxt) logger.info(result) if result.get("errcode")== None: uploadAudit(result,businessNum,businessInfo["productNum"]) else: if businessInfo["isBaihu"]=="1": result = {"approveResult":"1"} uploadAudit(result, businessNum, businessInfo["productNum"]) logger.error(result["errmsg"]) except: info = sys.exc_info() logger.error(info[0]) logger.error(info[1]) # logging.log(logging.ERROR, info[2]) logger.error(traceback.extract_tb(info[2], 1)) #调用xxwjar包 def invokeXxw(businessInfo,txtPath): try: businessNum = businessInfo["businessNum"] coopBusinessNum = businessInfo["coopBusinessNum"] customerNum = businessInfo["customerNum"] certificateNum = businessInfo["certificateNum"] pboc = PBOC() jarTxt = pboc.calcXxw(coopBusinessNum,customerNum,txtPath); logger.info("jarTxt:"+jarTxt) result = json.loads(jarTxt) jsonPath = basePath+certificateNum+".txt" # logger.info(jsonPath) with open(jsonPath, 'w', encoding='utf-8') as fp: fp.write(jarTxt) uploadJsonFile(businessInfo,jsonPath) if not os.path.exists(basePath + "execed_txt/"): os.mkdir(basePath + "execed_txt/") descJsonPath = basePath + "execed_txt/" + os.path.basename(jsonPath) shutil.move(jsonPath, descJsonPath) except: info = sys.exc_info() logger.error(info[0]) logger.error(info[1]) logger.error(traceback.extract_tb(info[2], 1)) #上传审批结果 def uploadAudit(result,businessNum,productNum): approvalType = result["approveResult"] if productNum == productNumJz: if approvalType=="1": approvalOpinion = "征信通过" approvalType = "4" else: approvalOpinion = "征信拒绝" approvalType = "3" elif productNum == productNumKn: if approvalType=="1": approvalOpinion = "征信通过" approvalType = "4" else:#快牛 需要添加拒绝原因 approvalOpinion = "征信拒绝" + "#" + result["rule"] approvalType = "3" taskKey = config.get("baseconf","taskKey") appoveApiUrl = config.get("baseconf","appoveApiUrl") key = config.get("baseconf", "AESKey") data = {"header":{ "ticket": "2938123198320412343", "timestamp": int(int(round(time.time() * 1000+60*1000))), "nonce": config.get("baseconf", "nonce") }, "body":{"approvalType": approvalType, "businessNum": businessNum,"taskKey":taskKey,"approvalOpinion":approvalOpinion}} access_token = dbController.getToken() appoveApiUrl = appoveApiUrl+"?access_token="+access_token headers = {"Content-Type": "application/json"} jsonStr = json.dumps(data); jsonStr = jsonStr.replace('"',"\\\"")#必须替换才行 logger.info(jsonStr) pboc = PBOC(); encryData = pboc.encrypt(jsonStr,key) encryData = encryData[0:len(encryData)-2] logger.info(encryData) response = requests.post(appoveApiUrl, data=encryData,headers=headers) text = response.text pboc = PBOC(); resultText = pboc.decrypt(text, config.get("baseconf", "AESKey")) logger.info(businessNum + "#" + "uploadAudit upload_result:" + resultText) #上传解析json def uploadJsonFile(businessInfo,json_path): # =================================== try: fileName = os.path.basename(json_path) #上传文件逻辑 logger.info(json_path+"#"+"准备上传文件") uploadApiUrl = config.get("baseconf", "uploadApiUrl"); uploadApiUrl = uploadApiUrl + "?access_token=" + dbController.getToken() files = {'file': open(json_path, 'rb')} logger.info(fileName+"#"+"businessNum:"+businessInfo["businessNum"]) data = {'docType': "23", 'businessNum': businessInfo["businessNum"]} response = requests.post(uploadApiUrl, files=files, data=data) text = response.text logger.info("上传结果:"+text) pboc = PBOC(); resultText = pboc.decrypt(text,config.get("baseconf", "AESKey")) logger.info(fileName+"#"+"uploadJsonFile:" + resultText) except: info = sys.exc_info() logger.error(info[0]) logger.error(info[1]) # logging.log(logging.ERROR, info[2]) logger.error(traceback.extract_tb(info[2], 1)) if __name__ == '__main__': file_name = "" # basePath = "D:/mydocument/myproject/git/parse/" basePath = "D:/mydocument/myprojects/creditreport/parse/" file_name = "杨璨瑜_532301198406113721_248101219631900677.xml" xml_path = basePath + file_name start = timeit.default_timer(); if len(sys.argv) > 1: basePath = sys.argv[1] xml_path = basePath + sys.argv[2] file_name = sys.argv[2] logger.info(xml_path+" 解析开始") businessInfo = getBusinessInfo(xml_path) # logger.info(businessInfo) process(businessInfo,basePath,xml_path) s = timeit.default_timer() - start; logger.info(str(s) + " 秒") logger.info(xml_path+" 解析完成")