Kamixitong/app/utils/license_generator.py
2025-12-12 11:35:14 +08:00

121 lines
3.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
"""
许可证生成工具类
"""
from app import db
from app.models import License, Product, Package
from datetime import datetime, timedelta
import secrets
import string
class LicenseGenerator:
"""许可证生成器"""
def __init__(self):
"""初始化"""
pass
def generate_license_key(self, length=32, prefix=''):
"""
生成卡密格式XXXX-XXXX-XXXX-XXXX
:param length: 卡密长度
:param prefix: 卡密前缀
:return: 生成的卡密
"""
# 生成随机字符串
chars = string.ascii_uppercase + string.digits
# 生成32位字符4组每组8位
random_chars = ''.join(secrets.choice(chars) for _ in range(32 - len(prefix)))
# 格式化为XXXX-XXXX-XXXX-XXXX格式
formatted_key = '-'.join([
random_chars[i:i+8] for i in range(0, len(random_chars), 8)
])
# 组合前缀和格式化后的密钥
license_key = prefix + formatted_key if prefix else formatted_key
# 确保唯一性
while License.query.filter_by(license_key=license_key).first():
random_chars = ''.join(secrets.choice(chars) for _ in range(32 - len(prefix)))
formatted_key = '-'.join([
random_chars[i:i+8] for i in range(0, len(random_chars), 8)
])
license_key = prefix + formatted_key if prefix else formatted_key
return license_key
def generate_license(self, product_id, package_id, contact_person, phone, quantity=1, license_type=1):
"""
生成许可证
:param product_id: 产品ID
:param package_id: 套餐ID
:param contact_person: 联系人
:param phone: 手机号
:param quantity: 数量
:param license_type: 许可证类型 (0=试用, 1=正式)
:return: 生成的许可证密钥
"""
# 查询产品和套餐
product = Product.query.filter_by(product_id=product_id).first()
if not product:
raise ValueError(f"产品不存在: {product_id}")
package = Package.query.filter_by(package_id=package_id).first()
if not package:
raise ValueError(f"套餐不存在: {package_id}")
# 计算有效期
if package.duration == -1: # 永久卡
expire_time = None
else:
expire_time = datetime.utcnow() + timedelta(days=package.duration)
# 生成许可证密钥
license_key = self.generate_license_key()
# 创建许可证记录
license_obj = License(
license_key=license_key,
product_id=product_id,
package_id=package_id,
contact_person=contact_person,
phone=phone,
license_type=license_type,
expire_time=expire_time,
max_devices=package.max_devices,
status=1 # 启用
)
# 保存到数据库
db.session.add(license_obj)
db.session.commit()
return license_key
def generate_batch(self, product_id, package_id, contact_person, phone, count=1, license_type=1):
"""
批量生成许可证
:param product_id: 产品ID
:param package_id: 套餐ID
:param contact_person: 联系人
:param phone: 手机号
:param count: 数量
:param license_type: 许可证类型
:return: 生成的许可证密钥列表
"""
license_keys = []
for _ in range(count):
license_key = self.generate_license(
product_id=product_id,
package_id=package_id,
contact_person=contact_person,
phone=phone,
quantity=1,
license_type=license_type
)
license_keys.append(license_key)
return license_keys