增加商家订单的模板消息发送

This commit is contained in:
aaron 2025-03-23 10:10:37 +08:00
parent d2edbceb5b
commit a99e3a254c
4 changed files with 42 additions and 0 deletions

View File

@ -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()

View File

@ -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"
# 商家投诉企业微信

View File

@ -19,3 +19,19 @@ async def sent_order_status_change_message(openid: str, orderid: str, template_i
)
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)}")

Binary file not shown.