liufumingren 9b1f656790 feat: notify.reply 支持 extend_props.do 动作(openDoor / registerLicensePlate)
入站通知新增两条相互独立的处理线:文本回复 + 动作指令。

- content trim 后为空(含纯空白)→ 不对外发送,并跳过 IM 通道在线检测,
  避免纯动作请求被 503 拒绝、WMS 反复重试;ext_id 幂等去重仍生效。
- extend_props.do 映射到 notify/action/ 下的动作类(大小写不敏感),
  extend_props.value 为泛类型入参(字符串/数字/布尔/对象/数组/JSON/null)。
- 新增 openDoor:Redis db2 写 {WAREHOUSEID}_channelNum{N}(TTL 300s)+ 写 door_log。
- 新增 registerLicensePlate:Redis db7 写 {WAREHOUSEID}_{车牌} NX EX 21600(6 小时临时通行),
  与 Chat::daohuo()/tihuo() 同一套机制,入口设备读到即放行并删键。
- 公共能力(Redis 连接、仓库解析、操作人解析、车牌提取)上移到 action/Base,
  两个动作共用;仓库解析顺序:value.warehouseId → group_id 反查群 → 默认 5400。
2026-09-16 21:10:15 +08:00

176 lines
4.9 KiB
PHP
Raw 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\index\library\new_wms\notify;
use app\index\library\new_wms\notify\action\Base;
/**
* extend_props.do 动作分发器
*
* 入站通知的 data.extend_props 结构:
* {
* "do": "openDoor", // 要执行的动作,映射到 notify\action\ 下的类
* "value": <泛类型> // 隐藏入参:字符串 / 数字 / 布尔 / 对象 / 数组 / null
* }
*
* 已注册动作:
* openDoor — 远程开门(value: 闸口号 / 对象)
* registerLicensePlate— 登记车牌临时通行 6 小时(value: 车牌号 / 对象)
*
* 与文本回复相互独立:
* - content 非空 → 照常推送到 IM 群
* - do 非空 → 执行对应动作(content 为空时也要执行,例如纯开门指令)
*
* 新增动作:
* 1. 在 application/index/library/new_wms/notify/action/ 下新建类,继承 action\Base
* 2. 在下面的 $map 里加一行 do => 类名(大小写不敏感匹配)
*/
class ActionDispatcher
{
/**
* do => 动作类名
* @var array
*/
private static $map = [
'openDoor' => \app\index\library\new_wms\notify\action\OpenDoor::class,
'registerLicensePlate' => \app\index\library\new_wms\notify\action\RegisterLicensePlate::class,
];
/**
* 已注册的动作名列表
* @return array
*/
public static function registered()
{
return array_keys(self::$map);
}
/**
* 注册(或覆盖)一个动作
*
* @param string $do 动作名(与 extend_props.do 对应)
* @param string $class 动作类名(需继承 action\Base)
*/
public static function register($do, $class)
{
self::$map[trim((string) $do)] = $class;
}
/**
* 是否存在可执行的动作
*
* @param mixed $do
* @return bool
*/
public static function has($do)
{
return self::resolve($do) !== null;
}
/**
* 执行动作
*
* 不抛异常:动作失败只记录日志并把失败原因带回响应,不影响文本回复流程。
*
* @param mixed $do extend_props.do
* @param mixed $value extend_props.value,泛类型
* @param array $data 通知 data 节点
* @return array|null null = 没有 do,未执行任何动作
*/
public static function run($do, $value, array $data = [])
{
$do = is_string($do) ? trim($do) : '';
if ($do === '') {
return null;
}
$start = microtime(true);
$class = self::resolve($do);
if (!$class || !class_exists($class)) {
\think\Log::error("[WmsNotify] 动作未注册: do={$do},已注册: " . implode(', ', self::registered()));
return [
'do' => $do,
'ok' => false,
'msg' => '动作未注册',
'cost' => self::cost($start),
];
}
try {
/** @var Base $action */
$action = new $class();
$result = $action->execute($value, $data);
$cost = self::cost($start);
\think\Log::info("[WmsNotify] 动作执行成功: do={$do} cost={$cost}ms value=" . self::encode($value) . " result=" . self::encode($result));
return [
'do' => $do,
'ok' => true,
'msg' => 'ok',
'result' => $result,
'cost' => $cost,
];
} catch (\Throwable $e) {
$cost = self::cost($start);
\think\Log::error("[WmsNotify] 动作执行失败: do={$do} cost={$cost}ms value=" . self::encode($value) . " err=" . $e->getMessage());
return [
'do' => $do,
'ok' => false,
'msg' => $e->getMessage(),
'cost' => $cost,
];
}
}
/**
* 解析 do 对应的动作类名(大小写不敏感)
*
* @param mixed $do
* @return string|null
*/
private static function resolve($do)
{
$do = is_string($do) ? trim($do) : '';
if ($do === '') {
return null;
}
if (isset(self::$map[$do])) {
return self::$map[$do];
}
foreach (self::$map as $name => $class) {
if (strcasecmp($name, $do) === 0) {
return $class;
}
}
return null;
}
/**
* 计算耗时(毫秒)
* @param float $start
* @return float
*/
private static function cost($start)
{
return round((microtime(true) - $start) * 1000, 2);
}
/**
* JSON 编码(便于日志查看,失败时退回 var_export)
* @param mixed $value
* @return string
*/
private static function encode($value)
{
$json = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
return $json === false ? var_export($value, true) : $json;
}
}