package com.xiaobodata.biz.modules.indicatrix.util;
import cn.hutool.core.util.IdUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.sun.istack.Nullable;
import com.xiaobodata.biz.common.system.api.ISysBaseAPI;
import com.xiaobodata.biz.common.system.vo.DictModel;
import com.xiaobodata.biz.common.system.vo.LoginUser;
import com.xiaobodata.biz.modules.indicatrix.exception.XiaoBoDataException;
import com.xiaobodata.biz.modules.system.service.impl.SysBaseApiImpl;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* @Classname CheckUtils
* @Description TODO
* @Created by 李杰
* @Date 2022/2/8 10:10
*/
@Component
public class CheckUtils {
private static ISysBaseAPI sysBaseAPI;
@Autowired
public void setSysBaseAPI(ISysBaseAPI sysBaseAPI) {
CheckUtils.sysBaseAPI = sysBaseAPI;
}
/**
* 判断集合是否为空
* @param collectionName 集合名称
* @param collection 集合参数
*/
public static void checkCollections(@Nullable String collectionName, Collection<?> collection){
if(CollectionUtils.isEmpty(collection)){
throw new XiaoBoDataException(collectionName + "为空");
}
}
/**
* 判断对象是否为空
*/
public static void checkNotNull(@Nullable String name,Object object){
if(object == null){
throw new XiaoBoDataException(name + "为空");
}
}
/**
* 判断字符串参数是否为空
* @param paramName 参数名称
* @param param 参数
*/
public static void checkString(@Nullable String paramName, String param){
if(StringUtils.isEmpty(param)){
throw new XiaoBoDataException(paramName + "参数为空");
}
}
/**
* 校验字符串长度
* @param paramName 返回字符串名称
* @param param 参数
* @param limit 长度
*/
public static void checkLength(String paramName,String param,int limit){
checkString(paramName,param);
if(param.length() > limit){
throw new XiaoBoDataException(paramName + "长度超出最大长度" + limit + " ,请重新输入");
}
}
/**
* 校验字符串长度 不检验为空
* @param paramName 返回字符串名称
* @param param 参数
* @param limit 长度
*/
public static void checkLengthNotCheckIsNull(String paramName,String param,int limit){
if(param==null)
return ;
if(param.length() > limit){
throw new XiaoBoDataException(paramName + "长度超出最大长度" + limit + " ,请重新输入");
}
}
// /**
// * 校验(m-n位)数字 正则不太对
// * @param content
// * @return
// */
// public static void isMToNFigures(String paramName,String content,Integer m,Integer n){
// if(!Pattern.matches("^([0-9]{1,3}+\\.[0-9]){"+m+","+n+"}$", content)){
// throw new XiaoBoDataException(paramName+" 仅支持"+m+"-"+n+"位数字");
// }
// }
/**
* 校验数字 英文 汉字
* @param content
* @return
*/
public static void isChineseAndEnglishAndFigures(String paramName,String content,Integer lengthStart,Integer lengthEnd){
if(!Pattern.matches("^[\\u4E00-\\u9FA5A-Za-z0-9]{"+lengthStart+","+lengthEnd+"}+$", content)){
throw new XiaoBoDataException(paramName+" 仅支持"+lengthEnd+"位中英文、数字");
}
}
/**
* 校验数字 英文 汉字 _
* @param content
* @return
*/
public static void isChineseAndEnglishAndFiguresAndUnderline(String paramName,String content,Integer lengthStart,Integer lengthEnd){
if (!Pattern.matches("^[\\u4E00-\\u9FA5_A-Za-z0-9]{"+lengthStart+","+lengthEnd+"}+$", content)) {
throw new XiaoBoDataException(paramName+" 仅支持"+lengthEnd+"位中英文、数字和_");
}
}
/**
* 校验数字 英文 汉字 ()
* @param content
* @return
*/
public static void isChineseAndEnglishAndFiguresAndHaveBracket(String paramName,String content,Integer lengthStart,Integer lengthEnd){
if (!Pattern.matches("^[\\u4E00-\\u9FA5()()A-Za-z0-9]{"+lengthStart+","+lengthEnd+"}+$", content)) {
throw new XiaoBoDataException(paramName+" 仅支持"+lengthEnd+"位中英文、数字和{}");
}
}
/**
* 校验数字 英文 汉字 ,
* @param content
* @return
*/
public static void isChineseAndEnglishAndFiguresAndComma(String paramName,String content,Integer lengthStart,Integer lengthEnd){
if (!Pattern.matches("^[\\u4E00-\\u9FA5,,A-Za-z0-9]{"+lengthStart+","+lengthEnd+"}+$", content)) {
throw new XiaoBoDataException(paramName+" 仅支持"+lengthEnd+"位中英文、数字和,");
}
}
/**
* 获取当前登录用户信息
*/
public static LoginUser getCurrentUser(){
return (LoginUser) SecurityUtils.getSubject().getPrincipal();
}
public static String getId(){
return IdUtil.createSnowflake(0L,0L).nextIdStr();
}
public static void CheckDictItem(String paramName,String code,String param){
List<DictModel> dictItems = sysBaseAPI.getDictItems(code);
if(!dictItems.isEmpty()){
Set<String> collect = dictItems.stream().map(DictModel::getValue).collect(Collectors.toSet());
if(!collect.contains(param)){
throw new XiaoBoDataException("请选择正确的"+paramName);
}
}
}
}
java Controller 基础 校验 类
最新推荐文章于 2024-09-12 00:04:33 发布
此篇博客介绍了CheckUtils工具类,用于Java项目中对集合、对象、字符串长度、正则表达式等进行有效校验,以及获取登录用户信息和生成唯一ID。核心功能包括空值检查、长度限制、字符类型验证和业务逻辑集成。
940

被折叠的 条评论
为什么被折叠?



