feat: 优化ws event ticket remind三种方法

This commit is contained in:
caoqianming 2023-09-05 11:12:13 +08:00
parent 77e92062fb
commit 28313b0c33
3 changed files with 43 additions and 6 deletions

View File

@ -25,6 +25,7 @@ from apps.ai.main import algo_dict
from datetime import datetime
import uuid
from apps.utils.img import compress_image
from apps.ecm.serializers import EventSerializer
@shared_task(base=CustomTask)
def store_img(code: str, duration: int):
@ -205,10 +206,27 @@ def compressed_all_ecm_image():
event.save()
@shared_task(base=CustomTask)
def remind_push(userId: str):
def remind_push(remindId: str):
from apps.ecm.models import Remind
from apps.ecm.serializers import RemindSerializer
remind = Remind.objects.get(id=remindId)
channel_layer = get_channel_layer()
data = {
'type': 'remind',
'remind': RemindSerializer(instance=remind).data,
'msg': ''
}
async_to_sync(channel_layer.group_send)(f"user_{remind.recipient.id}", data)
@shared_task(base=CustomTask)
def event_push(eventId: str):
event = Event.objects.get(id=eventId)
channel_layer = get_channel_layer()
data = {
'type': 'event',
'event': EventSerializer(instance=event).data,
'msg': ''
}
async_to_sync(channel_layer.group_send)(f"user_{userId}", data)
async_to_sync(channel_layer.group_send)('event', data)

View File

@ -8,6 +8,7 @@ from apps.utils.sms import send_sms
from apps.utils.tasks import CustomTask
from celery import shared_task
from apps.wf.models import State, Ticket, TicketFlow, Transition
from apps.wf.serializers import TicketDetailSerializer
import time
from apps.utils.tasks import send_mail_task
from channels.layers import get_channel_layer
@ -16,10 +17,12 @@ from asgiref.sync import async_to_sync
myLogger = logging.getLogger('log')
@shared_task(base=CustomTask)
def ticket_push(userId):
def ticket_push(ticketId, userId):
ticket = Ticket.objects.get(id=ticketId)
channel_layer = get_channel_layer()
data = {
'type': 'ticket',
'ticket': TicketDetailSerializer(instance=ticket).data,
'msg': ''
}
async_to_sync(channel_layer.group_send)(f"user_{userId}", data)
@ -36,13 +39,13 @@ def send_ticket_notice(ticket_id):
# ws推送
# 发送短信通知
pt = User.objects.filter(id=ticket.participant).first()
ticket_push.delay(pt.id)
ticket_push.delay(ticket.id, pt.id)
if pt and pt.phone:
send_sms(pt.phone, 1002, params)
elif ticket.participant_type == 2:
pts = User.objects.filter(id__in=ticket.participant)
for i in pts:
ticket_push.delay(pt.id)
ticket_push.delay(ticket.id, i.id)
if i.phone:
send_sms(i.phone, 1002, params)

View File

@ -58,22 +58,38 @@ class MyConsumer(AsyncWebsocketConsumer):
await self.channel_layer.group_send(
self.room_group_name,
{
'type': 'event',
'type': 'remind',
'msg': '你好,' + self.scope['user'].username,
'from': '系统'
}
)
await self.accept()
async def receive(self, text_data=None, bytes_data=None):
if text_data:
content = json.loads(text_data)
if content['type'] == 'event':
await self.channel_layer.group_add(
'event',
self.channel_name
)
async def disconnect(self, close_code):
# Leave room group
await self.channel_layer.group_discard(
self.room_group_name,
self.channel_name
)
await self.channel_layer.group_discard(
'event',
self.channel_name
)
async def event(self, content):
await self.send(json.dumps(content, ensure_ascii=False))
async def ticket(self, content):
await self.send(json.dumps(content, ensure_ascii=False))
async def remind(self, content):
await self.send(json.dumps(content, ensure_ascii=False))