diff --git a/ruoyi-modules/ruoyi-file/src/main/java/com/xhpc/file/controller/SysFileController.java b/ruoyi-modules/ruoyi-file/src/main/java/com/xhpc/file/controller/SysFileController.java
index 6b6306dc..249195ef 100644
--- a/ruoyi-modules/ruoyi-file/src/main/java/com/xhpc/file/controller/SysFileController.java
+++ b/ruoyi-modules/ruoyi-file/src/main/java/com/xhpc/file/controller/SysFileController.java
@@ -73,10 +73,10 @@ public class SysFileController
}
}
- /*
- * TODO 阿里云图片上传
- * @author fjd
- * @date 2020-07-03 16:13
+ /**
+ * 阿里云图片上传
+ * @author
+ * @date 2021-09-01 17:15
*/
@PostMapping("photoupload")
@ApiOperation(value = "上传", notes = "上传")
diff --git a/ruoyi-modules/ruoyi-file/src/main/java/com/xhpc/file/domain/XhpcImg.java b/ruoyi-modules/ruoyi-file/src/main/java/com/xhpc/file/domain/XhpcImg.java
index 5f2710f2..cc33092f 100644
--- a/ruoyi-modules/ruoyi-file/src/main/java/com/xhpc/file/domain/XhpcImg.java
+++ b/ruoyi-modules/ruoyi-file/src/main/java/com/xhpc/file/domain/XhpcImg.java
@@ -30,6 +30,21 @@ public class XhpcImg extends BaseEntity {
*/
private Integer delFlag;
+ /**
+ * 生成的图片所对应的终端的终端号
+ */
+ private Long serialNumber;
+
+ public Long getSerialNumber() {
+
+ return serialNumber;
+ }
+
+ public void setSerialNumber(Long serialNumber) {
+
+ this.serialNumber = serialNumber;
+ }
+
public Long getImgId() {
return imgId;
}
diff --git a/xhpc-modules/xhpc-charging-station/pom.xml b/xhpc-modules/xhpc-charging-station/pom.xml
index 4cba97da..3976e61e 100644
--- a/xhpc-modules/xhpc-charging-station/pom.xml
+++ b/xhpc-modules/xhpc-charging-station/pom.xml
@@ -22,6 +22,26 @@
+
+
+ junit
+ junit
+ 4.12
+
+
+
+
+ com.google.zxing
+ core
+ 3.2.1
+
+
+
+ com.google.zxing
+ javase
+ 3.2.1
+
+
com.alibaba.cloud
@@ -102,6 +122,13 @@
3.0.0
compile
+
+ com.aliyun.oss
+ aliyun-sdk-oss
+ 3.10.2
+ compile
+
+
${project.artifactId}
diff --git a/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/mapper/XhpcImgMapper.java b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/mapper/XhpcImgMapper.java
new file mode 100644
index 00000000..a2d0ffe1
--- /dev/null
+++ b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/mapper/XhpcImgMapper.java
@@ -0,0 +1,22 @@
+package com.xhpc.charging.station.mapper;
+
+import org.apache.ibatis.annotations.Param;
+
+/**
+ * 上传图片信息
+ *
+ * @author
+ */
+public interface XhpcImgMapper {
+
+ /**
+ * 新增 上传图片
+ *
+ *
+ * @param imgUrl 图片的url
+ * @param terminalId 图片所对应的终端的id
+ * @return
+ */
+ void insert(@Param("imgUrl") String imgUrl,@Param("terminalId") Long terminalId);
+
+}
diff --git a/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/service/XhpcChargingPileServiceImpl.java b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/service/XhpcChargingPileServiceImpl.java
index 428a8791..9bb78c12 100644
--- a/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/service/XhpcChargingPileServiceImpl.java
+++ b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/service/XhpcChargingPileServiceImpl.java
@@ -5,13 +5,16 @@ import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.domain.XhpcChargingPile;
import com.xhpc.common.domain.XhpcTerminal;
import com.xhpc.charging.station.mapper.XhpcChargingPileMapper;
+import com.xhpc.charging.station.mapper.XhpcImgMapper;
import com.xhpc.charging.station.mapper.XhpcTerminalMapper;
+import com.xhpc.charging.station.utils.QrImgUtils;
import com.xhpc.common.api.PowerPileService;
import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.domain.XhpcChargingPile;
import com.xhpc.common.domain.XhpcTerminal;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -31,6 +34,14 @@ public class XhpcChargingPileServiceImpl implements IXhpcChargingPileService {
@Autowired
private PowerPileService powerPileService;
+ //上传图片全局变量
+ @Autowired
+ private Environment environment;
+ //上传图片Mapper
+ @Autowired
+ private XhpcImgMapper xhpcImgMapper;
+
+
/**
* 桩列表
@@ -82,13 +93,63 @@ public class XhpcChargingPileServiceImpl implements IXhpcChargingPileService {
xhpcTerminal.setStatus(0);
xhpcTerminal.setRateModelId(rateModelId);
xhpcTerminalMapper.addXhpcTerminal(xhpcTerminal);
+
+ //生成二维码图片,并上传
+ QrImgUtils.uploadImg(xhpcTerminal,String.valueOf(chargingPileId),environment,xhpcImgMapper);
+
+// //生成桩的二维码图片
+// //1.生成二维码链接内容:
+// StringBuilder prefix = new StringBuilder("https://xhpc.scxhua.com?pNum=");
+// String QrContent = prefix.append(xhpcTerminal.getSerialNumber()).toString();
+//
+// //2.生成图片:
+// try {
+// //2.1生成指定规则的图片名
+// String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmssSSS") + chargingPileId+".png"; //2021090814171100766.png
+//
+// //2.2生成图片
+// //2.1.1设置生成的图片二维码的存放位置:
+// String destPath = "C:\\Users\\14554\\Desktop\\公司项目\\图片\\小华充电二维码";
+// //2.1.2获取Logo图片位置
+// String imgPath = "C:\\Users\\14554\\Desktop\\公司项目\\图片\\小华充电logo.png";
+//
+// QRCodeUtil.encode(QrContent,
+// imgPath,
+// destPath,
+// true,
+// fileName
+// );
+//
+// //3.上传图片至服务器
+// // 创建OSSClient实例
+// OSSClient ossClient = new OSSClient(environment.getProperty("oss.endpoint"), environment.getProperty("oss.access-key"), environment.getProperty("oss.secret-key"));
+// // 上传文件流
+// ossClient.putObject(environment.getProperty("oss.bucket-name"), serialNumber+"/"+fileName, new File("C:\\Users\\14554\\Desktop\\公司项目\\图片\\小华充电二维码"+"\\"+fileName));
+// ossClient.shutdown();
+//
+// //4.将生成的图片的文件名和对应的终端的id放入数据库xhpc_img表中
+// Long terminalId = xhpcTerminal.getTerminalId();
+// xhpcImgMapper.insert(fileName,terminalId);
+//
+// //4.删除生成的二维码图片
+// File QrImg = new File(destPath +"\\"+ fileName);
+// if(QrImg.exists()){
+// QrImg.delete();
+// }else {
+// throw new RuntimeException("名字为" + fileName + "的二维码不存在");
+// }
+//
+// } catch (Exception e) {
+// e.printStackTrace();
+// System.out.println(e.getMessage());
+// }
}
}
//插入redis 桩的编号
- HashSet noSet = new HashSet<>();
- noSet.add(serialNumber);
- R r = powerPileService.addPileWhitelist(chargingStationId, noSet);
+// HashSet noSet = new HashSet<>();
+// noSet.add(serialNumber);
+// R r = powerPileService.addPileWhitelist(chargingStationId, noSet);
return AjaxResult.success();
}
diff --git a/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/utils/QRCodeUtil.java b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/utils/QRCodeUtil.java
new file mode 100644
index 00000000..6a121e38
--- /dev/null
+++ b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/utils/QRCodeUtil.java
@@ -0,0 +1,144 @@
+package com.xhpc.charging.station.utils;
+
+import com.google.zxing.BarcodeFormat;
+import com.google.zxing.EncodeHintType;
+import com.google.zxing.MultiFormatWriter;
+import com.google.zxing.common.BitMatrix;
+import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
+
+import javax.imageio.ImageIO;
+import java.awt.*;
+import java.awt.geom.RoundRectangle2D;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.util.Hashtable;
+
+/**
+ *
+ * @ClassName: QRCodeUtil
+ * @Description: 二维码生成工具类
+ * @author w
+ *
+ */
+public class QRCodeUtil {
+
+ private static final String CHARSET = "utf-8";
+ private static final String FORMAT_NAME = "PNG";
+ // 二维码尺寸
+ private static final int QRCODE_SIZE = 400;
+ // LOGO宽度
+ private static final int WIDTH = 100;
+ // LOGO高度
+ private static final int HEIGHT = 100;
+
+ private static BufferedImage createImage(String content, String imgPath,
+ boolean needCompress) throws Exception {
+ Hashtable hints = new Hashtable();
+ hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
+ hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
+ hints.put(EncodeHintType.MARGIN, 1);
+ BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
+ BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
+ int width = bitMatrix.getWidth();
+ int height = bitMatrix.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, bitMatrix.get(x, y) ? 0xFF000000
+ : 0xFFFFFFFF);
+ }
+ }
+ if (imgPath == null || "".equals(imgPath)) {
+ return image;
+ }
+ // 插入图片
+ QRCodeUtil.insertImage(image, imgPath, needCompress);
+ return image;
+ }
+
+ /**
+ * 插入LOGO
+ *
+ * @param source
+ * 二维码图片
+ * @param imgPath
+ * LOGO图片地址
+ * @param needCompress
+ * 是否压缩
+ * @throws Exception
+ */
+ private static void insertImage(BufferedImage source, String imgPath,
+ boolean needCompress) throws Exception {
+ File file = new File(imgPath);
+ if (!file.exists()) {
+ System.err.println(""+imgPath+" 该文件不存在!");
+ return;
+ }
+ Image src = ImageIO.read(new File(imgPath));
+ int width = src.getWidth(null);
+ int height = src.getHeight(null);
+ if (needCompress) { // 压缩LOGO
+ if (width > WIDTH) {
+ width = WIDTH;
+ }
+ if (height > HEIGHT) {
+ height = HEIGHT;
+ }
+ Image image = src.getScaledInstance(width, height,
+ Image.SCALE_SMOOTH);
+ BufferedImage tag = new BufferedImage(width, height,
+ BufferedImage.TYPE_INT_RGB);
+ Graphics g = tag.getGraphics();
+ g.drawImage(image, 0, 0, null); // 绘制缩小后的图
+ g.dispose();
+ src = image;
+ }
+ // 插入LOGO
+ Graphics2D graph = source.createGraphics();
+ int x = (QRCODE_SIZE - width) / 2;
+ int y = (QRCODE_SIZE - height) / 2;
+ graph.drawImage(src, x, y, width, height, null);
+ Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
+ graph.setStroke(new BasicStroke(3f));
+ graph.draw(shape);
+ graph.dispose();
+ }
+
+ /**
+ * 生成二维码(内嵌LOGO)
+ *
+ * @param content
+ * 内容
+ * @param imgPath
+ * LOGO地址
+ * @param destPath
+ * 存放目录
+ * @param needCompress
+ * 是否压缩LOGO
+ * @param fileName
+ * 生成的图片名称
+ * @throws Exception
+ */
+ public static void encode(String content, String imgPath, String destPath,
+ boolean needCompress,String fileName) throws Exception {
+ BufferedImage image = QRCodeUtil.createImage(content, imgPath,
+ needCompress);
+ mkdirs(destPath);
+// String file = new Random().nextInt(99999999)+".png";
+// String file = fileName+".png";
+ ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+fileName));
+ }
+
+ /**
+ * 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
+ * @param destPath 存放目录
+ */
+ public static void mkdirs(String destPath) {
+ File file =new File(destPath);
+ //当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
+ if (!file.exists() && !file.isDirectory()) {
+ file.mkdirs();
+ }
+ }
+}
\ No newline at end of file
diff --git a/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/utils/QrImgUtils.java b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/utils/QrImgUtils.java
new file mode 100644
index 00000000..efeba076
--- /dev/null
+++ b/xhpc-modules/xhpc-charging-station/src/main/java/com/xhpc/charging/station/utils/QrImgUtils.java
@@ -0,0 +1,92 @@
+package com.xhpc.charging.station.utils;
+
+import cn.hutool.core.date.DateUtil;
+import com.aliyun.oss.OSSClient;
+import com.xhpc.charging.station.mapper.XhpcImgMapper;
+import com.xhpc.common.domain.XhpcTerminal;
+import org.springframework.core.env.Environment;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Date;
+import java.util.Properties;
+
+
+public class QrImgUtils {
+
+ //生成的二维码存放位置:
+ private static String destPath;
+ //获取Logo图片位置:
+ private static String imgPath;
+
+ //进行工具初始化
+ static {
+
+ InputStream is = QrImgUtils.class.getClassLoader().getResourceAsStream("QrImgUtils.properties");
+ Properties properties = new Properties();
+ try {
+ properties.load(is);
+ } catch (IOException e) {
+ e.printStackTrace();
+ System.out.println("QrImgUtils无法加载配置文件");
+ }
+
+ destPath = properties.getProperty("destPath");
+ imgPath = properties.getProperty("imgPath");
+ }
+
+ /**
+ * @Author WH
+ * @Date 2021/9/8 15:55
+ * @Description upload images to Server
+ * @Param [xhpcTerminal, chargingPileId, environment, xhpcImgMapper]
+ * @Return void
+ * @Since version-1.0
+ */
+ public static void uploadImg(XhpcTerminal xhpcTerminal, String chargingPileId, Environment environment, XhpcImgMapper xhpcImgMapper) {
+ //生成桩的二维码图片
+ //1.生成二维码链接内容:
+ StringBuilder prefix = new StringBuilder("https://xhpc.scxhua.com?pNum=");
+ String QrContent = prefix.append(xhpcTerminal.getSerialNumber()).toString();
+
+ //2.生成图片:
+ try {
+ //2.1生成指定规则的图片名
+ String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmssSSS") + chargingPileId + ".png"; //2021090814171100766.png
+
+ //2.2生成图片
+ QRCodeUtil.encode(QrContent,
+ imgPath,
+ destPath,
+ true,
+ fileName
+ );
+
+ //3.上传图片至服务器
+ // 创建OSSClient实例
+ OSSClient ossClient = new OSSClient(environment.getProperty("oss.endpoint"), environment.getProperty("oss.access-key"), environment.getProperty("oss.secret-key"));
+ // 上传文件流
+ ossClient.putObject(environment.getProperty("oss.bucket-name"), xhpcTerminal.getPileSerialNumber() + "/" + fileName, new File(destPath + "\\" + fileName));
+ ossClient.shutdown();
+
+ //4.将生成的图片的文件名和对应的终端的id放入数据库xhpc_img表中
+ Long terminalId = xhpcTerminal.getTerminalId();
+ xhpcImgMapper.insert(fileName, terminalId);
+
+ //4.删除生成的二维码图片
+ File QrImg = new File(destPath + "\\" + fileName);
+ if (QrImg.exists()) {
+ QrImg.delete();
+ } else {
+ throw new RuntimeException("名字为" + fileName + "的二维码不存在");
+ }
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ System.out.println(e.getMessage());
+ }
+
+ }
+
+}
diff --git a/xhpc-modules/xhpc-charging-station/src/main/resources/QrImgUtils.properties b/xhpc-modules/xhpc-charging-station/src/main/resources/QrImgUtils.properties
new file mode 100644
index 00000000..7ad06636
--- /dev/null
+++ b/xhpc-modules/xhpc-charging-station/src/main/resources/QrImgUtils.properties
@@ -0,0 +1,4 @@
+#ɵĶάλã
+destPath=D:\\Enterprise_Resources\\XiaoHuaQrImgs
+#LogoͼƬȡλã
+imgPath=D:\\Enterprise_Resources\\XiaoHuaLogoImg\\xhcd_logo.png
\ No newline at end of file
diff --git a/xhpc-modules/xhpc-charging-station/src/main/resources/mapper/XhpcImgMapper.xml b/xhpc-modules/xhpc-charging-station/src/main/resources/mapper/XhpcImgMapper.xml
new file mode 100644
index 00000000..e419470d
--- /dev/null
+++ b/xhpc-modules/xhpc-charging-station/src/main/resources/mapper/XhpcImgMapper.xml
@@ -0,0 +1,11 @@
+
+
+
+
+
+ INSERT INTO xhpc_img(url,terminal_id) VALUES(#{imgUrl},#{terminalId})
+
+
+