Browse Source

二维码生成

dzx 1 year ago
parent
commit
362a3a5c9c

+ 32 - 12
src/main/java/com/xjrsoft/common/utils/QrCodeUtil.java

@@ -11,22 +11,14 @@ import java.awt.image.BufferedImage;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.nio.charset.StandardCharsets;
+import java.util.Base64;
 import java.util.HashMap;
 
 public class QrCodeUtil {
 
-	/**
-	 *	返回png格式的BufferedImage
-	 * @param url 链接地址
-	 * @param width 二维码的宽度
-	 * @param height 二维码的高度
-	 * @param margin 边距,只能是整数
-	 * @return
-	 * @throws Exception
-	 */
-	public static BufferedImage create(String url, int width, int height, int margin) throws Exception{
+	private static ByteArrayInputStream create(String url, int width, int height, int margin) throws Exception{
         HashMap<EncodeHintType, Object> hints = new HashMap<>();
-        hints.put(EncodeHintType.CHARACTER_SET, StandardCharsets.UTF_8);
+        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
 		hints.put(EncodeHintType.MARGIN, margin);
 
         ByteArrayOutputStream stream = new ByteArrayOutputStream();
@@ -35,8 +27,36 @@ public class QrCodeUtil {
         MatrixToImageWriter.writeToStream(bitMatrix, "png", stream);
 
 		ByteArrayInputStream input = new ByteArrayInputStream(stream.toByteArray());
-        return ImageIO.read(input);
+        return input;
 	}
 
+	/**
+	 * @param url 链接地址
+	 * @param width 二维码的宽度
+	 * @param height 二维码的高度
+	 * @param margin 边距,只能是整数
+	 * @return 返回png格式的BufferedImage
+	 * @throws Exception
+	 */
+	public static BufferedImage createBufferedImage(String url, int width, int height, int margin) throws Exception{
+		ByteArrayInputStream input = create(url, width, height, margin);
+		return ImageIO.read(input);
+	}
 
+	/**
+	 * @param url 链接地址
+	 * @param width 二维码的宽度
+	 * @param height 二维码的高度
+	 * @param margin 边距,只能是整数
+	 * @return 返回png格式的Base64
+	 * @throws Exception
+	 */
+	public static String createBase64(String url, int width, int height, int margin) throws Exception{
+		ByteArrayInputStream input = create(url, width, height, margin);
+		byte[] bytes = new byte[1024];
+		input.read(bytes);
+		Base64.Encoder encoder = Base64.getEncoder();
+		String QRCodeBase64 = encoder.encodeToString(bytes);
+		return QRCodeBase64;
+	}
 }

+ 22 - 1
src/main/java/com/xjrsoft/module/textbook/controller/WfStudentTextbookClaimController.java

@@ -11,7 +11,9 @@ import com.baomidou.mybatisplus.core.toolkit.StringPool;
 import com.xjrsoft.common.page.ConventPage;
 import com.xjrsoft.common.page.PageOutput;
 import com.xjrsoft.common.model.result.RT;
+import com.xjrsoft.common.utils.QrCodeUtil;
 import com.xjrsoft.common.utils.VoToColumnUtil;
+import com.xjrsoft.config.CommonPropertiesConfig;
 import com.xjrsoft.module.textbook.dto.AddWfStudentTextbookClaimDto;
 import com.xjrsoft.module.textbook.dto.UpdateWfStudentTextbookClaimDto;
 import cn.dev33.satoken.annotation.SaCheckPermission;
@@ -45,6 +47,7 @@ public class WfStudentTextbookClaimController {
 
 
     private final IWfStudentTextbookClaimService wfStudentTextbookClaimService;
+    private final CommonPropertiesConfig commonPropertiesConfig;
 
     @GetMapping(value = "/page")
     @ApiOperation(value="教材申领列表(分页)")
@@ -71,6 +74,24 @@ public class WfStudentTextbookClaimController {
         return RT.ok(BeanUtil.toBean(wfStudentTextbookClaim, WfStudentTextbookClaimVo.class));
     }
 
+    @GetMapping(value = "/info-qrcode")
+    @ApiOperation(value="教材领取-生成二维码")
+    @SaCheckPermission("wfstudenttextbookclaim:detail")
+    public RT<String> qrcode(@RequestParam Long id) throws Exception {
+        String url = commonPropertiesConfig.getDomainApp() + "/pages/material/grant?id=" + id;
+        int width = 200;
+        int height = 200;
+        int margin = 1;
+
+        try {
+            String base64 = QrCodeUtil.createBase64(url, width, height, margin);
+            return RT.ok(base64);
+        } catch (Exception e) {
+            throw e;
+        }
+
+    }
+
 
     @PostMapping
     @ApiOperation(value = "新增教材申领")
@@ -78,7 +99,7 @@ public class WfStudentTextbookClaimController {
     public RT<Boolean> add(@Valid @RequestBody AddWfStudentTextbookClaimDto dto){
         WfStudentTextbookClaim wfStudentTextbookClaim = BeanUtil.toBean(dto, WfStudentTextbookClaim.class);
         boolean isSuccess = wfStudentTextbookClaimService.add(wfStudentTextbookClaim);
-    return RT.ok(isSuccess);
+        return RT.ok(isSuccess);
     }
 
     @PutMapping