2023-03-17 12:12:45 +00:00
|
|
|
from io import BytesIO
|
|
|
|
|
2022-08-10 15:12:43 +00:00
|
|
|
import qrcode
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
|
|
|
import cqwu
|
2023-03-08 12:48:12 +00:00
|
|
|
from cqwu.errors.auth import CookieError
|
2023-03-17 12:12:45 +00:00
|
|
|
from cqwu.errors.epay import EPayQrCodeError
|
2022-08-10 15:12:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GenPayQrcode:
|
|
|
|
async def gen_pay_qrcode(
|
|
|
|
self: "cqwu.Client",
|
2023-03-17 12:12:45 +00:00
|
|
|
show_qrcode: bool = True,
|
2022-08-10 15:12:43 +00:00
|
|
|
) -> None:
|
|
|
|
"""
|
|
|
|
生成支付二维码
|
|
|
|
"""
|
2023-03-08 12:48:12 +00:00
|
|
|
url = "http://218.194.176.214:8382/epay/thirdconsume/qrcode"
|
|
|
|
html = await self.oauth(url)
|
2022-08-10 15:12:43 +00:00
|
|
|
if not html:
|
2023-03-08 12:48:12 +00:00
|
|
|
raise CookieError()
|
|
|
|
if html.url != url:
|
|
|
|
raise CookieError()
|
2022-08-10 15:12:43 +00:00
|
|
|
soup = BeautifulSoup(html.text, "lxml")
|
|
|
|
try:
|
|
|
|
data = soup.find("input", attrs={"id": "myText"})["value"]
|
|
|
|
except (ValueError, TypeError, KeyError, IndexError):
|
|
|
|
return
|
|
|
|
qr = qrcode.QRCode()
|
|
|
|
qr.add_data(data)
|
|
|
|
img = qrcode.make(data)
|
2023-03-17 12:12:45 +00:00
|
|
|
if not show_qrcode:
|
|
|
|
img_bytes = BytesIO()
|
|
|
|
img.save(img_bytes)
|
|
|
|
img_bytes.seek(0)
|
|
|
|
raise EPayQrCodeError(img_bytes.getvalue())
|
|
|
|
qr.print_ascii(invert=True)
|
2022-08-10 15:12:43 +00:00
|
|
|
img.save("qrcode.png")
|
|
|
|
print("生成支付码到 qrcode.png 成功,请打开该文件查看")
|