桌码下载修改

This commit is contained in:
张松
2025-02-28 09:37:27 +08:00
parent 41faa3a5f1
commit 10e399bde5
3 changed files with 19 additions and 9 deletions

View File

@@ -16,6 +16,7 @@ import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@@ -38,11 +39,11 @@ public class ShopTableController {
*/
@SaAdminCheckPermission("shopTable:code")
@PostMapping("/code")
public void createCode(@RequestParam Integer num, HttpServletResponse response) throws IOException {
public void createCode(@RequestParam Integer num, HttpServletResponse response, HttpServletRequest request) throws IOException {
if (num > 100) {
throw new ApiNotPrintException("单次最多可获取100个");
}
shopTableService.createQrCode(StpKit.USER.getShopId(), num, response);
shopTableService.createQrCode(StpKit.USER.getShopId(), num, response, request);
}
/**

View File

@@ -4,6 +4,7 @@ import com.czg.account.dto.table.ShopTableAddDTO;
import com.czg.account.dto.table.ShopTableBindDTO;
import com.czg.account.entity.ShopTable;
import com.mybatisflex.core.service.IService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
@@ -22,7 +23,7 @@ public interface ShopTableService extends IService<ShopTable> {
ShopTable getOneByTableCode(@NotNull Long shopId,@NotBlank String tableCode);
void createQrCode(Long shopId, Integer num, HttpServletResponse response) throws IOException;
void createQrCode(Long shopId, Integer num, HttpServletResponse response, HttpServletRequest request) throws IOException;
Boolean bind(Long shopId, ShopTableBindDTO shopTableBindDTO);

View File

@@ -1,7 +1,9 @@
package com.czg.service.account.service.impl;
import cn.dev33.satoken.context.SaHolder;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
@@ -20,6 +22,7 @@ import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.dubbo.config.annotation.DubboService;
@@ -86,7 +89,7 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
}
@Override
public void createQrCode(Long shopId, Integer num, HttpServletResponse response) throws IOException {
public void createQrCode(Long shopId, Integer num, HttpServletResponse response, HttpServletRequest request) throws IOException {
// 查询 shop 下面有多少台桌
long count = queryChain().eq(ShopTable::getShopId, shopId).count();
if (count == 0) {
@@ -111,7 +114,7 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
// 预生成所有二维码数据和记录避免在写入ZIP过程中出现异常
List<ShopTableCode> codeList = new ArrayList<>();
List<byte[]> qrBytesList = new ArrayList<>();
List<ByteArrayOutputStream> qrBytesList = new ArrayList<>();
List<String> fileNames = new ArrayList<>();
for (int i = 0; i < num; i++) {
@@ -126,7 +129,7 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
BufferedImage qrImage = QrCodeUtil.generate(tableCode, 300, 300);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImgUtil.write(qrImage, ImgUtil.IMAGE_TYPE_PNG, baos);
qrBytesList.add(baos.toByteArray());
qrBytesList.add(baos);
fileNames.add(tableCode + ".png");
}
@@ -134,19 +137,24 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
shopTableCodeService.saveBatch(codeList);
// 设置响应头
response.setContentType("application/zip");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=shop_qrcodes_" + shopId + ".zip");
response.setCharacterEncoding(request.getCharacterEncoding());
// 写入ZIP文件
try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream())) {
for (int i = 0; i < num; i++) {
ZipEntry entry = new ZipEntry(fileNames.get(i));
zipOut.putNextEntry(entry);
zipOut.write(qrBytesList.get(i));
ByteArrayOutputStream byteArrayOutputStream = qrBytesList.get(i);
zipOut.write(byteArrayOutputStream.toByteArray());
zipOut.closeEntry();
byteArrayOutputStream.close();
}
zipOut.finish();
zipOut.flush();
}
}