deliveryman-api/app/core/utils.py
2025-02-06 23:20:46 +09:00

51 lines
1.6 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.

from datetime import datetime
import time
from typing import Optional
class CommonUtils:
"""工具类"""
@staticmethod
def generate_order_id(prefix: str) -> str:
"""
生成订单号
:param prefix: 订单前缀,如 M(商品订单)、P(支付订单)、D(配送订单)
:return: 15位订单号前缀(1位) + 日期(8位) + 时间戳(6位)
"""
now = datetime.now()
date_str = now.strftime('%Y%m%d')
# 取时间戳后6位
timestamp = str(int(time.time() * 1000))[-6:]
return f"{prefix}{date_str}{timestamp}"
@staticmethod
def generate_verify_code() -> str:
"""
生成核销码
:return: 16位数字核销码日期(8位) + 时间戳(8位)
"""
now = datetime.now()
date_str = now.strftime('%Y%m%d')
timestamp = str(int(time.time() * 1000))[-8:]
return f"{date_str}{timestamp}"
@staticmethod
def optimized_image_url(url: Optional[str], quality: int = 60) -> Optional[str]:
"""
为图片URL添加腾讯云图片处理参数
:param url: 原始图片URL
:param quality: 图片质量范围1-100默认80
:return: 处理后的URL
"""
if not url:
return url
# 确保quality在有效范围内
quality = max(1, min(100, quality))
# 如果URL已经包含图片处理参数则不重复添加
if "?imageMogr2/quality/" in url:
return url
# 添加图片处理参数
return f"{url}?imageMogr2/quality/{quality}"