Commit 59fcd0766f6250962530ef7aa51841b1b2fecb16
1 parent
63890800
feat: 生成二维码
Showing
8 changed files
with
156 additions
and
4 deletions
trash-admin/src/main/resources/application-dev.yml
| ... | ... | @@ -9,8 +9,9 @@ trash: |
| 9 | 9 | # 实例演示开关 |
| 10 | 10 | demoEnabled: true |
| 11 | 11 | # 文件路径 示例( Windows配置D:/trash/uploadPath,Linux配置 /home/trash/uploadPath,存储桶 trash/uploadPath,nginx配置 /trash/upload) |
| 12 | + profile: D:/trash/uploadPath | |
| 12 | 13 | # profile: E:/trash/uploadPath |
| 13 | - profile: F:/work/project/Documents/uploadPath/trash | |
| 14 | +# profile: F:/work/project/Documents/uploadPath/trash | |
| 14 | 15 | # 获取ip地址开关 |
| 15 | 16 | addressEnabled: false |
| 16 | 17 | # 验证码类型 math 数组计算 char 字符验证 |
| ... | ... | @@ -127,4 +128,8 @@ huawei: |
| 127 | 128 | nginx: |
| 128 | 129 | enabled: false |
| 129 | 130 | path: |
| 130 | - url: | |
| 131 | 131 | \ No newline at end of file |
| 132 | + url: | |
| 133 | + | |
| 134 | +# 生成二维码 前端地址 | |
| 135 | +front: | |
| 136 | + url: "http://61.169.120.202:5173/pages/order/guest/index?orderId=" | |
| 132 | 137 | \ No newline at end of file | ... | ... |
trash-framework/src/main/java/com/trash/framework/config/SecurityConfig.java
| ... | ... | @@ -118,6 +118,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter |
| 118 | 118 | .antMatchers("/*/api-docs").anonymous() |
| 119 | 119 | .antMatchers("/druid/**").anonymous() |
| 120 | 120 | .antMatchers("/user/login").anonymous() |
| 121 | + .antMatchers("/order/webDetail/**").anonymous() | |
| 121 | 122 | .antMatchers("/user/send/verify").anonymous() |
| 122 | 123 | // .antMatchers("/user/logout").anonymous() |
| 123 | 124 | .antMatchers("/system/post/all").anonymous() | ... | ... |
trash-garbage/src/main/java/com/trash/garbage/controller/GarQRController.java
0 → 100644
| 1 | +package com.trash.garbage.controller; | |
| 2 | + | |
| 3 | +import com.trash.garbage.global.Result; | |
| 4 | +import com.trash.garbage.service.GarQRCodeService; | |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 6 | +import org.springframework.web.bind.annotation.GetMapping; | |
| 7 | +import org.springframework.web.bind.annotation.PathVariable; | |
| 8 | +import org.springframework.web.bind.annotation.RequestMapping; | |
| 9 | +import org.springframework.web.bind.annotation.RestController; | |
| 10 | + | |
| 11 | +import javax.servlet.http.HttpServletResponse; | |
| 12 | + | |
| 13 | +@RestController | |
| 14 | +@RequestMapping("/QRCode") | |
| 15 | +public class GarQRController { | |
| 16 | + | |
| 17 | + @Autowired | |
| 18 | + private GarQRCodeService qrCodeService; | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * 生成订单详情二维码 | |
| 22 | + */ | |
| 23 | + @GetMapping("/create/{orderId}") | |
| 24 | + public Result<String> createQRCode(@PathVariable("orderId") String orderId, HttpServletResponse response){ | |
| 25 | + return Result.OK(qrCodeService.createQRCode(orderId,response)); | |
| 26 | + } | |
| 27 | + | |
| 28 | + | |
| 29 | +} | ... | ... |
trash-garbage/src/main/java/com/trash/garbage/controller/GarbageOrderController.java
| ... | ... | @@ -112,7 +112,6 @@ public class GarbageOrderController { |
| 112 | 112 | return Result.OK(garOrderService.queryOrderListByGarOrderPage(dto)); |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | - | |
| 116 | 115 | @GetMapping("/webDetail/{id}") |
| 117 | 116 | public Result<OrderDetailVo> queryOrderWebDetail(@PathVariable("id") String id) { |
| 118 | 117 | return Result.OK(garOrderService.queryOrderWebDetail(id)); | ... | ... |
trash-garbage/src/main/java/com/trash/garbage/global/GlobalStatus.java
trash-garbage/src/main/java/com/trash/garbage/service/GarQRCodeService.java
0 → 100644
trash-garbage/src/main/java/com/trash/garbage/service/impl/GarQRCodeServiceImpl.java
0 → 100644
| 1 | +package com.trash.garbage.service.impl; | |
| 2 | + | |
| 3 | +import com.google.zxing.WriterException; | |
| 4 | +import com.trash.garbage.service.GarQRCodeService; | |
| 5 | +import com.trash.garbage.utils.QRCodeUtil; | |
| 6 | +import org.springframework.beans.factory.annotation.Value; | |
| 7 | +import org.springframework.stereotype.Service; | |
| 8 | + | |
| 9 | +import javax.imageio.ImageIO; | |
| 10 | +import javax.servlet.ServletOutputStream; | |
| 11 | +import javax.servlet.http.HttpServletResponse; | |
| 12 | +import java.awt.image.BufferedImage; | |
| 13 | +import java.io.ByteArrayOutputStream; | |
| 14 | +import java.io.IOException; | |
| 15 | +import java.io.OutputStream; | |
| 16 | +import java.util.Base64; | |
| 17 | + | |
| 18 | + | |
| 19 | +@Service | |
| 20 | +public class GarQRCodeServiceImpl implements GarQRCodeService { | |
| 21 | + | |
| 22 | + @Value("${front.url}") | |
| 23 | + private String url; | |
| 24 | + | |
| 25 | + @Override | |
| 26 | + public String createQRCode(String orderId, HttpServletResponse response) { | |
| 27 | + String base64 = ""; | |
| 28 | + try { | |
| 29 | + BufferedImage image = QRCodeUtil.createQRCode(url + orderId, 200); | |
| 30 | + base64 = bufferedImageToBase64(image); | |
| 31 | + } catch (WriterException e) { | |
| 32 | + throw new RuntimeException(e); | |
| 33 | + } catch (IOException e) { | |
| 34 | + throw new RuntimeException(e); | |
| 35 | + } | |
| 36 | + | |
| 37 | + return base64; | |
| 38 | + } | |
| 39 | + private String bufferedImageToBase64(BufferedImage image) throws IOException { | |
| 40 | + // 创建一个字节数组输出流 | |
| 41 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
| 42 | + | |
| 43 | + // 将BufferedImage写入字节数组输出流 | |
| 44 | + ImageIO.write(image, "jpg", baos); | |
| 45 | + | |
| 46 | + // 转换为字节数组 | |
| 47 | + byte[] imageBytes = baos.toByteArray(); | |
| 48 | + | |
| 49 | + // 使用Base64编码 | |
| 50 | + String base64String = Base64.getEncoder().encodeToString(imageBytes); | |
| 51 | + | |
| 52 | + // 关闭字节数组输出流 | |
| 53 | + baos.close(); | |
| 54 | + | |
| 55 | + return "data:image/jpg;base64," + base64String; | |
| 56 | + } | |
| 57 | + | |
| 58 | +} | ... | ... |
trash-garbage/src/main/java/com/trash/garbage/utils/QRCodeUtil.java
0 → 100644
| 1 | +package com.trash.garbage.utils; | |
| 2 | + | |
| 3 | +import com.google.zxing.BarcodeFormat; | |
| 4 | +import com.google.zxing.EncodeHintType; | |
| 5 | +import com.google.zxing.WriterException; | |
| 6 | +import com.google.zxing.common.BitMatrix; | |
| 7 | +import com.google.zxing.qrcode.QRCodeWriter; | |
| 8 | + | |
| 9 | +import javax.imageio.ImageIO; | |
| 10 | +import java.awt.*; | |
| 11 | +import java.awt.image.BufferedImage; | |
| 12 | +import java.io.File; | |
| 13 | +import java.io.IOException; | |
| 14 | +import java.util.HashMap; | |
| 15 | + | |
| 16 | +public class QRCodeUtil { | |
| 17 | + | |
| 18 | + /** | |
| 19 | + * 生成指定网址大小及图片格式的二维码 | |
| 20 | + * | |
| 21 | + * @param url 网址 | |
| 22 | + * @param size 尺寸 | |
| 23 | + */ | |
| 24 | + public static BufferedImage createQRCode(String url, int size) | |
| 25 | + throws WriterException, IOException { | |
| 26 | + | |
| 27 | + HashMap<EncodeHintType, Object> hintMap = new HashMap<EncodeHintType, Object>(); | |
| 28 | + hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8"); | |
| 29 | + | |
| 30 | + QRCodeWriter qrCodeWriter = new QRCodeWriter(); | |
| 31 | + BitMatrix bitMatrix = qrCodeWriter.encode(url, BarcodeFormat.QR_CODE, size, size, hintMap); | |
| 32 | + | |
| 33 | + int matrixWidth = bitMatrix.getWidth(); | |
| 34 | + BufferedImage image = new BufferedImage(matrixWidth, matrixWidth, BufferedImage.TYPE_INT_RGB); | |
| 35 | + image.createGraphics(); | |
| 36 | + | |
| 37 | + Graphics2D graphics = (Graphics2D) image.getGraphics(); | |
| 38 | + graphics.setColor(Color.WHITE); | |
| 39 | + graphics.fillRect(0, 0, matrixWidth, matrixWidth); | |
| 40 | + graphics.setColor(Color.BLACK); | |
| 41 | + | |
| 42 | + for (int i = 0; i < matrixWidth; i++) { | |
| 43 | + for (int j = 0; j < matrixWidth; j++) { | |
| 44 | + if (bitMatrix.get(i, j)) { | |
| 45 | + graphics.fillRect(i, j, 1, 1); | |
| 46 | + } | |
| 47 | + } | |
| 48 | + } | |
| 49 | + return image; | |
| 50 | +// ImageIO.write(image, fileType, qrFile); | |
| 51 | + } | |
| 52 | +} | |
| 0 | 53 | \ No newline at end of file | ... | ... |