This commit is contained in:
54
backend/shop/signals.py
Normal file
54
backend/shop/signals.py
Normal file
@@ -0,0 +1,54 @@
|
||||
from django.db.models.signals import pre_save, post_save
|
||||
from django.dispatch import receiver
|
||||
from .models import Order
|
||||
from .sms_utils import notify_admins_order_paid, notify_user_order_paid, notify_user_order_shipped
|
||||
|
||||
@receiver(pre_save, sender=Order)
|
||||
def track_order_changes(sender, instance, **kwargs):
|
||||
"""
|
||||
在保存之前检查状态变化
|
||||
"""
|
||||
if instance.pk:
|
||||
try:
|
||||
old_instance = Order.objects.get(pk=instance.pk)
|
||||
|
||||
# 检查是否从非支付状态变为支付状态
|
||||
if old_instance.status != 'paid' and instance.status == 'paid':
|
||||
instance._was_paid = True
|
||||
|
||||
# 检查是否发货 (状态变为 shipped 且有单号)
|
||||
# 或者已经是 shipped 状态但刚填入单号
|
||||
if instance.status == 'shipped' and instance.tracking_number:
|
||||
if old_instance.status != 'shipped' or not old_instance.tracking_number:
|
||||
instance._was_shipped = True
|
||||
|
||||
except Order.DoesNotExist:
|
||||
pass
|
||||
|
||||
@receiver(post_save, sender=Order)
|
||||
def send_order_notifications(sender, instance, created, **kwargs):
|
||||
"""
|
||||
在保存之后发送通知
|
||||
"""
|
||||
if created:
|
||||
return
|
||||
|
||||
# 1. 处理支付成功通知
|
||||
if getattr(instance, '_was_paid', False):
|
||||
try:
|
||||
print(f"订单 {instance.id} 支付成功,触发短信通知流程...")
|
||||
notify_admins_order_paid(instance)
|
||||
notify_user_order_paid(instance)
|
||||
# 清除标记防止重复发送 (虽然实例通常是新的,但保险起见)
|
||||
instance._was_paid = False
|
||||
except Exception as e:
|
||||
print(f"发送支付成功短信失败: {str(e)}")
|
||||
|
||||
# 2. 处理发货通知
|
||||
if getattr(instance, '_was_shipped', False):
|
||||
try:
|
||||
print(f"订单 {instance.id} 已发货,触发短信通知流程...")
|
||||
notify_user_order_shipped(instance)
|
||||
instance._was_shipped = False
|
||||
except Exception as e:
|
||||
print(f"发送发货短信失败: {str(e)}")
|
||||
Reference in New Issue
Block a user