| | |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | import java.util.zip.ZipEntry; |
| | | import java.util.zip.ZipOutputStream; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | @RequiresPermissions(value = {"/chargingGun/download_qr_code"}, logical = Logical.OR) |
| | | @ApiOperation(tags = {"管理后台-接口信息"}, value = "批量导出接口二维码") |
| | | @PutMapping(value = "/exportQRCodeZip") |
| | | @Log(title = "【接口信息】批量导出接口二维码", businessType = BusinessType.EXPORT, operatorType = OperatorType.MANAGE) |
| | | public void exportQRCodeZip(@RequestBody TChargingGunQuery query, HttpServletResponse response) { |
| | | try { |
| | | List<TChargingGunVO> list = chargingGunService.exportQRCodeList(query == null ? new TChargingGunQuery() : query); |
| | | String zipName = URLEncoder.encode("接口二维码.zip", "UTF-8"); |
| | | response.setContentType("application/zip"); |
| | | response.setHeader("Content-Disposition", "attachment;filename=" + zipName); |
| | | List<String> names = new ArrayList<>(); |
| | | try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream())) { |
| | | for (TChargingGunVO gun : list) { |
| | | String code = StringUtils.isNotEmpty(gun.getFullNumber()) ? gun.getFullNumber() : gun.getChargingPileCode() + gun.getCode(); |
| | | if (StringUtils.isEmpty(code)) { |
| | | continue; |
| | | } |
| | | String baseName = sanitizeFileName((StringUtils.isNotEmpty(gun.getSiteName()) ? gun.getSiteName() : "电站") |
| | | + (StringUtils.isNotEmpty(gun.getName()) ? gun.getName() : code)); |
| | | String entryName = baseName + ".jpg"; |
| | | int index = 1; |
| | | while (names.contains(entryName)) { |
| | | entryName = baseName + "(" + index++ + ").jpg"; |
| | | } |
| | | names.add(entryName); |
| | | zipOut.putNextEntry(new ZipEntry(entryName)); |
| | | QRCodeUtils.encode("https://mxcd.zhinenganguan.com?No=" + code, zipOut); |
| | | zipOut.closeEntry(); |
| | | } |
| | | zipOut.finish(); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | private String sanitizeFileName(String name) { |
| | | if (StringUtils.isEmpty(name)) { |
| | | return "qrcode"; |
| | | } |
| | | return name.replaceAll("[\\\\/:*?\\\"<>|]", "_"); |
| | | } |
| | | @Resource |
| | | private TokenService tokenService; |
| | | /** |