Commit 2159f48d by huluobin

update

parent c628f1d6
......@@ -250,6 +250,14 @@
<optional>true</optional>
</dependency>
<!--spring boot 邮件服务-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
<build>
......
package com.blt.other.common.exception;
import com.bailuntec.common.JsonUtilByJackson;
import com.blt.other.common.interceptor.mail.MailService;
import com.blt.other.common.wrapper.RequestBakRequestWrapper;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.HashMap;
import java.util.Map;
......@@ -23,6 +30,11 @@ import java.util.Map;
@Slf4j
public class GlobalExceptionHandler {
@Resource
MailService mailService;
@Value("${spring.profiles.active}")
private String profile;
/**
* 自定义异常
......@@ -84,15 +96,31 @@ public class GlobalExceptionHandler {
@ResponseBody
@ExceptionHandler(Exception.class)
public Map<String, Object> handleException(HttpServletRequest request,
Exception e) {
Exception ex) {
Map<String, Object> result = new HashMap<>();
result.put("success", false);
result.put("msg", e.getMessage() + e.getStackTrace()[0].toString());
result.put("message", e.getMessage() + e.getStackTrace()[0].toString());
result.put("msg", ex.getMessage() + ex.getStackTrace()[0].toString());
result.put("message", ex.getMessage() + ex.getStackTrace()[0].toString());
result.put("code", "500");
RequestBakRequestWrapper bakRequestWrapper = (RequestBakRequestWrapper) request;
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
ErrorLog errorLog = ErrorLog.builder()
.uri(bakRequestWrapper.getRequestURI())
.param(JsonUtilByJackson.writeValueAsString(bakRequestWrapper.getParameterMap()))
.payload(bakRequestWrapper.getCachedContent().toString())
.errorMsg(ex.getMessage())
.StackTrace(sw.toString())
.build();
log.error(ex.getMessage(), ex);
mailService.sendSimpleMail("robbendev@qq.com", "fee_" + profile + "_异常", JsonUtilByJackson.writeValueAsString(errorLog));
log.error(e.getMessage(), e);
return result;
}
......
package com.blt.other.common.interceptor.mail;
public interface MailService {
/**
* 发送纯文本的简单邮件
*
* @param to
* @param subject
* @param content
*/
void sendSimpleMail(String to, String subject, String content);
/**
* 发送html格式的邮件
*
* @param to
* @param subject
* @param content
*/
void sendHtmlMail(String to, String subject, String content);
/**
* 发送带附件的邮件
*
* @param to
* @param subject
* @param content
* @param filePath
*/
void sendAttachmentsMail(String to, String subject, String content, String filePath);
/**
* 发送嵌入静态资源(一般是图片)的邮件
*
* @param to
* @param subject
* @param content 邮件内容,需要包括一个静态资源的id,比如:<img src=\"cid:rscId01\" >
* @param rscPath 静态资源路径和文件名
* @param rscId 静态资源id
*/
void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId);
}
package com.blt.other.common.interceptor.mail.impl;
import com.blt.other.common.interceptor.mail.MailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
@Service
@Slf4j
public class MailServiceImpl implements MailService {
@Resource
JavaMailSender sender;
@Value("${spring.mail.username}")
private String from;
/**
* 发送纯文本的简单邮件
*
* @param to
* @param subject
* @param content
*/
@Override
@Async
public void sendSimpleMail(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
try {
sender.send(message);
log.info("简单邮件已经发送。");
} catch (Exception e) {
log.error("发送简单邮件时发生异常!", e);
}
}
/**
* 发送html格式的邮件
*
* @param to
* @param subject
* @param content
*/
@Override
@Async
public void sendHtmlMail(String to, String subject, String content) {
MimeMessage message = sender.createMimeMessage();
try {
//true表示需要创建一个multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
sender.send(message);
log.info("html邮件已经发送。");
} catch (MessagingException e) {
log.error("发送html邮件时发生异常!", e);
}
}
/**
* 发送带附件的邮件
*
* @param to
* @param subject
* @param content
* @param filePath
*/
@Override
@Async
public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
MimeMessage message = sender.createMimeMessage();
try {
//true表示需要创建一个multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
helper.addAttachment(fileName, file);
sender.send(message);
log.info("带附件的邮件已经发送。");
} catch (MessagingException e) {
log.error("发送带附件的邮件时发生异常!", e);
}
}
/**
* 发送嵌入静态资源(一般是图片)的邮件
*
* @param to
* @param subject
* @param content 邮件内容,需要包括一个静态资源的id,比如:<img src=\"cid:rscId01\" >
* @param rscPath 静态资源路径和文件名
* @param rscId 静态资源id
*/
@Override
@Async
public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) {
MimeMessage message = sender.createMimeMessage();
try {
//true表示需要创建一个multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource res = new FileSystemResource(new File(rscPath));
helper.addInline(rscId, res);
sender.send(message);
log.info("嵌入静态资源的邮件已经发送。");
} catch (MessagingException e) {
log.error("发送嵌入静态资源的邮件时发生异常!", e);
}
}
}
package com.blt.other.common.wrapper;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import java.io.ByteArrayInputStream;
/**
* Request 备份流
*
* @author robbendev
*/
public class RequestBakInputStream extends ServletInputStream {
private ByteArrayInputStream byteArrayInputStream = null;
public RequestBakInputStream(ByteArrayInputStream byteArrayInputStream) {
this.byteArrayInputStream = byteArrayInputStream;
}
@Override
public int read() {
if (this.byteArrayInputStream != null) {
return byteArrayInputStream.read();
}
return 0;
}
public boolean isFinished() {
return true;
}
public boolean isReady() {
return (this.byteArrayInputStream != null);
}
public void setReadListener(ReadListener readListener) {
}
}
package com.blt.other.common.wrapper;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;
import java.util.Map;
/**
* 备份request中的请求内容
*
* @author robbendev
*/
public class RequestBakRequestWrapper extends HttpServletRequestWrapper {
/**
* 备份的request请求信息
*/
private final ByteArrayOutputStream cachedContent = new ByteArrayOutputStream(1024);
private ServletInputStream inputStream;
private BufferedReader reader;
private Map<String, String[]> parameterMap;
/**
* 对request中的流进行备份
*
* @param request
* @throws IOException
*/
public RequestBakRequestWrapper(HttpServletRequest request) throws IOException {
super(request);
this.parameterMap = request.getParameterMap();
// 将request中的输入流读入字节数组输出流中
byte[] buffer = new byte[1024];
int len;
while ((len = request.getInputStream().read(buffer)) > -1) {
cachedContent.write(buffer, 0, len);
}
cachedContent.flush();
// 设置输入流为备份流
this.inputStream = new RequestBakInputStream(new ByteArrayInputStream(cachedContent.toByteArray()));
}
@Override
public ServletInputStream getInputStream() throws IOException {
return this.inputStream;
}
@Override
public String getCharacterEncoding() {
String characterEncoding = super.getCharacterEncoding();
return (characterEncoding != null ? characterEncoding : "ISO-8859-1");
}
@Override
public BufferedReader getReader() throws IOException {
if (this.reader == null) {
this.reader = new BufferedReader(new InputStreamReader(this.inputStream, getCharacterEncoding()));
}
return this.reader;
}
/**
* 返回流中的byte字节数组信息
*
* @return byte
*/
public byte[] getContentAsByteArray() {
return this.cachedContent.toByteArray();
}
public ByteArrayOutputStream getCachedContent() {
return this.cachedContent;
}
@Override
public String getParameter(String name) {
if (this.parameterMap != null) {
String[] params = this.parameterMap.get(name);
if (params != null && params.length > 0) {
return params[0];
}
}
return null;
}
@Override
public Map<String, String[]> getParameterMap() {
return parameterMap;
}
}
......@@ -14,15 +14,9 @@ import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
......@@ -99,16 +93,17 @@ public class CostApiController implements CostApi {
@RequestParam(name = "createUserId", required = false) Integer createUserId,
@RequestParam(name = "payUserId", required = false) Integer payUserId) {
if (!StringUtils.isEmpty(departmentName)) {
departmentName = departmentName.toLowerCase();
}
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime startDate = LocalDateTime.parse(startDateStr, df);
LocalDateTime endDate = LocalDateTime.parse(endDateStr, df);
List<ManageCostDto> manageCostDtoList = costApiService.getMangeCostList(startDate, endDate, feeSuperType, feeSubType, companyValue, companyName, departmentName, createUserId, payUserId);
return CostResult.success(manageCostDtoList);
// if (!StringUtils.isEmpty(departmentName)) {
// departmentName = departmentName.toLowerCase();
// }
// DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//
// LocalDateTime startDate = LocalDateTime.parse(startDateStr, df);
// LocalDateTime endDate = LocalDateTime.parse(endDateStr, df);
//
// List<ManageCostDto> manageCostDtoList = costApiService.getMangeCostList(startDate, endDate, feeSuperType, feeSubType, companyValue, companyName, departmentName, createUserId, payUserId);
// //todo
return CostResult.success();
}
......@@ -126,18 +121,18 @@ public class CostApiController implements CostApi {
@RequestParam(name = "departmentName", required = false) String departmentName,
@RequestParam(name = "createUserId", required = false) Integer createUserId,
@RequestParam(name = "payUserId", required = false) Integer payUserId) {
if (!StringUtils.isEmpty(departmentName)) {
departmentName = departmentName.toLowerCase();
}
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime startDate = LocalDateTime.parse(startDateStr, df);
LocalDateTime endDate = LocalDateTime.parse(endDateStr, df);
List<ManageCostDto> manageCostDtoList = costApiService.getLogisticsCostList(startDate, endDate, feeSuperType, feeSubType, companyValue, companyName, departmentName, createUserId, payUserId);
return CostResult.success(manageCostDtoList);
// if (!StringUtils.isEmpty(departmentName)) {
// departmentName = departmentName.toLowerCase();
// }
// DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//
// LocalDateTime startDate = LocalDateTime.parse(startDateStr, df);
// LocalDateTime endDate = LocalDateTime.parse(endDateStr, df);
//
// List<ManageCostDto> manageCostDtoList = costApiService.getLogisticsCostList(startDate, endDate, feeSuperType, feeSubType, companyValue, companyName, departmentName, createUserId, payUserId);
// return CostResult.success(manageCostDtoList);
return CostResult.success();
}
@LoginIgnore
......@@ -159,18 +154,19 @@ public class CostApiController implements CostApi {
@GetMapping("/balanceSheetCost")
public CostResult<List<CostDto>> balanceSheetCostList(@RequestParam(name = "startDate") String startDateStr,
@RequestParam(name = "endDate") String endDateStr) {
logger.warn("获取资产负债表相关费用单");
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = sdf.parse(startDateStr);
Date endDate = sdf.parse(endDateStr);
List<CostDomain> balanceSheetCostList = costApiService.getBalanceSheetCost(startDate, endDate);
return CostResult.success(balanceSheetCostList.stream().map(CostDomain::castToDto).collect(Collectors.toList()));
} catch (ParseException e) {
e.printStackTrace();
return CostResult.error();
}
// logger.warn("获取资产负债表相关费用单");
// try {
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// Date startDate = sdf.parse(startDateStr);
// Date endDate = sdf.parse(endDateStr);
// List<CostDomain> balanceSheetCostList = costApiService.getBalanceSheetCost(startDate, endDate);
// return CostResult.success(balanceSheetCostList.stream().map(CostDomain::castToDto).collect(Collectors.toList()));
//
// } catch (ParseException e) {
// e.printStackTrace();
// return CostResult.error();
// }
return CostResult.success();
}
......
......@@ -20,7 +20,18 @@ spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
mail:
host: smtp.qq.com
username: robbendev@qq.com
password: zaaiclinciptbgdd
properties:
mail:
smtp:
auth: true
ssl:
enable: true
required: true
port: 465
#mybatis plus 配置
mybatis-plus:
mapper-locations:
......
......@@ -21,6 +21,18 @@ spring:
time-zone: GMT+8
profiles:
active: prod
mail:
host: smtp.qq.com
username: robbendev@qq.com
password: zaaiclinciptbgdd
properties:
mail:
smtp:
auth: true
ssl:
enable: true
required: true
port: 465
#mybatis plus 配置
mybatis-plus:
......
......@@ -20,6 +20,18 @@ spring:
time-zone: GMT+8
profiles:
active: test
mail:
host: smtp.qq.com
username: robbendev@qq.com
password: zaaiclinciptbgdd
properties:
mail:
smtp:
auth: true
ssl:
enable: true
required: true
port: 465
#mybatis plus 配置
mybatis-plus:
......
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