Commit 7b2b6f3e by 李秋伟

Merge branch 'dev' into 'master'

Dev

See merge request mall/arch/matrix!24
parents 18ea8cd1 2e307686
package com.secoo.mall.common.util.date;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.joda.time.Days;
import java.text.ParseException;
import java.util.Date;
public class DateUtil extends DateUtils {
private static String[] parsePatterns = {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss",
"yyyy.MM.dd HH:mm", "yyyy.MM"};
/*
* 得到当前日期字符串 格式(yyyy-MM-dd)
*/
public static String getDate() {
return getDate("yyyy-MM-dd");
}
/*
* 得到当前日期字符串 格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
*/
public static String getDate(String pattern) {
return DateFormatUtils.format(new Date(), pattern);
}
/*
* 得到日期字符串 默认格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
*/
public static String formatDate(Date date, Object... pattern) {
String formatDate = null;
if (pattern != null && pattern.length > 0) {
formatDate = DateFormatUtils.format(date, pattern[0].toString());
} else {
formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
}
return formatDate;
}
/*
* 得到日期时间字符串,转换格式(yyyy-MM-dd HH:mm:ss)
*/
public static String formatDateTime(Date date) {
return formatDate(date, "yyyy-MM-dd HH:mm:ss");
}
/*
* 得到当前时间字符串 格式(HH:mm:ss)
*/
public static String getTime() {
return formatDate(new Date(), "HH:mm:ss");
}
/*
* 得到当前日期和时间字符串 格式(yyyy-MM-dd HH:mm:ss)
*/
public static String getDateTime() {
return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
}
/*
* 得到当前年份字符串 格式(yyyy)
*/
public static String getYear() {
return formatDate(new Date(), "yyyy");
}
/*
* 得到当前月份字符串 格式(MM)
*/
public static String getMonth() {
return formatDate(new Date(), "MM");
}
/*
* 得到当天字符串 格式(dd)
*/
public static String getDay() {
return formatDate(new Date(), "dd");
}
/*
* 得到当前星期字符串 格式(E)星期几
*/
public static String getWeek() {
return formatDate(new Date(), "E");
}
/*
* 日期型字符串转化为日期 格式 { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
* "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy.MM.dd",
* "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" }
*/
public static Date parseDate(Object str) {
if (str == null) {
return null;
}
try {
return parseDate(str.toString(), parsePatterns);
} catch (ParseException e) {
return null;
}
}
/*
* 获取过去的天数
* @param date
*/
public static long pastDays(Date date) {
long t = new Date().getTime() - date.getTime();
return t / (24 * 60 * 60 * 1000);
}
/*
* 获取过去的小时
*
* @param date
*/
public static long pastHour(Date date) {
long t = new Date().getTime() - date.getTime();
return t / (60 * 60 * 1000);
}
/*
* 获取过去的分钟
*
* @param date
*/
public static long pastMinutes(Date date) {
long t = new Date().getTime() - date.getTime();
return t / (60 * 1000);
}
/*
* 转换为时间(天,时:分:秒.毫秒)
*
* @param timeMillis
*/
public static String formatDateTime(long timeMillis) {
long day = timeMillis / (24 * 60 * 60 * 1000);
long hour = (timeMillis / (60 * 60 * 1000) - day * 24);
long min = ((timeMillis / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (timeMillis / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
long sss = (timeMillis - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - s * 1000);
return (day > 0 ? day + "," : "") + hour + ":" + min + ":" + s + "." + sss;
}
/*
* 获取两个日期之间的天数
*
* @param before
* @param after
*/
public static double getDistanceOfTwoDate(Date before, Date after) {
long beforeTime = before.getTime();
long afterTime = after.getTime();
return (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
}
}
package com.secoo.mall.common.util.web;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.util.WebUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class WebUtil {
public class WebUtil extends WebUtils {
private static String sessionUser = "sessionUser";
private static String[] headers = new String[]{"Cdn-Src-Ip", "X-Real-IP", "X-Forwarded-For", "Proxy-Client-IP", "WL-Proxy-Client-IP", "HTTP_CLIENT_IP", "HTTP_X_FORWARDED_FOR"};
private static String localIP = "127.0.0.1";
public static String getParameter(String name) {
return getRequest().getParameter(name);
......@@ -52,4 +56,47 @@ public class WebUtil {
return getAttribute(sessionUser);
}
/**
* 获取请求客户端IP地址,支持代理服务器
*
* @param request {@link HttpServletRequest} 对象
* @return IP地址
*/
public static String getRemoteAddr(HttpServletRequest request) {
// 1、获取客户端IP地址,支持代理服务器
String remoteAddr = null;
for (String header : headers) {
remoteAddr = request.getHeader(header);
if (StringUtils.hasText(remoteAddr) && !remoteAddr.equals("unknown")) {
break;
}
}
// 2、没有取得特定标记的值
if (!StringUtils.hasText(remoteAddr)) {
remoteAddr = request.getRemoteAddr();
}
// 3、判断是否localhost访问
if (remoteAddr.equals("localhost")) {
remoteAddr = localIP;
}
return remoteAddr;
}
/**
* 获得请求的客户端信息【ip,port,name】
*
* @param request {@link HttpServletRequest} 对象
* @return 客户端信息[ip, port, name]
*/
public static String[] getRemoteInfo(HttpServletRequest request) {
if (request == null) {
return new String[]{"", "", ""};
}
return new String[]{getRemoteAddr(request), request.getRemotePort() + "", request.getRemoteHost()};
}
}
......@@ -36,5 +36,10 @@
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -25,12 +25,7 @@
<module>logger-starter</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
......@@ -42,6 +37,15 @@
<dependencyManagement>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.9.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--matrix-->
<dependency>
<groupId>com.secoo.mall</groupId>
<artifactId>logger-starter</artifactId>
......@@ -154,7 +158,12 @@
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.20</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
......@@ -165,14 +174,7 @@
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<!--spring boot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Aapche Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
......@@ -316,4 +318,5 @@
</distributionManagement>
</project>
\ No newline at end of file
</project>
......@@ -7,6 +7,7 @@ import com.secoo.mall.common.core.exception.SystemInternalException;
import com.secoo.mall.common.processor.ExceptionProcessor;
import com.secoo.mall.common.util.log.LoggerUtil;
import com.secoo.mall.common.util.response.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
......@@ -36,13 +37,13 @@ public class ProtocolExceptionHandler {
LoggerUtil.info(msg);
return ResponseUtil.getFailResponse(ex.getCode(), msg);
} catch (SystemInternalException ex) {
LoggerUtil.error(e);
LoggerUtil.warn("WarnError",ex);
return ResponseUtil.getFailResponse(CommonErrorCode.SYSTEM_INTERNAL_EXCEPTION.getCode(), CommonErrorCode.SYSTEM_INTERNAL_EXCEPTION.getMsg());
} catch (BusinessException ex) {
LoggerUtil.warn("businessException info:", ex);
LoggerUtil.warn("BizError:"+getMsg(ex),ex);
return ResponseUtil.getFailResponse(ex.getCode(), getMsg(ex));
} catch (Exception ex) {
LoggerUtil.error(ex);
LoggerUtil.error("Error:",ex);
return ResponseUtil.getFailResponse(CommonErrorCode.SYSTEM_INTERNAL_EXCEPTION.getCode(), CommonErrorCode.SYSTEM_INTERNAL_EXCEPTION.getMsg());
}
......
package com.secoo.mall.common.processor;
import com.secoo.mall.common.core.exception.SystemInternalException;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public abstract class AbsExceptionProcessor implements ExceptionProcessor{
public abstract class AbsExceptionProcessor implements ExceptionProcessor {
public void process(Exception e) throws Exception{
log.warn("warn:",e);
public void process(Exception e) throws Exception {
convertException(e);
//如果不进行运行时异常抛出,则转换为运行时异常
if (!(e instanceof RuntimeException)) {
log.error("error:", e);
throw new SystemInternalException();
}
throw e;
}
protected abstract void convertException(Exception e);
protected abstract void convertException(Exception e);
}
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