| package com.dy.pmsGlobal.util; | 
|   | 
| import com.alibaba.excel.EasyExcel; | 
| import com.alibaba.excel.converters.Converter; | 
| import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; | 
| import com.dy.common.util.QrCodeGenerator; | 
| import com.google.zxing.WriterException; | 
| import jakarta.servlet.http.HttpServletResponse; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.springframework.core.io.ClassPathResource; | 
|   | 
| import java.awt.image.BufferedImage; | 
| import java.io.File; | 
| import java.io.IOException; | 
| import java.net.URLEncoder; | 
| import java.util.List; | 
| import java.util.concurrent.ConcurrentHashMap; | 
| @Slf4j | 
| public class QrCodeUtil { | 
|     private static final ConcurrentHashMap<String, File> logoCache = new ConcurrentHashMap<>(); | 
|     private static final String LOGO_PATH = "/images/logo.png"; | 
|   | 
|   | 
|     /** | 
|      * 生成带Logo的二维码图片字节数组。 | 
|      * @param code 二维码内容 | 
|      * @return 二维码图片的字节数组 | 
|      * @throws IOException IO异常 | 
|      * @throws WriterException 二维码生成异常 | 
|      */ | 
|     public static byte[] genQrCode(String code) throws IOException, WriterException { | 
|         File logoFile = logoCache.computeIfAbsent(LOGO_PATH, key -> { | 
|             try { | 
|                 ClassPathResource resource = new ClassPathResource(key); | 
|                 return resource.getFile(); | 
|             } catch (IOException e) { | 
|                 throw new RuntimeException("加载Logo图片失败", e); | 
|             } | 
|         }); | 
|   | 
|         BufferedImage qrCodeImage = QrCodeGenerator.toBufferedImage(QrCodeGenerator.createBitMatrix(code, QrCodeConstant.MarkQrCodeWidth, QrCodeConstant.MarkQrCodeHeight)); | 
|         if (logoFile != null) { | 
|             qrCodeImage = QrCodeGenerator.addQrCodeLogo(qrCodeImage, logoFile); | 
|         } | 
|         return QrCodeGenerator.bufferedImageToByteArray(qrCodeImage, "JPEG"); | 
|     } | 
|   | 
|     public static void downloadExcel(HttpServletResponse response, String fileName, String sheetName, List<Converter> list){ | 
|         if(CollectionUtils.isEmpty(list)){ | 
|             return; | 
|         } | 
|         try{ | 
|             response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); | 
|             response.setCharacterEncoding("utf-8"); | 
|             fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20"); | 
|             response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); | 
|             EasyExcel.write(response.getOutputStream(), list.get(0).getClass()).sheet(sheetName).doWrite(list); | 
|             EasyExcel.write(response.getOutputStream(), Converter.class).sheet(sheetName).doWrite(list); | 
|         }catch (Exception e){ | 
|             log.error("导出产品信息异常", e); | 
|         } | 
|     } | 
| } |