package com.doumee.core.constants; import org.apache.commons.codec.binary.Hex; import org.apache.commons.lang3.StringUtils; import javax.servlet.http.HttpServletRequest; import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.net.InetAddress; import java.net.UnknownHostException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.List; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Constants { public static final String DEFAULT_PWD = "DEFAULT_PWD"; public static final String TRUE = "t"; public static final String FALSE = "f"; public static final String TOKEN ="TOKEN" ; public static String REDIS_DEBUG_STR="test_"; /** * 数据权限范围:0只看自己;1只看自己所在校区;2看全部校区;3看指定校区(多选) */ public interface PermissionType{ int self =0; int self_campus =1; int all =2; int partful = 3; } public interface RedisKeys { public static final String submission_period_key = "sp_"; } public static final String ACCESS_ID="ACCESS_ID"; public static final String BUCKETNAME = "BUCKETNAME"; public static final String OSS = "OSS"; public static final String ACCESS_KEY = "ACCESS_KEY"; public static final String SCRATCH_REDIRECT = "SCRATCH_REDIRECT"; public static final String ENDPOINT = "ENDPOINT"; public static final String TESTCASE_UPLOAD_PATH = "TESTCASE_UPLOAD_PATH"; public static final String RESOURCE_PATH = "RESOURCE_PATH"; public static final String UPLOAD_FILE = "UPLOAD_FILE"; public static final String AVATAR_FILE = "AVATAR_FILE"; public static final String FILES_DIR = "FILES_DIR"; public static final String SCRATCH_REDIRECT_URL = "SCRATCH_REDIRECT_URL"; public static final Integer ZERO = 0 ; public static final Integer ONE = 1 ; public static final Integer TWO = 2 ; public static int formatIntegerNum(Integer num){ if(num == null){ return 0; } return num; } public interface UserActionsType{ String cancelPrePay="CANCEL_PRE_PAY"; //订单结算订单 String close="CLOSE"; String forceRefund="FORCE_REFUND"; String login="LOGIN"; String pay="PAY"; String preForceRefund="PRE_FORCE_REFUND"; String prePay="PRE_PAY"; String preRefund="PRE_REFUND"; String refund="REFUND"; String register="REGISTER"; String rent="RENT"; } public static BigDecimal formatDecimalNum(BigDecimal num){ if(num == null){ return new BigDecimal(0); } return num; } public static int compareBigdecimal(BigDecimal num,BigDecimal num2){ if(num == null){ num =new BigDecimal(0); } if(num2 == null){ num2 =new BigDecimal(0); } if (num.compareTo(num2) == 1) { return 1; } else if (num.compareTo(num2)== -1) { return -1; } return 0; } public static long formatLongNum(Long num){ if(num == null){ return 0; } return num; } public static String formatImgPath(String avatar, String imgPath) { if(StringUtils.isBlank(avatar)){ return null; } return imgPath+avatar; } public static String getIpAddr(HttpServletRequest request) { String ipAddress = request.getHeader("x-forwarded-for"); if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getHeader("Proxy-Client-IP"); } if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getHeader("WL-Proxy-Client-IP"); } if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getRemoteAddr(); String localIp = "127.0.0.1"; String localIpv6 = "0:0:0:0:0:0:0:1"; if (ipAddress.equals(localIp) || ipAddress.equals(localIpv6)) { // 根据网卡取本机配置的IP InetAddress inet = null; try { inet = InetAddress.getLocalHost(); ipAddress = inet.getHostAddress(); } catch (UnknownHostException e) { e.printStackTrace(); } } } // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割 String ipSeparate = ","; int ipLength = 15; if (ipAddress != null && ipAddress.length() > ipLength) { if (ipAddress.indexOf(ipSeparate) > 0) { ipAddress = ipAddress.substring(0, ipAddress.indexOf(ipSeparate)); } } return ipAddress; } public static String getSHA256Str(String str) { MessageDigest messageDigest; String encdeStr = ""; try { messageDigest = MessageDigest.getInstance("SHA-256"); byte[] hash = messageDigest.digest(str.getBytes("UTF-8")); encdeStr = Hex.encodeHexString(hash); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return encdeStr; } public static String trimEndSpaceStr(String outPut){ String newOutPut = ""; if(StringUtils.isNotBlank(outPut)){ String[] ostrs = outPut.split("\n"); for(String str : ostrs){ String text = str.replace("\n","").replaceAll("\\s+$", ""); newOutPut += text+"\n"; } if(newOutPut.endsWith("\n")){ newOutPut = newOutPut.substring(0,newOutPut.length()-1); } } return newOutPut; } public static String delHTMLTag(String htmlStr) { if (StringUtils.isBlank(htmlStr)) { return ""; } String regEx_script = "]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式 String regEx_style = "]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式 String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式 Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE); Matcher m_script = p_script.matcher(htmlStr); htmlStr = m_script.replaceAll(""); // 过滤script标签 Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE); Matcher m_style = p_style.matcher(htmlStr); htmlStr = m_style.replaceAll(""); // 过滤style标签 Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE); Matcher m_html = p_html.matcher(htmlStr); htmlStr = m_html.replaceAll(""); // 过滤html标签 htmlStr = htmlStr.replace("  "," "); // 过滤空格标签 htmlStr = htmlStr.replace(" "," "); // 过滤空格标签 return htmlStr.trim(); // 返回文本字符串 } public static List checkUUID(String checkValue){ String [] str = checkValue.split(","); List strList = new ArrayList<>(); for (String s:str) { try{ String uuid = UUID.fromString(s).toString(); strList.add(uuid); //do something } catch (IllegalArgumentException exception){ continue; } } return strList; } }