forked from erp-dev/erp
feat: wecom robot markdown message support
This commit is contained in:
@@ -11,7 +11,7 @@ class PrintingConfig(AppConfig):
|
||||
|
||||
def ready(self):
|
||||
"""注册信号处理函数"""
|
||||
from stateflow.signals import process_completed
|
||||
from stateflow.signals import process_completed, state_advanced
|
||||
from .models import PrintingJob
|
||||
from . import handlers
|
||||
|
||||
@@ -20,8 +20,18 @@ class PrintingConfig(AppConfig):
|
||||
handlers.on_printing_job_process_completed,
|
||||
sender=PrintingJob
|
||||
)
|
||||
|
||||
# 监听 PrintingJob 状态推进信号(每次推进都会触发)
|
||||
state_advanced.connect(
|
||||
handlers.on_printing_job_state_advanced,
|
||||
sender=PrintingJob
|
||||
)
|
||||
|
||||
logger.info(
|
||||
f'[printing.apps] 已注册 process_completed 信号处理器, '
|
||||
f'sender={PrintingJob}, handler={handlers.on_printing_job_process_completed}'
|
||||
)
|
||||
logger.info(
|
||||
f'[printing.apps] 已注册 state_advanced 信号处理器, '
|
||||
f'sender={PrintingJob}, handler={handlers.on_printing_job_state_advanced}'
|
||||
)
|
||||
@@ -8,6 +8,7 @@ import logging
|
||||
from decimal import Decimal, InvalidOperation
|
||||
from django.conf import settings
|
||||
from django.db import transaction
|
||||
from django.utils import timezone
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -149,3 +150,77 @@ def on_printing_job_process_completed(sender, **kwargs):
|
||||
f'PrintingJob #{printing_job.id} 流程完成后创建销售品失败: {e}',
|
||||
exc_info=True
|
||||
)
|
||||
|
||||
|
||||
def on_printing_job_state_advanced(sender, **kwargs):
|
||||
"""
|
||||
PrintingJob 每次状态推进时的处理(监听 stateflow.signals.state_advanced)。
|
||||
|
||||
需求:
|
||||
- 仅监听 PrintingJob
|
||||
- 在事务提交后发送企业微信机器人通知(markdown)
|
||||
- 消息包含:
|
||||
- PrintingOrder.id
|
||||
- PrintingJob.id
|
||||
- 被推进状态名 state.name
|
||||
- 推进时间(state_log.completed_at)
|
||||
"""
|
||||
# 测试环境默认关闭,避免单测出网/刷屏
|
||||
if not getattr(settings, 'PRINTING_JOB_STATE_ADVANCED_WECOM_NOTIFY_ENABLED', True):
|
||||
return
|
||||
if getattr(settings, 'TESTING', False):
|
||||
return
|
||||
|
||||
content_object = kwargs.get('content_object')
|
||||
state = kwargs.get('state')
|
||||
state_log = kwargs.get('state_log')
|
||||
if content_object is None or state is None or state_log is None:
|
||||
logger.warning('[printing.handlers] state_advanced 信号缺少关键参数,跳过通知')
|
||||
return
|
||||
|
||||
printing_job = content_object
|
||||
printing_job_id = getattr(printing_job, 'id', None)
|
||||
printing_order = getattr(printing_job, 'printing_order', None)
|
||||
printing_order_id = getattr(printing_order, 'id', None)
|
||||
|
||||
state_name = getattr(state, 'name', None) or str(state)
|
||||
advanced_at_dt = getattr(state_log, 'completed_at', None) or getattr(state_log, 'created_at', None)
|
||||
advanced_at = (
|
||||
timezone.localtime(advanced_at_dt).strftime('%Y-%m-%d %H:%M:%S')
|
||||
if advanced_at_dt is not None
|
||||
else timezone.localtime(timezone.now()).strftime('%Y-%m-%d %H:%M:%S')
|
||||
)
|
||||
|
||||
template = getattr(
|
||||
settings,
|
||||
'PRINTING_JOB_STATE_ADVANCED_WECOM_MARKDOWN_TEMPLATE',
|
||||
(
|
||||
"### PrintingJob 状态推进\n"
|
||||
"\n"
|
||||
"- **PrintingOrder.id**: `{printing_order_id}`\n"
|
||||
"- **PrintingJob.id**: `{printing_job_id}`\n"
|
||||
"- **State**: **{state_name}**\n"
|
||||
"- **AdvancedAt**: `{advanced_at}`\n"
|
||||
),
|
||||
)
|
||||
message = template.format(
|
||||
printing_order_id=str(printing_order_id) if printing_order_id is not None else '-',
|
||||
printing_job_id=str(printing_job_id) if printing_job_id is not None else '-',
|
||||
state_name=str(state_name),
|
||||
advanced_at=str(advanced_at),
|
||||
)
|
||||
|
||||
def _send_wecom():
|
||||
try:
|
||||
from api_v1.utils.wecom_webhook import send_wecom_webhook_message
|
||||
resp = send_wecom_webhook_message(content=message, msgtype='markdown')
|
||||
if not resp.ok:
|
||||
logger.warning(
|
||||
'[printing.handlers] WeCom webhook 返回失败:errcode=%s, errmsg=%s, raw=%s',
|
||||
resp.errcode, resp.errmsg, resp.raw
|
||||
)
|
||||
except Exception:
|
||||
logger.exception('[printing.handlers] 发送 WeCom webhook 失败(已忽略,不影响主流程)')
|
||||
|
||||
# 在事务提交后再发送,避免事务回滚但通知已发出
|
||||
transaction.on_commit(_send_wecom)
|
||||
|
||||
Reference in New Issue
Block a user