Fix API compatibility and add user/role/permission and asset import/export
This commit is contained in:
86
backend_new/app/utils/qrcode.py
Normal file
86
backend_new/app/utils/qrcode.py
Normal file
@@ -0,0 +1,86 @@
|
||||
"""
|
||||
二维码生成工具
|
||||
"""
|
||||
import os
|
||||
import qrcode
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from app.core.config import settings
|
||||
|
||||
|
||||
def generate_qr_code(asset_code: str, save_path: str = None) -> str:
|
||||
"""
|
||||
生成资产二维码
|
||||
|
||||
Args:
|
||||
asset_code: 资产编码
|
||||
save_path: 保存路径(可选)
|
||||
|
||||
Returns:
|
||||
二维码文件相对路径
|
||||
"""
|
||||
# 如果未指定保存路径,使用默认路径
|
||||
if not save_path:
|
||||
qr_dir = Path(settings.QR_CODE_DIR)
|
||||
else:
|
||||
qr_dir = Path(save_path)
|
||||
|
||||
# 确保目录存在
|
||||
qr_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# 生成文件名
|
||||
filename = f"{asset_code}.png"
|
||||
file_path = qr_dir / filename
|
||||
|
||||
# 创建二维码
|
||||
qr = qrcode.QRCode(
|
||||
version=1,
|
||||
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
||||
box_size=10,
|
||||
border=settings.QR_CODE_BORDER,
|
||||
)
|
||||
qr.add_data(asset_code)
|
||||
qr.make(fit=True)
|
||||
|
||||
# 生成图片
|
||||
img = qr.make_image(fill_color="black", back_color="white")
|
||||
|
||||
# 保存文件
|
||||
img.save(str(file_path))
|
||||
|
||||
# 返回相对路径
|
||||
return f"{settings.QR_CODE_DIR}/{filename}"
|
||||
|
||||
|
||||
def get_qr_code_url(asset_code: str) -> str:
|
||||
"""
|
||||
获取二维码URL
|
||||
|
||||
Args:
|
||||
asset_code: 资产编码
|
||||
|
||||
Returns:
|
||||
二维码URL
|
||||
"""
|
||||
filename = f"{asset_code}.png"
|
||||
return f"/static/{settings.QR_CODE_DIR}/{filename}"
|
||||
|
||||
|
||||
def delete_qr_code(asset_code: str) -> bool:
|
||||
"""
|
||||
删除二维码文件
|
||||
|
||||
Args:
|
||||
asset_code: 资产编码
|
||||
|
||||
Returns:
|
||||
是否删除成功
|
||||
"""
|
||||
try:
|
||||
file_path = Path(settings.QR_CODE_DIR) / f"{asset_code}.png"
|
||||
if file_path.exists():
|
||||
file_path.unlink()
|
||||
return True
|
||||
return False
|
||||
except Exception:
|
||||
return False
|
||||
Reference in New Issue
Block a user