|
|
@@ -215,6 +215,12 @@ class GiftCardInfoLists extends BaseAdminDataLists implements ListsExcelInterfac
|
|
|
$row = 2; //从第二行开始
|
|
|
$qrCodeColumnIndex = array_search('qr_code_url', array_keys($excelFields)); // 获取二维码列的索引
|
|
|
|
|
|
+ // 创建临时目录
|
|
|
+ $tempDir = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'excel_qr_codes';
|
|
|
+ if (!is_dir($tempDir)) {
|
|
|
+ mkdir($tempDir, 0755, true);
|
|
|
+ }
|
|
|
+
|
|
|
foreach ($data as $rowIndex => $item) {
|
|
|
$column = 1;
|
|
|
foreach ($item as $key => $value) {
|
|
|
@@ -226,41 +232,59 @@ class GiftCardInfoLists extends BaseAdminDataLists implements ListsExcelInterfac
|
|
|
// 设置单元格为空,为图片腾出空间
|
|
|
$sheet->setCellValueByColumnAndRow($column, $row, '');
|
|
|
|
|
|
+ // 创建临时文件路径
|
|
|
+ $tempFile = $tempDir . DIRECTORY_SEPARATOR . 'qr_' . $rowIndex . '_' . time() . '_' . mt_rand(1000, 9999) . '.png';
|
|
|
+
|
|
|
// 下载图片到临时文件
|
|
|
- $imageContent = @file_get_contents($value);
|
|
|
- if ($imageContent !== false) {
|
|
|
- $tempFile = sys_get_temp_dir() . '/qr_' . $rowIndex . '_' . time() . '.png';
|
|
|
- file_put_contents($tempFile, $imageContent);
|
|
|
-
|
|
|
- // 创建图片对象
|
|
|
- $drawing = new Drawing();
|
|
|
- $drawing->setName('QR Code');
|
|
|
- $drawing->setDescription('小程序码');
|
|
|
- $drawing->setPath($tempFile);
|
|
|
- $drawing->setHeight(60); // 设置图片高度
|
|
|
- $drawing->setWidth(60); // 设置图片宽度
|
|
|
-
|
|
|
- // 设置图片位置
|
|
|
- $cellCoordinate = $letter_column[$columnIndex] . $row;
|
|
|
- $drawing->setCoordinates($cellCoordinate);
|
|
|
- $drawing->setOffsetX(5); // X轴偏移
|
|
|
- $drawing->setOffsetY(5); // Y轴偏移
|
|
|
-
|
|
|
- // 将图片添加到工作表
|
|
|
- $drawing->setWorksheet($sheet);
|
|
|
-
|
|
|
- // 设置行高以适应图片
|
|
|
- $spreadsheet->getActiveSheet()->getRowDimension($row)->setRowHeight(70);
|
|
|
-
|
|
|
- // 清理临时文件
|
|
|
- @unlink($tempFile);
|
|
|
+ $context = stream_context_create([
|
|
|
+ 'http' => [
|
|
|
+ 'timeout' => 10,
|
|
|
+ 'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
|
|
+ ]
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $imageContent = @file_get_contents($value, false, $context);
|
|
|
+ if ($imageContent !== false && strlen($imageContent) > 0) {
|
|
|
+ // 写入临时文件
|
|
|
+ if (file_put_contents($tempFile, $imageContent) !== false && file_exists($tempFile)) {
|
|
|
+ // 验证是否为有效图片
|
|
|
+ $imageInfo = @getimagesize($tempFile);
|
|
|
+ if ($imageInfo !== false) {
|
|
|
+ // 创建图片对象
|
|
|
+ $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
|
|
|
+ $drawing->setName('QR Code');
|
|
|
+ $drawing->setDescription('小程序码');
|
|
|
+ $drawing->setPath($tempFile);
|
|
|
+ $drawing->setHeight(60); // 设置图片高度
|
|
|
+ $drawing->setWidth(60); // 设置图片宽度
|
|
|
+
|
|
|
+ // 设置图片位置
|
|
|
+ $cellCoordinate = $letter_column[$columnIndex] . $row;
|
|
|
+ $drawing->setCoordinates($cellCoordinate);
|
|
|
+ $drawing->setOffsetX(5); // X轴偏移
|
|
|
+ $drawing->setOffsetY(5); // Y轴偏移
|
|
|
+
|
|
|
+ // 将图片添加到工作表
|
|
|
+ $drawing->setWorksheet($sheet);
|
|
|
+
|
|
|
+ // 设置行高以适应图片
|
|
|
+ $spreadsheet->getActiveSheet()->getRowDimension($row)->setRowHeight(70);
|
|
|
+ } else {
|
|
|
+ // 图片无效,显示URL
|
|
|
+ $sheet->setCellValueByColumnAndRow($column, $row, '图片无效');
|
|
|
+ @unlink($tempFile);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 文件写入失败,显示URL
|
|
|
+ $sheet->setCellValueByColumnAndRow($column, $row, '文件写入失败');
|
|
|
+ }
|
|
|
} else {
|
|
|
- // 如果图片下载失败,显示URL
|
|
|
- $sheet->setCellValueByColumnAndRow($column, $row, $value);
|
|
|
+ // 如果图片下载失败,显示提示
|
|
|
+ $sheet->setCellValueByColumnAndRow($column, $row, '图片下载失败');
|
|
|
}
|
|
|
} catch (\Exception $e) {
|
|
|
- // 如果图片插入失败,显示URL
|
|
|
- $sheet->setCellValueByColumnAndRow($column, $row, $value);
|
|
|
+ // 如果图片插入失败,显示错误信息
|
|
|
+ $sheet->setCellValueByColumnAndRow($column, $row, '图片处理失败: ' . $e->getMessage());
|
|
|
}
|
|
|
} else {
|
|
|
// 普通文本数据
|
|
|
@@ -321,9 +345,29 @@ class GiftCardInfoLists extends BaseAdminDataLists implements ListsExcelInterfac
|
|
|
}
|
|
|
$writer->save($src . $this->fileName);
|
|
|
|
|
|
+ // 清理临时文件
|
|
|
+ $this->cleanupTempFiles($tempDir);
|
|
|
+
|
|
|
//设置本地excel缓存并返回下载地址
|
|
|
return (string)(url('index/download/export', ['file' => $exportCache->setFile($this->fileName)], true, true));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @notes 清理临时文件
|
|
|
+ * @param string $tempDir
|
|
|
+ */
|
|
|
+ private function cleanupTempFiles($tempDir)
|
|
|
+ {
|
|
|
+ if (is_dir($tempDir)) {
|
|
|
+ $files = glob($tempDir . DIRECTORY_SEPARATOR . '*');
|
|
|
+ foreach ($files as $file) {
|
|
|
+ if (is_file($file)) {
|
|
|
+ @unlink($file);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ @rmdir($tempDir);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
}
|