weixin_44532019
2022-01-03 15:37
采纳率: 70%
浏览 40
已结题

python downloadUrl 字段是 Base64 求转码方法

对接票通发票!
返回值中

import json
import time
import random
import pyDes
import requests
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import MD5, SHA1, SHA256
import base64
from flask import current_app
import warnings
import base64
import requests
import os.path
from io import BytesIO
import Ym_FP_sql




class Dzfp_lp():
    def __init__(self,dz,dh,khh,yhzh,skr,fhr,kpr,sh):
        self.taxpayerNum = '500102201007206608'  # 测试环境用此税号,正式环境用商家税号
        warnings.filterwarnings("ignore")
        self.privateKey = 'MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIVLAoolDaE7m5oMB1ZrILHkMXMF6qmC8I/FCejz4hwBcj59H3rbtcycBEmExOJTGwexFkNgRakhqM+3uP3VybWu1GBYNmqVzggWKKzThul9VPE3+OTMlxeG4H63RsCO1//J0MoUavXMMkL3txkZBO5EtTqek182eePOV8fC3ZxpAgMBAAECgYBp4Gg3BTGrZaa2mWFmspd41lK1E/kPBrRA7vltMfPj3P47RrYvp7/js/Xv0+d0AyFQXcjaYelTbCokPMJT1nJumb2A/Cqy3yGKX3Z6QibvByBlCKK29lZkw8WVRGFIzCIXhGKdqukXf8RyqfhInqHpZ9AoY2W60bbSP6EXj/rhNQJBAL76SmpQOrnCI8Xu75di0eXBN/bE9tKsf7AgMkpFRhaU8VLbvd27U9vRWqtu67RY3sOeRMh38JZBwAIS8tp5hgcCQQCyrOS6vfXIUxKoWyvGyMyhqoLsiAdnxBKHh8tMINo0ioCbU+jc2dgPDipL0ym5nhvg5fCXZC2rvkKUltLEqq4PAkAqBf9b932EpKCkjFgyUq9nRCYhaeP6JbUPN3Z5e1bZ3zpfBjV4ViE0zJOMB6NcEvYpy2jNR/8rwRoUGsFPq8//AkAklw18RJyJuqFugsUzPznQvad0IuNJV7jnsmJqo6ur6NUvef6NA7ugUalNv9+imINjChO8HRLRQfRGk6B0D/P3AkBt54UBMtFefOLXgUdilwLdCUSw4KpbuBPw+cyWlMjcXCkj4rHoeksekyBH1GrBJkLqDMRqtVQUubuFwSzBAtlc'
        # 票通公钥(票通提供)
        ptPublicKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCJkx3HelhEm/U7jOCor29oHsIjCMSTyKbX5rpoAY8KDIs9mmr5Y9r+jvNJH8pK3u5gNnvleT6rQgJQW1mk0zHuPO00vy62tSA53fkSjtM+n0oC1Fkm4DRFd5qJgoP7uFQHR5OEffMjy2qIuxChY4Au0kq+6RruEgIttb7wUxy8TwIDAQAB'
        # 3DES秘钥(票通提供)
        self.password = 'lsBnINDxtct8HZB7KCMyhWSJ'
        # 请更换请求平台简称(票通提供)
        self.platform_alias = 'DEMO'
        # 请更换请求平台编码(票通提供)
        self.platform_code = '11111111'
