导入
This commit is contained in:
@@ -264,10 +264,8 @@ class SkuController extends BaseController
|
||||
return '';
|
||||
}
|
||||
|
||||
// 1. 加载 docx
|
||||
$phpWord = WordIOFactory::load($docxPath, 'Word2007');
|
||||
$phpWord = \PhpOffice\PhpWord\IOFactory::load($docxPath);
|
||||
|
||||
// 2. 临时目录(非常重要)
|
||||
$tmpDir = runtime_path('tmp/' . uniqid());
|
||||
if (!is_dir($tmpDir)) {
|
||||
mkdir($tmpDir, 0755, true);
|
||||
@@ -275,50 +273,77 @@ class SkuController extends BaseController
|
||||
|
||||
$tmpHtml = $tmpDir . '/index.html';
|
||||
|
||||
// 3. 导出 HTML
|
||||
$writer = WordIOFactory::createWriter($phpWord, 'HTML');
|
||||
$writer = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
|
||||
$writer->save($tmpHtml);
|
||||
|
||||
$html = file_get_contents($tmpHtml);
|
||||
|
||||
// 4. 清洗 Word 垃圾 HTML(关键)
|
||||
$html = $this->cleanWordHtml($html);
|
||||
|
||||
// 1️⃣ 上传图片到 OSS
|
||||
$uploadService = new UploadService();
|
||||
|
||||
// 5. 处理图片
|
||||
$html = preg_replace_callback(
|
||||
'/<img[^>]+src="([^"]+)"[^>]*>/i',
|
||||
function ($matches) use ($tmpDir, $uploadService) {
|
||||
$src = $matches[1];
|
||||
function ($m) use ($tmpDir, $uploadService) {
|
||||
$src = $m[1];
|
||||
|
||||
// 只处理本地图片
|
||||
if (preg_match('#^(http|https|data:)#i', $src)) {
|
||||
return $matches[0];
|
||||
if (!str_starts_with($src, 'http')) {
|
||||
$localImg = $tmpDir . '/' . ltrim($src, './');
|
||||
if (is_file($localImg)) {
|
||||
$ossUrl = $uploadService->uploadLocalFile([
|
||||
'path' => $localImg,
|
||||
'scene' => 'desc'
|
||||
]);
|
||||
return preg_replace('/src="[^"]+"/', 'src="'.$ossUrl.'"', $m[0]);
|
||||
}
|
||||
}
|
||||
|
||||
$localImg = realpath($tmpDir . '/' . $src);
|
||||
if (!$localImg || !is_file($localImg)) {
|
||||
return $matches[0];
|
||||
}
|
||||
|
||||
// 上传 OSS
|
||||
$ossUrl = $uploadService->uploadLocalFile([
|
||||
'path' => $localImg,
|
||||
'scene' => 'desc'
|
||||
]);
|
||||
|
||||
return str_replace($src, $ossUrl, $matches[0]);
|
||||
return $m[0];
|
||||
},
|
||||
$html
|
||||
);
|
||||
|
||||
// 6. 清理临时目录
|
||||
// 2️⃣ 关键:HTML 清洗(适配 wangEditor)
|
||||
$html = $this->cleanWordHtmlForWangEditor($html);
|
||||
|
||||
$this->deleteDir($tmpDir);
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
function cleanWordHtmlForWangEditor(string $html): string
|
||||
{
|
||||
// 1. 移除 Word 专属标签
|
||||
$html = preg_replace('/<o:p>.*?<\/o:p>/is', '', $html);
|
||||
$html = preg_replace('/<\/?xml[^>]*>/i', '', $html);
|
||||
$html = preg_replace('/<\/?v:[^>]*>/i', '', $html);
|
||||
|
||||
// 2. 移除 class / style
|
||||
$html = preg_replace('/\sclass="[^"]*"/i', '', $html);
|
||||
$html = preg_replace('/\sstyle="[^"]*"/i', '', $html);
|
||||
|
||||
// 3. 清理 span(保留内容)
|
||||
while (preg_match('/<span[^>]*>(.*?)<\/span>/is', $html)) {
|
||||
$html = preg_replace('/<span[^>]*>(.*?)<\/span>/is', '$1', $html);
|
||||
}
|
||||
|
||||
// 4. 转换 div → p
|
||||
$html = preg_replace('/<div[^>]*>/i', '<p>', $html);
|
||||
$html = preg_replace('/<\/div>/i', '</p>', $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);
|
||||
}
|
||||
|
||||
|
||||
function cleanWordHtml(string $html): string
|
||||
{
|
||||
// 删除 Word 注释
|
||||
|
||||
Reference in New Issue
Block a user