This commit is contained in:
2025-12-16 14:59:13 +08:00
parent e8a38f493e
commit ef3a9f1068
+41 -38
View File
@@ -264,10 +264,8 @@ class SkuController extends BaseController
return ''; return '';
} }
// 1. 读取 docx $phpWord = \PhpOffice\PhpWord\IOFactory::load($docxPath);
$phpWord = WordIOFactory::load($docxPath);
// 2. 导出 HTML
$tmpDir = runtime_path('tmp/' . uniqid()); $tmpDir = runtime_path('tmp/' . uniqid());
if (!is_dir($tmpDir)) { if (!is_dir($tmpDir)) {
mkdir($tmpDir, 0755, true); mkdir($tmpDir, 0755, true);
@@ -275,48 +273,37 @@ class SkuController extends BaseController
$tmpHtml = $tmpDir . '/index.html'; $tmpHtml = $tmpDir . '/index.html';
$writer = WordIOFactory::createWriter($phpWord, 'HTML'); $writer = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
$writer->save($tmpHtml); $writer->save($tmpHtml);
$html = file_get_contents($tmpHtml); $html = file_get_contents($tmpHtml);
// 3. 只取 body 内容(关键) // 1️⃣ 上传图片到 OSS
if (preg_match('/<body[^>]*>(.*?)<\/body>/is', $html, $m)) {
$html = $m[1];
}
// 4. 清洗 Word 垃圾样式(关键)
$html = $this->cleanWordHtml($html);
// 5. 处理图片并上传 OSS
$uploadService = new UploadService(); $uploadService = new UploadService();
$html = preg_replace_callback( $html = preg_replace_callback(
'/<img[^>]+src="([^"]+)"[^>]*>/i', '/<img[^>]+src="([^"]+)"[^>]*>/i',
function ($matches) use ($tmpDir, $uploadService) { function ($m) use ($tmpDir, $uploadService) {
$src = $matches[1]; $src = $m[1];
// Word 导出的是相对路径
if (!str_starts_with($src, 'http')) { if (!str_starts_with($src, 'http')) {
$localImg = $tmpDir . '/' . ltrim($src, '/'); $localImg = $tmpDir . '/' . ltrim($src, './');
if (is_file($localImg)) { if (is_file($localImg)) {
$ossUrl = $uploadService->uploadLocalFile([ $ossUrl = $uploadService->uploadLocalFile([
'path' => $localImg, 'path' => $localImg,
'scene' => 'desc' 'scene' => 'desc'
]); ]);
return preg_replace('/src="[^"]+"/', 'src="' . $ossUrl . '"', $matches[0]); return preg_replace('/src="[^"]+"/', 'src="'.$ossUrl['file_url'].'"', $m[0]);
} }
} }
return $m[0];
return $matches[0];
}, },
$html $html
); );
// 6. 清理临时目录 // 2️⃣ 关键:HTML 清洗(适配 wangEditor)
$this->deleteDir($tmpDir); $html = $this->cleanWordHtmlForWangEditor($html);
return trim($html); return $html;
} }
function deleteDir(string $dir): void function deleteDir(string $dir): void
@@ -325,28 +312,44 @@ class SkuController extends BaseController
foreach (scandir($dir) as $file) { foreach (scandir($dir) as $file) {
if ($file === '.' || $file === '..') continue; if ($file === '.' || $file === '..') continue;
$path = $dir . '/' . $file; $path = $dir . '/' . $file;
is_dir($path) ? deleteDir($path) : @unlink($path); is_dir($path) ? $this->deleteDir($path) : @unlink($path);
} }
@rmdir($dir); @rmdir($dir);
} }
function cleanWordHtml(string $html): string function cleanWordHtmlForWangEditor(string $html): string
{ {
// 移除 Office 命名空间、样式 // 1. 移除 Word 专属标签
$html = preg_replace('/\s*mso-[^:]+:[^;"]+;?/i', '', $html); $html = preg_replace('/<o:p>.*?<\/o:p>/is', '', $html);
$html = preg_replace('/\s*class="[^"]*"/i', '', $html); $html = preg_replace('/<\/?xml[^>]*>/i', '', $html);
$html = preg_replace('/\s*style="[^"]*"/i', '', $html); $html = preg_replace('/<\/?v:[^>]*>/i', '', $html);
// 移除多余标签 // 2. 移除 class / style
$html = preg_replace('/<(\/?)(html|head|meta|link|span|font|xml|o:p)[^>]*>/i', '', $html); $html = preg_replace('/\sclass="[^"]*"/i', '', $html);
$html = preg_replace('/\sstyle="[^"]*"/i', '', $html);
// 清理空段落 // 3. 清理 span(保留内容)
$html = preg_replace('/<p>\s*<\/p>/i', '', $html); while (preg_match('/<span[^>]*>(.*?)<\/span>/is', $html)) {
$html = preg_replace('/<span[^>]*>(.*?)<\/span>/is', '$1', $html);
}
// 规范段落 // 4. 转换 div → p
$html = str_replace(['<p>', '</p>'], ["<p>", "</p>\n"], $html); $html = preg_replace('/<div[^>]*>/i', '<p>', $html);
$html = preg_replace('/<\/div>/i', '</p>', $html);
return $html; // 5. img 只保留 src
$html = preg_replace('/<img[^>]*src="([^"]+)"[^>]*>/i', '<img src="$1">', $html);
// 6. table 清洗
$html = preg_replace('/<table[^>]*>/i', '<table>', $html);
$html = preg_replace('/<td[^>]*>/i', '<td>', $html);
$html = preg_replace('/<th[^>]*>/i', '<th>', $html);
// 7. 去除多余空白
$html = preg_replace('/\s+/u', ' ', $html);
$html = str_replace(['<p></p>', '<p> </p>'], '', $html);
return trim($html);
} }