1.获取HTTP对象工具类
import com.yuetong.core.exception.ServiceException;
import com.yuetong.core.exception.enums.ServerExceptionEnum;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
public class HttpServletUtil {
public HttpServletUtil() {
}
public static HttpServletRequest getRequest() {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
if (requestAttributes == null) {
throw new ServiceException(ServerExceptionEnum.REQUEST_EMPTY);
} else {
return requestAttributes.getRequest();
}
}
public static HttpServletResponse getResponse() {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
if (requestAttributes == null) {
throw new ServiceException(ServerExceptionEnum.REQUEST_EMPTY);
} else {
return requestAttributes.getResponse();
}
}
}
2.mybatis获取分页工具类
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuetong.core.util.HttpServletUtil;
import javax.servlet.http.HttpServletRequest;
public class PageFactory {
private static final String PAGE_SIZE_PARAM_NAME = "pageSize";
private static final String PAGE_NO_PARAM_NAME = "pageNo";
public PageFactory() {
}
public static <T> Page<T> defaultPage() {
int pageSize = 20;
int pageNo = 1;
HttpServletRequest request = HttpServletUtil.getRequest();
String pageSizeString = request.getParameter("pageSize");
if (ObjectUtil.isNotEmpty(pageSizeString)) {
pageSize = Integer.parseInt(pageSizeString);
}
String pageNoString = request.getParameter("pageNo");
if (ObjectUtil.isNotEmpty(pageNoString)) {
pageNo = Integer.parseInt(pageNoString);
}
return new Page((long)pageNo, (long)pageSize);
}
}
3.获取IP工具类
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.log.Log;
import com.alibaba.fastjson.JSONPath;
import com.yuetong.core.context.constant.ConstantContextHolder;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
public class IpAddressUtil {
private static final Log log = Log.get();
private static final String LOCAL_IP = "127.0.0.1";
private static final String LOCAL_REMOTE_HOST = "0:0:0:0:0:0:0:1";
public IpAddressUtil() {
}
public static String getIp(HttpServletRequest request) {
if (ObjectUtil.isEmpty(request)) {
return "127.0.0.1";
} else {
String remoteHost = ServletUtil.getClientIP(request, new String[0]);
return "0:0:0:0:0:0:0:1".equals(remoteHost) ? "127.0.0.1" : remoteHost;
}
}
public static String getAddress(HttpServletRequest request) {
String resultJson = "-";
String ip = getIp(request);
if (!ObjectUtil.isEmpty(ip) && !NetUtil.isInnerIP(ip)) {
try {
String api = ConstantContextHolder.getIpGeoApi();
String appCode = ConstantContextHolder.getIpGeoAppCode();
if (ObjectUtil.isAllNotEmpty(new Object[]{api, appCode})) {
String path = "$['data']['country','region','city','isp']";
String appCodeSymbol = "APPCODE";
HttpRequest http = HttpUtil.createGet(String.format(api, ip));
http.header("Authorization", appCodeSymbol + " " + appCode);
resultJson = http.timeout(3000).execute().body();
resultJson = String.join("", (List)JSONPath.read(resultJson, path));
}
} catch (Exception var8) {
resultJson = "-";
}
return resultJson;
} else {
return resultJson;
}
}
}
4.时间工具类
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import java.util.Date;
public class PastTimeFormatUtil {
private static final long ONE_MINUTE_SECONDS = 60L;
private static final int BEFORE_DAWN_HOUR = 6;
private static final int MORNING_END_HOUR = 12;
private static final int NOON_END_HOUR = 13;
private static final int AFTERNOON_END_HOUR = 18;
private static final int NIGHT_END_HOUR = 24;
public PastTimeFormatUtil() {
}
public static String formatPastTime(Date date) {
if (DateUtil.between(date, DateUtil.date(), DateUnit.SECOND, false) < 0L) {
return DateUtil.format(date, "yyyy-MM-dd HH:mm");
} else if (DateUtil.thisYear() == DateUtil.year(date)) {
if (DateUtil.isSameDay(date, DateUtil.date())) {
long betweenMinute = DateUtil.between(date, DateUtil.date(), DateUnit.MINUTE);
if (betweenMinute < 60L) {
return betweenMinute < 1L ? "刚刚" : betweenMinute + "分钟前";
} else {
return getTodayHour(date) + " " + DateUtil.format(date, "HH:mm");
}
} else if (DateUtil.isSameDay(date, DateUtil.yesterday())) {
return "昨天 " + DateUtil.format(date, "HH:mm");
} else if (isThisWeek(date)) {
int dayOfWeek = DateUtil.dayOfWeek(date) - 1;
String weekday;
switch(dayOfWeek) {
case 1:
weekday = "周一";
break;
case 2:
weekday = "周二";
break;
case 3:
weekday = "周三";
break;
case 4:
weekday = "周四";
break;
case 5:
weekday = "周五";
break;
case 6:
weekday = "周六";
break;
default:
weekday = "周日";
}
return weekday + " " + DateUtil.format(date, "HH:mm");
} else {
return DateUtil.format(date, "MM-dd HH:mm");
}
} else {
return DateUtil.format(date, "yyyy-MM-dd HH:mm");
}
}
private static boolean isThisWeek(Date date) {
DateTime beginOfWeek = DateUtil.beginOfWeek(DateUtil.date());
long betweenBegin = DateUtil.between(date, beginOfWeek, DateUnit.DAY, false) + 1L;
return DateUtil.isSameDay(date, beginOfWeek) || betweenBegin < 0L;
}
private static String getTodayHour(Date date) {
String result = "";
int hour = DateUtil.hour(date, true);
if (hour >= 0 && hour <= 6) {
result = "凌晨";
}
if (hour > 6 && hour < 12) {
result = "上午";
}
if (hour == 12) {
result = "中午";
}
if (hour >= 13 && hour <= 18) {
result = "下午";
}
if (hour > 18 && hour <= 24) {
result = "晚上";
}
return result;
}
}