#------------------------------------------------------------------------------------------------------------------------------------------------------------------------>
        self.sellerAddress = dz#'销货方地址'
        self.sellerTel = dh#  '1304133333'
        self.sellerBankName = khh#'销方开户行'
        self.sellerBankAccount = yhzh# '11111111'#银行账户
        self.casherName = skr# '销方收款人姓名'
        self.reviewerName = fhr# '销方复核人姓名'
        self.drawerName = kpr#'开票人姓名'



    def kaip(self):
        self.url = "http://fpkj.testnw.vpiaotong.cn/tp/openapi/invoiceBlue.pt"
        self.huoqu_shuju() #获取数据在类内可以随时调用
        content = json.dumps(self.map)
        post = self.assembly(content)
        response = self.Httppost(self.url, post) #发送请求获取响应
        myres = json.loads(response) #转换数据
        print(f"状态码:"+ myres['code'], "状态:" + myres['msg'])
        # print(self.DES_Decrypt(self.password, myres['content']))#解密

    def chaxu_fp_hc(self):
        self.url = "http://fpkj.testnw.vpiaotong.cn/tp/openapi/queryInvoiceInfo.pt"
        self.gdinvoiceReqSerialNo = self.invoiceReqSerialNo()
        self.map = {'invoiceReqSerialNo' : 'DEMO1641194159978500','taxpayerNum':  self.taxpayerNum}
        content = json.dumps(self.map)
        post = self.assembly(content)
        response = self.Httppost(self.url, post)  # 发送请求获取响应
        myres = json.loads(response)  # 转换数据
        # print(myres)
        print(f"状态码:" + myres['code'], "状态:" + myres['msg'])
        fp = self.DES_Decrypt(self.password, myres['content'])#解密
        print(fp)

    def huoqu_shuju(self):
        self.SQL_sj = Ym_FP_sql.SQL_FP()  # 创建查询数据库实例

        self.gdinvoiceReqSerialNo = self.invoiceReqSerialNo()  # 获取流水号 保存为固定流水号

        self.dqdjbh = self.SQL_sj.SQL_fp_djbh()  # 获取当前订单ID
        self.SQL_sj.SQL_fp_djbh_lsh(self.dqdjbh,self.gdinvoiceReqSerialNo) #存入流水号
        self.buyerName = self.SQL_sj.SQL_fp_chauxn_khmc(self.dqdjbh, 'fp_khmc').replace('\n', '').replace('\r', '') # 客户名称
        self.buyerTaxpayerNum = self.SQL_sj.SQL_fp_chauxn_khmc(self.dqdjbh, 'fp_sh')  # 税号
        self.tradeNo = self.dqdjbh  # 订单编号
        self.buyerAddress = self.SQL_sj.SQL_fp_chauxn_khmc(self.dqdjbh, 'fp_cedz')  # 注册地址
        self.buyerBankName = self.SQL_sj.SQL_fp_chauxn_khmc(self.dqdjbh, 'fp_yhzh')  # 购放开户行
        self.buyerBankAccount = self.SQL_sj.SQL_fp_chauxn_khmc(self.dqdjbh, 'fp_khh')  # 购方开户行

        self.itemlist = self.SQL_sj.SQL_fp_spxxmx(self.dqdjbh)#通过单据编号获取发票详细信息

        self.map = {'taxpayerNum': self.taxpayerNum,
                    'invoiceReqSerialNo': self.gdinvoiceReqSerialNo,
                    'buyerName': self.buyerName,
                    'buyerTaxpayerNum': self.buyerTaxpayerNum,
                    'sellerAddress' : self.sellerAddress,
                    'sellerTel' : self.sellerTel,
                    'sellerBankName': self.sellerBankName,
                    'sellerBankAccount' : self.sellerBankAccount,
                    'casherName' : self.casherName,
                    'reviewerName':self.reviewerName,
                    'drawerName' : self.drawerName ,'itemList': self.itemlist}


        return self.dqdjbh

    def RSA_sign(self,data, privateKey):
        private_keyBytes = base64.b64decode(self.privateKey)
        priKey = RSA.importKey(private_keyBytes)
        signer = PKCS1_v1_5.new(priKey, )
        hash_obj = SHA1.new(data.encode('utf-8'))

        signature = base64.b64encode(signer.sign(hash_obj))
        signature = signature.decode('utf8')
        return signature

    # 生成请求流水号
    def invoiceReqSerialNo(self):
        return self.platform_alias + str(round(time.time() * 1000)) + str(random.randrange(100, 1000, 100))

    # http请求
    def Httppost(self,url, values):
        headers = {
            "Content-Type": "application/json; charset=UTF-8"
        }
        response = requests.post(url, data=json.dumps(values), headers=headers).text
        return response

    # 公共报文组装
    def assembly(self,content):
        # 格式化成2016-03-20 11:45:39形式
        timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

        serialNo = str(self.gdinvoiceReqSerialNo) + '_12345'

        contentstr = str(self.DES_Encode(self.password, content), 'utf-8')  # 加密位置
        wait_sign = 'content=' + str(
            contentstr) + '&format=JSON&platformCode=' + self.platform_code + '&serialNo=' + serialNo + '&signType=RSA&timestamp=' + timestamp + '&version=1.0'

        sign = self.RSA_sign(str(wait_sign), self.privateKey)

        postjson = {'platformCode': str(self.platform_code), 'signType': 'RSA', 'sign': str(sign), 'format': 'JSON',
                    'timestamp': str(timestamp), 'version': '1.0', 'serialNo': str(serialNo),
                    'content': contentstr}
        json.dumps(postjson)
        return postjson

    # 3des加密
    def DES_Encode(self,keys, encrypt):
        k = pyDes.triple_des(keys, pyDes.ECB, "\0\0\0\0\0\0\0\0", pad=None, padmode=pyDes.PAD_PKCS5)
        d = k.encrypt(encrypt)
        return base64.b64encode(d)
    # 3des解密
    def DES_Decrypt(self,keys, encrypt):
        k = pyDes.triple_des(keys, pyDes.ECB, "\0\0\0\0\0\0\0\0", pad=None, padmode=pyDes.PAD_PKCS5)
        encrypt = base64.b64decode(encrypt)
        d = k.decrypt(encrypt)
        return str(d, "utf-8")



if __name__ == '__main__':
    a = Dzfp_lp(1,1,1,1,1,1,1,1)
    a.chaxu_fp_hc()

###### 问题相关代码,请勿粘贴截图 

###### 运行结果及报错内容 

###### 我的解答思路和尝试过的方法 

###### 我想要达到的结果

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • CSDN专家-HGJ 2022-01-03 16:29
    最佳回答

    应该是图片格式为base64码,用如下代码转换成图片:

    import base64
    from PIL import Image,ImageTk
    from io import BytesIO,StringIO
    def cov2img(s: str):
        s = s.split(',')[-1]
        img = base64.b64decode(s)
        img = Image.open(BytesIO(img))
        return img
    
    

    如对你有帮助或启发,请点击采纳按钮。

    评论
    解决 无用
    打赏 举报 编辑记录

相关推荐 更多相似问题