修改错误日志权限

This commit is contained in:
wuchunlei
2024-09-14 16:14:48 +08:00
parent 717323019d
commit a895687731
16 changed files with 886 additions and 27 deletions

View File

@@ -318,7 +318,7 @@ public class ClassEntityServiceImpl extends ServiceImpl<ClassEntityDao, ClassEnt
}else {
//结班限制
int classExamUsers = classExamUserDao.selectCount(new LambdaQueryWrapper<ClassExamUser>()
.eq(ClassExamUser::getClassId,classEntity.getId())
.eq(ClassExamUser::getRelationId,classEntity.getId())
.eq(ClassExamUser::getScoreSuccess,0));
if (classExamUsers>0){
return R.error("有学员正在考试,请稍后再试");
@@ -634,7 +634,7 @@ public class ClassEntityServiceImpl extends ServiceImpl<ClassEntityDao, ClassEnt
wrapper.leftJoin(ClassModel.class,ClassModel::getId,ClassEntity::getModelId);
wrapper.leftJoin(ClassCourse.class,ClassCourse::getModelId,ClassModel::getId);
wrapper.eq(ClassCourse::getCourseId,params.get("courseId"));
wrapper.ne(ClassEntity::getState,"2");
wrapper.eq(ClassEntity::getState,"0");
List<ClassEntity> classes = this.baseMapper.selectList(wrapper);
if (classes.size() > 0){
for (ClassEntity classEntity:classes){
@@ -1097,7 +1097,7 @@ public class ClassEntityServiceImpl extends ServiceImpl<ClassEntityDao, ClassEnt
if (classModel.getIsExam()==1){
List<Map<String,Object>> classExamUsers = classExamUserDao.selectMaps(new MPJLambdaWrapper<ClassExamUser>()
.eq(ClassExamUser::getUserId,params.get("userId").toString())
.eq(ClassExamUser::getClassId,classEntity.getId())
.eq(ClassExamUser::getRelationId,classEntity.getId())
.eq(ClassExamUser::getScoreSuccess,1)
.selectAs(ClassExamUser::getId,"id")
.selectAs(ClassExamUser::getScore,"score")
@@ -1214,6 +1214,35 @@ public class ClassEntityServiceImpl extends ServiceImpl<ClassEntityDao, ClassEnt
return resultList;
}
@Override
public List<Map<String,Object>> classCourseInfoClassId(int classId) {
List<Map<String,Object>> res = new ArrayList<>();
ClassEntity classEntity = this.baseMapper.selectById(classId);
MPJLambdaWrapper<CourseEntity> wrapper = new MPJLambdaWrapper<>();
wrapper.selectAll(CourseEntity.class);
wrapper.leftJoin(ClassCourse.class,ClassCourse::getCourseId,CourseEntity::getId);
wrapper.leftJoin(ClassModel.class,ClassModel::getId,ClassCourse::getModelId);
wrapper.eq(ClassModel::getId,classEntity.getModelId());
List<CourseEntity> courseEntityList = courseDao.selectList(wrapper);
for (CourseEntity course:courseEntityList){
Map<String,Object> candk = new HashMap<>();
MPJLambdaWrapper<CourseCatalogueChapterEntity> w = new MPJLambdaWrapper<>();
w.leftJoin(CourseCatalogueChapterVideoEntity.class,CourseCatalogueChapterVideoEntity::getChapterId,CourseCatalogueChapterEntity::getId);
w.eq(CourseCatalogueChapterEntity::getCourseId,course.getId());
w.selectSum(CourseCatalogueChapterVideoEntity::getDuration);
Map<String,Object> map = courseCatalogueChapterDao.selectJoinMap(w);
double duration = Double.parseDouble(map.get("duration").toString());
double minute = duration/60;
double keshi = Math.ceil(minute/45);
candk.put("courseId",course.getId());
candk.put("courseTitle",course.getTitle());
candk.put("courseETitle",course.getEtitle());
candk.put("titleAbbr",course.getTitleAbbr());
candk.put("keshi",keshi);
res.add(candk);
}
return res;
}
private MessagePostProcessor messagePostProcessor(long date) {

View File

@@ -147,22 +147,32 @@ public class ClassExamServiceImpl extends ServiceImpl<ClassExamDao, ClassExam> i
if (list.size() > 0){
return R.error("存在正在考试,请结束再试");
}
ClassEntity classEntity = classEntityDao.selectById(params.get("classId").toString());
ClassModel classModel = classModelDao.selectById(classEntity.getModelId());
int examTime = classModel.getExamTime();
MPJLambdaWrapper<ClassCourse> courseWrapper = new MPJLambdaWrapper<>();
courseWrapper.leftJoin(ClassModel.class,ClassModel::getId,ClassCourse::getModelId);
courseWrapper.leftJoin(ClassEntity.class,ClassEntity::getModelId,ClassModel::getId);
courseWrapper.eq(ClassEntity::getId,classEntity.getId());
courseWrapper.selectAll(ClassCourse.class);
List<ClassCourse> courseList = classCourseDao.selectList(courseWrapper);
List<ClassCourse> courseList = new ArrayList<>();
int examTime = 45;//考试持续时间
int stotal = 60;//单选题总数
int mtotal = 40;//多选题总数
if ("1".equals(params.get("type").toString())){
ClassEntity classEntity = classEntityDao.selectById(params.get("relationId").toString());
ClassModel classModel = classModelDao.selectById(classEntity.getModelId());
examTime = classModel.getExamTime();//考试持续时间
MPJLambdaWrapper<ClassCourse> courseWrapper = new MPJLambdaWrapper<>();
courseWrapper.leftJoin(ClassModel.class,ClassModel::getId,ClassCourse::getModelId);
courseWrapper.leftJoin(ClassEntity.class,ClassEntity::getModelId,ClassModel::getId);
courseWrapper.eq(ClassEntity::getId,classEntity.getId());
courseWrapper.selectAll(ClassCourse.class);
courseList = classCourseDao.selectList(courseWrapper);
stotal = Integer.parseInt(classModel.getExamProportion().split(":")[0]);//单选题总数
mtotal = Integer.parseInt(classModel.getExamProportion().split(":")[1]);//多选题总数
}else {
ClassCourse classCourse = new ClassCourse();
classCourse.setCourseId((Integer) params.get("relationId"));
courseList.add(classCourse);
}
List<Map<String,Object>> resultList = new ArrayList<>();
List<String> answerList = new ArrayList<>();
List<Map<String,Object>> sList = new ArrayList<>();
List<Map<String,Object>> mList = new ArrayList<>();
if (courseList.size() > 0) {
int stotal = Integer.parseInt(classModel.getExamProportion().split(":")[0]);//单选题总数
int mtotal = Integer.parseInt(classModel.getExamProportion().split(":")[1]);//多选题总数
int snum = (int)Math.floor(stotal/courseList.size());
int mnum = (int)Math.floor(mtotal/courseList.size());
for (ClassCourse classCourse:courseList){
@@ -219,7 +229,8 @@ public class ClassExamServiceImpl extends ServiceImpl<ClassExamDao, ClassExam> i
}
}
ClassExamUser classExamUser = new ClassExamUser();
classExamUser.setClassId(classEntity.getId());
classExamUser.setType(params.get("type").toString());
classExamUser.setRelationId(Integer.parseInt(params.get("relationId").toString()));
classExamUser.setSubject(JSONUtil.toJsonStr(resultList));
classExamUser.setAnswer(JSONUtil.toJsonStr(answerList));
classExamUser.setUserId(ShiroUtils.getUId());
@@ -234,7 +245,7 @@ public class ClassExamServiceImpl extends ServiceImpl<ClassExamDao, ClassExam> i
messagePostProcessor(examTime*60*1000+3000)//3秒延迟
);
return R.ok().put("examPaper",resultList).put("id",classExamUser.getId())
.put("startTime",classExamUser.getStartTime()).put("planEndTime",DateUtils.addDateMinutes(startTime,examTime));
.put("startTime",classExamUser.getStartTime()).put("planEndTime",DateUtils.addDateMinutes(startTime,examTime).getTime());
}
@@ -243,10 +254,15 @@ public class ClassExamServiceImpl extends ServiceImpl<ClassExamDao, ClassExam> i
ClassExamUser classExamUser = classExamUserDao.selectOne(new LambdaQueryWrapper<ClassExamUser>()
.eq(ClassExamUser::getUserId,ShiroUtils.getUId()).eq(ClassExamUser::getScoreSuccess,0));
if (classExamUser!=null){
ClassEntity classEntity = classEntityDao.selectById(classExamUser.getClassId());
ClassModel classModel = classModelDao.selectById(classEntity.getModelId());
return R.ok().put("classExamUser",classExamUser)
.put("planEndTime",DateUtils.addDateMinutes(classExamUser.getStartTime(),classModel.getExamTime()));
if ("1".equals(classExamUser.getType())){
ClassEntity classEntity = classEntityDao.selectById(classExamUser.getRelationId());
ClassModel classModel = classModelDao.selectById(classEntity.getModelId());
return R.ok().put("classExamUser",classExamUser)
.put("planEndTime",DateUtils.addDateMinutes(classExamUser.getStartTime(),classModel.getExamTime()).getTime());
}else {
return R.ok().put("classExamUser",classExamUser)
.put("planEndTime",DateUtils.addDateMinutes(classExamUser.getStartTime(),45).getTime());
}
}else {
return R.ok().put("classExamUser",null).put("planEndTime",null);
}
@@ -313,7 +329,7 @@ public class ClassExamServiceImpl extends ServiceImpl<ClassExamDao, ClassExam> i
@Override
public Object getExamPaperList(Map<String, Object> params) {
List<ClassExamUser> classExamUserList = classExamUserDao.selectList(new LambdaQueryWrapper<ClassExamUser>()
.eq(ClassExamUser::getClassId,params.get("classId"))
.eq(ClassExamUser::getRelationId,params.get("classId"))
.eq(ClassExamUser::getUserId,params.get("userId"))
.orderByDesc(ClassExamUser::getScore));
return classExamUserList;
@@ -322,10 +338,15 @@ public class ClassExamServiceImpl extends ServiceImpl<ClassExamDao, ClassExam> i
@Override
public R getExamPaperInfo(Map<String, Object> params) {
ClassExamUser classExamUser = classExamUserDao.selectById(params.get("id").toString());
ClassEntity classEntity = classEntityDao.selectById(classExamUser.getClassId());
ClassModel classModel = classModelDao.selectById(classEntity.getModelId());
return R.ok().put("examPaper",classExamUser)
.put("planEndTime",DateUtils.addDateMinutes(classExamUser.getStartTime(),classModel.getExamTime()));
if ("1".equals(classExamUser.getType())){
ClassEntity classEntity = classEntityDao.selectById(classExamUser.getRelationId());
ClassModel classModel = classModelDao.selectById(classEntity.getModelId());
return R.ok().put("examPaper",classExamUser)
.put("planEndTime",DateUtils.addDateMinutes(classExamUser.getStartTime(),classModel.getExamTime()).getTime());
}else {
return R.ok().put("examPaper",classExamUser)
.put("planEndTime",DateUtils.addDateMinutes(classExamUser.getStartTime(),45).getTime());
}
}
private MessagePostProcessor messagePostProcessor(long date) {

View File

@@ -0,0 +1,13 @@
package com.peanut.modules.common.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peanut.modules.common.dao.ClassExamUserDao;
import com.peanut.modules.common.entity.ClassExamUser;
import com.peanut.modules.common.service.ClassExamUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service("commonClassExamUserService")
public class ClassExamUserServiceImpl extends ServiceImpl<ClassExamUserDao, ClassExamUser> implements ClassExamUserService {
}

View File

@@ -0,0 +1,453 @@
package com.peanut.modules.common.service.impl;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.promeg.pinyinhelper.Pinyin;
import com.peanut.common.utils.ConstantPropertiesUtils;
import com.peanut.modules.common.dao.UserCertificateDao;
import com.peanut.modules.common.entity.UserCertificate;
import com.peanut.modules.common.service.UserCertificateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.UUID;
@Slf4j
@Service("commonUserCertificateService")
public class UserCertificateServiceImpl extends ServiceImpl<UserCertificateDao, UserCertificate> implements UserCertificateService {
public static void main(String[] args) {
System.out.println(" ");
}
@Override
public String generateCertificate(String type,String certificateNo,String iconUrl,String realName,String des,String edes,
String endYear,String endMonth,String endDay) {
String url = "";
if ("A".equals(type)){
url = generateACertificate(certificateNo, iconUrl, realName, des, endYear, endMonth, endDay);
}else if ("B".equals(type)) {
url = generateBCertificate(certificateNo, iconUrl, realName, des, endYear, endMonth, endDay);
}else if ("ZK".equals(type)){
url = generateZKCertificate(certificateNo, iconUrl, realName, des, endYear, endMonth, endDay);
}
String eurl = generateECertificate(certificateNo,realName, edes);
return url+","+eurl;
}
public String generateZKCertificate(String no,String iconUrl,String name,String des,String endy,String endm,String endd) {
try {
Image src = ImageIO.read(new URL("https://ehh-private-01.oss-cn-beijing.aliyuncs.com/certificate/ZK.png"));
// 获取图片的高和宽
int wideth = src.getWidth(null);
int height = src.getHeight(null);
// 新增一个图片缓冲
BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(src, 0, 0, wideth, height, null);
// 设置字体颜色
g.setColor(new Color(0,75,63));
g.setFont(new Font("Arial", Font.BOLD, 30));
FontMetrics metrics = g.getFontMetrics();
int nox = (wideth - metrics.stringWidth(no))/2;
int noy = 415;
g.drawString(no, nox, noy);
if (StringUtils.isNotBlank(iconUrl)){
Image images = ImageIO.read(new URL(iconUrl));
g.drawImage(images,(wideth-images.getWidth(null))/2,435,245,280,null);
}
g.setColor(new Color(0,75,63));
g.setFont(new Font("宋体", Font.BOLD, 40));
FontMetrics metricss = g.getFontMetrics();
int namex = (wideth - metricss.stringWidth(name))/2;
int namey = 765;
g.drawString(name, namex, namey);
g.setColor(Color.BLACK);
g.setFont(new Font("黑体", Font.BOLD, 30));
FontMetrics dess = g.getFontMetrics();
int alreadyWriteLine = 0; //已经写了多少行
int nowWidth = 0; //目前一行的长度
boolean space = true; //第一行空两格
for (int i = 0; i < des.length(); i++) {
int oneWordWidth = dess.charWidth(des.charAt(i)); //获取单个字符的长度
if (nowWidth+oneWordWidth>650){
alreadyWriteLine++;
int writeY = 850 + alreadyWriteLine * (dess.getHeight()+10);//10是行间距
g.drawString(des.charAt(i) + "", 180, writeY);
nowWidth = 0;
space = false;
nowWidth += oneWordWidth;
}else {
int writeY = 850 + alreadyWriteLine * (dess.getHeight()+10);//10是行间距
if (nowWidth==0&&space){//换行画
g.drawString(des.charAt(i)+"", 180+oneWordWidth, writeY);
nowWidth += oneWordWidth*2;
}else {//一字一字画
g.drawString(des.charAt(i)+"", 180+nowWidth, writeY);
nowWidth += oneWordWidth;
}
}
}
g.setColor(new Color(0,75,63));
g.setFont(new Font("黑体", Font.BOLD, 28));
g.drawString(endy,595,1123);
g.drawString(endm,703,1123);
g.drawString(endd,770,1123);
// 释放资源
g.dispose();
File tempFile = File.createTempFile("tempfile_"+UUID.randomUUID().toString(), ".jpg");
ImageIO.write(image, "jpg", tempFile);
String res = uploadFile(tempFile);
tempFile.delete();
return res;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public String generateBCertificate(String no,String iconUrl,String name,String des,String endy,String endm,String endd) {
try {
Image src = ImageIO.read(new URL("https://ehh-private-01.oss-cn-beijing.aliyuncs.com/certificate/B.png"));
// 获取图片的高和宽
int wideth = src.getWidth(null);
int height = src.getHeight(null);
// 新增一个图片缓冲
BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(src, 0, 0, wideth, height, null);
// 设置字体颜色
g.setColor(new Color(160,113,0));
g.setFont(new Font("Arial", Font.BOLD, 48));
FontMetrics metrics = g.getFontMetrics();
int nox = (wideth - metrics.stringWidth(no))/2;
int noy = 380;
g.drawString(no, nox, noy);
if (StringUtils.isNotBlank(iconUrl)){
Image images = ImageIO.read(new URL(iconUrl));
g.drawImage(images,(wideth-images.getWidth(null))/2,400,245,280,null);
}
g.setColor(Color.BLACK);
g.setFont(new Font("宋体", Font.BOLD, 40));
FontMetrics metricss = g.getFontMetrics();
int namex = (wideth - metricss.stringWidth(name))/2;
int namey = 730;
g.drawString(name, namex, namey);
g.setFont(new Font("黑体", Font.BOLD, 30));
FontMetrics dess = g.getFontMetrics();
int alreadyWriteLine = 0; //已经写了多少行
int nowWidth = 0; //目前一行的长度
boolean space = true; //第一行空两格
for (int i = 0; i < des.length(); i++) {
int oneWordWidth = dess.charWidth(des.charAt(i)); //获取单个字符的长度
if (nowWidth+oneWordWidth>650){
alreadyWriteLine++;
int writeY = 850 + alreadyWriteLine * (dess.getHeight()+10);//10是行间距
g.drawString(des.charAt(i) + "", 180, writeY);
nowWidth = 0;
space = false;
nowWidth += oneWordWidth;
}else {
int writeY = 850 + alreadyWriteLine * (dess.getHeight()+10);//10是行间距
if (nowWidth==0&&space){//换行画
g.drawString(des.charAt(i)+"", 180+oneWordWidth, writeY);
nowWidth += oneWordWidth*2;
}else {//一字一字画
g.drawString(des.charAt(i)+"", 180+nowWidth, writeY);
nowWidth += oneWordWidth;
}
}
}
g.setFont(new Font("黑体", Font.BOLD, 28));
g.drawString(endy,600,1158);
g.drawString(endm,695,1158);
g.drawString(endd,748,1158);
// 释放资源
g.dispose();
File tempFile = File.createTempFile("tempfile_"+UUID.randomUUID().toString(), ".jpg");
ImageIO.write(image, "jpg", tempFile);
String res = uploadFile(tempFile);
tempFile.delete();
return res;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public String generateACertificate(String no,String iconUrl,String name,String des,String endy,String endm,String endd) {
try {
Image src = ImageIO.read(new URL("https://ehh-private-01.oss-cn-beijing.aliyuncs.com/certificate/A.png"));
// 获取图片的高和宽
int wideth = src.getWidth(null);
int height = src.getHeight(null);
// 新增一个图片缓冲
BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(src, 0, 0, wideth, height, null);
// 设置字体颜色
g.setColor(new Color(160,113,0));
g.setFont(new Font("Arial", Font.BOLD, 48));
FontMetrics metrics = g.getFontMetrics();
int nox = (wideth - metrics.stringWidth(no))/2;
int noy = 355;
g.drawString(no, nox, noy);
if (StringUtils.isNotBlank(iconUrl)){
Image images = ImageIO.read(new URL(iconUrl));
g.drawImage(images,(wideth-images.getWidth(null))/2,480,245,280,null);
}
g.setColor(Color.BLACK);
g.setFont(new Font("宋体", Font.BOLD, 50));
FontMetrics metricss = g.getFontMetrics();
int namex = (wideth - metricss.stringWidth(name))/2;
int namey = 815;
g.drawString(name, namex, namey);
g.setFont(new Font("黑体", Font.BOLD, 30));
FontMetrics dess = g.getFontMetrics();
int alreadyWriteLine = 0; //已经写了多少行
int nowWidth = 0; //目前一行的长度
boolean space = true; //第一行空两格
for (int i = 0; i < des.length(); i++) {
int oneWordWidth = dess.charWidth(des.charAt(i)); //获取单个字符的长度
if (nowWidth+oneWordWidth>650){
alreadyWriteLine++;
int writeY = 865 + alreadyWriteLine * (dess.getHeight()+10);//10是行间距
g.drawString(des.charAt(i) + "", 180, writeY);
nowWidth = 0;
space = false;
nowWidth += oneWordWidth;
}else {
int writeY = 865 + alreadyWriteLine * (dess.getHeight()+10);//10是行间距
if (nowWidth==0&&space){//换行画
g.drawString(des.charAt(i)+"", 180+oneWordWidth, writeY);
nowWidth += oneWordWidth*2;
}else {//一字一字画
g.drawString(des.charAt(i)+"", 180+nowWidth, writeY);
nowWidth += oneWordWidth;
}
}
}
g.setFont(new Font("黑体", Font.BOLD, 28));
g.drawString(endy,605,1185);
g.drawString(endm,715,1185);
g.drawString(endd,780,1185);
// 释放资源
g.dispose();
File tempFile = File.createTempFile("tempfile_"+UUID.randomUUID().toString(), ".jpg");
ImageIO.write(image, "jpg", tempFile);
String res = uploadFile(tempFile);
tempFile.delete();
return res;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public String generateECertificate(String no,String name,String des) {
try {
Image src = ImageIO.read(new URL("https://ehh-private-01.oss-cn-beijing.aliyuncs.com/certificate/E.png"));
String filename = "eee";
// 获取图片的高和宽
int wideth = src.getWidth(null);
int height = src.getHeight(null);
// 新增一个图片缓冲
BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(src, 0, 0, wideth, height, null);
// 设置字体颜色
g.setColor(new Color(160,113,0));
g.setFont(new Font("Arial", Font.BOLD, 48));
FontMetrics metrics = g.getFontMetrics();
int nox = (wideth - metrics.stringWidth(no))/2;
int noy = 500;
g.drawString(no, nox, noy);
g.setColor(Color.BLACK);
g.setFont(new Font("Arial", Font.PLAIN, 58));
FontMetrics metricss = g.getFontMetrics();
//转换成拼音
name = nameToPinyin(name);
int namex = (wideth - metricss.stringWidth(name))/2;
int namey = 660;
g.drawString(name, namex, namey);
g.setFont(new Font("Arial", Font.PLAIN, 30));
FontMetrics dess = g.getFontMetrics();
int alreadyWriteLine = 0; //已经写了多少行
int nowWidth = 0; //目前一行的长度
boolean space = true; //第一行空两格
for (int i = 0; i < des.length(); i++) {
int oneWordWidth = dess.charWidth(des.charAt(i)); //获取单个字符的长度
if (nowWidth+oneWordWidth>700){//换行画
alreadyWriteLine++;
int writeY = 780 + alreadyWriteLine * (dess.getHeight()+10);//10是行间距
g.drawString(des.charAt(i) + "", 155, writeY);
nowWidth = 0;
space = false;
nowWidth += oneWordWidth;
}else {//一字一字画
int writeY = 780 + alreadyWriteLine * (dess.getHeight()+10);//10是行间距
if (nowWidth==0&&space){//首行空一格
g.drawString(des.charAt(i)+"", 155+oneWordWidth, writeY);
nowWidth += oneWordWidth*2;
}else {
g.drawString(des.charAt(i)+"", 155+nowWidth, writeY);
nowWidth += oneWordWidth;
}
}
}
// 释放资源
g.dispose();
File tempFile = File.createTempFile("tempfile_"+UUID.randomUUID().toString(), ".jpg");
ImageIO.write(image, "jpg", tempFile);
String res = uploadFile(tempFile);
tempFile.delete();
return res;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public String nameToPinyin(String realName){
String pinyin = Pinyin.toPinyin(realName, " ").toLowerCase();
String[] sss = pinyin.split(" ");
String nick = "";
String name = "";
for (int i = 0; i < sss.length; i++) {
String ss = sss[i];
ss = ss.replace(ss.charAt(0),Character.toUpperCase(ss.charAt(0)));
if (i==0){
nick = ss;
}else {
name = name+" "+ss;
}
}
return name+" "+nick;
}
public void generateTestCertificate(String no,String name,String time,String des,String trans,String trans2,
String trans3,String endy,String endm,String endd) {
try {
Image src = ImageIO.read(new File("F:\\certificate\\template\\a.png"));
String filename = "aaa";
// 获取图片的高和宽
int wideth = src.getWidth(null);
int height = src.getHeight(null);
// 新增一个图片缓冲
BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(src, 0, 0, wideth, height, null);
// 设置字体颜色
g.setColor(new Color(160,113,0));
g.setFont(new Font("Arial", Font.BOLD, 48));
FontMetrics metrics = g.getFontMetrics();
int nox = (wideth - metrics.stringWidth(no))/2;
// int noy = 1037 - metrics.getHeight();//1305
int noy = 355;
g.drawString(no, nox, noy);
g.setColor(Color.BLACK);
g.setFont(new Font("宋体", Font.BOLD, 50));
FontMetrics metricss = g.getFontMetrics();
int namex = (wideth - metricss.stringWidth(name))/2;
int namey = 815;
g.drawString(name, namex, namey);
g.setFont(new Font("宋体", Font.BOLD, 115));
FontMetrics times = g.getFontMetrics();
int timex = (wideth - times.stringWidth(time))/2;
int timey = 2370 - times.getHeight();//2270
g.drawString(time, timex, timey);
FontMetrics dess = g.getFontMetrics();
int desx = (wideth - dess.stringWidth(des))/2;
int desy = 2635 - dess.getHeight();//2535
g.drawString(des, desx, desy);
g.setFont(new Font("Arial", Font.BOLD, 75));
FontMetrics transs = g.getFontMetrics();
int transx = (wideth - transs.stringWidth(trans))/2;
int transy = 2735 - transs.getHeight();//2685
g.drawString(trans, transx, transy);
FontMetrics trans2s = g.getFontMetrics();
int trans2x = (wideth - trans2s.stringWidth(trans2))/2;
g.drawString(trans2, trans2x, transy+120);
FontMetrics trans3s = g.getFontMetrics();
int trans3x = (wideth - trans3s.stringWidth(trans3))/2;
g.drawString(trans3, trans3x, transy+240);
g.setFont(new Font("宋体", Font.BOLD, 80));
g.drawString(endy,1800,3440);
g.drawString(endm,2090,3440);
g.drawString(endd,2250,3440);
// 释放资源
g.dispose();
File outputfile = new File("F:\\certificate\\template\\"+ filename +".jpg");
ImageIO.write(image, "jpg", outputfile);
System.out.println(no +","+ name +","+ filename);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static String uploadFile(File file){
// 工具类获取值
String endpoint = ConstantPropertiesUtils.END_POIND;
String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
String bucketName = ConstantPropertiesUtils.BUCKET_NAME;
try {
// 创建OSS实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
String fileName = UUID.randomUUID().toString()+".jpg";
//把上传之后文件路径返回 需要把上传到阿里云oss路径手动拼接出来
// https://edu-guli-1010.oss-cn-beijing.aliyuncs.com/01.jpg
String url = "https://"+bucketName+"."+endpoint+"/certificate/"+fileName;
//调用oss方法实现上传
ossClient.putObject(bucketName,"certificate/"+fileName, file);
// 关闭OSSClient。
ossClient.shutdown();
return url;
}catch(Exception e) {
e.printStackTrace();
return null;
}
}
}