This commit is contained in:
aaron 2025-02-01 17:49:03 +08:00
parent b4fda2abc1
commit 666896047e
6 changed files with 18 additions and 13 deletions

View File

@ -21,6 +21,8 @@ from app.models.point import PointRecordDB
from app.core.account import AccountManager
from app.core.wechat import WeChatClient
from pydantic import BaseModel
from app.core.config import settings
router = APIRouter()
@router.post("", response_model=ResponseModel)
@ -77,6 +79,7 @@ async def create_merchant_order(
merchant_product_id=order.merchant_product_id,
order_amount=original_amount,
pay_amount=pay_amount,
gift_points=float(product.sale_price) * settings.POINT_RATIO,
status=MerchantOrderStatus.CREATED,
order_verify_code=verify_code
)
@ -357,14 +360,13 @@ async def calculate_order_price(
product = db.query(MerchantProductDB).filter(
MerchantProductDB.id == order.merchant_product_id
).first()
if not product:
return error_response(code=404, message="商品不存在")
return success_response(data={
"original_price": float(product.sale_price),
"final_amount": product.sale_price
"gift_points": float(product.sale_price) * settings.POINT_RATIO,
"amount": product.sale_price
})
class RefundRequest(BaseModel):

View File

@ -14,6 +14,7 @@ from app.api.deps import get_current_user, get_admin_user
from app.models.user import UserDB
from app.core.response import success_response, error_response, ResponseModel
from datetime import datetime, timezone
from app.core.config import settings
router = APIRouter()
@ -39,7 +40,8 @@ async def create_pay_order(
order_id=order_id,
merchant_id=order.merchant_id,
user_id=current_user.userid,
amount=order.amount
amount=order.amount,
gift_points=order.amount * settings.POINT_RATIO
)
try:

View File

@ -164,7 +164,8 @@ async def create_payment(
result = await wechat.create_jsapi_payment(
openid=current_user.openid,
out_trade_no=request.order_id,
total_amount=int(float(amount) * 100), # 转换为分
# total_amount=int(float(amount) * 100), # 转换为分
total_amount=int(1), # 测试 1 分钱
description=description
)
@ -232,16 +233,14 @@ async def payment_notify(
if trade_state == "SUCCESS":
# 添加积分
points = order.pay_amount * 10
point_manager = PointManager(db)
point_manager.add_points(order.user_id, points, f"订单完成,获得{settings.POINT_ALIAS} 奖励")
point_manager.add_points(order.user_id, order.gift_points, PointRecordType.CONSUME_RETURN, f"团购订单奖励", order.order_id)
# 更新订单状态
order.pay_status = True
order.status = MerchantOrderStatus.UNVERIFIED
order.transaction_id = transaction_id
order.pay_time = datetime.fromisoformat(success_time.replace('Z', '+00:00'))
order.gift_points = points
elif out_trade_no.startswith("P"): # 商家在线买单
order = db.query(MerchantPayOrderDB).filter(
@ -253,16 +252,14 @@ async def payment_notify(
if trade_state == "SUCCESS":
# 添加积分
points = order.amount * 10
point_manager = PointManager(db)
point_manager.add_points(order.user_id, points, f"订单完成,获得{settings.POINT_ALIAS} 奖励")
point_manager.add_points(order.user_id, order.gift_points, PointRecordType.CONSUME_RETURN, f"买单订单奖励", order.order_id)
# 更新订单状态
order.pay_status = True
order.status = MerchantPayOrderStatus.PAID
order.transaction_id = transaction_id
order.pay_time = datetime.fromisoformat(success_time.replace('Z', '+00:00'))
order.gift_points = points
else:
return error_response(code=400, message="未知的订单类型")

View File

@ -10,6 +10,7 @@ class Settings(BaseSettings):
# 积分别名
POINT_ALIAS: str = "蜂蜜"
POINT_RATIO: float = 10.0 # 积分兑换比例
# 订单价格配置
ORDER_BASE_PRICE: float = 3.0 # 基础费用

View File

@ -32,6 +32,7 @@ class MerchantDB(Base):
longitude = Column(DECIMAL(9, 6), nullable=False) # 经度精确到小数点后6位
latitude = Column(DECIMAL(9, 6), nullable=False) # 纬度精确到小数点后6位
phone = Column(String(20), nullable=False)
brand_image_url = Column(String(200)) # 品牌图片地址
create_time = Column(DateTime(timezone=True), server_default=func.now())
update_time = Column(DateTime(timezone=True), onupdate=func.now())
category_id = Column(Integer, ForeignKey("merchant_categories.id"), nullable=True)
@ -57,6 +58,7 @@ class MerchantCreate(BaseModel):
longitude: float = Field(..., ge=-180, le=180, description="经度")
latitude: float = Field(..., ge=-90, le=90, description="纬度")
phone: str = Field(..., max_length=20, pattern=r'^\d+$')
brand_image_url: Optional[str] = Field(None, max_length=200)
images: List[MerchantImage] = []
category_id: Optional[int] = None
@ -68,6 +70,7 @@ class MerchantUpdate(BaseModel):
longitude: Optional[float] = Field(None, ge=-180, le=180, description="经度")
latitude: Optional[float] = Field(None, ge=-90, le=90, description="纬度")
phone: Optional[str] = Field(None, max_length=20, pattern=r'^\d+$')
brand_image_url: Optional[str] = Field(None, max_length=200)
images: Optional[List[MerchantImage]] = None
category_id: Optional[int] = None
@ -83,6 +86,7 @@ class MerchantInfo(BaseModel):
longitude: float
latitude: float
phone: str
brand_image_url: Optional[str] = None
images: List[MerchantImage]
create_time: datetime
update_time: Optional[datetime]

View File

@ -38,7 +38,6 @@ class MerchantOrderDB(Base):
class MerchantOrderCreate(BaseModel):
merchant_product_id: int
order_amount: float = Field(..., gt=0)
class MerchantOrderInfo(BaseModel):
id: int