Commit db2eff32 authored by liuyang's avatar liuyang

完成用户证书生成功能

parent f12c0db8
...@@ -5,6 +5,7 @@ import cn.binarywang.wx.miniapp.api.WxMaUserService; ...@@ -5,6 +5,7 @@ import cn.binarywang.wx.miniapp.api.WxMaUserService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo; import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.qiankun.annotation.Auth; import com.qiankun.annotation.Auth;
import com.qiankun.config.WxMaConfiguration; import com.qiankun.config.WxMaConfiguration;
...@@ -20,10 +21,7 @@ import com.qiankun.service.UserService; ...@@ -20,10 +21,7 @@ import com.qiankun.service.UserService;
import com.qiankun.utils.JwtTokenUtil; import com.qiankun.utils.JwtTokenUtil;
import com.qiankun.utils.QRCodeUtil; import com.qiankun.utils.QRCodeUtil;
import com.qiankun.utils.UserUtils; import com.qiankun.utils.UserUtils;
import com.qiankun.vo.ActivityVo; import com.qiankun.vo.*;
import com.qiankun.vo.DecryptedDataVo;
import com.qiankun.vo.PaperIdLoginVo;
import com.qiankun.vo.UserVo;
import com.sun.mail.imap.ResyncData; import com.sun.mail.imap.ResyncData;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
...@@ -41,9 +39,11 @@ import org.springframework.web.multipart.MultipartFile; ...@@ -41,9 +39,11 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.List;
@RestController @RestController
@RequestMapping("/wx/mdp") @RequestMapping("/wx/mdp")
...@@ -64,6 +64,8 @@ public class MdpController extends BaseController<Object> { ...@@ -64,6 +64,8 @@ public class MdpController extends BaseController<Object> {
return propertis.getConfigs().get(1).getAppid(); return propertis.getConfigs().get(1).getAppid();
} }
private String uploadAvatarPath = File.separator + "mdp" + File.separator + "uploadAvatar"; private String uploadAvatarPath = File.separator + "mdp" + File.separator + "uploadAvatar";
private String certificateTemplate = "classpath:certificate.jpeg";
private String userCertificatePath = File.separator + "mdp" + File.separator + "certificate";
@Auth(verifyLogin = false,verifyURL = false) @Auth(verifyLogin = false,verifyURL = false)
@RequestMapping(value = "/login",method = RequestMethod.GET) @RequestMapping(value = "/login",method = RequestMethod.GET)
...@@ -435,4 +437,49 @@ public class MdpController extends BaseController<Object> { ...@@ -435,4 +437,49 @@ public class MdpController extends BaseController<Object> {
result.put("activityReg",reg); result.put("activityReg",reg);
return result; return result;
} }
@Auth(verifyLogin = false,verifyURL = false)
@RequestMapping(value = "/user/generateCertificate",method = RequestMethod.GET)
public Map<String,Object> generateCertificate(@RequestHeader String sKey){
Map<String,Object> result = Maps.newHashMap();
String openid = tokenUtil.getUsernameFromToken(sKey);
User user = userService.findByOpenid(openid);
if (user == null){
result.put("status","error");
result.put("errorMsg","用户不存在");
return result;
}
if (user.getStatus().equals("1") || user.getStatus().equals("")){
//未采血或退库的没有证书
result.put("status","error");
result.put("errorMsg","您还未采血,不能生成证书");
return result;
}
if (Strings.isNullOrEmpty(user.getCerfificatePath())){
try {
generateCertificate(user);
} catch (IOException e) {
logger.error(e.getMessage(),e);
result.put("status","error");
result.put("errorMsg","生成证书错误");
return result;
}
userService.save(user);
}
result.put("status","ok");
result.put("certificateImage",user.getCerfificatePath());
return result;
}
private void generateCertificate(User user) throws IOException {
List<CertificateVo> addTexts = Lists.newArrayList();
Calendar now = Calendar.getInstance();
addTexts.add(new CertificateVo(user.getName(),new Font("微软雅黑",Font.BOLD,50),250,1100));
addTexts.add(new CertificateVo(now.get(Calendar.YEAR)+"",new Font("微软雅黑",Font.PLAIN,30),550,1245));
addTexts.add(new CertificateVo(now.get(Calendar.MONTH) +1+"",new Font("微软雅黑",Font.PLAIN,30),680,1245));
addTexts.add(new CertificateVo(now.get(Calendar.DATE)+"",new Font("微软雅黑",Font.PLAIN,30),790,1245));
String outputFilePath = rb.getString("file_path") + this.userCertificatePath + File.separator + user.getId() + ".jpeg";
UserUtils.generateCertificate(this.certificateTemplate,outputFilePath,addTexts);
user.setCerfificatePath(this.userCertificatePath + File.separator + user.getId() + ".jpeg");
}
} }
...@@ -71,6 +71,7 @@ public class User { ...@@ -71,6 +71,7 @@ public class User {
private String status; //状态 1.未采血 2.已采血 3退库 4 再次采血 private String status; //状态 1.未采血 2.已采血 3退库 4 再次采血
private boolean login = false; //是否可以登后台; private boolean login = false; //是否可以登后台;
private String avatarImage; //头像 private String avatarImage; //头像
private String cerfificatePath;//证书图片路径
private Date createTime = new Date(); private Date createTime = new Date();
private Date updateTime = new Date(); private Date updateTime = new Date();
...@@ -361,4 +362,12 @@ public class User { ...@@ -361,4 +362,12 @@ public class User {
public void setAvatarImage(String avatarImage) { public void setAvatarImage(String avatarImage) {
this.avatarImage = avatarImage; this.avatarImage = avatarImage;
} }
public String getCerfificatePath() {
return cerfificatePath;
}
public void setCerfificatePath(String cerfificatePath) {
this.cerfificatePath = cerfificatePath;
}
} }
package com.qiankun.utils; package com.qiankun.utils;
import com.google.common.collect.Lists;
import com.qiankun.entity.Contacts; import com.qiankun.entity.Contacts;
import com.qiankun.entity.User; import com.qiankun.entity.User;
import com.qiankun.vo.CertificateVo;
import com.qiankun.vo.UserVo; import com.qiankun.vo.UserVo;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.ResourceUtils;
import java.util.Iterator; import javax.imageio.ImageIO;
import java.util.Set; import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.*;
import java.util.List;
public class UserUtils { public class UserUtils {
...@@ -45,4 +57,63 @@ public class UserUtils { ...@@ -45,4 +57,63 @@ public class UserUtils {
} }
return vo; return vo;
} }
public static void generateCertificate(String imageTemplatePath, String outputImagePath, List<CertificateVo> addTexts) throws IOException {
InputStream templateInput = null;
FileOutputStream fileOutput = null;
try {
templateInput = loadTemplateImage(imageTemplatePath);
Image image = ImageIO.read(templateInput);
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
BufferedImage bufferedImage = new BufferedImage(imageWidth,imageHeight,BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = bufferedImage.createGraphics();
graphics.drawImage(image,0,0,imageWidth,imageHeight,null);
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,RenderingHints.VALUE_STROKE_NORMALIZE);
graphics.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
//draw text
for (CertificateVo txtVo : addTexts){
graphics.setFont(txtVo.getFont());
graphics.setColor(txtVo.getColor());
graphics.drawString(txtVo.getText(),txtVo.getX(),txtVo.getY());
}
File file = new File(outputImagePath);
if (!file.getParentFile().exists()){
FileUtils.forceMkdir(file.getParentFile());
}
fileOutput = new FileOutputStream(outputImagePath);
ImageIO.write(bufferedImage,"jpeg",fileOutput);
fileOutput.flush();
}finally {
if (templateInput != null){
IOUtils.closeQuietly(templateInput);
}
if (fileOutput != null){
IOUtils.closeQuietly(fileOutput);
}
}
}
private static InputStream loadTemplateImage(String imageTemplatePath) throws IOException {
URL url = ResourceUtils.getURL(imageTemplatePath);
return url.openStream();
}
// public static void main(String[] args){
// try {
// List<CertificateVo> addTexts = Lists.newArrayList();
// Calendar now = Calendar.getInstance();
// addTexts.add(new CertificateVo("刘扬",new Font("微软雅黑",Font.BOLD,50),250,1100));
// addTexts.add(new CertificateVo(now.get(Calendar.YEAR)+"",new Font("微软雅黑",Font.PLAIN,30),550,1245));
// addTexts.add(new CertificateVo(now.get(Calendar.MONTH) +1+"",new Font("微软雅黑",Font.PLAIN,30),680,1245));
// addTexts.add(new CertificateVo(now.get(Calendar.DATE)+"",new Font("微软雅黑",Font.PLAIN,30),790,1245));
// UserUtils.generateCertificate("classpath:certificate.jpeg","/Users/liuyang/Pictures/certificate.jpeg",addTexts);
// }catch (IOException e){
// e.printStackTrace();;
// }
//
// }
} }
package com.qiankun.vo;
import java.awt.*;
public class CertificateVo {
private String text;
private Color color = Color.black;
private Font font = new Font("微软雅黑",Font.BOLD,50);
private int x;
private int y;
public CertificateVo(){
}
public CertificateVo(String text,Font font,int x,int y){
this.text = text;
this.font = font;
this.x = x;
this.y = y;
}
public CertificateVo(String text, Color color, Font font, int x, int y) {
this.text = text;
this.color = color;
this.font = font;
this.x = x;
this.y = y;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public Font getFont() {
return font;
}
public void setFont(Font font) {
this.font = font;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment