2026-06-03 12:10:25 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace app\common\service;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 分流链接自动回复语句(一行一条)
|
|
|
|
|
*/
|
|
|
|
|
class SplitAutoReplyService
|
|
|
|
|
{
|
|
|
|
|
/** 最多条数 */
|
|
|
|
|
private const MAX_LINES = 200;
|
|
|
|
|
|
|
|
|
|
/** 单条最大字符数 */
|
|
|
|
|
private const MAX_LINE_LENGTH = 500;
|
|
|
|
|
|
2026-06-05 04:22:29 +08:00
|
|
|
/** 列表预览最大字符数 */
|
|
|
|
|
private const LIST_PREVIEW_MAX = 20;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 列表单元格预览(单行最多 50 字,超出加 ...)
|
|
|
|
|
*/
|
|
|
|
|
public static function previewForList(string $raw, int $max = self::LIST_PREVIEW_MAX): string
|
|
|
|
|
{
|
|
|
|
|
$display = self::formatDisplay($raw);
|
|
|
|
|
if ($display === '') {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
$flat = preg_replace('/\s+/u', ' ', str_replace(["\r\n", "\r", "\n"], ' ', $display));
|
|
|
|
|
$flat = trim((string) $flat);
|
|
|
|
|
if ($flat === '') {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
if (mb_strlen($flat, 'UTF-8') <= $max) {
|
|
|
|
|
return $flat;
|
|
|
|
|
}
|
|
|
|
|
return mb_substr($flat, 0, $max, 'UTF-8') . '...';
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-03 12:10:25 +08:00
|
|
|
/**
|
|
|
|
|
* 解析为多行文本数组
|
|
|
|
|
*
|
|
|
|
|
* @return array<int, string>
|
|
|
|
|
*/
|
|
|
|
|
public static function parseLines(string $raw): array
|
|
|
|
|
{
|
|
|
|
|
$raw = trim($raw);
|
|
|
|
|
if ($raw === '') {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
$parts = preg_split('/\r\n|\r|\n/', $raw) ?: [];
|
|
|
|
|
$lines = [];
|
|
|
|
|
foreach ($parts as $part) {
|
|
|
|
|
$line = trim((string) $part);
|
|
|
|
|
if ($line === '') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (strlen($line) > self::MAX_LINE_LENGTH) {
|
|
|
|
|
$line = mb_substr($line, 0, self::MAX_LINE_LENGTH, 'UTF-8');
|
|
|
|
|
}
|
|
|
|
|
$lines[] = $line;
|
|
|
|
|
if (count($lines) >= self::MAX_LINES) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $lines;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 格式化为数据库存储(换行分隔)
|
|
|
|
|
*
|
|
|
|
|
* @param string|array<int, string> $value
|
|
|
|
|
*/
|
|
|
|
|
public static function formatStorage($value): string
|
|
|
|
|
{
|
|
|
|
|
if (is_array($value)) {
|
|
|
|
|
$lines = $value;
|
|
|
|
|
} else {
|
|
|
|
|
$lines = self::parseLines((string) $value);
|
|
|
|
|
}
|
|
|
|
|
return implode("\n", $lines);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 供表单 textarea 回显
|
|
|
|
|
*/
|
|
|
|
|
public static function formatDisplay(string $stored): string
|
|
|
|
|
{
|
|
|
|
|
$lines = self::parseLines($stored);
|
|
|
|
|
return implode("\n", $lines);
|
|
|
|
|
}
|
|
|
|
|
}
|