package com.navitek.maternal.common.utils;
import com.alibaba.fastjson.JSONObject;
import com.navitek.maternal.common.constants.WxContstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.*;
/**
* @author 26968
*/
@Slf4j
public class Tools {
/**
* 将 map转化为xml格式
*
* @param params
* @return
*/
public static String toXml(Map<String, String> params) {
StringBuilder xml = new StringBuilder();
xml.append("<xml>");
Iterator iterator = params.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = (Map.Entry) iterator.next();
String key = (String) entry.getKey();
String value = (String) entry.getValue();
if (!StringUtils.isEmpty(value)) {
xml.append("<").append(key).append(">");
xml.append((String) entry.getValue());
xml.append("</").append(key).append(">");
}
}
xml.append("</xml>");
return xml.toString();
}
/**
* 将xml格式的字符串转化为 map
*
* @param xmlStr
* @return
*/
public static Map<String, String> xmlToMap(String xmlStr) {
XmlHelper xmlHelper = XmlHelper.of(xmlStr);
return xmlHelper.toMap();
}
public static String getRealIp(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
public static String getRealIpV2(HttpServletRequest request) {
String accessIP = request.getHeader("x-forwarded-for");
return null == accessIP ? request.getRemoteAddr() : accessIP;
}
/**
* 获取access_Token
*
* @return
*/
public static String getAccessToken() {
/**
* @Todo 暂时存在静态 map中
*/
String accessToken = null;
//先判断时间
Long tokenTime = StringUtils.isEmpty((Long) WxContstants.currentHashMap.get(WxContstants.ACCESS_TOKEN)) ? 0:(Long) WxContstants.currentHashMap.get(WxContstants.ACCESS_TOKEN);
//如果当前时间大于设置的时间则进行重新获取accessToken
if (Instant.now().getEpochSecond() > tokenTime) {
JSONObject result = HttpClientUtils.httpRequest(WxContstants.ACCESSTOKENURL, "GET", "");
if (StringUtils.isEmpty(result.get(WxContstants.ACCESS_TOKEN))){
log.info("调取微信接口获取access_token失败=="+result);
}
accessToken = String.valueOf(result.get(WxContstants.ACCESS_TOKEN));
WxContstants.TOKEN_MAP.put(WxContstants.ACCESS_TOKEN,accessToken);
WxContstants.currentHashMap.put(WxContstants.ACCESS_TOKEN,Instant.now().getEpochSecond()+6000);
}
accessToken = String.valueOf(WxContstants.TOKEN_MAP.get(WxContstants.ACCESS_TOKEN));
return accessToken;
}
public static String getRandCode(){
Random ne=new Random();
return String.valueOf(ne.nextInt(9999-1000+1)+1000);
}
public static String getUUID(){
UUID uuid=UUID.randomUUID();
String str = uuid.toString();
String uuidStr=str.replace("-", "");
return uuidStr;
}
public static String getOrderNo() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Calendar nowTime = Calendar.getInstance();
String dateStr = sdf.format(nowTime.getTime());
dateStr =dateStr+System.currentTimeMillis() ;
return dateStr;
}
private static boolean isEmojiCharacter(char codePoint) {
return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) || (codePoint == 0xD)
|| ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
|| ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
}
/**
* 检测是否有emoji字符
*
* @param source
* @return 一旦含有就抛出
*/
public static boolean containsEmoji(String source) {
if (StringUtils.isEmpty(source)) {
return false;
}
int len = source.length();
for (int i = 0; i < len; i++) {
char codePoint = source.charAt(i);
if (isEmojiCharacter(codePoint)) {
// do nothing,判断到了这里表明,确认有表情字符
return true;
}
}
return false;
}
/**
* 过滤emoji 或者 其他非文字类型的字符
* @param source
* @return
*/
public static String filterEmoji(String source) {
// 如果不包含,直接返回
if (!containsEmoji(source)) {
return source;
}
// 到这里铁定包含
StringBuilder buf = null;
int len = source.length();
for (int i = 0; i < len; i++) {
char codePoint = source.charAt(i);
if (isEmojiCharacter(codePoint)) {
if (buf == null) {
buf = new StringBuilder(source.length());
}
buf.append(codePoint);
} else {
}
}
// 如果没有找到 emoji表情,则返回源字符串
if (buf == null) {
return source;
} else {
// 这里的意义在于尽可能少的toString,因为会重新生成字符串
if (buf.length() == len) {
buf = null;
return source;
} else {
return buf.toString();
}
}
}
}