优化服务号订单广播(Celery并发)与群聊状态实时推送

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
XingQue
2026-06-24 00:43:30 +08:00
parent 56c66846cc
commit 49ba67ecaf
9 changed files with 458 additions and 331 deletions

View File

@@ -3272,48 +3272,25 @@ class KehuTianxieDingdanView(APIView):
}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def send_order_notification_async(self, dingdan, youxi_nicheng, zhiding_dashou, zhuangtai):
"""异步发送订单通知(客户填写订单后)"""
"""客户填写订单后 — Celery 广播服务号模板消息"""
try:
# 避免阻塞主线程,使用线程异步执行
def notification_task():
try:
# 获取游戏类型名称
try:
leixing_obj = ShangpinLeixing.objects.get(id=dingdan.leixing_id)
game_type = leixing_obj.jieshao
except:
game_type = '未知游戏'
# 构建订单信息
order_info = {
'dingdan_id': dingdan.dingdan_id,
'game_type': game_type,
'amount': str(dingdan.jine),
'order_desc': dingdan.jieshao[:50] if dingdan.jieshao else '新订单',
'youxi_nicheng': youxi_nicheng,
'order_type': '指定订单' if zhuangtai == 7 else '普通订单',
'zhiding_dashou': zhiding_dashou or ''
}
# 调用微信广播发送器
sender = WeixinBroadcastSender()
result = sender.broadcast_order(order_info)
if result['success']:
logger.info(f"【客户填写订单】微信通知发送成功: {result['msg']}")
else:
logger.warning(f"【客户填写订单】微信通知发送失败: {result['msg']}")
except Exception as e:
logger.error(f"【客户填写订单】发送通知任务异常: {str(e)}")
# 启动异步线程
thread = threading.Thread(target=notification_task)
thread.daemon = True
thread.start()
from utils.order_broadcast import submit_dingdan_guangbo
try:
leixing_obj = ShangpinLeixing.objects.get(id=dingdan.leixing_id)
game_type = leixing_obj.jieshao
except Exception:
game_type = '未知游戏'
order_info = {
'dingdan_id': dingdan.dingdan_id,
'game_type': game_type,
'amount': str(dingdan.jine),
'order_desc': dingdan.jieshao[:50] if dingdan.jieshao else '新订单',
'order_type': '指定订单' if zhuangtai == 7 else '普通订单',
}
submit_dingdan_guangbo(order_info)
except Exception as e:
logger.error(f"【客户填写订单】启动通知线程异常: {str(e)}")
logger.error(f'【客户填写订单】提交广播失败: {e}', exc_info=True)