完成用户邀请码功能

master
胡锐 2 months ago
parent 1ff35f0c36
commit d4cde237c5
  1. 3
      cweb/src/main/java/com/hfkj/config/ConfigListener.java
  2. 50
      service/src/main/java/com/hfkj/common/QRCodeGenerator.java
  3. 49
      service/src/main/java/com/hfkj/service/user/impl/BsUserInviteCodeServiceImpl.java
  4. 4
      service/src/main/resources/dev/commonConfig.properties

@ -10,10 +10,13 @@ public class ConfigListener implements ServletContextListener {
@Resource @Resource
private SysConfig sysConfig; private SysConfig sysConfig;
@Resource
private CommonSysConfig commonSysConfig;
@Override @Override
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
SysConst.setSysConfig(sysConfig); SysConst.setSysConfig(sysConfig);
CommonSysConst.setSysConfig(commonSysConfig);
} }
@Override @Override

@ -1,6 +1,7 @@
package com.hfkj.common; package com.hfkj.common;
import com.google.zxing.BarcodeFormat; import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException; import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix; import com.google.zxing.common.BitMatrix;
@ -12,6 +13,8 @@ import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/** /**
* @Auther: 胡锐 * @Auther: 胡锐
@ -29,8 +32,9 @@ public class QRCodeGenerator {
*/ */
public static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException { public static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException {
QRCodeWriter qrCodeWriter = new QRCodeWriter(); QRCodeWriter qrCodeWriter = new QRCodeWriter();
Map<EncodeHintType,Object> hints = new HashMap<>();
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height); hints.put(EncodeHintType.MARGIN, 0);
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height,hints);
File file = new File(filePath); File file = new File(filePath);
if(!file.exists()){ if(!file.exists()){
@ -56,6 +60,48 @@ public class QRCodeGenerator {
ImageIO.write(read , "png" , new File(CommonSysConst.getSysConfig().getFilesystem() + "/" + path)); ImageIO.write(read , "png" , new File(CommonSysConst.getSysConfig().getFilesystem() + "/" + path));
} }
public static String overlapImage(String path ,String number) {
try {
// 创建BufferedImage对象
int width = 100;
int height = 45;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
// 获取Graphics2D对象
Graphics2D g2d = image.createGraphics();
// 设置背景颜色并填充
g2d.setComposite(AlphaComposite.Clear);
g2d.fillRect(0, 0, width, height);
g2d.setComposite(AlphaComposite.SrcOver);
// 设置背景颜色并填充
//g2d.setColor(Color.BLACK);
//g2d.fillRect(0, 0, width, height);
// 设置字体和颜色
Font font = new Font("Arial", Font.BOLD, 30);
g2d.setFont(font);
g2d.setColor(Color.BLACK);
// 绘制数字到图片上
int x = (width - g2d.getFontMetrics().stringWidth(number)) / 2;
int y = (height + g2d.getFontMetrics().getHeight()) / 2;
g2d.drawString(number, x, y);
// 释放Graphics2D对象的资源
g2d.dispose();
// 将BufferedImage保存为图片文件
File outputFile = new File(path + number + ".png");
ImageIO.write(image, "png", outputFile);
return outputFile.getName();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) { public static void main(String[] args) {
try { try {
generateQRCodeImage("This is my first QR Code", 350, 350, "D:\\/ss/qr1.png"); generateQRCodeImage("This is my first QR Code", 350, 350, "D:\\/ss/qr1.png");

@ -13,6 +13,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -37,24 +42,56 @@ public class BsUserInviteCodeServiceImpl implements BsUserInviteCodeService {
data.setUserId(userId); data.setUserId(userId);
data.setCreateTime(new Date()); data.setCreateTime(new Date());
data.setStatus(1); data.setStatus(1);
System.out.println(commonSysConfig.getDomain());
// 生成二维码 // 生成二维码
String fileUrl = "/userInviteCode/"+data.getUserId()+".png"; String fileUrl = "/userInviteCode/"+data.getUserId()+"_"+System.currentTimeMillis()+".png";
QRCodeGenerator.generateQRCodeImage( QRCodeGenerator.generateQRCodeImage(
""+data.getUserId(), ""+data.getUserId(),
260, 180,
260, 180,
commonSysConfig.getFilesystem() + fileUrl commonSysConfig.getFilesystem() + fileUrl
); );
// 背景图 // 背景图
// QRCodeGenerator.overlapImage(fileUrl, "/template/qrCodeBackdrop1_1.png",740,110); QRCodeGenerator.overlapImage(fileUrl, "/template/invite_background.png",660,1470);
data.setQrCodeImg(fileUrl);
// 邀请码存放目录
String invitationCodeUrl = "userInviteCode/";
// 生成邀请码图片
String overlapImage = QRCodeGenerator.overlapImage(commonSysConfig.getFilesystem() + "/" + invitationCodeUrl, "" + userId);
QRCodeGenerator.overlapImage(
"/"+invitationCodeUrl+overlapImage,
fileUrl,285,1566);
data.setQrCodeImg(CommonSysConst.getSysConfig().getDomain() + "/filesystem/" + invitationCodeUrl+overlapImage);
userInviteCodeMapper.insert(data); userInviteCodeMapper.insert(data);
// 缓存 // 缓存
redisUtil.hset(CACHE_KEY, data.getUserId()+"", data); redisUtil.hset(CACHE_KEY, data.getUserId()+"", data);
// 删除临时文件
new File(CommonSysConst.getSysConfig().getFilesystem() + fileUrl).delete();
return data; return data;
} }
/* public static void main(String[] args) throws Exception {
String userId = "123456";
// 生成二维码
String fileUrl = "/userInviteCode/"+userId+"_"+System.currentTimeMillis()+".png";
QRCodeGenerator.generateQRCodeImage(
userId,
155,
155,
"D:/home/project/youtao/filesystem/" + fileUrl
);
// 背景图
QRCodeGenerator.overlapImage2(fileUrl, "D:/home/project/youtao/filesystem/template/invite_background.png",660,1470);
// 邀请码
String invitationCodeUrl = "D:/home/project/youtao/filesystem/userInviteCode/";
QRCodeGenerator.overlapImage2("userInviteCode/"+QRCodeGenerator.overlapImage(invitationCodeUrl, "100001"),
"D:/home/project/youtao/filesystem/"+fileUrl,285,1566);
}*/
@Override @Override
public BsUserInviteCode getDetail(Long userId) { public BsUserInviteCode getDetail(Long userId) {

@ -1,6 +1,6 @@
filesystem=/home/project/youtao/filesystem filesystem=/home/project/youtao/filesystem
domain=47.92.163.212 domain=http://47.92.163.212
taoBaoPostUrl = https://eco.taobao.com/router/rest taoBaoPostUrl = https://eco.taobao.com/router/rest
taoBaoAppKey = 34807616 taoBaoAppKey = 34807616
taoBaoAppSecret = f04baedca9cd794665dfa04a2fcbfd86 taoBaoAppSecret = f04baedca9cd794665dfa04a2fcbfd86

Loading…
Cancel
Save