wip
This commit is contained in:
50
app/common/service/TicketNotifier.php
Normal file
50
app/common/service/TicketNotifier.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -89,6 +89,10 @@ class TicketService
|
||||
|
||||
// 当前无相同网站
|
||||
$ticketResult = Tickets::create($data);
|
||||
|
||||
// 通知前端实时刷新(静默,广播服务不可用不影响主流程)
|
||||
TicketNotifier::notify($ticketResult->id, 'create');
|
||||
|
||||
return $ticketResult;
|
||||
} catch (\Exception $e) {
|
||||
Cache::delete($lockKey);
|
||||
|
||||
Reference in New Issue
Block a user