对图片进行缩放裁剪
This commit is contained in:
parent
5099de621c
commit
dc9c44b626
@ -205,7 +205,7 @@ async def list_merchants(
|
||||
merchant_products[merchant_id] = {
|
||||
"product_id": product.id,
|
||||
"product_name": product.name,
|
||||
"product_image": product.image_url,
|
||||
"product_image": product.optimized_image_url,
|
||||
"product_price": float(product.sale_price),
|
||||
"purchase_limit": product.purchase_limit
|
||||
}
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
from datetime import datetime
|
||||
import time
|
||||
from typing import Optional
|
||||
|
||||
class CommonUtils:
|
||||
"""订单工具类"""
|
||||
"""工具类"""
|
||||
|
||||
@staticmethod
|
||||
def generate_order_id(prefix: str) -> str:
|
||||
@ -26,4 +27,25 @@ class CommonUtils:
|
||||
now = datetime.now()
|
||||
date_str = now.strftime('%Y%m%d')
|
||||
timestamp = str(int(time.time() * 1000))[-8:]
|
||||
return f"{date_str}{timestamp}"
|
||||
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}"
|
||||
@ -5,6 +5,7 @@ from pydantic import BaseModel, Field
|
||||
from typing import Optional, List
|
||||
from datetime import datetime
|
||||
from .database import Base
|
||||
from app.core.utils import CommonUtils
|
||||
|
||||
# 数据库模型
|
||||
class MerchantDB(Base):
|
||||
@ -25,6 +26,10 @@ class MerchantDB(Base):
|
||||
update_time = Column(DateTime(timezone=True), onupdate=func.now())
|
||||
category_id = Column(Integer, ForeignKey("merchant_categories.id"), nullable=True)
|
||||
|
||||
@property
|
||||
def optimized_brand_image_url(self):
|
||||
return CommonUtils.optimized_image_url(self.brand_image_url)
|
||||
|
||||
class MerchantCreate(BaseModel):
|
||||
user_id: int
|
||||
name: str = Field(..., max_length=100)
|
||||
@ -66,6 +71,7 @@ class MerchantInfo(BaseModel):
|
||||
pay_gift_points_rate: float
|
||||
pay_share_rate: float
|
||||
brand_image_url: Optional[str] = None
|
||||
optimized_brand_image_url: Optional[str] = None
|
||||
create_time: datetime
|
||||
update_time: Optional[datetime]
|
||||
distance: Optional[int] = None # 距离(米)
|
||||
|
||||
@ -6,6 +6,7 @@ from typing import Optional, List
|
||||
from datetime import datetime
|
||||
from .database import Base
|
||||
import enum
|
||||
from app.core.utils import CommonUtils
|
||||
|
||||
|
||||
class ProductStatus(str, enum.Enum):
|
||||
@ -30,6 +31,9 @@ class MerchantProductDB(Base):
|
||||
update_time = Column(DateTime(timezone=True), onupdate=func.now())
|
||||
status = Column(Enum(ProductStatus), nullable=False, default=ProductStatus.UNLISTING)
|
||||
|
||||
@property
|
||||
def optimized_image_url(self):
|
||||
return CommonUtils.optimized_image_url(self.image_url)
|
||||
|
||||
# Pydantic 模型
|
||||
class MerchantProductCreate(BaseModel):
|
||||
@ -63,6 +67,7 @@ class MerchantProductInfo(BaseModel):
|
||||
merchant_name: Optional[str] = None # 添加商家名称字段
|
||||
name: str
|
||||
image_url: str
|
||||
optimized_image_url: Optional[str] = None
|
||||
product_price: float
|
||||
sale_price: float
|
||||
settlement_amount: float
|
||||
|
||||
@ -5,6 +5,7 @@ from sqlalchemy.sql import func
|
||||
from pydantic import BaseModel, Field
|
||||
from .database import Base
|
||||
import enum
|
||||
from app.core.utils import CommonUtils
|
||||
|
||||
class OrderStatus(str, enum.Enum):
|
||||
CREATED = "CREATED" # 已创建
|
||||
@ -47,6 +48,12 @@ class ShippingOrderDB(Base):
|
||||
pay_time = Column(DateTime(timezone=True)) # 支付时间
|
||||
transaction_id = Column(String(64)) # 微信支付交易号
|
||||
|
||||
@property
|
||||
def optimized_complete_images(self):
|
||||
if self.complete_images:
|
||||
return [CommonUtils.optimized_image_url(image) for image in self.complete_images.split(",")]
|
||||
return []
|
||||
|
||||
class ShippingOrderPackageDB(Base):
|
||||
__tablename__ = "shipping_order_packages"
|
||||
|
||||
@ -86,6 +93,7 @@ class OrderInfo(BaseModel):
|
||||
final_amount: float
|
||||
status: OrderStatus
|
||||
complete_images: Optional[List[str]] = None
|
||||
optimized_complete_images: Optional[List[str]] = None
|
||||
create_time: datetime
|
||||
delivery_method: DeliveryMethod
|
||||
deliveryman_user_id: Optional[int] = None
|
||||
|
||||
Loading…
Reference in New Issue
Block a user