ForcePilot/backend/package/yuxi/channel/security/qr_binding.py
Kris bab30f2715
Some checks failed
Deploy VitePress site to Pages / build (push) Has been cancelled
Ruff Format Check / Ruff Format & Lint (push) Has been cancelled
Deploy VitePress site to Pages / Deploy (push) Has been cancelled
feat:0715
2026-07-15 12:30:58 +08:00

49 lines
1.3 KiB
Python

"""多渠道网关配对二维码生成工具。"""
from __future__ import annotations
import io
import qrcode
class PairingQRGenerator:
def __init__(self, base_url: str | None = None):
self.base_url = base_url
def generate_content(
self,
code: str,
channel_type: str,
account_id: str,
peer_id: str,
mode: str = "plain",
) -> str:
"""生成二维码编码字符串。"""
if mode == "plain":
return code
base = self.base_url or ""
if not base:
return code
return f"{base}/pairing?code={code}&channel={channel_type}&account={account_id}&peer_id={peer_id}"
def generate_image(
self,
content: str,
box_size: int = 10,
border: int = 2,
) -> bytes:
"""生成 PNG 格式二维码图片字节。"""
qr = qrcode.QRCode(
version=None,
error_correction=qrcode.constants.ERROR_CORRECT_M,
box_size=box_size,
border=border,
)
qr.add_data(content)
qr.make(fit=True)
image = qr.make_image(fill_color="black", back_color="white")
buffer = io.BytesIO()
image.save(buffer, format="PNG")
return buffer.getvalue()