]*>/i', '
', $html);
$html = preg_replace('/<\/div>/i', '
', $html);
// 5. img 只保留 src
$html = preg_replace('/
![]()
]*src="([^"]+)"[^>]*>/i', '

', $html);
// 6. table 清洗
$html = preg_replace('/
]*>/i', '', $html);
$html = preg_replace('/| ]*>/i', ' | ', $html);
$html = preg_replace('/ | ]*>/i', ' | ', $html);
// 7. 去除多余空白
$html = preg_replace('/\s+/u', ' ', $html);
$html = str_replace(['', ' '], '', $html);
return trim($html);
}
function cleanWordHtml(string $html): string
{
// 删除 Word 注释
$html = preg_replace('//', '', $html);
// 删除 style / meta / link
$html = preg_replace('/<(style|meta|link)[^>]*>.*?<\/\1>/is', '', $html);
// 删除 xml 标签
$html = preg_replace('/.*?<\/xml>/is', '', $html);
// 删除 mso-xxx 样式
$html = preg_replace('/\s*mso-[^:]+:[^;"]+;?/i', '', $html);
// 清理 class="Mso..."
$html = preg_replace('/class="Mso[^"]*"/i', '', $html);
// 删除空 span
$html = preg_replace('/]*>\s*<\/span>/i', '', $html);
// 删除 Office 条件注释
$html = preg_replace('/.*?/is', '', $html);
// 只保留 body 内容
if (preg_match('/]*>(.*?)<\/body>/is', $html, $m)) {
$html = $m[1];
}
return $html;
}
/**
* docx 转 HTML(富文本可用,图片上传 OSS)
*/
/*function docxToHtmlWithImages(string $docxPath): string
{
if (!is_file($docxPath)) {
return '';
}
$phpWord = \PhpOffice\PhpWord\IOFactory::load($docxPath);
$tmpDir = runtime_path('tmp/' . uniqid());
if (!is_dir($tmpDir)) {
mkdir($tmpDir, 0755, true);
}
$tmpHtml = $tmpDir . '/index.html';
$writer = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
$writer->save($tmpHtml);
$html = file_get_contents($tmpHtml);
// 1️⃣ 上传图片到 OSS
$uploadService = new UploadService();
$html = preg_replace_callback(
'/ ]+src="([^"]+)"[^>]*>/i',
function ($m) use ($tmpDir, $uploadService) {
$src = $m[1];
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['file_url'].'"', $m[0]);
}
}
return $m[0];
},
$html
);
// // 2️⃣ 关键:HTML 清洗(适配 wangEditor)
$html = $this->cleanWordHtmlForWangEditor($html);
return $html;
}*/
function deleteDir(string $dir): void
{
if (!is_dir($dir)) return;
foreach (scandir($dir) as $file) {
if ($file === '.' || $file === '..') continue;
$path = $dir . '/' . $file;
is_dir($path) ? $this->deleteDir($path) : @unlink($path);
}
@rmdir($dir);
}
/*function cleanWordHtmlForWangEditor(string $html): string
{
// 1. 移除 Word 专属标签
$html = preg_replace('/.*?<\/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>/is', $html)) {
$html = preg_replace('/]*>(.*?)<\/span>/is', '$1', $html);
}
// 4. 转换 div → p
$html = preg_replace('/]*>/i', ' ', $html);
$html = preg_replace('/<\/div>/i', ' ', $html);
// 5. img 只保留 src
$html = preg_replace('/ ![]() ]*src="([^"]+)"[^>]*>/i', '  ', $html);
// 6. table 清洗
$html = preg_replace('/ ]*>/i', '', $html);
$html = preg_replace('/| ]*>/i', ' | ', $html);
$html = preg_replace('/ | ]*>/i', ' | ', $html);
// 7. 去除多余空白
$html = preg_replace('/\s+/u', ' ', $html);
$html = str_replace(['', ' '], '', $html);
return trim($html);
}*/
/**
* docx 转 HTML(自动上传图片到 OSS)
*
* @param string $docxPath docx 文件绝对路径
* @return string HTML
*/
/*function docxToHtmlWithImages(string $docxPath): string
{
if (!is_file($docxPath)) {
return '';
}
// 1. 读取 docx
$phpWord = WordIOFactory::load($docxPath);
// 2. 输出 HTML 到临时文件
$tmpHtml = runtime_path('tmp/' . uniqid() . '.html');
if (!is_dir(dirname($tmpHtml))) {
mkdir(dirname($tmpHtml), 0755, true);
}
$writer = WordIOFactory::createWriter($phpWord, 'HTML');
$writer->save($tmpHtml);
$html = file_get_contents($tmpHtml);
$uploadService = new UploadService();
// 3. 处理 HTML 中的图片
$html = preg_replace_callback(
'/ ]+src="([^"]+)"[^>]*>/i',
function ($matches) use ($tmpHtml,$uploadService) {
$src = $matches[1];
// docx 导出的图片一般是相对路径
if (!str_starts_with($src, 'http')) {
$localImg = dirname($tmpHtml) . '/' . $src;
if (is_file($localImg)) {
// 上传到 OSS
$ossUrl = $uploadService->uploadLocalFile(['path' => $localImg, 'scene' => 'desc']);
return str_replace($src, $ossUrl, $matches[0]);
}
}
return $matches[0];
},
$html
);
// 清理临时文件
@unlink($tmpHtml);
return $html;
}*/
private function extractImages($sheet): array
{
$images = [];
$uploadService = new UploadService();
foreach ($sheet->getDrawingCollection() as $drawing) {
if ($drawing instanceof Drawing) {
// 取行号
preg_match('/\d+/', $drawing->getCoordinates(), $m);
$row = (int)$m[0];
// Excel 临时图片路径
$tmpPath = $drawing->getPath();
if (!is_file($tmpPath)) {
continue;
}
// 上传到 OSS
$ossUrl = $uploadService->uploadLocalFile(['path' => $tmpPath, 'scene' => 'sku']);
// 一行只保留一张主图
$images[$row] = $ossUrl['file_url'];
}
}
return $images;
}
/**
* 下载 Excel 示例模板
*/
public function template(Request $request): Response
{
$filePath = runtime_path() . '/upload/import.xlsx';
if (!file_exists($filePath)) {
return response('文件不存在', 404);
}
return response()->download(
$filePath,
'导入示例.xlsx' // 下载时显示的文件名
);
}
} | |