桌码下载修改
This commit is contained in:
@@ -16,6 +16,7 @@ import com.mybatisflex.core.paginate.Page;
|
|||||||
import com.mybatisflex.core.query.QueryMethods;
|
import com.mybatisflex.core.query.QueryMethods;
|
||||||
import com.mybatisflex.core.query.QueryWrapper;
|
import com.mybatisflex.core.query.QueryWrapper;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
@@ -38,11 +39,11 @@ public class ShopTableController {
|
|||||||
*/
|
*/
|
||||||
@SaAdminCheckPermission("shopTable:code")
|
@SaAdminCheckPermission("shopTable:code")
|
||||||
@PostMapping("/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) {
|
if (num > 100) {
|
||||||
throw new ApiNotPrintException("单次最多可获取100个");
|
throw new ApiNotPrintException("单次最多可获取100个");
|
||||||
}
|
}
|
||||||
shopTableService.createQrCode(StpKit.USER.getShopId(), num, response);
|
shopTableService.createQrCode(StpKit.USER.getShopId(), num, response, request);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import com.czg.account.dto.table.ShopTableAddDTO;
|
|||||||
import com.czg.account.dto.table.ShopTableBindDTO;
|
import com.czg.account.dto.table.ShopTableBindDTO;
|
||||||
import com.czg.account.entity.ShopTable;
|
import com.czg.account.entity.ShopTable;
|
||||||
import com.mybatisflex.core.service.IService;
|
import com.mybatisflex.core.service.IService;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
@@ -22,7 +23,7 @@ public interface ShopTableService extends IService<ShopTable> {
|
|||||||
|
|
||||||
ShopTable getOneByTableCode(@NotNull Long shopId,@NotBlank String tableCode);
|
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);
|
Boolean bind(Long shopId, ShopTableBindDTO shopTableBindDTO);
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package com.czg.service.account.service.impl;
|
package com.czg.service.account.service.impl;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.context.SaHolder;
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import cn.hutool.core.img.ImgUtil;
|
import cn.hutool.core.img.ImgUtil;
|
||||||
|
import cn.hutool.core.io.IoUtil;
|
||||||
import cn.hutool.core.util.RandomUtil;
|
import cn.hutool.core.util.RandomUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.extra.qrcode.QrCodeUtil;
|
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.core.query.QueryWrapper;
|
||||||
import com.mybatisflex.spring.service.impl.ServiceImpl;
|
import com.mybatisflex.spring.service.impl.ServiceImpl;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import org.apache.dubbo.config.annotation.DubboService;
|
import org.apache.dubbo.config.annotation.DubboService;
|
||||||
|
|
||||||
@@ -86,7 +89,7 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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 下面有多少台桌
|
// 查询 shop 下面有多少台桌
|
||||||
long count = queryChain().eq(ShopTable::getShopId, shopId).count();
|
long count = queryChain().eq(ShopTable::getShopId, shopId).count();
|
||||||
if (count == 0) {
|
if (count == 0) {
|
||||||
@@ -111,7 +114,7 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
|
|||||||
|
|
||||||
// 预生成所有二维码数据和记录,避免在写入ZIP过程中出现异常
|
// 预生成所有二维码数据和记录,避免在写入ZIP过程中出现异常
|
||||||
List<ShopTableCode> codeList = new ArrayList<>();
|
List<ShopTableCode> codeList = new ArrayList<>();
|
||||||
List<byte[]> qrBytesList = new ArrayList<>();
|
List<ByteArrayOutputStream> qrBytesList = new ArrayList<>();
|
||||||
List<String> fileNames = new ArrayList<>();
|
List<String> fileNames = new ArrayList<>();
|
||||||
|
|
||||||
for (int i = 0; i < num; i++) {
|
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);
|
BufferedImage qrImage = QrCodeUtil.generate(tableCode, 300, 300);
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
ImgUtil.write(qrImage, ImgUtil.IMAGE_TYPE_PNG, baos);
|
ImgUtil.write(qrImage, ImgUtil.IMAGE_TYPE_PNG, baos);
|
||||||
qrBytesList.add(baos.toByteArray());
|
qrBytesList.add(baos);
|
||||||
fileNames.add(tableCode + ".png");
|
fileNames.add(tableCode + ".png");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,19 +137,24 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
|
|||||||
shopTableCodeService.saveBatch(codeList);
|
shopTableCodeService.saveBatch(codeList);
|
||||||
|
|
||||||
// 设置响应头
|
// 设置响应头
|
||||||
response.setContentType("application/zip");
|
response.setContentType("application/octet-stream");
|
||||||
response.setHeader("Content-Disposition", "attachment; filename=shop_qrcodes_" + shopId + ".zip");
|
response.setHeader("Content-Disposition", "attachment; filename=shop_qrcodes_" + shopId + ".zip");
|
||||||
|
response.setCharacterEncoding(request.getCharacterEncoding());
|
||||||
|
|
||||||
// 写入ZIP文件
|
// 写入ZIP文件
|
||||||
|
|
||||||
try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream())) {
|
try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream())) {
|
||||||
for (int i = 0; i < num; i++) {
|
for (int i = 0; i < num; i++) {
|
||||||
ZipEntry entry = new ZipEntry(fileNames.get(i));
|
ZipEntry entry = new ZipEntry(fileNames.get(i));
|
||||||
zipOut.putNextEntry(entry);
|
zipOut.putNextEntry(entry);
|
||||||
zipOut.write(qrBytesList.get(i));
|
ByteArrayOutputStream byteArrayOutputStream = qrBytesList.get(i);
|
||||||
|
zipOut.write(byteArrayOutputStream.toByteArray());
|
||||||
zipOut.closeEntry();
|
zipOut.closeEntry();
|
||||||
|
byteArrayOutputStream.close();
|
||||||
}
|
}
|
||||||
zipOut.finish();
|
zipOut.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user