Browse Source

Merge remote-tracking branch 'origin/dev_qr_code' into develop

dev
zxc 3 years ago
parent
commit
2dd34f6103
  1. 29
      epmet-module/gov-org/gov-org-client/src/main/java/com/epmet/dto/form/CreateQrCodeFormDTO.java
  2. 115
      epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/controller/AgencyController.java
  3. 2
      epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/controller/IcNeighborHoodController.java

29
epmet-module/gov-org/gov-org-client/src/main/java/com/epmet/dto/form/CreateQrCodeFormDTO.java

@ -0,0 +1,29 @@
package com.epmet.dto.form;
import com.epmet.commons.tools.validator.group.Group;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
/**
* @Author zxc
* @DateTime 2022/3/18 15:43
* @DESC
*/
@Data
public class CreateQrCodeFormDTO implements Serializable {
private static final long serialVersionUID = -6801094057381407439L;
public interface CreateQrCodeForm{}
@NotBlank(message = "id不能为空",groups = CreateQrCodeForm.class)
private String id;
/**
* community:社区neighborHood小区
*/
@NotBlank(message = "type不能为空",groups = CreateQrCodeForm.class)
private String type;
}

115
epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/controller/AgencyController.java

@ -21,13 +21,20 @@ import com.alibaba.fastjson.JSONObject;
import com.epmet.commons.rocketmq.messages.OrgOrStaffMQMsg;
import com.epmet.commons.tools.annotation.LoginUser;
import com.epmet.commons.tools.annotation.RequirePermission;
import com.epmet.commons.tools.constant.StrConstant;
import com.epmet.commons.tools.enums.EnvEnum;
import com.epmet.commons.tools.enums.RequirePermissionEnum;
import com.epmet.commons.tools.exception.EpmetErrorCode;
import com.epmet.commons.tools.exception.EpmetException;
import com.epmet.commons.tools.exception.RenException;
import com.epmet.commons.tools.security.dto.TokenDto;
import com.epmet.commons.tools.utils.BarcodeUtils;
import com.epmet.commons.tools.utils.Result;
import com.epmet.commons.tools.validator.ValidatorUtils;
import com.epmet.constant.CustomerAgencyConstant;
import com.epmet.constant.OrgInfoConstant;
import com.epmet.dto.CustomerAgencyDTO;
import com.epmet.dto.IcNeighborHoodDTO;
import com.epmet.dto.form.*;
import com.epmet.dto.result.*;
import com.epmet.entity.CustomerAgencyEntity;
@ -35,6 +42,7 @@ import com.epmet.feign.EpmetMessageOpenFeignClient;
import com.epmet.send.SendMqMsgUtil;
import com.epmet.service.AgencyService;
import com.epmet.service.CustomerAgencyService;
import com.epmet.service.IcNeighborHoodService;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
@ -46,9 +54,16 @@ import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URLEncoder;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -69,6 +84,8 @@ public class AgencyController {
private CustomerAgencyService customerAgencyService;
@Autowired
private EpmetMessageOpenFeignClient epmetMessageOpenFeignClient;
@Autowired
private IcNeighborHoodService neighborHoodService;
/**
* @param formDTO
@ -370,46 +387,64 @@ public class AgencyController {
return new Result<List<SonOrgResultDTO>>().ok(agencyService.getSonAgencyId(orgId,type));
}
@PostMapping("qr")
public void qr(){
try {
write();
} catch (IOException e) {
e.printStackTrace();
} catch (WriterException e) {
e.printStackTrace();
}
}
public static void write() throws IOException, WriterException {
String filePath = "/Users/liujianjun/Downloads";
String fileName = "qr.png";
//二维码内容场景一:json数据
JSONObject json = new JSONObject();
json.put("baidu","http://www.baidu.com");//二维码一般就是存储链接
// json.put("author", "lzz");//还可存储值(员工工号,员工姓名,年会二维码门票)
String content = json.toJSONString();//json 内容
//二维码内容场景二:直接某个超链接
// content="http://www.baidu.com";//直接某个超链接
int width = 200; // 图像宽度
int height = 200; // 图像高度
String format = "png";// 图像类型
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵
Path path = FileSystems.getDefault().getPath(filePath, fileName);
MatrixToImageWriter.writeToPath(bitMatrix, format, path);// 输出图像
System.out.println("输出成功.");
}
public static void main(String[] args) {
/**
* Desc: 生成某类型下的二维码
* @param formDTO
* @author zxc
* @date 2022/3/2 10:32 上午
*/
@PostMapping("create-qrcode")
public void createQrCode(@LoginUser TokenDto tokenDto, @RequestBody CreateQrCodeFormDTO formDTO, HttpServletResponse response){
ValidatorUtils.validateEntity(formDTO, CreateQrCodeFormDTO.CreateQrCodeForm.class);
String id = formDTO.getId();
String type = formDTO.getType();
String name = "";
try {
write();
} catch (IOException e) {
e.printStackTrace();
} catch (WriterException e) {
if (type.equals(OrgInfoConstant.COMMUNITY)){
CustomerAgencyDTO customerAgencyDTO = customerAgencyService.get(id);
if (customerAgencyDTO == null){
throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(),"组织信息不存在");
}
name = customerAgencyDTO.getOrganizationName();
}else if (type.equals(OrgInfoConstant.COMMUNITY)){
IcNeighborHoodDTO icNeighborHoodDTO = neighborHoodService.get(id);
if (icNeighborHoodDTO == null){
throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(),"小区信息不存在");
}
name = icNeighborHoodDTO.getNeighborHoodName();
}
//url组成:数字社区地址?小区id&用户id
//String url = "https://demo.tduckapp.com/s/7314b64b3a26455ab793fb8c640856b6?id="+id;
String url = EnvEnum.getCurrentEnv().getUrl().replace("api/", StrConstant.EPMETY_STR)
.concat("epmet-oper-gov/#/caiji/")
.concat(id).concat(StrConstant.QUESTION_MARK_TRANSFER)
.concat("name=").concat(name).concat(StrConstant.AND_MARK)
.concat("type=").concat(type).concat(StrConstant.AND_MARK)
.concat("userId=").concat(tokenDto.getUserId())
.concat(StrConstant.AND_MARK).concat(String.valueOf(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli()));
BufferedImage image = BarcodeUtils.drawQRImage(name, url);
//BufferedImage 转 InputStream
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageOutputStream imageOutput = ImageIO.createImageOutputStream(byteArrayOutputStream);
ImageIO.write(image, "png", imageOutput);
InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
long length = imageOutput.length();
String fileName = name+".png";
response.setContentType("application/octet-stream");
response.setContentLength((int)length);
response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(fileName, StrConstant.UTF_8));
//输出流
byte[] bytes = new byte[1024];
OutputStream outputStream = response.getOutputStream();
long count = 0;
while(count < length){
int len = inputStream.read(bytes, 0, 1024);
count +=len;
outputStream.write(bytes, 0, len);
}
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
}
}

2
epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/controller/IcNeighborHoodController.java

@ -189,7 +189,7 @@ public class IcNeighborHoodController {
//url组成:数字社区地址?小区id&用户id
//String url = "https://demo.tduckapp.com/s/7314b64b3a26455ab793fb8c640856b6?id="+id;
String url = EnvEnum.getCurrentEnv().getUrl().replace("api/",StrConstant.EPMETY_STR)
.concat("epmet-oper-gov/#/")
.concat("epmet-oper-gov/#/caiji/")
.concat(id)
.concat(StrConstant.AND_MARK).concat("userId=").concat(tokenDto.getUserId());
BufferedImage image = BarcodeUtils.drawQRImage(neighborhoodName, url);

Loading…
Cancel
Save