实现功能:批量生成画报,打成压缩包,前端下载解压,扫描每个二维码进入小程序,显示不同的编号
实现效果:
前端使用vue
downLoadCode(data) {
this.downloading = this.$loading({
lock: true,
text: "下载中,请稍候...", //加载提示文字
background: "rgba(0,0,0,0.8)", //遮罩层颜色
spinner: "el-icon-loading", //自定义加载图标类名
});
codeApi
.downloadCode(data.id) // 后台接口
.then((response) => {
const link = document.createElement("a");
let url = ""; // 图片地址
// 这里是将url转成blob地址
fetch(url)
.then((res) => res.blob())
.then((blob) => {
link.href = URL.createObjectURL(blob);
link.style.display = "none";
link.download = data.companyName + "_溯源码";
link.click();
this.downloading.close();
});
},
后台java实现,批量生成画报打成压缩包
@SneakyThrows
@PostMapping("downloadCode/{id}")
public R downloadCode(@PathVariable Long id){
CodeSummary obj = codeSummaryService.getById(id);
Batch byId = batchService.getById(obj.getBatchId());
List<CodeData> codeList = new ArrayList<>();
// 获取token
String tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + wxAppid + "&secret=" + wxSecret;
RestTemplate restTemplate = new RestTemplate();
String token = restTemplate.getForObject(tokenUrl, String.class);
ObjectMapper mapper = new ObjectMapper();
AuthJson authToken = mapper.readValue(token, AuthJson.class);
RestTemplate restTemplate2 = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
// 获取微信小程序商户自己的二维码
String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+authToken.getAccess_token();
String logoPath = weChatProperties.getUploadPath() + "plat/beijings.png";
BufferedImage logoImage = ImageIO.read(new File(logoPath));
/**
* 创建二维码暂时存放的路径
*/
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssss");
String longTime = sdf.format(new Date());
//下载文件的默认名称
String filename = longTime+"_sourceCode.zip";
File file = new File(weChatProperties.getUploadPath()+"/sourceCode/"+longTime+"_code");
TestZip.creatFile(file);
/**
* 将base64转成二维码图片,保存本地
*/
for (int i = 1; i <= obj.getCount(); i++) {
// 要打开的小程序版本。正式版为 "release",体验版为 "trial",开发版为 "develop"。默认是正式版。
JSONObject json = new JSONObject();
json.put("path", "pages/result/result"); // 每个批次有单独的号码
json.put("env_version", "release");
// json.put("width", 100);
json.put("scene", byId.getBatchNo()+i);
json.put("check_path", false);
HttpEntity<String> entity = new HttpEntity<String>(json.toString(), headers);
byte[] bytes = restTemplate2.postForObject(url, entity, byte[].class);
String base64Image = Base64.getEncoder().encodeToString(bytes);
BufferedImage qrCodeImage = convertToImage(bytes);
int width = 767;
int height = 606;
BufferedImage tarImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = tarImage.createGraphics();
// g2.setColor(new Color(168, 252, 229));
// 填充一个矩形,左上角坐标为(150,100),宽为10,高为20
g2.fillRect(0, 0, width, height);
Font font = new Font("PingFang SC", Font.PLAIN, 34);
g2.setColor(Color.BLACK);
g2.setFont(font);
g2.drawImage(logoImage, 0, 0, 767, 606, null);
g2.drawImage(qrCodeImage, 360, 162, 280, 280, null);
String name = "溯源码:"+byId.getBatchNo()+i;
g2.drawString(name, 80, 480);
// 绘制图片
g2.dispose();
// tarImage转inputStream
String IMAGE_TYPE = "png";
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
ImageIO.write(tarImage, IMAGE_TYPE, os);
byte[] imageBytes = os.toByteArray();
String encode = Base64.getEncoder().encodeToString(imageBytes);
CodeData codeData = new CodeData();
codeData.setCode(imageBytes);
codeData.setText(byId.getBatchNo()+i);
codeList.add(codeData);
String newName = byId.getBatchNo()+i + ".png";
String filePath = file + "/" + newName;
Boolean result = TestZip.Base64ToPicture(encode,filePath);
if (!result){
return R.error();
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 将溯源码码文件夹压缩
*/
String pathTo = weChatProperties.getUploadPath()+"/sourceCode/"+filename;
OutputStream is = new FileOutputStream(pathTo);//创建Test.zip文件
Boolean KeepDirStructure = true;
TestZip.toZip(file.toString(),is,KeepDirStructure);
/**
* 将二维码图片文件夹及其中所有文件删除
*/
TestZip.delFolder(file.toString());
return R.ok().data("list", "/sourceCode/"+filename);
}