diff --git a/ruoyi-common/pom.xml b/ruoyi-common/pom.xml index 78cdcba..4f3ef36 100644 --- a/ruoyi-common/pom.xml +++ b/ruoyi-common/pom.xml @@ -125,6 +125,12 @@ javax.servlet-api + + com.google.zxing + core + 3.3.3 + + \ No newline at end of file diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java index 07938a5..e8830ba 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java @@ -5,10 +5,13 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.file.Paths; -import java.util.Base64; -import java.util.Objects; -import java.util.UUID; +import java.util.*; +import com.google.zxing.BarcodeFormat; +import com.google.zxing.EncodeHintType; +import com.google.zxing.MultiFormatWriter; +import com.google.zxing.WriterException; +import com.google.zxing.common.BitMatrix; import org.apache.commons.io.FilenameUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -272,4 +275,27 @@ public class FileUploadUtils return null; } } + + public static String code(String content,String path) { + String picPath = ""; + try { + String codeName = UUID.randomUUID().toString();// 二维码的图片名 + String imageType = "jpg";// 图片类型 + picPath = codeName + "." + imageType; + MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); + Map hints = new HashMap(); + hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); + BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 400, 400, hints); + File file1 = new File(path, picPath); + MatrixToImageWriter.writeToFile(bitMatrix, imageType, file1); + + } catch (WriterException e) { + logger.error("code WriterException:", e); + e.printStackTrace(); + } catch (IOException e) { + logger.error("code IOException:", e); + e.printStackTrace(); + } + return picPath; + } } diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/MatrixToImageWriter.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/MatrixToImageWriter.java new file mode 100644 index 0000000..9a8f9b3 --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/MatrixToImageWriter.java @@ -0,0 +1,43 @@ +package com.ruoyi.common.utils.file; + +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; + +import com.google.zxing.common.BitMatrix; + +/** + * 配置图像写入器 + * + * @author 18316 + * + */ +public class MatrixToImageWriter { + private static final int BLACK = 0xFF000000; + private static final int WHITE = 0xFFFFFFFF; + + private MatrixToImageWriter() { + } + + public static BufferedImage toBufferedImage(BitMatrix matrix) { + int width = matrix.getWidth(); + int height = matrix.getHeight(); + BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE); + } + } + return image; + } + + public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException { + BufferedImage image = toBufferedImage(matrix); + if (!ImageIO.write(image, format, file)) { + throw new IOException("Could not write an image of format " + format + " to " + file); + } + } + +} diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java index 2b7832c..b8e4154 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java @@ -2,6 +2,7 @@ package com.ruoyi.framework.web.service; import javax.annotation.Resource; +import com.alibaba.fastjson.JSONObject; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.enums.UserStatus; import com.ruoyi.common.utils.*; @@ -34,6 +35,7 @@ import com.ruoyi.system.service.ISysConfigService; import com.ruoyi.system.service.ISysUserService; import java.util.Date; +import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -204,6 +206,13 @@ public class SysLoginService tDoctor.setUserId(sysUser.getUserId()); tDoctorMapper.insertTDoctor(tDoctor); tDoctor.setIdentifier("10000000" + tDoctor.getId()); + Map map = new HashMap(); + map.put("id", tDoctor.getId()); + map.put("from", "yinuo"); + String content = JSONObject.toJSONString(map); + String qrCode = FileUploadUtils.code(content, uploadFilePath); + tDoctor.setQrCode(qrCode); + log.info("update doctor :{}", tDoctor); tDoctorMapper.updateTDoctor(tDoctor); } LoginUser loginUser = new LoginUser(); diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WechatServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WechatServiceImpl.java index 3719712..b467451 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WechatServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WechatServiceImpl.java @@ -32,6 +32,8 @@ import org.springframework.stereotype.Service; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.util.Date; +import java.util.HashMap; +import java.util.Map; import java.util.Objects; @Service @@ -183,6 +185,14 @@ public class WechatServiceImpl implements IWechatService { tDoctor.setUserId(sysUser.getUserId()); tDoctorMapper.insertTDoctor(tDoctor); tDoctor.setIdentifier("10000000" + tDoctor.getId()); + + Map map = new HashMap(); + map.put("id", tDoctor.getId()); + map.put("from", "yinuo"); + String content = JSONObject.toJSONString(map); + String qrCode = FileUploadUtils.code(content, uploadFilePath); + tDoctor.setQrCode(qrCode); + logger.info("update doctor :{}", tDoctor); tDoctorMapper.updateTDoctor(tDoctor); wechatUser.setUserId(tDoctor.getId());