Files
fdesk/app/command/DomainCommand.php
T
2025-07-28 19:36:21 +08:00

58 lines
1.6 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();
}
return self::SUCCESS;
}
}
/**
* 增强版 - 为富文本内容中的图片URL添加域名前缀
*
* @param string $content 富文本HTML内容
* @param string $domain 域名(如: https://fdesk.hnxinyouhe.com)
* @return string 处理后的富文本内容
*/
function processRichTextImagesEnhanced($content, $domain = 'https://fdesk.hnxinyouhe.com') {
// 确保域名不以斜杠结尾
$domain = rtrim($domain, '/');
// 要处理的属性列表
$attributes = ['src', 'data-href', 'href', 'data-src', 'data-original'];
foreach ($attributes as $attr) {
$content = preg_replace(
'/('.$attr.'=["\'])(\/uploads\/)/i',
'$1' . $domain . '$2',
$content
);
}
return $content;
}