Files
fdesk/app/command/DomainCommand.php
T
2025-08-04 14:03:57 +08:00

79 lines
2.4 KiB
PHP

<?php
namespace app\command;
use app\common\model\InquiryLog;
use app\common\model\User;
use app\common\model\UserSn;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use support\Db;
/**
* 主要处理询单详情中的图片地址
*/
class DomainCommand extends Command
{
protected static $defaultName = 'domain';
protected static $defaultDescription = '';
protected function execute(InputInterface $input, OutputInterface $output): int
{
$log = new InquiryLog();
$list = $log->orderBy('id', 'desc')->get();
foreach ($list as $item) {
if (!$item->content) {
continue;
}
$item->content = processRichTextImagesEnhanced($item->content);
$item->save();
$output->writeln("处理了{$item->id} \n");
}
return self::SUCCESS;
}
}
/**
* 增强版 - 处理富文本内容中的图片URL
*
* @param string $content 富文本HTML内容
* @param string $newDomain 新域名(如: https://fdesk.hnxinyouhe.com)
* @param string $oldDomain 旧域名(如: http://219.157.255.213:25326)
* @return string 处理后的富文本内容
*/
function processRichTextImagesEnhanced($content, $newDomain = 'https://x-server.hnxinyouhe.com', $oldDomain = 'http://219.157.255.213:25326') {
// 标准化域名格式(去除末尾斜杠)
$newDomain = rtrim($newDomain, '/');
$oldDomain = rtrim($oldDomain, '/');
// 要处理的属性列表
$attributes = ['src', 'data-href', 'href', 'data-src', 'data-original'];
foreach ($attributes as $attr) {
// 情况1:替换旧域名为新域名
$content = preg_replace(
'/('.$attr.'=["\'])'.preg_quote($oldDomain, '/').'(\/uploads\/)/i',
'$1' . $newDomain . '$2',
$content
);
// 情况2:给无域名的路径添加新域名
$content = preg_replace(
'/('.$attr.'=["\'])(\/uploads\/)/i',
'$1' . $newDomain . '$2',
$content
);
// 情况3:处理可能存在的其他域名(可选)
$content = preg_replace(
'/('.$attr.'=["\'])(https?:\/\/[^\/]+)(\/uploads\/)/i',
'$1' . $newDomain . '$3',
$content
);
}
return $content;
}