diff --git a/app/api/endpoints/merchant_order.py b/app/api/endpoints/merchant_order.py index 653afaa..3979767 100644 --- a/app/api/endpoints/merchant_order.py +++ b/app/api/endpoints/merchant_order.py @@ -33,10 +33,13 @@ from app.models.merchant_order import MerchantOrderVerify from app.core.point_manager import PointManager, PointRecordType from app.models.merchant_product import DeliveryType, DeliveryTimeType from datetime import timedelta +from fastapi import BackgroundTasks +from app.core.mpmessage import sent_merchant_order_status_change_message router = APIRouter() @router.post("", response_model=ResponseModel) async def create_merchant_order( + background_tasks: BackgroundTasks, order: MerchantOrderCreate, db: Session = Depends(get_db), current_user: UserDB = Depends(get_current_user) @@ -90,6 +93,25 @@ async def create_merchant_order( db.add(db_order) db.commit() db.refresh(db_order) + + # 发送商家订单创建成功消息 + if current_user.mp_openid: + data={ + "character_string1": order_id, + "thing2": product.name, + "amount3": pay_amount, + "time13": datetime.now().strftime("%Y-%m-%d %H:%M:%S") + } + + background_tasks.add_task( + sent_merchant_order_status_change_message, + openid=current_user.mp_openid, + template_id=settings.MERCHANT_ORDER_CREATED_TEMPLATE_ID, + data=data, + orderid=order_id + ) + + return success_response(data=MerchantOrderInfo.model_validate(db_order)) except Exception as e: db.rollback() diff --git a/app/core/config.py b/app/core/config.py index 81fe69f..90195da 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -106,6 +106,10 @@ class Settings(BaseSettings): # 加价请求 ADDITIONAL_FEE_REQUEST_TEMPLATE_ID: str = "oGOuil8q1vSXiIyzJ14ExSOSptVgE4A-b7WSmmGqiRw" + + # 商家订单创建成功 + MERCHANT_ORDER_CREATED_TEMPLATE_ID: str = "tyB7fncVCdEyNNCm-XUUWQgDmGnnGT_WORPbHsc_89k" + # 反馈需求企业微信 FEEDBACK_NEED_WECOM_BOT_WEBHOOK_URL: str = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=ccd6e8d4-4c8a-45b4-9b6b-dd4cae563176" # 商家投诉企业微信 diff --git a/app/core/mpmessage.py b/app/core/mpmessage.py index e656fd0..3128dc9 100644 --- a/app/core/mpmessage.py +++ b/app/core/mpmessage.py @@ -17,5 +17,21 @@ async def sent_order_status_change_message(openid: str, orderid: str, template_i "pagepath": f"/pages/order/detail/index?id={orderid}" } ) + except Exception as e: + logging.exception(f"发送模板消息失败: {str(e)}") + + +# 发送商家订单状态改变消息 +async def sent_merchant_order_status_change_message(openid: str, orderid: str, template_id: str, data: dict): + try: + await mp_client.send_template_message( + openid=openid, + template_id=template_id, + data=data, + miniprogram={ + "appid": settings.WECHAT_APPID, + "pagepath": f"/pages/merchant_order/detail/index?id={orderid}" + } + ) except Exception as e: logging.exception(f"发送模板消息失败: {str(e)}") \ No newline at end of file diff --git a/jobs.sqlite b/jobs.sqlite index 7c03c4e..775c7b5 100644 Binary files a/jobs.sqlite and b/jobs.sqlite differ