String类方法
1.类型互转
Integer taskType = Integer.valueOf(String.valueOf(params.get("taskType")));
2.判空方法
if (StrUtil.isBlankIfStr(params.get("index"))){
params.put("index",1);
}else {
Integer index = Integer.valueOf(String.valueOf(params.get("index")));
params.put("index",index);
}
3.符号分割
String taskId = (String) params.get("taskId");
String[] taskIdArr = taskId.split(",");
4.手机号脱敏
//被分享者手机号
String refId ="" ;
refId = (String)jobObject.get("rel_id");
if (!StrUtil.isBlankIfStr(refId) && refId.length()==11){
String phone = refId.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
refId =phone;
}
5.时间日期类型转换
//账期
String cDate = (String)jobObject.get("c_date");
if (StrUtil.isBlankIfStr(cDate)){
cDate = "";
}else {
String cTime = cDate; //String类型
Date gTime = DateUtil.parse(cTime); //String类型 先转换为date类型
SimpleDateFormat s = new SimpleDateFormat("yyyy/MM/dd"); //格式化
cDate = s.format(gTime);
}
6.校验是否有非法字符(重点)
/**
* @Description: 用于检验map中参数是否合法
* @param params
* @return boolean 参数合法返回false 不合法返回true
*/
public static boolean verificationParamMap(Map<String,Object> params) {
ArrayList<Boolean> resultList = new ArrayList<>();
for (Map.Entry<String,Object> param : params.entrySet()) {
Object paramVal = param.getValue();
String paramValStr = String.valueOf(paramVal);
boolean result = Pattern.compile(Regex.SPECIAL_CHAR_REGEX).matcher(paramValStr).find();
resultList.add(result);
}
//在map集合中只要有一个值中包含特殊字符,则校验不通过 返回true
boolean returnResult = false;
if (resultList.contains(true)) {
returnResult = true;
}
return returnResult;
}
/**
* @Description: 校验手机号是否合法
* @param phone
* @return boolean 参数合法返回true 不合法返回false
*/
public static boolean verificationStringPhone(String phone) {
boolean result = Pattern.matches(Regex.MOBILE_PHONE, phone);
return result;
}
/**
* ClassName: Regex <br/>
* Description: 正则表达式工具类
* <br/>
* Reason: TODO ADD REASON(可选). <br/>
*/
public class Regex {
/**
* MOBILE_PHONE:手机号,1开头的11位数字.
*/
public static final String MOBILE_PHONE = "^1[0-9]{10}$";
public static final String TIME_ZONE = "^((0[0-9])|([1][0-9])|([2][0-3]))([0-5][0-9])$";
/**
* POSITIVE_INTEGER 正整数
*/
public static final String POSITIVE_INTEGER = "^[0-9]*[1-9][0-9]*$";
/**
* SPECIAL_CHAR_REGEX 特殊字符 -start pitao 2022/12/4
*/
public static final String SPECIAL_CHAR_REGEX = "<|>|\"|'|%|\\(|\\)|\\+|\\\\|\\\\'|\\\\\"|&|\\.\\./";
/**
* AREA_CODE_REGEX 省编码、市编码、区编码正则校验 8开头的7位数字
*/
public static final String AREA_CODE_REGEX = "^8[0-9]{6}$";
//-end pitao 2022/12/4
}
try {
pageList=slTaskIdDecrypt(pageList);
} catch (Exception e) {
info(LogEntity.builder().masUserTel(user.getUserId()), "解密失败,失败原因"+e.getMessage(), mas_code);
return ResponseResult.failure(e.getMessage());
}
public List<Map<String,Object>> slTaskIdDecrypt(List<Map<String,Object>> pageList) throws Exception {
JSONArray jsonarray1= JSONArray.parseArray(JSON.toJSONString(pageList));
for (int j = 0; j < jsonarray1.size(); j++) {
String metaJson = String.valueOf(jsonarray1.getJSONObject(j).get("metaJson"));
JSONArray jsonarray2 = JSONArray.parseArray(metaJson);
for (int i = 0; i < jsonarray2.size(); i++) {
String prizeType = String.valueOf(jsonarray2.getJSONObject(i).get("type"));
JSONObject properties2 = (JSONObject) jsonarray2.getJSONObject(i).get("properties");
if ("ShareList".equals(prizeType)) {
String slTaskId = (String) properties2.get("slTaskId");
if (!StrUtil.isBlankIfStr(slTaskId)){
String encrypt = AessUtils.aesDecrypt(slTaskId, "d7e5a769-ebeb-5d");
properties2.put("slTaskId", encrypt);
jsonarray2.getJSONObject(i).put("properties",properties2);
}
}
}
metaJson=jsonarray2.toJSONString();
jsonarray1.getJSONObject(j).put("metaJson",metaJson);
}
return (List) jsonarray1;
}
try {
pageList=slTaskIdDecrypt(pageList);
} catch (Exception e) {
info(LogEntity.builder().masUserTel(user.getUserId()), "解密失败,失败原因"+e.getMessage(), mas_code);
return ResponseResult.failure(e.getMessage());
}
public List<Map<String,Object>> slTaskIdDecrypt(List<Map<String,Object>> pageList) throws Exception {
List<Map<String, Object>> collect = pageList.stream().map(p -> new HashMap<String, Object>(p)).collect(Collectors.toList());
// JSONArray jsonarray1= JSONArray.parseArray(JSON.toJSONString(pageList));
for (int j = 0; j < collect.size(); j++) {
String metaJson = String.valueOf(collect.get(j).get("metaJson"));
JSONArray jsonarray2 = JSONArray.parseArray(metaJson);
for (int i = 0; i < jsonarray2.size(); i++) {
String prizeType = String.valueOf(jsonarray2.getJSONObject(i).get("type"));
JSONObject properties2 = (JSONObject) jsonarray2.getJSONObject(i).get("properties");
if ("ShareList".equals(prizeType)) {
String slTaskId = (String) properties2.get("slTaskId");
if (!StrUtil.isBlankIfStr(slTaskId)){
String encrypt = AessUtils.aesDecrypt(slTaskId, "d7e5a769-ebeb-5d");
properties2.put("slTaskId", encrypt);
jsonarray2.getJSONObject(i).put("properties",properties2);
}
}
}
metaJson=jsonarray2.toJSONString();
collect.get(j).put("metaJson",metaJson);
}
return collect;
}