Files
NewTicket/app/common/service/TicketNotifier.php
2026-08-05 17:38:29 +08:00

51 lines
1.5 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\common\service;
/**
* 工单变更通知(通过 Workerman 广播服务推送到前端)
*
* 重要约定:通知失败绝不能影响主业务流程,因此所有异常静默处理。
* 广播服务未启动时,新增/更新工单照常执行,只是前端不实时刷新。
*/
class TicketNotifier
{
/** 广播服务内部触发地址(仅监听本机) */
private const PUSH_URL = 'http://127.0.0.1:2347/push';
/** 请求超时(秒),不能太长以免拖慢主流程 */
private const TIMEOUT = 2;
/**
* 广播工单变更
*
* @param int|string $ticketId 工单ID
* @param string $action create|update|delete
* @return bool 通知是否成功发出false 不表示业务失败)
*/
public static function notify($ticketId, $action = 'create')
{
$payload = json_encode([
'action' => (string)$action,
'ticket_id' => (int)$ticketId,
], JSON_UNESCAPED_UNICODE);
$context = stream_context_create([
'http' => [
'method' => 'POST',
'header' => "Content-Type: application/json\r\n",
'content' => $payload,
'timeout' => self::TIMEOUT,
'ignore_errors' => true,
],
]);
try {
$result = @file_get_contents(self::PUSH_URL, false, $context);
return $result !== false;
} catch (\Throwable $e) {
return false;
}
}
}