mirror of
https://github.com/jeecgboot/JeecgBoot.git
synced 2025-12-26 16:26:41 +08:00
jeecg-boot 2.0 模块开发版本发布
This commit is contained in:
@ -0,0 +1,104 @@
|
||||
package org.jeecg.common.api.vo;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import org.jeecg.common.constant.CommonConstant;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 接口返回数据格式
|
||||
* @author scott
|
||||
* @email jeecgos@163.com
|
||||
* @date 2019年1月19日
|
||||
*/
|
||||
@Data
|
||||
@ApiModel(value="接口返回对象", description="接口返回对象")
|
||||
public class Result<T> implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 成功标志
|
||||
*/
|
||||
@ApiModelProperty(value = "成功标志")
|
||||
private boolean success = true;
|
||||
|
||||
/**
|
||||
* 返回处理消息
|
||||
*/
|
||||
@ApiModelProperty(value = "返回处理消息")
|
||||
private String message = "操作成功!";
|
||||
|
||||
/**
|
||||
* 返回代码
|
||||
*/
|
||||
@ApiModelProperty(value = "返回代码")
|
||||
private Integer code = 0;
|
||||
|
||||
/**
|
||||
* 返回数据对象 data
|
||||
*/
|
||||
@ApiModelProperty(value = "返回数据对象")
|
||||
private T result;
|
||||
|
||||
public Result() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 时间戳
|
||||
*/
|
||||
@ApiModelProperty(value = "时间戳")
|
||||
private long timestamp = System.currentTimeMillis();
|
||||
|
||||
public void error500(String message) {
|
||||
this.message = message;
|
||||
this.code = CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
|
||||
this.success = false;
|
||||
}
|
||||
|
||||
public void success(String message) {
|
||||
this.message = message;
|
||||
this.code = CommonConstant.SC_OK_200;
|
||||
this.success = true;
|
||||
}
|
||||
|
||||
public static Result<Object> error(String msg) {
|
||||
return error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, msg);
|
||||
}
|
||||
|
||||
public static Result<Object> error(int code, String msg) {
|
||||
Result<Object> r = new Result<Object>();
|
||||
r.setCode(code);
|
||||
r.setMessage(msg);
|
||||
r.setSuccess(false);
|
||||
return r;
|
||||
}
|
||||
|
||||
public static Result<Object> ok() {
|
||||
Result<Object> r = new Result<Object>();
|
||||
r.setSuccess(true);
|
||||
r.setCode(CommonConstant.SC_OK_200);
|
||||
r.setMessage("成功");
|
||||
return r;
|
||||
}
|
||||
|
||||
public static Result<Object> ok(String msg) {
|
||||
Result<Object> r = new Result<Object>();
|
||||
r.setSuccess(true);
|
||||
r.setCode(CommonConstant.SC_OK_200);
|
||||
r.setMessage(msg);
|
||||
return r;
|
||||
}
|
||||
|
||||
public static Result<Object> ok(Object data) {
|
||||
Result<Object> r = new Result<Object>();
|
||||
r.setSuccess(true);
|
||||
r.setCode(CommonConstant.SC_OK_200);
|
||||
r.setResult(data);
|
||||
return r;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,36 @@
|
||||
package org.jeecg.common.aspect.annotation;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.jeecg.common.constant.CommonConstant;
|
||||
|
||||
/**
|
||||
* 系统日志注解
|
||||
*
|
||||
* @Author scott
|
||||
* @email jeecgos@163.com
|
||||
* @Date 2019年1月14日
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface AutoLog {
|
||||
|
||||
/**
|
||||
* 日志内容
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String value() default "";
|
||||
|
||||
/**
|
||||
* 日志类型
|
||||
*
|
||||
* @return 0:操作日志;1:登录日志;2:定时任务;
|
||||
*/
|
||||
int logType() default CommonConstant.LOG_TYPE_2;
|
||||
}
|
||||
@ -0,0 +1,42 @@
|
||||
package org.jeecg.common.aspect.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 类描述: 字典注解
|
||||
* 作 者: dangzhenghui
|
||||
* 日 期: 2019年03月17日-下午9:37:16
|
||||
*/
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Dict {
|
||||
/**
|
||||
* 方法描述: 数据code
|
||||
* 作 者: dangzhenghui
|
||||
* 日 期: 2019年03月17日-下午9:37:16
|
||||
*
|
||||
* @return 返回类型: String
|
||||
*/
|
||||
String dicCode();
|
||||
|
||||
/**
|
||||
* 方法描述: 数据Text
|
||||
* 作 者: dangzhenghui
|
||||
* 日 期: 2019年03月17日-下午9:37:16
|
||||
*
|
||||
* @return 返回类型: String
|
||||
*/
|
||||
String dicText() default "";
|
||||
|
||||
/**
|
||||
* 方法描述: 数据字典表
|
||||
* 作 者: dangzhenghui
|
||||
* 日 期: 2019年03月17日-下午9:37:16
|
||||
*
|
||||
* @return 返回类型: String
|
||||
*/
|
||||
String dictTable() default "";
|
||||
}
|
||||
@ -0,0 +1,29 @@
|
||||
package org.jeecg.common.aspect.annotation;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 数据权限注解
|
||||
* @Author taoyan
|
||||
* @Date 2019年4月11日
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.TYPE,ElementType.METHOD})
|
||||
@Documented
|
||||
public @interface PermissionData {
|
||||
/**
|
||||
* 暂时没用
|
||||
* @return
|
||||
*/
|
||||
String value() default "";
|
||||
|
||||
|
||||
/**
|
||||
* 配置菜单的组件路径,用于数据权限
|
||||
*/
|
||||
String pageComponent() default "";
|
||||
}
|
||||
@ -0,0 +1,80 @@
|
||||
package org.jeecg.common.constant;
|
||||
|
||||
public interface CommonConstant {
|
||||
|
||||
/**
|
||||
* 正常状态
|
||||
*/
|
||||
Integer STATUS_NORMAL = 0;
|
||||
|
||||
/**
|
||||
* 禁用状态
|
||||
*/
|
||||
Integer STATUS_DISABLE = -1;
|
||||
|
||||
/**
|
||||
* 删除标志
|
||||
*/
|
||||
Integer DEL_FLAG_1 = 1;
|
||||
|
||||
/**
|
||||
* 未删除
|
||||
*/
|
||||
Integer DEL_FLAG_0 = 0;
|
||||
|
||||
/**
|
||||
* 系统日志类型: 登录
|
||||
*/
|
||||
int LOG_TYPE_1 = 1;
|
||||
|
||||
/**
|
||||
* 系统日志类型: 操作
|
||||
*/
|
||||
int LOG_TYPE_2 = 2;
|
||||
|
||||
|
||||
/** {@code 500 Server Error} (HTTP/1.0 - RFC 1945) */
|
||||
public static final Integer SC_INTERNAL_SERVER_ERROR_500 = 500;
|
||||
/** {@code 200 OK} (HTTP/1.0 - RFC 1945) */
|
||||
public static final Integer SC_OK_200 = 200;
|
||||
|
||||
/** 登录用户拥有角色缓存KEY前缀 */
|
||||
public static String LOGIN_USER_CACHERULES_ROLE = "loginUser_cacheRules::Roles_";
|
||||
/** 登录用户拥有权限缓存KEY前缀 */
|
||||
public static String LOGIN_USER_CACHERULES_PERMISSION = "loginUser_cacheRules::Permissions_";
|
||||
/** 登录用户令牌缓存KEY前缀 */
|
||||
public static int TOKEN_EXPIRE_TIME = 3600; //3600秒即是一小时
|
||||
|
||||
public static String PREFIX_USER_TOKEN = "PREFIX_USER_TOKEN_";
|
||||
|
||||
/**
|
||||
* 0:一级菜单
|
||||
*/
|
||||
public static Integer MENU_TYPE_0 = 0;
|
||||
/**
|
||||
* 1:子菜单
|
||||
*/
|
||||
public static Integer MENU_TYPE_1 = 1;
|
||||
/**
|
||||
* 2:按钮权限
|
||||
*/
|
||||
public static Integer MENU_TYPE_2 = 2;
|
||||
|
||||
/**通告对象类型(USER:指定用户,ALL:全体用户)*/
|
||||
public static String MSG_TYPE_UESR = "USER";
|
||||
public static String MSG_TYPE_ALL = "ALL";
|
||||
|
||||
/**发布状态(0未发布,1已发布,2已撤销)*/
|
||||
public static String NO_SEND = "0";
|
||||
public static String HAS_SEND = "1";
|
||||
public static String HAS_CANCLE = "2";
|
||||
|
||||
/**阅读状态(0未读,1已读)*/
|
||||
public static String HAS_READ_FLAG = "1";
|
||||
public static String NO_READ_FLAG = "0";
|
||||
|
||||
/**优先级(L低,M中,H高)*/
|
||||
public static String PRIORITY_L = "L";
|
||||
public static String PRIORITY_M = "M ";
|
||||
public static String PRIORITY_H = "H";
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
package org.jeecg.common.constant;
|
||||
|
||||
/**
|
||||
* 系统通告 - 发布状态
|
||||
* @Author LeeShaoQing
|
||||
*
|
||||
*/
|
||||
public interface CommonSendStatus {
|
||||
|
||||
String UNPUBLISHED_STATUS_0 = "0"; //未发布
|
||||
|
||||
String PUBLISHED_STATUS_1 = "1"; //已发布
|
||||
|
||||
String REVOKE_STATUS_2 = "2"; //撤销
|
||||
}
|
||||
@ -0,0 +1,100 @@
|
||||
package org.jeecg.common.constant;
|
||||
/**
|
||||
* 数据库上下文常量
|
||||
*/
|
||||
public interface DataBaseConstant {
|
||||
|
||||
//*********系统上下文变量****************************************
|
||||
/**
|
||||
* 数据-所属机构编码
|
||||
*/
|
||||
public static final String SYS_ORG_CODE = "sysOrgCode";
|
||||
/**
|
||||
* 数据-所属机构编码
|
||||
*/
|
||||
public static final String SYS_MULTI_ORG_CODE = "sysMultiOrgCode";
|
||||
|
||||
/**
|
||||
* 数据-所属机构编码
|
||||
*/
|
||||
public static final String SYS_ORG_CODE_TABLE = "sys_org_code";
|
||||
/**
|
||||
* 数据-系统用户编码(对应登录用户账号)
|
||||
*/
|
||||
public static final String SYS_USER_CODE = "sysUserCode";
|
||||
/**
|
||||
* 数据-系统用户编码(对应登录用户账号)
|
||||
*/
|
||||
public static final String SYS_USER_CODE_TABLE = "sys_user_code";
|
||||
|
||||
/**
|
||||
* 登录用户真实姓名
|
||||
*/
|
||||
public static final String SYS_USER_NAME = "sysUserName";
|
||||
/**
|
||||
* 登录用户真实姓名
|
||||
*/
|
||||
public static final String SYS_USER_NAME_TABLE = "sys_user_name";
|
||||
/**
|
||||
* 系统日期"yyyy-MM-dd"
|
||||
*/
|
||||
public static final String SYS_DATE = "sysDate";
|
||||
/**
|
||||
* 系统日期"yyyy-MM-dd"
|
||||
*/
|
||||
public static final String SYS_DATE_TABLE = "sys_date";
|
||||
/**
|
||||
* 系统时间"yyyy-MM-dd HH:mm"
|
||||
*/
|
||||
public static final String SYS_TIME = "sysTime";
|
||||
/**
|
||||
* 系统时间"yyyy-MM-dd HH:mm"
|
||||
*/
|
||||
public static final String SYS_TIME_TABLE = "sys_time";
|
||||
//*********系统上下文变量****************************************
|
||||
|
||||
|
||||
//*********系统建表标准字段****************************************
|
||||
/**
|
||||
* 创建者登录名称
|
||||
*/
|
||||
public static final String CREATE_BY_TABLE = "create_by";
|
||||
/**
|
||||
* 创建者登录名称
|
||||
*/
|
||||
public static final String CREATE_BY = "createBy";
|
||||
/**
|
||||
* 创建日期时间
|
||||
*/
|
||||
public static final String CREATE_TIME_TABLE = "create_time";
|
||||
/**
|
||||
* 创建日期时间
|
||||
*/
|
||||
public static final String CREATE_TIME = "createTime";
|
||||
/**
|
||||
* 更新用户登录名称
|
||||
*/
|
||||
public static final String UPDATE_BY_TABLE = "update_by";
|
||||
/**
|
||||
* 更新用户登录名称
|
||||
*/
|
||||
public static final String UPDATE_BY = "updateBy";
|
||||
/**
|
||||
* 更新日期时间
|
||||
*/
|
||||
public static final String UPDATE_TIME = "updateTime";
|
||||
/**
|
||||
* 更新日期时间
|
||||
*/
|
||||
public static final String UPDATE_TIME_TABLE = "update_time";
|
||||
|
||||
/**
|
||||
* 业务流程状态
|
||||
*/
|
||||
public static final String BPM_STATUS = "bpmStatus";
|
||||
/**
|
||||
* 业务流程状态
|
||||
*/
|
||||
public static final String BPM_STATUS_TABLE = "bpm_status";
|
||||
//*********系统建表标准字段****************************************
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
package org.jeecg.common.exception;
|
||||
|
||||
public class JeecgBootException extends RuntimeException {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public JeecgBootException(String message){
|
||||
super(message);
|
||||
}
|
||||
|
||||
public JeecgBootException(Throwable cause)
|
||||
{
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public JeecgBootException(String message,Throwable cause)
|
||||
{
|
||||
super(message,cause);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,68 @@
|
||||
package org.jeecg.common.exception;
|
||||
|
||||
import org.apache.shiro.authz.AuthorizationException;
|
||||
import org.apache.shiro.authz.UnauthorizedException;
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.servlet.NoHandlerFoundException;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 异常处理器
|
||||
*
|
||||
* @Author scott
|
||||
* @Date 2019
|
||||
*/
|
||||
@RestControllerAdvice
|
||||
@Slf4j
|
||||
public class JeecgBootExceptionHandler {
|
||||
|
||||
/**
|
||||
* 处理自定义异常
|
||||
*/
|
||||
@ExceptionHandler(JeecgBootException.class)
|
||||
public Result<?> handleRRException(JeecgBootException e){
|
||||
log.error(e.getMessage(), e);
|
||||
return Result.error(e.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(NoHandlerFoundException.class)
|
||||
public Result<?> handlerNoFoundException(Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
return Result.error(404, "路径不存在,请检查路径是否正确");
|
||||
}
|
||||
|
||||
@ExceptionHandler(DuplicateKeyException.class)
|
||||
public Result<?> handleDuplicateKeyException(DuplicateKeyException e){
|
||||
log.error(e.getMessage(), e);
|
||||
return Result.error("数据库中已存在该记录");
|
||||
}
|
||||
|
||||
@ExceptionHandler({UnauthorizedException.class, AuthorizationException.class})
|
||||
public Result<?> handleAuthorizationException(AuthorizationException e){
|
||||
log.error(e.getMessage(), e);
|
||||
return Result.error("没有权限,请联系管理员授权");
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public Result<?> handleException(Exception e){
|
||||
log.error(e.getMessage(), e);
|
||||
return Result.error(e.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* @Author 政辉
|
||||
* @param e
|
||||
* @return
|
||||
*/
|
||||
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
||||
public Result<?> HttpRequestMethodNotSupportedException(Exception e){
|
||||
log.error(e.getMessage(), e);
|
||||
return Result.error("没有权限,请联系管理员授权");
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,75 @@
|
||||
package org.jeecg.common.system.api;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import org.jeecg.common.system.vo.DictModel;
|
||||
import org.jeecg.common.system.vo.LoginUser;
|
||||
|
||||
/**
|
||||
* @Description: 底层共通业务API,提供其他独立模块调用
|
||||
* @Author: scott
|
||||
* @Date:2019-4-20
|
||||
* @Version:V1.0
|
||||
*/
|
||||
public interface ISysBaseAPI {
|
||||
|
||||
/**
|
||||
* 日志添加
|
||||
* @param LogContent 内容
|
||||
* @param logType 日志类型(0:操作日志;1:登录日志;2:定时任务)
|
||||
* @param operatetype 操作类型(1:添加;2:修改;3:删除;)
|
||||
*/
|
||||
void addLog(String LogContent, Integer logType, Integer operatetype);
|
||||
|
||||
/**
|
||||
* 根据用户账号查询登录用户信息
|
||||
* @param username
|
||||
* @return
|
||||
*/
|
||||
public LoginUser getUserByName(String username);
|
||||
|
||||
/**
|
||||
* 通过用户账号查询角色集合
|
||||
* @param username
|
||||
* @return
|
||||
*/
|
||||
public List<String> getRolesByUsername(String username);
|
||||
|
||||
/**
|
||||
* 获取当前数据库类型
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public String getDatabaseType() throws SQLException;
|
||||
|
||||
/**
|
||||
* 获取数据字典
|
||||
* @param code
|
||||
* @return
|
||||
*/
|
||||
public List<DictModel> queryDictItemsByCode(String code);
|
||||
|
||||
/**
|
||||
* 获取表数据字典
|
||||
* @param table
|
||||
* @param text
|
||||
* @param code
|
||||
* @return
|
||||
*/
|
||||
List<DictModel> queryTableDictItemsByCode(String table, String text, String code);
|
||||
|
||||
/**
|
||||
* 查询所有部门 作为字典信息 id -->value,departName -->text
|
||||
* @return
|
||||
*/
|
||||
public List<DictModel> queryAllDepartBackDictModel();
|
||||
|
||||
/**
|
||||
* 发送系统消息
|
||||
* @param fromUser 发送人(用户登录账户)
|
||||
* @param toUser 发送给(用户登录账户)
|
||||
* @param title 消息主题
|
||||
* @param msgContent 消息内容
|
||||
*/
|
||||
public void sendSysAnnouncement(String fromUser,String toUser,String title, String msgContent);
|
||||
}
|
||||
@ -0,0 +1,101 @@
|
||||
package org.jeecg.common.system.base.controller;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
import org.jeecg.common.system.base.entity.JeecgEntity;
|
||||
import org.jeecg.common.system.base.service.JeecgService;
|
||||
import org.jeecg.common.system.query.QueryGenerator;
|
||||
import org.jeecg.common.system.vo.LoginUser;
|
||||
import org.jeecgframework.poi.excel.ExcelImportUtil;
|
||||
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
|
||||
import org.jeecgframework.poi.excel.entity.ExportParams;
|
||||
import org.jeecgframework.poi.excel.entity.ImportParams;
|
||||
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @Description: Controller基类
|
||||
* @Author: dangzhenghui@163.com
|
||||
* @Date: 2019-4-21 8:13
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Slf4j
|
||||
public class JeecgController<T extends JeecgEntity, S extends JeecgService<T>> {
|
||||
@Autowired
|
||||
S service;
|
||||
|
||||
/**
|
||||
* 导出excel
|
||||
*
|
||||
* @param request
|
||||
* @param response
|
||||
*/
|
||||
protected ModelAndView exportXls(HttpServletRequest request,T object,Class<T> clazz,String title) {
|
||||
//--------------------------------------------------------------------------------
|
||||
//获取当前用户
|
||||
LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
|
||||
//--------------------------------------------------------------------------------
|
||||
// Step.1 组装查询条件
|
||||
QueryWrapper<T> queryWrapper = QueryGenerator.initQueryWrapper(object, request.getParameterMap());
|
||||
|
||||
// Step.2 AutoPoi 导出Excel
|
||||
ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
|
||||
List<T> pageList = service.list(queryWrapper);
|
||||
// 导出文件名称
|
||||
mv.addObject(NormalExcelConstants.FILE_NAME, title); //此处设置的filename无效 ,前端会重更新设置一下
|
||||
mv.addObject(NormalExcelConstants.CLASS, clazz);
|
||||
mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(title + "报表", "导出人:"+sysUser.getRealname(), title + "表"));
|
||||
mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
|
||||
return mv;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过excel导入数据
|
||||
*
|
||||
* @param request
|
||||
* @param response
|
||||
* @return
|
||||
*/
|
||||
protected Result<?> importExcel(HttpServletRequest request, HttpServletResponse response, Class<T> clazz) {
|
||||
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
||||
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
|
||||
for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
|
||||
MultipartFile file = entity.getValue();// 获取上传文件对象
|
||||
ImportParams params = new ImportParams();
|
||||
params.setTitleRows(2);
|
||||
params.setHeadRows(1);
|
||||
params.setNeedSave(true);
|
||||
try {
|
||||
List<T> list = ExcelImportUtil.importExcel(file.getInputStream(), clazz, params);
|
||||
for (T t : list) {
|
||||
service.save(t);
|
||||
}
|
||||
return Result.ok("文件导入成功!数据行数:" + list.size());
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
return Result.error("文件导入失败:" + e.getMessage());
|
||||
} finally {
|
||||
try {
|
||||
file.getInputStream().close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return Result.error("文件导入失败!");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,50 @@
|
||||
package org.jeecg.common.system.base.entity;
|
||||
|
||||
import org.jeecgframework.poi.excel.annotation.Excel;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
/**
|
||||
* @Description: Entity基类
|
||||
* @Author: dangzhenghui@163.com
|
||||
* @Date: 2019-4-28
|
||||
* @Version: 1.1
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Accessors(chain = true)
|
||||
public class JeecgEntity {
|
||||
|
||||
/** ID */
|
||||
@TableId(type = IdType.UUID)
|
||||
@ApiModelProperty(value = "ID")
|
||||
private java.lang.String id;
|
||||
/** 创建人 */
|
||||
@ApiModelProperty(value = "创建人")
|
||||
@Excel(name = "创建人", width = 15)
|
||||
private java.lang.String createBy;
|
||||
/** 创建时间 */
|
||||
@ApiModelProperty(value = "创建时间")
|
||||
@Excel(name = "创建时间", width = 20, format = "yyyy-MM-dd HH:mm:ss")
|
||||
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private java.util.Date createTime;
|
||||
/** 更新人 */
|
||||
@ApiModelProperty(value = "更新人")
|
||||
@Excel(name = "更新人", width = 15)
|
||||
private java.lang.String updateBy;
|
||||
/** 更新时间 */
|
||||
@ApiModelProperty(value = "更新时间")
|
||||
@Excel(name = "更新时间", width = 20, format = "yyyy-MM-dd HH:mm:ss")
|
||||
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private java.util.Date updateTime;
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
package org.jeecg.common.system.base.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
/**
|
||||
* @Description: Service基类
|
||||
* @Author: dangzhenghui@163.com
|
||||
* @Date: 2019-4-21 8:13
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public interface JeecgService<T> extends IService<T> {
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
package org.jeecg.common.system.base.service.impl;
|
||||
|
||||
import org.jeecg.common.system.base.entity.JeecgEntity;
|
||||
import org.jeecg.common.system.base.service.JeecgService;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @Description: ServiceImpl基类
|
||||
* @Author: dangzhenghui@163.com
|
||||
* @Date: 2019-4-21 8:13
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Slf4j
|
||||
public class JeecgServiceImpl<M extends BaseMapper<T>, T extends JeecgEntity> extends ServiceImpl<M, T> implements JeecgService<T> {
|
||||
|
||||
}
|
||||
@ -0,0 +1,220 @@
|
||||
package org.jeecg.common.system.controller;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 用户表 前端控制器
|
||||
* </p>
|
||||
*
|
||||
* @Author scott
|
||||
* @since 2018-12-20
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/sys/common")
|
||||
public class CommonController {
|
||||
|
||||
@Value(value = "${jeecg.path.upload}")
|
||||
private String uploadpath;
|
||||
|
||||
/**
|
||||
* @Author 政辉
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/403")
|
||||
public Result<?> noauth() {
|
||||
return Result.error("没有权限,请联系管理员授权");
|
||||
}
|
||||
|
||||
@PostMapping(value = "/upload")
|
||||
public Result<?> upload(HttpServletRequest request, HttpServletResponse response) {
|
||||
Result<?> result = new Result<>();
|
||||
try {
|
||||
String ctxPath = uploadpath;
|
||||
String fileName = null;
|
||||
String bizPath = "files";
|
||||
String nowday = new SimpleDateFormat("yyyyMMdd").format(new Date());
|
||||
File file = new File(ctxPath + File.separator + bizPath + File.separator + nowday);
|
||||
if (!file.exists()) {
|
||||
file.mkdirs();// 创建文件根目录
|
||||
}
|
||||
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
||||
MultipartFile mf = multipartRequest.getFile("file");// 获取上传文件对象
|
||||
String orgName = mf.getOriginalFilename();// 获取文件名
|
||||
fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.indexOf("."));
|
||||
String savePath = file.getPath() + File.separator + fileName;
|
||||
File savefile = new File(savePath);
|
||||
FileCopyUtils.copy(mf.getBytes(), savefile);
|
||||
String dbpath = bizPath + File.separator + nowday + File.separator + fileName;
|
||||
if (dbpath.contains("\\")) {
|
||||
dbpath = dbpath.replace("\\", "/");
|
||||
}
|
||||
result.setMessage(dbpath);
|
||||
result.setSuccess(true);
|
||||
} catch (IOException e) {
|
||||
result.setSuccess(false);
|
||||
result.setMessage(e.getMessage());
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 预览图片
|
||||
* 请求地址:http://localhost:8080/common/view/{user/20190119/e1fe9925bc315c60addea1b98eb1cb1349547719_1547866868179.jpg}
|
||||
*
|
||||
* @param request
|
||||
* @param response
|
||||
*/
|
||||
@GetMapping(value = "/view/**")
|
||||
public void view(HttpServletRequest request, HttpServletResponse response) {
|
||||
// ISO-8859-1 ==> UTF-8 进行编码转换
|
||||
String imgPath = extractPathFromPattern(request);
|
||||
// 其余处理略
|
||||
InputStream inputStream = null;
|
||||
OutputStream outputStream = null;
|
||||
try {
|
||||
imgPath = imgPath.replace("..", "");
|
||||
if (imgPath.endsWith(",")) {
|
||||
imgPath = imgPath.substring(0, imgPath.length() - 1);
|
||||
}
|
||||
response.setContentType("image/jpeg;charset=utf-8");
|
||||
String localPath = uploadpath;
|
||||
String imgurl = localPath + File.separator + imgPath;
|
||||
inputStream = new BufferedInputStream(new FileInputStream(imgurl));
|
||||
outputStream = response.getOutputStream();
|
||||
byte[] buf = new byte[1024];
|
||||
int len;
|
||||
while ((len = inputStream.read(buf)) > 0) {
|
||||
outputStream.write(buf, 0, len);
|
||||
}
|
||||
response.flushBuffer();
|
||||
} catch (IOException e) {
|
||||
log.error("预览图片失败" + e.getMessage());
|
||||
// e.printStackTrace();
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
try {
|
||||
inputStream.close();
|
||||
} catch (IOException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
if (outputStream != null) {
|
||||
try {
|
||||
outputStream.close();
|
||||
} catch (IOException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载文件
|
||||
* 请求地址:http://localhost:8080/common/download/{user/20190119/e1fe9925bc315c60addea1b98eb1cb1349547719_1547866868179.jpg}
|
||||
*
|
||||
* @param request
|
||||
* @param response
|
||||
* @throws Exception
|
||||
*/
|
||||
@GetMapping(value = "/download/**")
|
||||
public void download(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||
// ISO-8859-1 ==> UTF-8 进行编码转换
|
||||
String filePath = extractPathFromPattern(request);
|
||||
// 其余处理略
|
||||
InputStream inputStream = null;
|
||||
OutputStream outputStream = null;
|
||||
try {
|
||||
filePath = filePath.replace("..", "");
|
||||
if (filePath.endsWith(",")) {
|
||||
filePath = filePath.substring(0, filePath.length() - 1);
|
||||
}
|
||||
String localPath = uploadpath;
|
||||
String downloadFilePath = localPath + File.separator + filePath;
|
||||
File file = new File(downloadFilePath);
|
||||
if (file.exists()) {
|
||||
response.setContentType("application/force-download");// 设置强制下载不打开
|
||||
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes("UTF-8"),"iso-8859-1"));
|
||||
inputStream = new BufferedInputStream(new FileInputStream(file));
|
||||
outputStream = response.getOutputStream();
|
||||
byte[] buf = new byte[1024];
|
||||
int len;
|
||||
while ((len = inputStream.read(buf)) > 0) {
|
||||
outputStream.write(buf, 0, len);
|
||||
}
|
||||
response.flushBuffer();
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.info("文件下载失败" + e.getMessage());
|
||||
// e.printStackTrace();
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
try {
|
||||
inputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (outputStream != null) {
|
||||
try {
|
||||
outputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* @功能:pdf预览Iframe
|
||||
* @param modelAndView
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping("/pdf/pdfPreviewIframe")
|
||||
public ModelAndView pdfPreviewIframe(ModelAndView modelAndView) {
|
||||
modelAndView.setViewName("pdfPreviewIframe");
|
||||
return modelAndView;
|
||||
}
|
||||
|
||||
/**
|
||||
* 把指定URL后的字符串全部截断当成参数
|
||||
* 这么做是为了防止URL中包含中文或者特殊字符(/等)时,匹配不了的问题
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
private static String extractPathFromPattern(final HttpServletRequest request) {
|
||||
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
|
||||
String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
|
||||
return new AntPathMatcher().extractPathWithinPattern(bestMatchPattern, path);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,55 @@
|
||||
package org.jeecg.common.system.query;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class QueryCondition implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 4740166316629191651L;
|
||||
|
||||
private String field;
|
||||
private String type;
|
||||
private String rule;
|
||||
private String val;
|
||||
|
||||
public String getField() {
|
||||
return field;
|
||||
}
|
||||
|
||||
public void setField(String field) {
|
||||
this.field = field;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getRule() {
|
||||
return rule;
|
||||
}
|
||||
|
||||
public void setRule(String rule) {
|
||||
this.rule = rule;
|
||||
}
|
||||
|
||||
public String getVal() {
|
||||
return val;
|
||||
}
|
||||
|
||||
public void setVal(String val) {
|
||||
this.val = val;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
StringBuffer sb =new StringBuffer();
|
||||
if(field == null || "".equals(field)){
|
||||
return "";
|
||||
}
|
||||
sb.append(this.field).append(" ").append(this.rule).append(" ").append(this.type).append(" ").append(this.val);
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,590 @@
|
||||
package org.jeecg.common.system.query;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.math.BigDecimal;
|
||||
import java.net.URLDecoder;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.beanutils.PropertyUtils;
|
||||
import org.jeecg.common.system.util.JeecgDataAutorUtils;
|
||||
import org.jeecg.common.system.util.JwtUtil;
|
||||
import org.jeecg.common.util.oConvertUtils;
|
||||
import org.jeecg.modules.system.entity.SysPermissionDataRule;
|
||||
import org.springframework.util.NumberUtils;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class QueryGenerator {
|
||||
|
||||
public static final String SQL_RULES_COLUMN = "SQL_RULES_COLUMN";
|
||||
|
||||
private static final String BEGIN = "_begin";
|
||||
private static final String END = "_end";
|
||||
private static final String STAR = "*";
|
||||
private static final String COMMA = ",";
|
||||
private static final String NOT_EQUAL = "!";
|
||||
/**高级查询前端传来的参数名*/
|
||||
private static final String SUPER_QUERY_PARAMS = "superQueryParams";
|
||||
|
||||
/**排序列*/
|
||||
private static final String ORDER_COLUMN = "column";
|
||||
/**排序方式*/
|
||||
private static final String ORDER_TYPE = "order";
|
||||
private static final String ORDER_TYPE_ASC = "ASC";
|
||||
|
||||
/**时间格式化 */
|
||||
private static final ThreadLocal<SimpleDateFormat> local = new ThreadLocal<SimpleDateFormat>();
|
||||
private static SimpleDateFormat getTime(){
|
||||
SimpleDateFormat time = local.get();
|
||||
if(time == null){
|
||||
time = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
|
||||
local.set(time);
|
||||
}
|
||||
return time;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取查询条件构造器QueryWrapper实例 通用查询条件已被封装完成
|
||||
* @param searchObj 查询实体
|
||||
* @param parameterMap request.getParameterMap()
|
||||
* @return QueryWrapper实例
|
||||
*/
|
||||
public static <T> QueryWrapper<T> initQueryWrapper(T searchObj,Map<String, String[]> parameterMap){
|
||||
long start = System.currentTimeMillis();
|
||||
QueryWrapper<T> queryWrapper = new QueryWrapper<T>();
|
||||
installMplus(queryWrapper, searchObj, parameterMap);
|
||||
log.debug("---查询条件构造器初始化完成,耗时:"+(System.currentTimeMillis()-start)+"毫秒----");
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 组装Mybatis Plus 查询条件
|
||||
* <p>使用此方法 需要有如下几点注意:
|
||||
* <br>1.使用QueryWrapper 而非LambdaQueryWrapper;
|
||||
* <br>2.实例化QueryWrapper时不可将实体传入参数
|
||||
* <br>错误示例:如QueryWrapper<JeecgDemo> queryWrapper = new QueryWrapper<JeecgDemo>(jeecgDemo);
|
||||
* <br>正确示例:QueryWrapper<JeecgDemo> queryWrapper = new QueryWrapper<JeecgDemo>();
|
||||
* <br>3.也可以不使用这个方法直接调用 {@link #initQueryWrapper}直接获取实例
|
||||
*/
|
||||
public static void installMplus(QueryWrapper<?> queryWrapper,Object searchObj,Map<String, String[]> parameterMap) {
|
||||
|
||||
/*
|
||||
* 注意:权限查询由前端配置数据规则 当一个人有多个所属部门时候 可以在规则配置包含条件 orgCode 包含 #{sys_org_code}
|
||||
但是不支持在自定义SQL中写orgCode in #{sys_org_code}
|
||||
当一个人只有一个部门 就直接配置等于条件: orgCode 等于 #{sys_org_code} 或者配置自定义SQL: orgCode = '#{sys_org_code}'
|
||||
*/
|
||||
|
||||
//区间条件组装 模糊查询 高级查询组装 简单排序 权限查询
|
||||
PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(searchObj);
|
||||
Map<String,SysPermissionDataRule> ruleMap = getRuleMap();
|
||||
|
||||
//权限规则自定义SQL表达式
|
||||
for (String c : ruleMap.keySet()) {
|
||||
if(oConvertUtils.isNotEmpty(c) && c.startsWith(SQL_RULES_COLUMN)){
|
||||
queryWrapper.and(i ->i.apply(getSqlRuleValue(ruleMap.get(c).getRuleValue())));
|
||||
}
|
||||
}
|
||||
|
||||
String name, type;
|
||||
for (int i = 0; i < origDescriptors.length; i++) {
|
||||
//aliasName = origDescriptors[i].getName(); mybatis 不存在实体属性 不用处理别名的情况
|
||||
name = origDescriptors[i].getName();
|
||||
type = origDescriptors[i].getPropertyType().toString();
|
||||
try {
|
||||
if (judgedIsUselessField(name)|| !PropertyUtils.isReadable(searchObj, name)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
//数据权限查询
|
||||
if(ruleMap.containsKey(name)) {
|
||||
addRuleToQueryWrapper(ruleMap.get(name), name, origDescriptors[i].getPropertyType(), queryWrapper);
|
||||
}
|
||||
|
||||
// 添加 判断是否有区间值
|
||||
String endValue = null,beginValue = null;
|
||||
if (parameterMap != null && parameterMap.containsKey(name + BEGIN)) {
|
||||
beginValue = parameterMap.get(name + BEGIN)[0].trim();
|
||||
addQueryByRule(queryWrapper, name, type, beginValue, QueryRuleEnum.GE);
|
||||
|
||||
}
|
||||
if (parameterMap != null && parameterMap.containsKey(name + END)) {
|
||||
endValue = parameterMap.get(name + END)[0].trim();
|
||||
addQueryByRule(queryWrapper, name, type, endValue, QueryRuleEnum.LE);
|
||||
}
|
||||
|
||||
//判断单值 参数带不同标识字符串 走不同的查询
|
||||
//TODO 这种前后带逗号的支持分割后模糊查询需要否 使多选字段的查询生效
|
||||
Object value = PropertyUtils.getSimpleProperty(searchObj, name);
|
||||
if (null != value && value.toString().startsWith(COMMA) && value.toString().endsWith(COMMA)) {
|
||||
String multiLikeval = value.toString().replace(",,", COMMA);
|
||||
String[] vals = multiLikeval.substring(1, multiLikeval.length()).split(COMMA);
|
||||
final String field = oConvertUtils.camelToUnderline(name);
|
||||
if(vals.length>1) {
|
||||
queryWrapper.and(j -> {
|
||||
j = j.like(field,vals[0]);
|
||||
for (int k=1;k<vals.length;k++) {
|
||||
j = j.or().like(field,vals[k]);
|
||||
}
|
||||
return j;
|
||||
});
|
||||
}else {
|
||||
queryWrapper.and(j -> j.like(field,vals[0]));
|
||||
}
|
||||
}else {
|
||||
//根据参数值带什么关键字符串判断走什么类型的查询
|
||||
QueryRuleEnum rule = convert2Rule(value);
|
||||
value = replaceValue(rule,value);
|
||||
addEasyQuery(queryWrapper, name, rule, value);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
// 排序逻辑 处理
|
||||
doMultiFieldsOrder(queryWrapper, parameterMap);
|
||||
|
||||
//高级查询
|
||||
doSuperQuery(queryWrapper, parameterMap);
|
||||
|
||||
}
|
||||
|
||||
//多字段排序 TODO 需要修改前端
|
||||
public static void doMultiFieldsOrder(QueryWrapper<?> queryWrapper,Map<String, String[]> parameterMap) {
|
||||
String column=null,order=null;
|
||||
if(parameterMap!=null&& parameterMap.containsKey(ORDER_COLUMN)) {
|
||||
column = parameterMap.get(ORDER_COLUMN)[0];
|
||||
}
|
||||
if(parameterMap!=null&& parameterMap.containsKey(ORDER_TYPE)) {
|
||||
order = parameterMap.get(ORDER_TYPE)[0];
|
||||
}
|
||||
log.debug("排序规则>>列:"+column+",排序方式:"+order);
|
||||
if (oConvertUtils.isNotEmpty(column) && oConvertUtils.isNotEmpty(order)) {
|
||||
if (order.toUpperCase().indexOf(ORDER_TYPE_ASC)>=0) {
|
||||
queryWrapper.orderByAsc(oConvertUtils.camelToUnderline(column));
|
||||
} else {
|
||||
queryWrapper.orderByDesc(oConvertUtils.camelToUnderline(column));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 高级查询
|
||||
* @param queryWrapper
|
||||
* @param parameterMap
|
||||
*/
|
||||
public static void doSuperQuery(QueryWrapper<?> queryWrapper,Map<String, String[]> parameterMap) {
|
||||
if(parameterMap!=null&& parameterMap.containsKey(SUPER_QUERY_PARAMS)){
|
||||
String superQueryParams = parameterMap.get(SUPER_QUERY_PARAMS)[0];
|
||||
// 解码
|
||||
try {
|
||||
superQueryParams = URLDecoder.decode(superQueryParams, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
log.error("--高级查询参数转码失败!", e);
|
||||
}
|
||||
List<QueryCondition> conditions = JSON.parseArray(superQueryParams, QueryCondition.class);
|
||||
log.info("---高级查询参数-->"+conditions.toString());
|
||||
|
||||
for (QueryCondition rule : conditions) {
|
||||
if(oConvertUtils.isNotEmpty(rule.getField()) && oConvertUtils.isNotEmpty(rule.getRule()) && oConvertUtils.isNotEmpty(rule.getVal())){
|
||||
addEasyQuery(queryWrapper, rule.getField(), QueryRuleEnum.getByValue(rule.getRule()), rule.getVal());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 根据所传的值 转化成对应的比较方式
|
||||
* 支持><= like in !
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
public static QueryRuleEnum convert2Rule(Object value) {
|
||||
// 避免空数据
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
String val = (value + "").toString().trim();
|
||||
if (val.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
QueryRuleEnum rule =null;
|
||||
// step 2 .>= =<
|
||||
if (rule == null && val.length() >= 2) {
|
||||
rule = QueryRuleEnum.getByValue(val.substring(0, 2));
|
||||
}
|
||||
// step 1 .> <
|
||||
if (rule == null && val.length() >= 1) {
|
||||
rule = QueryRuleEnum.getByValue(val.substring(0, 1));
|
||||
}
|
||||
|
||||
// step 3 like
|
||||
if (rule == null && val.contains(STAR)) {
|
||||
if (val.startsWith(STAR) && val.endsWith(STAR)) {
|
||||
rule = QueryRuleEnum.LIKE;
|
||||
} else if (val.startsWith(STAR)) {
|
||||
rule = QueryRuleEnum.LEFT_LIKE;
|
||||
} else if(val.endsWith(STAR)){
|
||||
rule = QueryRuleEnum.RIGHT_LIKE;
|
||||
}
|
||||
}
|
||||
// step 4 in
|
||||
if (rule == null && val.contains(COMMA)) {
|
||||
//TODO in 查询这里应该有个bug 如果一字段本身就是多选 此时用in查询 未必能查询出来
|
||||
rule = QueryRuleEnum.IN;
|
||||
}
|
||||
// step 5 !=
|
||||
if(rule == null && val.startsWith(NOT_EQUAL)){
|
||||
rule = QueryRuleEnum.NE;
|
||||
}
|
||||
return rule != null ? rule : QueryRuleEnum.EQ;
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换掉关键字字符
|
||||
*
|
||||
* @param rule
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
public static Object replaceValue(QueryRuleEnum rule, Object value) {
|
||||
if (rule == null) {
|
||||
return null;
|
||||
}
|
||||
if (! (value instanceof String)){
|
||||
return value;
|
||||
}
|
||||
String val = (value + "").toString().trim();
|
||||
if (rule == QueryRuleEnum.LIKE) {
|
||||
value = val.substring(1, val.length() - 1);
|
||||
} else if (rule == QueryRuleEnum.LEFT_LIKE || rule == QueryRuleEnum.NE) {
|
||||
value = val.substring(1);
|
||||
} else if (rule == QueryRuleEnum.RIGHT_LIKE) {
|
||||
value = val.substring(0, val.length() - 1);
|
||||
} else if (rule == QueryRuleEnum.IN) {
|
||||
value = val.split(",");
|
||||
} else {
|
||||
value = val.replace(rule.getValue(),"");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private static void addQueryByRule(QueryWrapper<?> queryWrapper,String name,String type,String value,QueryRuleEnum rule) throws ParseException {
|
||||
if(!"".equals(value)) {
|
||||
Object temp;
|
||||
switch (type) {
|
||||
case "class java.lang.Integer":
|
||||
temp = Integer.parseInt(value);
|
||||
break;
|
||||
case "class java.math.BigDecimal":
|
||||
temp = new BigDecimal(value);
|
||||
break;
|
||||
case "class java.lang.Short":
|
||||
temp = Short.parseShort(value);
|
||||
break;
|
||||
case "class java.lang.Long":
|
||||
temp = Long.parseLong(value);
|
||||
break;
|
||||
case "class java.lang.Float":
|
||||
temp = Float.parseFloat(value);
|
||||
break;
|
||||
case "class java.lang.Double":
|
||||
temp = Double.parseDouble(value);
|
||||
break;
|
||||
case "class java.util.Date":
|
||||
temp = getDateQueryByRule(value, rule);
|
||||
break;
|
||||
default:
|
||||
temp = value;
|
||||
break;
|
||||
}
|
||||
addEasyQuery(queryWrapper, name, rule, temp);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取日期类型的值
|
||||
* @param value
|
||||
* @param rule
|
||||
* @return
|
||||
* @throws ParseException
|
||||
*/
|
||||
private static Date getDateQueryByRule(String value,QueryRuleEnum rule) throws ParseException {
|
||||
Date date = null;
|
||||
if(value.length()==10) {
|
||||
if(rule==QueryRuleEnum.GE) {
|
||||
//比较大于
|
||||
date = getTime().parse(value + " 00:00:00");
|
||||
}else if(rule==QueryRuleEnum.LE) {
|
||||
//比较小于
|
||||
date = getTime().parse(value + " 23:59:59");
|
||||
}
|
||||
//TODO 日期类型比较特殊 可能oracle下不一定好使
|
||||
}
|
||||
if(date==null) {
|
||||
date = getTime().parse(value);
|
||||
}
|
||||
return date;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据规则走不同的查询
|
||||
* @param queryWrapper QueryWrapper
|
||||
* @param name 字段名字
|
||||
* @param rule 查询规则
|
||||
* @param value 查询条件值
|
||||
*/
|
||||
private static void addEasyQuery(QueryWrapper<?> queryWrapper, String name, QueryRuleEnum rule, Object value) {
|
||||
if (value == null || rule == null) {
|
||||
return;
|
||||
}
|
||||
name = oConvertUtils.camelToUnderline(name);
|
||||
log.info("--查询规则-->"+name+" "+rule.getValue()+" "+value);
|
||||
switch (rule) {
|
||||
case GT:
|
||||
queryWrapper.gt(name, value);
|
||||
break;
|
||||
case GE:
|
||||
queryWrapper.ge(name, value);
|
||||
break;
|
||||
case LT:
|
||||
queryWrapper.lt(name, value);
|
||||
break;
|
||||
case LE:
|
||||
queryWrapper.le(name, value);
|
||||
break;
|
||||
case EQ:
|
||||
queryWrapper.eq(name, value);
|
||||
break;
|
||||
case NE:
|
||||
queryWrapper.ne(name, value);
|
||||
break;
|
||||
case IN:
|
||||
if(value instanceof String) {
|
||||
queryWrapper.in(name, (Object[])value.toString().split(","));
|
||||
}else if(value instanceof String[]) {
|
||||
queryWrapper.in(name, (Object[]) value);
|
||||
}else {
|
||||
queryWrapper.in(name, value);
|
||||
}
|
||||
break;
|
||||
case LIKE:
|
||||
queryWrapper.like(name, value);
|
||||
break;
|
||||
case LEFT_LIKE:
|
||||
queryWrapper.likeLeft(name, value);
|
||||
break;
|
||||
case RIGHT_LIKE:
|
||||
queryWrapper.likeRight(name, value);
|
||||
break;
|
||||
default:
|
||||
log.info("--查询规则未匹配到---");
|
||||
break;
|
||||
}
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
private static boolean judgedIsUselessField(String name) {
|
||||
return "class".equals(name) || "ids".equals(name)
|
||||
|| "page".equals(name) || "rows".equals(name)
|
||||
|| "sort".equals(name) || "order".equals(name);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static Map<String, SysPermissionDataRule> getRuleMap() {
|
||||
Map<String, SysPermissionDataRule> ruleMap = new HashMap<String, SysPermissionDataRule>();
|
||||
List<SysPermissionDataRule> list =JeecgDataAutorUtils.loadDataSearchConditon();
|
||||
if(list != null&&list.size()>0){
|
||||
if(list.get(0)==null){
|
||||
return ruleMap;
|
||||
}
|
||||
for (SysPermissionDataRule rule : list) {
|
||||
String column = rule.getRuleColumn();
|
||||
if(QueryRuleEnum.SQL_RULES.getValue().equals(rule.getRuleConditions())) {
|
||||
column = SQL_RULES_COLUMN+rule.getId();
|
||||
}
|
||||
ruleMap.put(column, rule);
|
||||
}
|
||||
}
|
||||
return ruleMap;
|
||||
}
|
||||
|
||||
private static void addRuleToQueryWrapper(SysPermissionDataRule dataRule,String name, Class propertyType, QueryWrapper<?> queryWrapper) {
|
||||
QueryRuleEnum rule = QueryRuleEnum.getByValue(dataRule.getRuleConditions());
|
||||
if(rule.equals(QueryRuleEnum.IN) && ! propertyType.equals(String.class)) {
|
||||
String[] values = dataRule.getRuleValue().split(",");
|
||||
Object[] objs = new Object[values.length];
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
objs[i] = NumberUtils.parseNumber(values[i], propertyType);
|
||||
}
|
||||
addEasyQuery(queryWrapper, name, rule, objs);
|
||||
}else {
|
||||
if (propertyType.equals(String.class)) {
|
||||
addEasyQuery(queryWrapper, name, rule, converRuleValue(dataRule.getRuleValue()));
|
||||
} else {
|
||||
addEasyQuery(queryWrapper, name, rule, NumberUtils.parseNumber(dataRule.getRuleValue(), propertyType));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String converRuleValue(String ruleValue) {
|
||||
String value = JwtUtil.getSessionData(ruleValue);
|
||||
if(oConvertUtils.isEmpty(value)) {
|
||||
value = JwtUtil.getUserSystemData(ruleValue,null);
|
||||
}
|
||||
return value!= null ? value : ruleValue;
|
||||
}
|
||||
|
||||
public static String getSqlRuleValue(String sqlRule){
|
||||
try {
|
||||
Set<String> varParams = getSqlRuleParams(sqlRule);
|
||||
for(String var:varParams){
|
||||
String tempValue = converRuleValue(var);
|
||||
sqlRule = sqlRule.replace("#{"+var+"}",tempValue);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
return sqlRule;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取sql中的#{key} 这个key组成的set
|
||||
*/
|
||||
public static Set<String> getSqlRuleParams(String sql) {
|
||||
if(oConvertUtils.isEmpty(sql)){
|
||||
return null;
|
||||
}
|
||||
Set<String> varParams = new HashSet<String>();
|
||||
String regex = "\\#\\{\\w+\\}";
|
||||
|
||||
Pattern p = Pattern.compile(regex);
|
||||
Matcher m = p.matcher(sql);
|
||||
while(m.find()){
|
||||
String var = m.group();
|
||||
varParams.add(var.substring(var.indexOf("{")+1,var.indexOf("}")));
|
||||
}
|
||||
return varParams;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取查询条件
|
||||
* @param field
|
||||
* @param alias
|
||||
* @param value
|
||||
* @param isString
|
||||
* @return
|
||||
*/
|
||||
public static String getSingleQueryConditionSql(String field,String alias,Object value,boolean isString) {
|
||||
if (value == null) {
|
||||
return "";
|
||||
}
|
||||
field = alias+oConvertUtils.camelToUnderline(field);
|
||||
QueryRuleEnum rule = QueryGenerator.convert2Rule(value);
|
||||
String res = "";
|
||||
switch (rule) {
|
||||
case GT:
|
||||
res =field+rule.getValue()+getFieldConditionValue(value, isString);
|
||||
break;
|
||||
case GE:
|
||||
res = field+rule.getValue()+getFieldConditionValue(value, isString);
|
||||
break;
|
||||
case LT:
|
||||
res = field+rule.getValue()+getFieldConditionValue(value, isString);
|
||||
break;
|
||||
case LE:
|
||||
res = field+rule.getValue()+getFieldConditionValue(value, isString);
|
||||
break;
|
||||
case EQ:
|
||||
res = field+rule.getValue()+getFieldConditionValue(value, isString);
|
||||
break;
|
||||
case NE:
|
||||
res = field+" <> "+getFieldConditionValue(value, isString);
|
||||
break;
|
||||
case IN:
|
||||
res = field + " in "+getInConditionValue(value, isString);
|
||||
break;
|
||||
case LIKE:
|
||||
res = field + " like "+getLikeConditionValue(value);
|
||||
break;
|
||||
case LEFT_LIKE:
|
||||
res = field + " like "+getLikeConditionValue(value);
|
||||
break;
|
||||
case RIGHT_LIKE:
|
||||
res = field + " like "+getLikeConditionValue(value);
|
||||
break;
|
||||
default:
|
||||
res = field+" = "+getFieldConditionValue(value, isString);
|
||||
break;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
private static String getFieldConditionValue(Object value,boolean isString) {
|
||||
String str = value.toString().trim();
|
||||
if(str.startsWith("!")) {
|
||||
str = str.substring(1);
|
||||
}else if(str.startsWith(">=")) {
|
||||
str = str.substring(2);
|
||||
}else if(str.startsWith("<=")) {
|
||||
str = str.substring(2);
|
||||
}else if(str.startsWith(">")) {
|
||||
str = str.substring(1);
|
||||
}else if(str.startsWith("<")) {
|
||||
str = str.substring(1);
|
||||
}
|
||||
if(isString) {
|
||||
return " '"+str+"' ";
|
||||
}else {
|
||||
return value.toString();
|
||||
}
|
||||
}
|
||||
|
||||
private static String getInConditionValue(Object value,boolean isString) {
|
||||
if(isString) {
|
||||
String temp[] = value.toString().split(",");
|
||||
String res="";
|
||||
for (String string : temp) {
|
||||
res+=",'"+string+"'";
|
||||
}
|
||||
return "("+res.substring(1)+")";
|
||||
}else {
|
||||
return "("+value.toString()+")";
|
||||
}
|
||||
}
|
||||
|
||||
private static String getLikeConditionValue(Object value) {
|
||||
String str = value.toString().trim();
|
||||
if(str.startsWith("*") && str.endsWith("*")) {
|
||||
return "'%"+str.substring(1,str.length()-1)+"%'";
|
||||
}else if(str.startsWith("*")) {
|
||||
return "'%"+str.substring(1)+"'";
|
||||
}else if(str.endsWith("*")) {
|
||||
return "'"+str.substring(0,str.length()-1)+"%'";
|
||||
}else {
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,71 @@
|
||||
package org.jeecg.common.system.query;
|
||||
|
||||
import org.jeecg.common.util.oConvertUtils;
|
||||
|
||||
/**
|
||||
* Query 规则 常量
|
||||
* @Author Scott
|
||||
* @Date 2019年02月14日
|
||||
*/
|
||||
public enum QueryRuleEnum {
|
||||
|
||||
GT(">","gt","大于"),
|
||||
GE(">=","ge","大于等于"),
|
||||
LT("<","lt","小于"),
|
||||
LE("<=","le","小于等于"),
|
||||
EQ("=","eq","等于"),
|
||||
NE("!=","ne","不等于"),
|
||||
IN("IN","in","包含"),
|
||||
LIKE("LIKE","like","全模糊"),
|
||||
LEFT_LIKE("LEFT_LIKE","left_like","左模糊"),
|
||||
RIGHT_LIKE("RIGHT_LIKE","right_like","右模糊"),
|
||||
SQL_RULES("USE_SQL_RULES","ext","自定义SQL片段");
|
||||
|
||||
private String value;
|
||||
|
||||
private String condition;
|
||||
|
||||
private String msg;
|
||||
|
||||
QueryRuleEnum(String value, String condition, String msg){
|
||||
this.value = value;
|
||||
this.condition = condition;
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
public void setMsg(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public String getCondition() {
|
||||
return condition;
|
||||
}
|
||||
|
||||
public void setCondition(String condition) {
|
||||
this.condition = condition;
|
||||
}
|
||||
|
||||
public static QueryRuleEnum getByValue(String value){
|
||||
if(oConvertUtils.isEmpty(value)) {
|
||||
return null;
|
||||
}
|
||||
for(QueryRuleEnum val :values()){
|
||||
if (val.getValue().equals(value) || val.getCondition().equals(value)){
|
||||
return val;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,90 @@
|
||||
package org.jeecg.common.system.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.jeecg.common.system.vo.SysUserCacheInfo;
|
||||
import org.jeecg.common.util.SpringContextUtils;
|
||||
import org.jeecg.modules.system.entity.SysPermissionDataRule;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @ClassName: JeecgDataAutorUtils
|
||||
* @Description: 数据权限查询规则容器工具类
|
||||
* @Author: 张代浩
|
||||
* @Date: 2012-12-15 下午11:27:39
|
||||
*
|
||||
*/
|
||||
public class JeecgDataAutorUtils {
|
||||
|
||||
public static final String MENU_DATA_AUTHOR_RULES = "MENU_DATA_AUTHOR_RULES";
|
||||
|
||||
public static final String MENU_DATA_AUTHOR_RULE_SQL = "MENU_DATA_AUTHOR_RULE_SQL";
|
||||
|
||||
public static final String SYS_USER_INFO = "SYS_USER_INFO";
|
||||
|
||||
/**
|
||||
* 往链接请求里面,传入数据查询条件
|
||||
*
|
||||
* @param request
|
||||
* @param MENU_DATA_AUTHOR_RULES
|
||||
*/
|
||||
public static synchronized void installDataSearchConditon(HttpServletRequest request, List<SysPermissionDataRule> dataRules) {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<SysPermissionDataRule> list = (List<SysPermissionDataRule>)loadDataSearchConditon();// 1.先从request获取MENU_DATA_AUTHOR_RULES,如果存则获取到LIST
|
||||
if (list==null) {
|
||||
// 2.如果不存在,则new一个list
|
||||
list = new ArrayList<SysPermissionDataRule>();
|
||||
}
|
||||
for (SysPermissionDataRule tsDataRule : dataRules) {
|
||||
list.add(tsDataRule);
|
||||
}
|
||||
request.setAttribute(MENU_DATA_AUTHOR_RULES, list); // 3.往list里面增量存指
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求对应的数据权限规则
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static synchronized List<SysPermissionDataRule> loadDataSearchConditon() {
|
||||
return (List<SysPermissionDataRule>) SpringContextUtils.getHttpServletRequest().getAttribute(MENU_DATA_AUTHOR_RULES);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求对应的数据权限SQL
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
public static synchronized String loadDataSearchConditonSQLString() {
|
||||
return (String) SpringContextUtils.getHttpServletRequest().getAttribute(MENU_DATA_AUTHOR_RULE_SQL);
|
||||
}
|
||||
|
||||
/**
|
||||
* 往链接请求里面,传入数据查询条件
|
||||
*
|
||||
* @param request
|
||||
* @param MENU_DATA_AUTHOR_RULE_SQL
|
||||
*/
|
||||
public static synchronized void installDataSearchConditon(HttpServletRequest request, String sql) {
|
||||
String ruleSql = (String)loadDataSearchConditonSQLString();
|
||||
if (!StringUtils.hasText(ruleSql)) {
|
||||
request.setAttribute(MENU_DATA_AUTHOR_RULE_SQL,sql);
|
||||
}
|
||||
}
|
||||
|
||||
public static synchronized void installUserInfo(HttpServletRequest request, SysUserCacheInfo userinfo) {
|
||||
request.setAttribute(SYS_USER_INFO, userinfo);
|
||||
}
|
||||
|
||||
public static synchronized SysUserCacheInfo loadUserInfo() {
|
||||
return (SysUserCacheInfo) SpringContextUtils.getHttpServletRequest().getAttribute(SYS_USER_INFO);
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,178 @@
|
||||
package org.jeecg.common.system.util;
|
||||
|
||||
import com.auth0.jwt.JWT;
|
||||
import com.auth0.jwt.JWTVerifier;
|
||||
import com.auth0.jwt.algorithms.Algorithm;
|
||||
import com.auth0.jwt.exceptions.JWTDecodeException;
|
||||
import com.auth0.jwt.interfaces.DecodedJWT;
|
||||
import com.google.common.base.Joiner;
|
||||
|
||||
import java.util.Date;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
import org.jeecg.common.constant.DataBaseConstant;
|
||||
import org.jeecg.common.exception.JeecgBootException;
|
||||
import org.jeecg.common.system.vo.SysUserCacheInfo;
|
||||
import org.jeecg.common.util.SpringContextUtils;
|
||||
import org.jeecg.common.util.oConvertUtils;
|
||||
|
||||
/**
|
||||
* @Author Scott
|
||||
* @Date 2018-07-12 14:23
|
||||
* @Desc JWT工具类
|
||||
**/
|
||||
public class JwtUtil {
|
||||
|
||||
// 过期时间30分钟
|
||||
public static final long EXPIRE_TIME = 30 * 60 * 1000;
|
||||
|
||||
/**
|
||||
* 校验token是否正确
|
||||
*
|
||||
* @param token 密钥
|
||||
* @param secret 用户的密码
|
||||
* @return 是否正确
|
||||
*/
|
||||
public static boolean verify(String token, String username, String secret) {
|
||||
try {
|
||||
// 根据密码生成JWT效验器
|
||||
Algorithm algorithm = Algorithm.HMAC256(secret);
|
||||
JWTVerifier verifier = JWT.require(algorithm).withClaim("username", username).build();
|
||||
// 效验TOKEN
|
||||
DecodedJWT jwt = verifier.verify(token);
|
||||
return true;
|
||||
} catch (Exception exception) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得token中的信息无需secret解密也能获得
|
||||
*
|
||||
* @return token中包含的用户名
|
||||
*/
|
||||
public static String getUsername(String token) {
|
||||
try {
|
||||
DecodedJWT jwt = JWT.decode(token);
|
||||
return jwt.getClaim("username").asString();
|
||||
} catch (JWTDecodeException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成签名,5min后过期
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param secret 用户的密码
|
||||
* @return 加密的token
|
||||
*/
|
||||
public static String sign(String username, String secret) {
|
||||
Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
|
||||
Algorithm algorithm = Algorithm.HMAC256(secret);
|
||||
// 附带username信息
|
||||
return JWT.create().withClaim("username", username).withExpiresAt(date).sign(algorithm);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据request中的token获取用户账号
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
* @throws JeecgBootException
|
||||
*/
|
||||
public static String getUserNameByToken(HttpServletRequest request) throws JeecgBootException {
|
||||
String accessToken = request.getHeader("X-Access-Token");
|
||||
String username = getUsername(accessToken);
|
||||
if (oConvertUtils.isEmpty(username)) {
|
||||
throw new JeecgBootException("未获取到用户");
|
||||
}
|
||||
return username;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从session中获取变量
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public static String getSessionData(String key) {
|
||||
//${myVar}%
|
||||
//得到${} 后面的值
|
||||
String moshi = "";
|
||||
if(key.indexOf("}")!=-1){
|
||||
moshi = key.substring(key.indexOf("}")+1);
|
||||
}
|
||||
String returnValue = null;
|
||||
if (key.contains("#{")) {
|
||||
key = key.substring(2,key.indexOf("}"));
|
||||
}
|
||||
if (oConvertUtils.isNotEmpty(key)) {
|
||||
HttpSession session = SpringContextUtils.getHttpServletRequest().getSession();
|
||||
returnValue = (String) session.getAttribute(key);
|
||||
}
|
||||
//结果加上${} 后面的值
|
||||
if(returnValue!=null){returnValue = returnValue + moshi;}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从当前用户中获取变量
|
||||
* @param key
|
||||
* @param user
|
||||
* @return
|
||||
*/
|
||||
public static String getUserSystemData(String key,SysUserCacheInfo user) {
|
||||
if(user==null) {
|
||||
user = JeecgDataAutorUtils.loadUserInfo();
|
||||
}
|
||||
//#{sys_user_code}%
|
||||
String moshi = "";
|
||||
if(key.indexOf("}")!=-1){
|
||||
moshi = key.substring(key.indexOf("}")+1);
|
||||
}
|
||||
String returnValue = null;
|
||||
//针对特殊标示处理#{sysOrgCode},判断替换
|
||||
if (key.contains("#{")) {
|
||||
key = key.substring(2,key.indexOf("}"));
|
||||
} else {
|
||||
key = key;
|
||||
}
|
||||
//替换为系统登录用户帐号
|
||||
if (key.equals(DataBaseConstant.SYS_USER_CODE)|| key.equals(DataBaseConstant.SYS_USER_CODE_TABLE)) {
|
||||
returnValue = user.getSysUserCode();
|
||||
}
|
||||
//替换为系统登录用户真实名字
|
||||
if (key.equals(DataBaseConstant.SYS_USER_NAME)|| key.equals(DataBaseConstant.SYS_USER_NAME_TABLE)) {
|
||||
returnValue = user.getSysUserName();
|
||||
}
|
||||
|
||||
//替换为系统用户登录所使用的机构编码
|
||||
if (key.equals(DataBaseConstant.SYS_ORG_CODE)|| key.equals(DataBaseConstant.SYS_ORG_CODE_TABLE)) {
|
||||
returnValue = user.getSysOrgCode();
|
||||
}
|
||||
//替换为系统用户所拥有的所有机构编码
|
||||
if (key.equals(DataBaseConstant.SYS_MULTI_ORG_CODE)|| key.equals(DataBaseConstant.SYS_MULTI_ORG_CODE)) {
|
||||
if(user.isOneDepart()) {
|
||||
returnValue = user.getSysMultiOrgCode().get(0);
|
||||
}else {
|
||||
returnValue = Joiner.on(",").join(user.getSysMultiOrgCode());
|
||||
}
|
||||
}
|
||||
//替换为当前系统时间(年月日)
|
||||
if (key.equals(DataBaseConstant.SYS_DATE)|| key.equals(DataBaseConstant.SYS_DATE_TABLE)) {
|
||||
returnValue = user.getSysDate();
|
||||
}
|
||||
//替换为当前系统时间(年月日时分秒)
|
||||
if (key.equals(DataBaseConstant.SYS_TIME)|| key.equals(DataBaseConstant.SYS_TIME_TABLE)) {
|
||||
returnValue = user.getSysTime();
|
||||
}
|
||||
//流程状态默认值(默认未发起)
|
||||
if (key.equals(DataBaseConstant.BPM_STATUS_TABLE)|| key.equals(DataBaseConstant.BPM_STATUS_TABLE)) {
|
||||
returnValue = "1";
|
||||
}
|
||||
if(returnValue!=null){returnValue = returnValue + moshi;}
|
||||
return returnValue;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
package org.jeecg.common.system.vo;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Accessors(chain = true)
|
||||
public class DictModel implements Serializable{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 字典value
|
||||
*/
|
||||
private String value;
|
||||
/**
|
||||
* 字典文本
|
||||
*/
|
||||
private String text;
|
||||
|
||||
/**
|
||||
* 特殊用途: JgEditableTable
|
||||
* @return
|
||||
*/
|
||||
public String getTitle() {
|
||||
return this.text;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,83 @@
|
||||
package org.jeecg.common.system.vo;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 在线用户信息
|
||||
* </p>
|
||||
*
|
||||
* @Author scott
|
||||
* @since 2018-12-20
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Accessors(chain = true)
|
||||
public class LoginUser {
|
||||
|
||||
/**
|
||||
* 登录人id
|
||||
*/
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* 登录人账号
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* 登录人名字
|
||||
*/
|
||||
private String realname;
|
||||
|
||||
/**
|
||||
* 当前登录部门code
|
||||
*/
|
||||
private String orgCode;
|
||||
/**
|
||||
* 头像
|
||||
*/
|
||||
private String avatar;
|
||||
|
||||
/**
|
||||
* 生日
|
||||
*/
|
||||
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd")
|
||||
private Date birthday;
|
||||
|
||||
/**
|
||||
* 性别(1:男 2:女)
|
||||
*/
|
||||
private Integer sex;
|
||||
|
||||
/**
|
||||
* 电子邮件
|
||||
*/
|
||||
private String email;
|
||||
|
||||
/**
|
||||
* 电话
|
||||
*/
|
||||
private String phone;
|
||||
|
||||
/**
|
||||
* 状态(1:正常 2:冻结 )
|
||||
*/
|
||||
private Integer status;
|
||||
|
||||
private String delFlag;
|
||||
/**
|
||||
* 同步工作流引擎1同步0不同步
|
||||
*/
|
||||
private String activitiSync;
|
||||
|
||||
}
|
||||
@ -0,0 +1,67 @@
|
||||
package org.jeecg.common.system.vo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.jeecg.common.util.DateUtils;
|
||||
|
||||
public class SysUserCacheInfo {
|
||||
|
||||
private String sysUserCode;
|
||||
|
||||
private String sysUserName;
|
||||
|
||||
private String sysOrgCode;
|
||||
|
||||
private List<String> sysMultiOrgCode;
|
||||
|
||||
private boolean oneDepart;
|
||||
|
||||
public boolean isOneDepart() {
|
||||
return oneDepart;
|
||||
}
|
||||
|
||||
public void setOneDepart(boolean oneDepart) {
|
||||
this.oneDepart = oneDepart;
|
||||
}
|
||||
|
||||
public String getSysDate() {
|
||||
return DateUtils.formatDate();
|
||||
}
|
||||
|
||||
public String getSysTime() {
|
||||
return DateUtils.now();
|
||||
}
|
||||
|
||||
public String getSysUserCode() {
|
||||
return sysUserCode;
|
||||
}
|
||||
|
||||
public void setSysUserCode(String sysUserCode) {
|
||||
this.sysUserCode = sysUserCode;
|
||||
}
|
||||
|
||||
public String getSysUserName() {
|
||||
return sysUserName;
|
||||
}
|
||||
|
||||
public void setSysUserName(String sysUserName) {
|
||||
this.sysUserName = sysUserName;
|
||||
}
|
||||
|
||||
public String getSysOrgCode() {
|
||||
return sysOrgCode;
|
||||
}
|
||||
|
||||
public void setSysOrgCode(String sysOrgCode) {
|
||||
this.sysOrgCode = sysOrgCode;
|
||||
}
|
||||
|
||||
public List<String> getSysMultiOrgCode() {
|
||||
return sysMultiOrgCode;
|
||||
}
|
||||
|
||||
public void setSysMultiOrgCode(List<String> sysMultiOrgCode) {
|
||||
this.sysMultiOrgCode = sysMultiOrgCode;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
package org.jeecg.common.util;
|
||||
|
||||
/**
|
||||
*
|
||||
* @Author 张代浩
|
||||
*
|
||||
*/
|
||||
public enum BrowserType {
|
||||
IE11,IE10,IE9,IE8,IE7,IE6,Firefox,Safari,Chrome,Opera,Camino,Gecko
|
||||
}
|
||||
@ -0,0 +1,194 @@
|
||||
package org.jeecg.common.util;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
*
|
||||
* @Author 张代浩
|
||||
*
|
||||
*/
|
||||
public class BrowserUtils {
|
||||
|
||||
// 判断是否是IE
|
||||
public static boolean isIE(HttpServletRequest request) {
|
||||
return (request.getHeader("USER-AGENT").toLowerCase().indexOf("msie") > 0 || request
|
||||
.getHeader("USER-AGENT").toLowerCase().indexOf("rv:11.0") > 0) ? true
|
||||
: false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取IE版本
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
public static Double getIEversion(HttpServletRequest request) {
|
||||
Double version = 0.0;
|
||||
if (getBrowserType(request, IE11)) {
|
||||
version = 11.0;
|
||||
} else if (getBrowserType(request, IE10)) {
|
||||
version = 10.0;
|
||||
} else if (getBrowserType(request, IE9)) {
|
||||
version = 9.0;
|
||||
} else if (getBrowserType(request, IE8)) {
|
||||
version = 8.0;
|
||||
} else if (getBrowserType(request, IE7)) {
|
||||
version = 7.0;
|
||||
} else if (getBrowserType(request, IE6)) {
|
||||
version = 6.0;
|
||||
}
|
||||
return version;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取浏览器类型
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
public static BrowserType getBrowserType(HttpServletRequest request) {
|
||||
BrowserType browserType = null;
|
||||
if (getBrowserType(request, IE11)) {
|
||||
browserType = BrowserType.IE11;
|
||||
}
|
||||
if (getBrowserType(request, IE10)) {
|
||||
browserType = BrowserType.IE10;
|
||||
}
|
||||
if (getBrowserType(request, IE9)) {
|
||||
browserType = BrowserType.IE9;
|
||||
}
|
||||
if (getBrowserType(request, IE8)) {
|
||||
browserType = BrowserType.IE8;
|
||||
}
|
||||
if (getBrowserType(request, IE7)) {
|
||||
browserType = BrowserType.IE7;
|
||||
}
|
||||
if (getBrowserType(request, IE6)) {
|
||||
browserType = BrowserType.IE6;
|
||||
}
|
||||
if (getBrowserType(request, FIREFOX)) {
|
||||
browserType = BrowserType.Firefox;
|
||||
}
|
||||
if (getBrowserType(request, SAFARI)) {
|
||||
browserType = BrowserType.Safari;
|
||||
}
|
||||
if (getBrowserType(request, CHROME)) {
|
||||
browserType = BrowserType.Chrome;
|
||||
}
|
||||
if (getBrowserType(request, OPERA)) {
|
||||
browserType = BrowserType.Opera;
|
||||
}
|
||||
if (getBrowserType(request, "Camino")) {
|
||||
browserType = BrowserType.Camino;
|
||||
}
|
||||
return browserType;
|
||||
}
|
||||
|
||||
private static boolean getBrowserType(HttpServletRequest request,
|
||||
String brosertype) {
|
||||
return request.getHeader("USER-AGENT").toLowerCase()
|
||||
.indexOf(brosertype) > 0 ? true : false;
|
||||
}
|
||||
|
||||
private final static String IE11 = "rv:11.0";
|
||||
private final static String IE10 = "MSIE 10.0";
|
||||
private final static String IE9 = "MSIE 9.0";
|
||||
private final static String IE8 = "MSIE 8.0";
|
||||
private final static String IE7 = "MSIE 7.0";
|
||||
private final static String IE6 = "MSIE 6.0";
|
||||
private final static String MAXTHON = "Maxthon";
|
||||
private final static String QQ = "QQBrowser";
|
||||
private final static String GREEN = "GreenBrowser";
|
||||
private final static String SE360 = "360SE";
|
||||
private final static String FIREFOX = "Firefox";
|
||||
private final static String OPERA = "Opera";
|
||||
private final static String CHROME = "Chrome";
|
||||
private final static String SAFARI = "Safari";
|
||||
private final static String OTHER = "其它";
|
||||
|
||||
public static String checkBrowse(HttpServletRequest request) {
|
||||
String userAgent = request.getHeader("USER-AGENT");
|
||||
if (regex(OPERA, userAgent)) {
|
||||
return OPERA;
|
||||
}
|
||||
if (regex(CHROME, userAgent)) {
|
||||
return CHROME;
|
||||
}
|
||||
if (regex(FIREFOX, userAgent)) {
|
||||
return FIREFOX;
|
||||
}
|
||||
if (regex(SAFARI, userAgent)) {
|
||||
return SAFARI;
|
||||
}
|
||||
if (regex(SE360, userAgent)) {
|
||||
return SE360;
|
||||
}
|
||||
if (regex(GREEN, userAgent)) {
|
||||
return GREEN;
|
||||
}
|
||||
if (regex(QQ, userAgent)) {
|
||||
return QQ;
|
||||
}
|
||||
if (regex(MAXTHON, userAgent)) {
|
||||
return MAXTHON;
|
||||
}
|
||||
if (regex(IE11, userAgent)) {
|
||||
return IE11;
|
||||
}
|
||||
if (regex(IE10, userAgent)) {
|
||||
return IE10;
|
||||
}
|
||||
if (regex(IE9, userAgent)) {
|
||||
return IE9;
|
||||
}
|
||||
if (regex(IE8, userAgent)) {
|
||||
return IE8;
|
||||
}
|
||||
if (regex(IE7, userAgent)) {
|
||||
return IE7;
|
||||
}
|
||||
if (regex(IE6, userAgent)) {
|
||||
return IE6;
|
||||
}
|
||||
return OTHER;
|
||||
}
|
||||
|
||||
public static boolean regex(String regex, String str) {
|
||||
Pattern p = Pattern.compile(regex, Pattern.MULTILINE);
|
||||
Matcher m = p.matcher(str);
|
||||
return m.find();
|
||||
}
|
||||
|
||||
|
||||
private static Map<String, String> langMap = new HashMap<String, String>();
|
||||
private final static String ZH = "zh";
|
||||
private final static String ZH_CN = "zh-cn";
|
||||
|
||||
private final static String EN = "en";
|
||||
private final static String EN_US = "en";
|
||||
|
||||
|
||||
static
|
||||
{
|
||||
langMap.put(ZH, ZH_CN);
|
||||
langMap.put(EN, EN_US);
|
||||
}
|
||||
|
||||
public static String getBrowserLanguage(HttpServletRequest request) {
|
||||
|
||||
String browserLang = request.getLocale().getLanguage();
|
||||
String browserLangCode = (String)langMap.get(browserLang);
|
||||
|
||||
if(browserLangCode == null)
|
||||
{
|
||||
browserLangCode = EN_US;
|
||||
}
|
||||
return browserLangCode;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,622 @@
|
||||
package org.jeecg.common.util;
|
||||
|
||||
import java.beans.PropertyEditorSupport;
|
||||
import java.sql.Timestamp;
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
*
|
||||
* 类描述:时间操作定义类
|
||||
*
|
||||
* @Author: 张代浩
|
||||
* @Date:2012-12-8 12:15:03
|
||||
* @Version 1.0
|
||||
*/
|
||||
public class DateUtils extends PropertyEditorSupport {
|
||||
// 各种时间格式
|
||||
public static final SimpleDateFormat date_sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||
// 各种时间格式
|
||||
public static final SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
|
||||
// 各种时间格式
|
||||
public static final SimpleDateFormat date_sdf_wz = new SimpleDateFormat("yyyy年MM月dd日");
|
||||
public static final SimpleDateFormat time_sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
||||
public static final SimpleDateFormat yyyymmddhhmmss = new SimpleDateFormat("yyyyMMddHHmmss");
|
||||
public static final SimpleDateFormat short_time_sdf = new SimpleDateFormat("HH:mm");
|
||||
public static final SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
// 以毫秒表示的时间
|
||||
private static final long DAY_IN_MILLIS = 24 * 3600 * 1000;
|
||||
private static final long HOUR_IN_MILLIS = 3600 * 1000;
|
||||
private static final long MINUTE_IN_MILLIS = 60 * 1000;
|
||||
private static final long SECOND_IN_MILLIS = 1000;
|
||||
|
||||
// 指定模式的时间格式
|
||||
private static SimpleDateFormat getSDFormat(String pattern) {
|
||||
return new SimpleDateFormat(pattern);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前日历,这里用中国时间表示
|
||||
*
|
||||
* @return 以当地时区表示的系统当前日历
|
||||
*/
|
||||
public static Calendar getCalendar() {
|
||||
return Calendar.getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定毫秒数表示的日历
|
||||
*
|
||||
* @param millis 毫秒数
|
||||
* @return 指定毫秒数表示的日历
|
||||
*/
|
||||
public static Calendar getCalendar(long millis) {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
// --------------------cal.setTimeInMillis(millis);
|
||||
cal.setTime(new Date(millis));
|
||||
return cal;
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////////
|
||||
// getDate
|
||||
// 各种方式获取的Date
|
||||
// ////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* 当前日期
|
||||
*
|
||||
* @return 系统当前时间
|
||||
*/
|
||||
public static Date getDate() {
|
||||
return new Date();
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定毫秒数表示的日期
|
||||
*
|
||||
* @param millis 毫秒数
|
||||
* @return 指定毫秒数表示的日期
|
||||
*/
|
||||
public static Date getDate(long millis) {
|
||||
return new Date(millis);
|
||||
}
|
||||
|
||||
/**
|
||||
* 时间戳转换为字符串
|
||||
*
|
||||
* @param time
|
||||
* @return
|
||||
*/
|
||||
public static String timestamptoStr(Timestamp time) {
|
||||
Date date = null;
|
||||
if (null != time) {
|
||||
date = new Date(time.getTime());
|
||||
}
|
||||
return date2Str(date_sdf);
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串转换时间戳
|
||||
*
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public static Timestamp str2Timestamp(String str) {
|
||||
Date date = str2Date(str, date_sdf);
|
||||
return new Timestamp(date.getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串转换成日期
|
||||
*
|
||||
* @param str
|
||||
* @param sdf
|
||||
* @return
|
||||
*/
|
||||
public static Date str2Date(String str, SimpleDateFormat sdf) {
|
||||
if (null == str || "".equals(str)) {
|
||||
return null;
|
||||
}
|
||||
Date date = null;
|
||||
try {
|
||||
date = sdf.parse(str);
|
||||
return date;
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期转换为字符串
|
||||
*
|
||||
* @param date 日期
|
||||
* @param format 日期格式
|
||||
* @return 字符串
|
||||
*/
|
||||
public static String date2Str(SimpleDateFormat date_sdf) {
|
||||
Date date = getDate();
|
||||
if (null == date) {
|
||||
return null;
|
||||
}
|
||||
return date_sdf.format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化时间
|
||||
*
|
||||
* @param date
|
||||
* @param format
|
||||
* @return
|
||||
*/
|
||||
public static String dateformat(String date, String format) {
|
||||
SimpleDateFormat sformat = new SimpleDateFormat(format);
|
||||
Date _date = null;
|
||||
try {
|
||||
_date = sformat.parse(date);
|
||||
} catch (ParseException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return sformat.format(_date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期转换为字符串
|
||||
*
|
||||
* @param date 日期
|
||||
* @param format 日期格式
|
||||
* @return 字符串
|
||||
*/
|
||||
public static String date2Str(Date date, SimpleDateFormat date_sdf) {
|
||||
if (null == date) {
|
||||
return null;
|
||||
}
|
||||
return date_sdf.format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期转换为字符串
|
||||
*
|
||||
* @param date 日期
|
||||
* @param format 日期格式
|
||||
* @return 字符串
|
||||
*/
|
||||
public static String getDate(String format) {
|
||||
Date date = new Date();
|
||||
if (null == date) {
|
||||
return null;
|
||||
}
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(format);
|
||||
return sdf.format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定毫秒数的时间戳
|
||||
*
|
||||
* @param millis 毫秒数
|
||||
* @return 指定毫秒数的时间戳
|
||||
*/
|
||||
public static Timestamp getTimestamp(long millis) {
|
||||
return new Timestamp(millis);
|
||||
}
|
||||
|
||||
/**
|
||||
* 以字符形式表示的时间戳
|
||||
*
|
||||
* @param time 毫秒数
|
||||
* @return 以字符形式表示的时间戳
|
||||
*/
|
||||
public static Timestamp getTimestamp(String time) {
|
||||
return new Timestamp(Long.parseLong(time));
|
||||
}
|
||||
|
||||
/**
|
||||
* 系统当前的时间戳
|
||||
*
|
||||
* @return 系统当前的时间戳
|
||||
*/
|
||||
public static Timestamp getTimestamp() {
|
||||
return new Timestamp(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前时间,格式 yyyy-MM-dd HH:mm:ss
|
||||
*
|
||||
* @return 当前时间的标准形式字符串
|
||||
*/
|
||||
public static String now() {
|
||||
return datetimeFormat.format(getCalendar().getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定日期的时间戳
|
||||
*
|
||||
* @param date 指定日期
|
||||
* @return 指定日期的时间戳
|
||||
*/
|
||||
public static Timestamp getTimestamp(Date date) {
|
||||
return new Timestamp(date.getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定日历的时间戳
|
||||
*
|
||||
* @param cal 指定日历
|
||||
* @return 指定日历的时间戳
|
||||
*/
|
||||
public static Timestamp getCalendarTimestamp(Calendar cal) {
|
||||
// ---------------------return new Timestamp(cal.getTimeInMillis());
|
||||
return new Timestamp(cal.getTime().getTime());
|
||||
}
|
||||
|
||||
public static Timestamp gettimestamp() {
|
||||
Date dt = new Date();
|
||||
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
String nowTime = df.format(dt);
|
||||
java.sql.Timestamp buydate = java.sql.Timestamp.valueOf(nowTime);
|
||||
return buydate;
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////////
|
||||
// getMillis
|
||||
// 各种方式获取的Millis
|
||||
// ////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* 系统时间的毫秒数
|
||||
*
|
||||
* @return 系统时间的毫秒数
|
||||
*/
|
||||
public static long getMillis() {
|
||||
return System.currentTimeMillis();
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定日历的毫秒数
|
||||
*
|
||||
* @param cal 指定日历
|
||||
* @return 指定日历的毫秒数
|
||||
*/
|
||||
public static long getMillis(Calendar cal) {
|
||||
// --------------------return cal.getTimeInMillis();
|
||||
return cal.getTime().getTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定日期的毫秒数
|
||||
*
|
||||
* @param date 指定日期
|
||||
* @return 指定日期的毫秒数
|
||||
*/
|
||||
public static long getMillis(Date date) {
|
||||
return date.getTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定时间戳的毫秒数
|
||||
*
|
||||
* @param ts 指定时间戳
|
||||
* @return 指定时间戳的毫秒数
|
||||
*/
|
||||
public static long getMillis(Timestamp ts) {
|
||||
return ts.getTime();
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////////
|
||||
// formatDate
|
||||
// 将日期按照一定的格式转化为字符串
|
||||
// ////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* 默认方式表示的系统当前日期,具体格式:年-月-日
|
||||
*
|
||||
* @return 默认日期按“年-月-日“格式显示
|
||||
*/
|
||||
public static String formatDate() {
|
||||
return date_sdf.format(getCalendar().getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认方式表示的系统当前日期,具体格式:yyyy-MM-dd HH:mm:ss
|
||||
*
|
||||
* @return 默认日期按“yyyy-MM-dd HH:mm:ss“格式显示
|
||||
*/
|
||||
public static String formatDateTime() {
|
||||
return datetimeFormat.format(getCalendar().getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取时间字符串
|
||||
*/
|
||||
public static String getDataString(SimpleDateFormat formatstr) {
|
||||
return formatstr.format(getCalendar().getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定日期的默认显示,具体格式:年-月-日
|
||||
*
|
||||
* @param cal 指定的日期
|
||||
* @return 指定日期按“年-月-日“格式显示
|
||||
*/
|
||||
public static String formatDate(Calendar cal) {
|
||||
return date_sdf.format(cal.getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定日期的默认显示,具体格式:年-月-日
|
||||
*
|
||||
* @param date 指定的日期
|
||||
* @return 指定日期按“年-月-日“格式显示
|
||||
*/
|
||||
public static String formatDate(Date date) {
|
||||
return date_sdf.format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定毫秒数表示日期的默认显示,具体格式:年-月-日
|
||||
*
|
||||
* @param millis 指定的毫秒数
|
||||
* @return 指定毫秒数表示日期按“年-月-日“格式显示
|
||||
*/
|
||||
public static String formatDate(long millis) {
|
||||
return date_sdf.format(new Date(millis));
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认日期按指定格式显示
|
||||
*
|
||||
* @param pattern 指定的格式
|
||||
* @return 默认日期按指定格式显示
|
||||
*/
|
||||
public static String formatDate(String pattern) {
|
||||
return getSDFormat(pattern).format(getCalendar().getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定日期按指定格式显示
|
||||
*
|
||||
* @param cal 指定的日期
|
||||
* @param pattern 指定的格式
|
||||
* @return 指定日期按指定格式显示
|
||||
*/
|
||||
public static String formatDate(Calendar cal, String pattern) {
|
||||
return getSDFormat(pattern).format(cal.getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定日期按指定格式显示
|
||||
*
|
||||
* @param date 指定的日期
|
||||
* @param pattern 指定的格式
|
||||
* @return 指定日期按指定格式显示
|
||||
*/
|
||||
public static String formatDate(Date date, String pattern) {
|
||||
return getSDFormat(pattern).format(date);
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////////
|
||||
// formatTime
|
||||
// 将日期按照一定的格式转化为字符串
|
||||
// ////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* 默认方式表示的系统当前日期,具体格式:年-月-日 时:分
|
||||
*
|
||||
* @return 默认日期按“年-月-日 时:分“格式显示
|
||||
*/
|
||||
public static String formatTime() {
|
||||
return time_sdf.format(getCalendar().getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定毫秒数表示日期的默认显示,具体格式:年-月-日 时:分
|
||||
*
|
||||
* @param millis 指定的毫秒数
|
||||
* @return 指定毫秒数表示日期按“年-月-日 时:分“格式显示
|
||||
*/
|
||||
public static String formatTime(long millis) {
|
||||
return time_sdf.format(new Date(millis));
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定日期的默认显示,具体格式:年-月-日 时:分
|
||||
*
|
||||
* @param cal 指定的日期
|
||||
* @return 指定日期按“年-月-日 时:分“格式显示
|
||||
*/
|
||||
public static String formatTime(Calendar cal) {
|
||||
return time_sdf.format(cal.getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定日期的默认显示,具体格式:年-月-日 时:分
|
||||
*
|
||||
* @param date 指定的日期
|
||||
* @return 指定日期按“年-月-日 时:分“格式显示
|
||||
*/
|
||||
public static String formatTime(Date date) {
|
||||
return time_sdf.format(date);
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////////
|
||||
// formatShortTime
|
||||
// 将日期按照一定的格式转化为字符串
|
||||
// ////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* 默认方式表示的系统当前日期,具体格式:时:分
|
||||
*
|
||||
* @return 默认日期按“时:分“格式显示
|
||||
*/
|
||||
public static String formatShortTime() {
|
||||
return short_time_sdf.format(getCalendar().getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定毫秒数表示日期的默认显示,具体格式:时:分
|
||||
*
|
||||
* @param millis 指定的毫秒数
|
||||
* @return 指定毫秒数表示日期按“时:分“格式显示
|
||||
*/
|
||||
public static String formatShortTime(long millis) {
|
||||
return short_time_sdf.format(new Date(millis));
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定日期的默认显示,具体格式:时:分
|
||||
*
|
||||
* @param cal 指定的日期
|
||||
* @return 指定日期按“时:分“格式显示
|
||||
*/
|
||||
public static String formatShortTime(Calendar cal) {
|
||||
return short_time_sdf.format(cal.getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定日期的默认显示,具体格式:时:分
|
||||
*
|
||||
* @param date 指定的日期
|
||||
* @return 指定日期按“时:分“格式显示
|
||||
*/
|
||||
public static String formatShortTime(Date date) {
|
||||
return short_time_sdf.format(date);
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////////
|
||||
// parseDate
|
||||
// parseCalendar
|
||||
// parseTimestamp
|
||||
// 将字符串按照一定的格式转化为日期或时间
|
||||
// ////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间
|
||||
*
|
||||
* @param src 将要转换的原始字符窜
|
||||
* @param pattern 转换的匹配格式
|
||||
* @return 如果转换成功则返回转换后的日期
|
||||
* @throws ParseException
|
||||
* @throws AIDateFormatException
|
||||
*/
|
||||
public static Date parseDate(String src, String pattern) throws ParseException {
|
||||
return getSDFormat(pattern).parse(src);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间
|
||||
*
|
||||
* @param src 将要转换的原始字符窜
|
||||
* @param pattern 转换的匹配格式
|
||||
* @return 如果转换成功则返回转换后的日期
|
||||
* @throws ParseException
|
||||
* @throws AIDateFormatException
|
||||
*/
|
||||
public static Calendar parseCalendar(String src, String pattern) throws ParseException {
|
||||
|
||||
Date date = parseDate(src, pattern);
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(date);
|
||||
return cal;
|
||||
}
|
||||
|
||||
public static String formatAddDate(String src, String pattern, int amount) throws ParseException {
|
||||
Calendar cal;
|
||||
cal = parseCalendar(src, pattern);
|
||||
cal.add(Calendar.DATE, amount);
|
||||
return formatDate(cal);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间
|
||||
*
|
||||
* @param src 将要转换的原始字符窜
|
||||
* @param pattern 转换的匹配格式
|
||||
* @return 如果转换成功则返回转换后的时间戳
|
||||
* @throws ParseException
|
||||
* @throws AIDateFormatException
|
||||
*/
|
||||
public static Timestamp parseTimestamp(String src, String pattern) throws ParseException {
|
||||
Date date = parseDate(src, pattern);
|
||||
return new Timestamp(date.getTime());
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////////
|
||||
// dateDiff
|
||||
// 计算两个日期之间的差值
|
||||
// ////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* 计算两个时间之间的差值,根据标志的不同而不同
|
||||
*
|
||||
* @param flag 计算标志,表示按照年/月/日/时/分/秒等计算
|
||||
* @param calSrc 减数
|
||||
* @param calDes 被减数
|
||||
* @return 两个日期之间的差值
|
||||
*/
|
||||
public static int dateDiff(char flag, Calendar calSrc, Calendar calDes) {
|
||||
|
||||
long millisDiff = getMillis(calSrc) - getMillis(calDes);
|
||||
|
||||
if (flag == 'y') {
|
||||
return (calSrc.get(calSrc.YEAR) - calDes.get(calDes.YEAR));
|
||||
}
|
||||
|
||||
if (flag == 'd') {
|
||||
return (int) (millisDiff / DAY_IN_MILLIS);
|
||||
}
|
||||
|
||||
if (flag == 'h') {
|
||||
return (int) (millisDiff / HOUR_IN_MILLIS);
|
||||
}
|
||||
|
||||
if (flag == 'm') {
|
||||
return (int) (millisDiff / MINUTE_IN_MILLIS);
|
||||
}
|
||||
|
||||
if (flag == 's') {
|
||||
return (int) (millisDiff / SECOND_IN_MILLIS);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* String类型 转换为Date, 如果参数长度为10 转换格式”yyyy-MM-dd“ 如果参数长度为19 转换格式”yyyy-MM-dd
|
||||
* HH:mm:ss“ * @param text String类型的时间值
|
||||
*/
|
||||
@Override
|
||||
public void setAsText(String text) throws IllegalArgumentException {
|
||||
if (StringUtils.hasText(text)) {
|
||||
try {
|
||||
if (text.indexOf(":") == -1 && text.length() == 10) {
|
||||
setValue(this.date_sdf.parse(text));
|
||||
} else if (text.indexOf(":") > 0 && text.length() == 19) {
|
||||
setValue(this.datetimeFormat.parse(text));
|
||||
} else {
|
||||
throw new IllegalArgumentException("Could not parse date, date format is error ");
|
||||
}
|
||||
} catch (ParseException ex) {
|
||||
IllegalArgumentException iae = new IllegalArgumentException("Could not parse date: " + ex.getMessage());
|
||||
iae.initCause(ex);
|
||||
throw iae;
|
||||
}
|
||||
} else {
|
||||
setValue(null);
|
||||
}
|
||||
}
|
||||
|
||||
public static int getYear() {
|
||||
GregorianCalendar calendar = new GregorianCalendar();
|
||||
calendar.setTime(getDate());
|
||||
return calendar.get(Calendar.YEAR);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,58 @@
|
||||
package org.jeecg.common.util;
|
||||
|
||||
import com.alibaba.druid.util.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* IP地址
|
||||
*
|
||||
* @Author scott
|
||||
* @email jeecgos@163.com
|
||||
* @Date 2019年01月14日
|
||||
*/
|
||||
public class IPUtils {
|
||||
private static Logger logger = LoggerFactory.getLogger(IPUtils.class);
|
||||
|
||||
/**
|
||||
* 获取IP地址
|
||||
*
|
||||
* 使用Nginx等反向代理软件, 则不能通过request.getRemoteAddr()获取IP地址
|
||||
* 如果使用了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP地址,X-Forwarded-For中第一个非unknown的有效IP字符串,则为真实IP地址
|
||||
*/
|
||||
public static String getIpAddr(HttpServletRequest request) {
|
||||
String ip = null;
|
||||
try {
|
||||
ip = request.getHeader("x-forwarded-for");
|
||||
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("Proxy-Client-IP");
|
||||
}
|
||||
if (StringUtils.isEmpty(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("WL-Proxy-Client-IP");
|
||||
}
|
||||
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("HTTP_CLIENT_IP");
|
||||
}
|
||||
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
|
||||
}
|
||||
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
|
||||
ip = request.getRemoteAddr();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("IPUtils ERROR ", e);
|
||||
}
|
||||
|
||||
// //使用代理,则获取第一个IP地址
|
||||
// if(StringUtils.isEmpty(ip) && ip.length() > 15) {
|
||||
// if(ip.indexOf(",") > 0) {
|
||||
// ip = ip.substring(0, ip.indexOf(","));
|
||||
// }
|
||||
// }
|
||||
|
||||
return ip;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,43 @@
|
||||
package org.jeecg.common.util;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
|
||||
public class MD5Util {
|
||||
|
||||
public static String byteArrayToHexString(byte b[]) {
|
||||
StringBuffer resultSb = new StringBuffer();
|
||||
for (int i = 0; i < b.length; i++){
|
||||
resultSb.append(byteToHexString(b[i]));
|
||||
}
|
||||
return resultSb.toString();
|
||||
}
|
||||
|
||||
private static String byteToHexString(byte b) {
|
||||
int n = b;
|
||||
if (n < 0) {
|
||||
n += 256;
|
||||
}
|
||||
int d1 = n / 16;
|
||||
int d2 = n % 16;
|
||||
return hexDigits[d1] + hexDigits[d2];
|
||||
}
|
||||
|
||||
public static String MD5Encode(String origin, String charsetname) {
|
||||
String resultString = null;
|
||||
try {
|
||||
resultString = new String(origin);
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
if (charsetname == null || "".equals(charsetname)) {
|
||||
resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
|
||||
} else {
|
||||
resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname)));
|
||||
}
|
||||
} catch (Exception exception) {
|
||||
}
|
||||
return resultString;
|
||||
}
|
||||
|
||||
private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
|
||||
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
|
||||
|
||||
}
|
||||
@ -0,0 +1,92 @@
|
||||
package org.jeecg.common.util;
|
||||
|
||||
/**
|
||||
* @Author 张代浩
|
||||
*/
|
||||
public class MyClassLoader extends ClassLoader {
|
||||
public static Class getClassByScn(String className) {
|
||||
Class myclass = null;
|
||||
try {
|
||||
myclass = Class.forName(className);
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException(className+" not found!");
|
||||
}
|
||||
return myclass;
|
||||
}
|
||||
|
||||
// 获得类的全名,包括包名
|
||||
public static String getPackPath(Object object) {
|
||||
// 检查用户传入的参数是否为空
|
||||
if (object == null) {
|
||||
throw new java.lang.IllegalArgumentException("参数不能为空!");
|
||||
}
|
||||
// 获得类的全名,包括包名
|
||||
String clsName = object.getClass().getName();
|
||||
return clsName;
|
||||
}
|
||||
|
||||
public static String getAppPath(Class cls) {
|
||||
// 检查用户传入的参数是否为空
|
||||
if (cls == null) {
|
||||
throw new java.lang.IllegalArgumentException("参数不能为空!");
|
||||
}
|
||||
ClassLoader loader = cls.getClassLoader();
|
||||
// 获得类的全名,包括包名
|
||||
String clsName = cls.getName() + ".class";
|
||||
// 获得传入参数所在的包
|
||||
Package pack = cls.getPackage();
|
||||
String path = "";
|
||||
// 如果不是匿名包,将包名转化为路径
|
||||
if (pack != null) {
|
||||
String packName = pack.getName();
|
||||
// 此处简单判定是否是Java基础类库,防止用户传入JDK内置的类库
|
||||
if (packName.startsWith("java.") || packName.startsWith("javax.")) {
|
||||
throw new java.lang.IllegalArgumentException("不要传送系统类!");
|
||||
}
|
||||
// 在类的名称中,去掉包名的部分,获得类的文件名
|
||||
clsName = clsName.substring(packName.length() + 1);
|
||||
// 判定包名是否是简单包名,如果是,则直接将包名转换为路径,
|
||||
if (packName.indexOf(".") < 0) {
|
||||
path = packName + "/";
|
||||
} else {// 否则按照包名的组成部分,将包名转换为路径
|
||||
int start = 0, end = 0;
|
||||
end = packName.indexOf(".");
|
||||
while (end != -1) {
|
||||
path = path + packName.substring(start, end) + "/";
|
||||
start = end + 1;
|
||||
end = packName.indexOf(".", start);
|
||||
}
|
||||
path = path + packName.substring(start) + "/";
|
||||
}
|
||||
}
|
||||
// 调用ClassLoader的getResource方法,传入包含路径信息的类文件名
|
||||
java.net.URL url = loader.getResource(path + clsName);
|
||||
// 从URL对象中获取路径信息
|
||||
String realPath = url.getPath();
|
||||
// 去掉路径信息中的协议名"file:"
|
||||
int pos = realPath.indexOf("file:");
|
||||
if (pos > -1) {
|
||||
realPath = realPath.substring(pos + 5);
|
||||
}
|
||||
// 去掉路径信息最后包含类文件信息的部分,得到类所在的路径
|
||||
pos = realPath.indexOf(path + clsName);
|
||||
realPath = realPath.substring(0, pos - 1);
|
||||
// 如果类文件被打包到JAR等文件中时,去掉对应的JAR等打包文件名
|
||||
if (realPath.endsWith("!")) {
|
||||
realPath = realPath.substring(0, realPath.lastIndexOf("/"));
|
||||
}
|
||||
/*------------------------------------------------------------
|
||||
ClassLoader的getResource方法使用了utf-8对路径信息进行了编码,当路径
|
||||
中存在中文和空格时,他会对这些字符进行转换,这样,得到的往往不是我们想要
|
||||
的真实路径,在此,调用了URLDecoder的decode方法进行解码,以便得到原始的
|
||||
中文及空格路径
|
||||
-------------------------------------------------------------*/
|
||||
try {
|
||||
realPath = java.net.URLDecoder.decode(realPath, "utf-8");
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return realPath;
|
||||
}// getAppPath定义结束
|
||||
}
|
||||
@ -0,0 +1,179 @@
|
||||
package org.jeecg.common.util;
|
||||
|
||||
import java.security.Key;
|
||||
import java.security.SecureRandom;
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.SecretKey;
|
||||
import javax.crypto.SecretKeyFactory;
|
||||
import javax.crypto.spec.PBEKeySpec;
|
||||
import javax.crypto.spec.PBEParameterSpec;
|
||||
public class PasswordUtil {
|
||||
|
||||
/**
|
||||
* JAVA6支持以下任意一种算法 PBEWITHMD5ANDDES PBEWITHMD5ANDTRIPLEDES
|
||||
* PBEWITHSHAANDDESEDE PBEWITHSHA1ANDRC2_40 PBKDF2WITHHMACSHA1
|
||||
* */
|
||||
|
||||
/**
|
||||
* 定义使用的算法为:PBEWITHMD5andDES算法
|
||||
*/
|
||||
public static final String ALGORITHM = "PBEWithMD5AndDES";//加密算法
|
||||
public static final String Salt = "63293188";//密钥
|
||||
|
||||
/**
|
||||
* 定义迭代次数为1000次
|
||||
*/
|
||||
private static final int ITERATIONCOUNT = 1000;
|
||||
|
||||
/**
|
||||
* 获取加密算法中使用的盐值,解密中使用的盐值必须与加密中使用的相同才能完成操作. 盐长度必须为8字节
|
||||
*
|
||||
* @return byte[] 盐值
|
||||
* */
|
||||
public static byte[] getSalt() throws Exception {
|
||||
// 实例化安全随机数
|
||||
SecureRandom random = new SecureRandom();
|
||||
// 产出盐
|
||||
return random.generateSeed(8);
|
||||
}
|
||||
|
||||
public static byte[] getStaticSalt() {
|
||||
// 产出盐
|
||||
return Salt.getBytes();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据PBE密码生成一把密钥
|
||||
*
|
||||
* @param password
|
||||
* 生成密钥时所使用的密码
|
||||
* @return Key PBE算法密钥
|
||||
* */
|
||||
private static Key getPBEKey(String password) {
|
||||
// 实例化使用的算法
|
||||
SecretKeyFactory keyFactory;
|
||||
SecretKey secretKey = null;
|
||||
try {
|
||||
keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
|
||||
// 设置PBE密钥参数
|
||||
PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
|
||||
// 生成密钥
|
||||
secretKey = keyFactory.generateSecret(keySpec);
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return secretKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密明文字符串
|
||||
*
|
||||
* @param plaintext
|
||||
* 待加密的明文字符串
|
||||
* @param password
|
||||
* 生成密钥时所使用的密码
|
||||
* @param salt
|
||||
* 盐值
|
||||
* @return 加密后的密文字符串
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String encrypt(String plaintext, String password, String salt) {
|
||||
|
||||
Key key = getPBEKey(password);
|
||||
byte[] encipheredData = null;
|
||||
PBEParameterSpec parameterSpec = new PBEParameterSpec(salt.getBytes(), ITERATIONCOUNT);
|
||||
try {
|
||||
Cipher cipher = Cipher.getInstance(ALGORITHM);
|
||||
|
||||
cipher.init(Cipher.ENCRYPT_MODE, key, parameterSpec);
|
||||
|
||||
encipheredData = cipher.doFinal(plaintext.getBytes());
|
||||
} catch (Exception e) {
|
||||
}
|
||||
return bytesToHexString(encipheredData);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密密文字符串
|
||||
*
|
||||
* @param ciphertext
|
||||
* 待解密的密文字符串
|
||||
* @param password
|
||||
* 生成密钥时所使用的密码(如需解密,该参数需要与加密时使用的一致)
|
||||
* @param salt
|
||||
* 盐值(如需解密,该参数需要与加密时使用的一致)
|
||||
* @return 解密后的明文字符串
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String decrypt(String ciphertext, String password, String salt) {
|
||||
|
||||
Key key = getPBEKey(password);
|
||||
byte[] passDec = null;
|
||||
PBEParameterSpec parameterSpec = new PBEParameterSpec(salt.getBytes(), ITERATIONCOUNT);
|
||||
try {
|
||||
Cipher cipher = Cipher.getInstance(ALGORITHM);
|
||||
|
||||
cipher.init(Cipher.DECRYPT_MODE, key, parameterSpec);
|
||||
|
||||
passDec = cipher.doFinal(hexStringToBytes(ciphertext));
|
||||
}
|
||||
|
||||
catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
}
|
||||
return new String(passDec);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字节数组转换为十六进制字符串
|
||||
*
|
||||
* @param src
|
||||
* 字节数组
|
||||
* @return
|
||||
*/
|
||||
public static String bytesToHexString(byte[] src) {
|
||||
StringBuilder stringBuilder = new StringBuilder("");
|
||||
if (src == null || src.length <= 0) {
|
||||
return null;
|
||||
}
|
||||
for (int i = 0; i < src.length; i++) {
|
||||
int v = src[i] & 0xFF;
|
||||
String hv = Integer.toHexString(v);
|
||||
if (hv.length() < 2) {
|
||||
stringBuilder.append(0);
|
||||
}
|
||||
stringBuilder.append(hv);
|
||||
}
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将十六进制字符串转换为字节数组
|
||||
*
|
||||
* @param hexString
|
||||
* 十六进制字符串
|
||||
* @return
|
||||
*/
|
||||
public static byte[] hexStringToBytes(String hexString) {
|
||||
if (hexString == null || hexString.equals("")) {
|
||||
return null;
|
||||
}
|
||||
hexString = hexString.toUpperCase();
|
||||
int length = hexString.length() / 2;
|
||||
char[] hexChars = hexString.toCharArray();
|
||||
byte[] d = new byte[length];
|
||||
for (int i = 0; i < length; i++) {
|
||||
int pos = i * 2;
|
||||
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
private static byte charToByte(char c) {
|
||||
return (byte) "0123456789ABCDEF".indexOf(c);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,574 @@
|
||||
package org.jeecg.common.util;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* redis 工具类
|
||||
* @Author Scott
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
public class RedisUtil {
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
/**
|
||||
* 指定缓存失效时间
|
||||
*
|
||||
* @param key 键
|
||||
* @param time 时间(秒)
|
||||
* @return
|
||||
*/
|
||||
public boolean expire(String key, long time) {
|
||||
try {
|
||||
if (time > 0) {
|
||||
redisTemplate.expire(key, time, TimeUnit.SECONDS);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据key 获取过期时间
|
||||
*
|
||||
* @param key 键 不能为null
|
||||
* @return 时间(秒) 返回0代表为永久有效
|
||||
*/
|
||||
public long getExpire(String key) {
|
||||
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断key是否存在
|
||||
*
|
||||
* @param key 键
|
||||
* @return true 存在 false不存在
|
||||
*/
|
||||
public boolean hasKey(String key) {
|
||||
try {
|
||||
return redisTemplate.hasKey(key);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存
|
||||
*
|
||||
* @param key 可以传一个值 或多个
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public void del(String... key) {
|
||||
if (key != null && key.length > 0) {
|
||||
if (key.length == 1) {
|
||||
redisTemplate.delete(key[0]);
|
||||
} else {
|
||||
redisTemplate.delete(CollectionUtils.arrayToList(key));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ============================String=============================
|
||||
/**
|
||||
* 普通缓存获取
|
||||
*
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
public Object get(String key) {
|
||||
return key == null ? null : redisTemplate.opsForValue().get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 普通缓存放入
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @return true成功 false失败
|
||||
*/
|
||||
public boolean set(String key, Object value) {
|
||||
try {
|
||||
redisTemplate.opsForValue().set(key, value);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 普通缓存放入并设置时间
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
|
||||
* @return true成功 false 失败
|
||||
*/
|
||||
public boolean set(String key, Object value, long time) {
|
||||
try {
|
||||
if (time > 0) {
|
||||
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
|
||||
} else {
|
||||
set(key, value);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 递增
|
||||
*
|
||||
* @param key 键
|
||||
* @param by 要增加几(大于0)
|
||||
* @return
|
||||
*/
|
||||
public long incr(String key, long delta) {
|
||||
if (delta < 0) {
|
||||
throw new RuntimeException("递增因子必须大于0");
|
||||
}
|
||||
return redisTemplate.opsForValue().increment(key, delta);
|
||||
}
|
||||
|
||||
/**
|
||||
* 递减
|
||||
*
|
||||
* @param key 键
|
||||
* @param by 要减少几(小于0)
|
||||
* @return
|
||||
*/
|
||||
public long decr(String key, long delta) {
|
||||
if (delta < 0) {
|
||||
throw new RuntimeException("递减因子必须大于0");
|
||||
}
|
||||
return redisTemplate.opsForValue().increment(key, -delta);
|
||||
}
|
||||
|
||||
// ================================Map=================================
|
||||
/**
|
||||
* HashGet
|
||||
*
|
||||
* @param key 键 不能为null
|
||||
* @param item 项 不能为null
|
||||
* @return 值
|
||||
*/
|
||||
public Object hget(String key, String item) {
|
||||
return redisTemplate.opsForHash().get(key, item);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取hashKey对应的所有键值
|
||||
*
|
||||
* @param key 键
|
||||
* @return 对应的多个键值
|
||||
*/
|
||||
public Map<Object, Object> hmget(String key) {
|
||||
return redisTemplate.opsForHash().entries(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* HashSet
|
||||
*
|
||||
* @param key 键
|
||||
* @param map 对应多个键值
|
||||
* @return true 成功 false 失败
|
||||
*/
|
||||
public boolean hmset(String key, Map<String, Object> map) {
|
||||
try {
|
||||
redisTemplate.opsForHash().putAll(key, map);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* HashSet 并设置时间
|
||||
*
|
||||
* @param key 键
|
||||
* @param map 对应多个键值
|
||||
* @param time 时间(秒)
|
||||
* @return true成功 false失败
|
||||
*/
|
||||
public boolean hmset(String key, Map<String, Object> map, long time) {
|
||||
try {
|
||||
redisTemplate.opsForHash().putAll(key, map);
|
||||
if (time > 0) {
|
||||
expire(key, time);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 向一张hash表中放入数据,如果不存在将创建
|
||||
*
|
||||
* @param key 键
|
||||
* @param item 项
|
||||
* @param value 值
|
||||
* @return true 成功 false失败
|
||||
*/
|
||||
public boolean hset(String key, String item, Object value) {
|
||||
try {
|
||||
redisTemplate.opsForHash().put(key, item, value);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 向一张hash表中放入数据,如果不存在将创建
|
||||
*
|
||||
* @param key 键
|
||||
* @param item 项
|
||||
* @param value 值
|
||||
* @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
|
||||
* @return true 成功 false失败
|
||||
*/
|
||||
public boolean hset(String key, String item, Object value, long time) {
|
||||
try {
|
||||
redisTemplate.opsForHash().put(key, item, value);
|
||||
if (time > 0) {
|
||||
expire(key, time);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除hash表中的值
|
||||
*
|
||||
* @param key 键 不能为null
|
||||
* @param item 项 可以使多个 不能为null
|
||||
*/
|
||||
public void hdel(String key, Object... item) {
|
||||
redisTemplate.opsForHash().delete(key, item);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断hash表中是否有该项的值
|
||||
*
|
||||
* @param key 键 不能为null
|
||||
* @param item 项 不能为null
|
||||
* @return true 存在 false不存在
|
||||
*/
|
||||
public boolean hHasKey(String key, String item) {
|
||||
return redisTemplate.opsForHash().hasKey(key, item);
|
||||
}
|
||||
|
||||
/**
|
||||
* hash递增 如果不存在,就会创建一个 并把新增后的值返回
|
||||
*
|
||||
* @param key 键
|
||||
* @param item 项
|
||||
* @param by 要增加几(大于0)
|
||||
* @return
|
||||
*/
|
||||
public double hincr(String key, String item, double by) {
|
||||
return redisTemplate.opsForHash().increment(key, item, by);
|
||||
}
|
||||
|
||||
/**
|
||||
* hash递减
|
||||
*
|
||||
* @param key 键
|
||||
* @param item 项
|
||||
* @param by 要减少记(小于0)
|
||||
* @return
|
||||
*/
|
||||
public double hdecr(String key, String item, double by) {
|
||||
return redisTemplate.opsForHash().increment(key, item, -by);
|
||||
}
|
||||
|
||||
// ============================set=============================
|
||||
/**
|
||||
* 根据key获取Set中的所有值
|
||||
*
|
||||
* @param key 键
|
||||
* @return
|
||||
*/
|
||||
public Set<Object> sGet(String key) {
|
||||
try {
|
||||
return redisTemplate.opsForSet().members(key);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据value从一个set中查询,是否存在
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @return true 存在 false不存在
|
||||
*/
|
||||
public boolean sHasKey(String key, Object value) {
|
||||
try {
|
||||
return redisTemplate.opsForSet().isMember(key, value);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将数据放入set缓存
|
||||
*
|
||||
* @param key 键
|
||||
* @param values 值 可以是多个
|
||||
* @return 成功个数
|
||||
*/
|
||||
public long sSet(String key, Object... values) {
|
||||
try {
|
||||
return redisTemplate.opsForSet().add(key, values);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将set数据放入缓存
|
||||
*
|
||||
* @param key 键
|
||||
* @param time 时间(秒)
|
||||
* @param values 值 可以是多个
|
||||
* @return 成功个数
|
||||
*/
|
||||
public long sSetAndTime(String key, long time, Object... values) {
|
||||
try {
|
||||
Long count = redisTemplate.opsForSet().add(key, values);
|
||||
if (time > 0) {
|
||||
expire(key, time);
|
||||
}
|
||||
return count;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取set缓存的长度
|
||||
*
|
||||
* @param key 键
|
||||
* @return
|
||||
*/
|
||||
public long sGetSetSize(String key) {
|
||||
try {
|
||||
return redisTemplate.opsForSet().size(key);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除值为value的
|
||||
*
|
||||
* @param key 键
|
||||
* @param values 值 可以是多个
|
||||
* @return 移除的个数
|
||||
*/
|
||||
public long setRemove(String key, Object... values) {
|
||||
try {
|
||||
Long count = redisTemplate.opsForSet().remove(key, values);
|
||||
return count;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
// ===============================list=================================
|
||||
|
||||
/**
|
||||
* 获取list缓存的内容
|
||||
*
|
||||
* @param key 键
|
||||
* @param start 开始
|
||||
* @param end 结束 0 到 -1代表所有值
|
||||
* @return
|
||||
*/
|
||||
public List<Object> lGet(String key, long start, long end) {
|
||||
try {
|
||||
return redisTemplate.opsForList().range(key, start, end);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取list缓存的长度
|
||||
*
|
||||
* @param key 键
|
||||
* @return
|
||||
*/
|
||||
public long lGetListSize(String key) {
|
||||
try {
|
||||
return redisTemplate.opsForList().size(key);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过索引 获取list中的值
|
||||
*
|
||||
* @param key 键
|
||||
* @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
|
||||
* @return
|
||||
*/
|
||||
public Object lGetIndex(String key, long index) {
|
||||
try {
|
||||
return redisTemplate.opsForList().index(key, index);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将list放入缓存
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param time 时间(秒)
|
||||
* @return
|
||||
*/
|
||||
public boolean lSet(String key, Object value) {
|
||||
try {
|
||||
redisTemplate.opsForList().rightPush(key, value);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将list放入缓存
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param time 时间(秒)
|
||||
* @return
|
||||
*/
|
||||
public boolean lSet(String key, Object value, long time) {
|
||||
try {
|
||||
redisTemplate.opsForList().rightPush(key, value);
|
||||
if (time > 0) {
|
||||
expire(key, time);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将list放入缓存
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param time 时间(秒)
|
||||
* @return
|
||||
*/
|
||||
public boolean lSet(String key, List<Object> value) {
|
||||
try {
|
||||
redisTemplate.opsForList().rightPushAll(key, value);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将list放入缓存
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param time 时间(秒)
|
||||
* @return
|
||||
*/
|
||||
public boolean lSet(String key, List<Object> value, long time) {
|
||||
try {
|
||||
redisTemplate.opsForList().rightPushAll(key, value);
|
||||
if (time > 0) {
|
||||
expire(key, time);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据索引修改list中的某条数据
|
||||
*
|
||||
* @param key 键
|
||||
* @param index 索引
|
||||
* @param value 值
|
||||
* @return
|
||||
*/
|
||||
public boolean lUpdateIndex(String key, long index, Object value) {
|
||||
try {
|
||||
redisTemplate.opsForList().set(key, index, value);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除N个值为value
|
||||
*
|
||||
* @param key 键
|
||||
* @param count 移除多少个
|
||||
* @param value 值
|
||||
* @return 移除的个数
|
||||
*/
|
||||
public long lRemove(String key, long count, Object value) {
|
||||
try {
|
||||
Long remove = redisTemplate.opsForList().remove(key, count, value);
|
||||
return remove;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,84 @@
|
||||
package org.jeecg.common.util;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
@Component
|
||||
public class SpringContextUtils implements ApplicationContextAware {
|
||||
|
||||
/**
|
||||
* 上下文对象实例
|
||||
*/
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取applicationContext
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static ApplicationContext getApplicationContext() {
|
||||
return applicationContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取HttpServletRequest
|
||||
*/
|
||||
public static HttpServletRequest getHttpServletRequest() {
|
||||
return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
||||
}
|
||||
|
||||
public static String getDomain(){
|
||||
HttpServletRequest request = getHttpServletRequest();
|
||||
StringBuffer url = request.getRequestURL();
|
||||
return url.delete(url.length() - request.getRequestURI().length(), url.length()).toString();
|
||||
}
|
||||
|
||||
public static String getOrigin(){
|
||||
HttpServletRequest request = getHttpServletRequest();
|
||||
return request.getHeader("Origin");
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过name获取 Bean.
|
||||
*
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
public static Object getBean(String name) {
|
||||
return getApplicationContext().getBean(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过class获取Bean.
|
||||
*
|
||||
* @param clazz
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
public static <T> T getBean(Class<T> clazz) {
|
||||
return getApplicationContext().getBean(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过name,以及Clazz返回指定的Bean
|
||||
*
|
||||
* @param name
|
||||
* @param clazz
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
public static <T> T getBean(String name, Class<T> clazz) {
|
||||
return getApplicationContext().getBean(name, clazz);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,95 @@
|
||||
package org.jeecg.common.util;
|
||||
|
||||
|
||||
import java.net.InetAddress;
|
||||
|
||||
/**
|
||||
*
|
||||
* @Author 张代浩
|
||||
*
|
||||
*/
|
||||
public class UUIDGenerator {
|
||||
|
||||
|
||||
/**
|
||||
* 产生一个32位的UUID
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
|
||||
public static String generate() {
|
||||
return new StringBuilder(32).append(format(getIP())).append(
|
||||
format(getJVM())).append(format(getHiTime())).append(
|
||||
format(getLoTime())).append(format(getCount())).toString();
|
||||
|
||||
}
|
||||
|
||||
private static final int IP;
|
||||
static {
|
||||
int ipadd;
|
||||
try {
|
||||
ipadd = toInt(InetAddress.getLocalHost().getAddress());
|
||||
} catch (Exception e) {
|
||||
ipadd = 0;
|
||||
}
|
||||
IP = ipadd;
|
||||
}
|
||||
|
||||
private static short counter = (short) 0;
|
||||
|
||||
private static final int JVM = (int) (System.currentTimeMillis() >>> 8);
|
||||
|
||||
private final static String format(int intval) {
|
||||
String formatted = Integer.toHexString(intval);
|
||||
StringBuilder buf = new StringBuilder("00000000");
|
||||
buf.replace(8 - formatted.length(), 8, formatted);
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
private final static String format(short shortval) {
|
||||
String formatted = Integer.toHexString(shortval);
|
||||
StringBuilder buf = new StringBuilder("0000");
|
||||
buf.replace(4 - formatted.length(), 4, formatted);
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
private final static int getJVM() {
|
||||
return JVM;
|
||||
}
|
||||
|
||||
private final static short getCount() {
|
||||
synchronized (UUIDGenerator.class) {
|
||||
if (counter < 0) {
|
||||
counter = 0;
|
||||
}
|
||||
return counter++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unique in a local network
|
||||
*/
|
||||
private final static int getIP() {
|
||||
return IP;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unique down to millisecond
|
||||
*/
|
||||
private final static short getHiTime() {
|
||||
return (short) (System.currentTimeMillis() >>> 32);
|
||||
}
|
||||
|
||||
private final static int getLoTime() {
|
||||
return (int) System.currentTimeMillis();
|
||||
}
|
||||
|
||||
private final static int toInt(byte[] bytes) {
|
||||
int result = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
result = (result << 8) - Byte.MIN_VALUE + (int) bytes[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,173 @@
|
||||
package org.jeecg.common.util;
|
||||
|
||||
import io.netty.util.internal.StringUtil;
|
||||
|
||||
/**
|
||||
* 流水号生成规则(按默认规则递增,数字从1-99开始递增,数字到99,递增字母;位数不够增加位数)
|
||||
* A001
|
||||
* A001A002
|
||||
* @Author zhangdaihao
|
||||
*
|
||||
*/
|
||||
public class YouBianCodeUtil {
|
||||
|
||||
// 数字位数(默认生成3位的数字)
|
||||
|
||||
private static final int numLength = 2;//代表数字位数
|
||||
|
||||
public static final int zhanweiLength = 1+numLength;
|
||||
|
||||
/**
|
||||
* 根据前一个code,获取同级下一个code
|
||||
* 例如:当前最大code为D01A04,下一个code为:D01A05
|
||||
*
|
||||
* @param code
|
||||
* @return
|
||||
*/
|
||||
public static synchronized String getNextYouBianCode(String code) {
|
||||
String newcode = "";
|
||||
if (code == null || code =="") {
|
||||
String zimu = "A";
|
||||
String num = getStrNum(1);
|
||||
newcode = zimu + num;
|
||||
} else {
|
||||
String before_code = code.substring(0, code.length() - 1- numLength);
|
||||
String after_code = code.substring(code.length() - 1 - numLength,code.length());
|
||||
char after_code_zimu = after_code.substring(0, 1).charAt(0);
|
||||
Integer after_code_num = Integer.parseInt(after_code.substring(1));
|
||||
// org.jeecgframework.core.util.LogUtil.info(after_code);
|
||||
// org.jeecgframework.core.util.LogUtil.info(after_code_zimu);
|
||||
// org.jeecgframework.core.util.LogUtil.info(after_code_num);
|
||||
|
||||
String nextNum = "";
|
||||
char nextZimu = 'A';
|
||||
// 先判断数字等于999*,则计数从1重新开始,递增
|
||||
if (after_code_num == getMaxNumByLength(numLength)) {
|
||||
nextNum = getNextStrNum(0);
|
||||
} else {
|
||||
nextNum = getNextStrNum(after_code_num);
|
||||
}
|
||||
// 先判断数字等于999*,则字母从A重新开始,递增
|
||||
if(after_code_num == getMaxNumByLength(numLength)) {
|
||||
nextZimu = getNextZiMu(after_code_zimu);
|
||||
}else{
|
||||
nextZimu = after_code_zimu;
|
||||
}
|
||||
|
||||
// 例如Z99,下一个code就是Z99A01
|
||||
if ('Z' == after_code_zimu && getMaxNumByLength(numLength) == after_code_num) {
|
||||
newcode = code + (nextZimu + nextNum);
|
||||
} else {
|
||||
newcode = before_code + (nextZimu + nextNum);
|
||||
}
|
||||
}
|
||||
return newcode;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据父亲code,获取下级的下一个code
|
||||
*
|
||||
* 例如:父亲CODE:A01
|
||||
* 当前CODE:A01B03
|
||||
* 获取的code:A01B04
|
||||
*
|
||||
* @param parentCode 上级code
|
||||
* @param localCode 同级code
|
||||
* @return
|
||||
*/
|
||||
public static synchronized String getSubYouBianCode(String parentCode,String localCode) {
|
||||
if(localCode!=null && localCode!=""){
|
||||
|
||||
// return parentCode + getNextYouBianCode(localCode);
|
||||
return getNextYouBianCode(localCode);
|
||||
|
||||
}else{
|
||||
parentCode = parentCode + "A"+ getNextStrNum(0);
|
||||
}
|
||||
return parentCode;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 将数字前面位数补零
|
||||
*
|
||||
* @param num
|
||||
* @return
|
||||
*/
|
||||
private static String getNextStrNum(int num) {
|
||||
return getStrNum(getNextNum(num));
|
||||
}
|
||||
|
||||
/**
|
||||
* 将数字前面位数补零
|
||||
*
|
||||
* @param num
|
||||
* @return
|
||||
*/
|
||||
private static String getStrNum(int num) {
|
||||
String s = String.format("%0" + numLength + "d", num);
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递增获取下个数字
|
||||
*
|
||||
* @param num
|
||||
* @return
|
||||
*/
|
||||
private static int getNextNum(int num) {
|
||||
num++;
|
||||
return num;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递增获取下个字母
|
||||
*
|
||||
* @param num
|
||||
* @return
|
||||
*/
|
||||
private static char getNextZiMu(char zimu) {
|
||||
if (zimu == 'Z') {
|
||||
return 'A';
|
||||
}
|
||||
zimu++;
|
||||
return zimu;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据数字位数获取最大值
|
||||
* @param length
|
||||
* @return
|
||||
*/
|
||||
private static int getMaxNumByLength(int length){
|
||||
if(length==0){
|
||||
return 0;
|
||||
}
|
||||
String max_num = "";
|
||||
for (int i=0;i<length;i++){
|
||||
max_num = max_num + "9";
|
||||
}
|
||||
return Integer.parseInt(max_num);
|
||||
}
|
||||
public static String[] cutYouBianCode(String code){
|
||||
if(code==null || StringUtil.isNullOrEmpty(code)){
|
||||
return null;
|
||||
}else{
|
||||
//获取标准长度为numLength+1,截取的数量为code.length/numLength+1
|
||||
int c = code.length()/(numLength+1);
|
||||
String[] cutcode = new String[c];
|
||||
for(int i =0 ; i <c;i++){
|
||||
cutcode[i] = code.substring(0,(i+1)*(numLength+1));
|
||||
}
|
||||
return cutcode;
|
||||
}
|
||||
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
// org.jeecgframework.core.util.LogUtil.info(getNextZiMu('C'));
|
||||
// org.jeecgframework.core.util.LogUtil.info(getNextNum(8));
|
||||
// org.jeecgframework.core.util.LogUtil.info(cutYouBianCode("C99A01B01")[2]);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,157 @@
|
||||
package org.jeecg.common.util.jsonschema;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.jeecg.common.system.vo.DictModel;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
/**
|
||||
* 验证通用属性
|
||||
*/
|
||||
public abstract class CommonProperty implements Serializable{
|
||||
|
||||
private static final long serialVersionUID = -426159949502493187L;
|
||||
|
||||
|
||||
protected String key;
|
||||
|
||||
|
||||
/**
|
||||
* <p>此关键字的值必须是字符串或数组。如果它是一个数组,那么数组的元素必须是字符串,并且必须是唯一的。
|
||||
* <p>字符串值必须是六种基本类型之一(“null”,“boolean”,“object”,“array”,“number”或“string”),或“integer”,它匹配任何数字,零分数部分。
|
||||
* <p>当且仅当实例位于为此关键字列出的任何集合中时,实例才会验证。
|
||||
*
|
||||
*/
|
||||
protected String type;
|
||||
|
||||
/**
|
||||
* 对应JsonSchema的enum
|
||||
* <p>该关键字的值必须是一个数组。这个数组应该至少有一个元素。数组中的元素应该是唯一的。如果实例的值等于此关键字的数组值中的某个元素,则实例将对此关键字成功验证。
|
||||
* 数组中的元素可以是任何值,包括null
|
||||
*
|
||||
* {
|
||||
* "type": "string",
|
||||
* "enum": ["1", "2", "3"] 需要的话可以通过这个include转一下
|
||||
* }
|
||||
*/
|
||||
protected List<DictModel> include;
|
||||
|
||||
/**
|
||||
* 对应JsonSchema的const
|
||||
* <p>此关键字的值可以是任何类型,包括null。
|
||||
* 如果实例的值等于关键字的值,则实例将针对此关键字成功验证。
|
||||
*/
|
||||
protected Object constant;
|
||||
|
||||
//三个自定义 属性
|
||||
protected String view;// 展示类型
|
||||
protected String title;//数据库字段备注
|
||||
protected Integer order;//字段显示排序
|
||||
|
||||
protected boolean disabled;//是否禁用
|
||||
|
||||
public boolean isDisabled() {
|
||||
return disabled;
|
||||
}
|
||||
|
||||
public void setDisabled(boolean disabled) {
|
||||
this.disabled = disabled;
|
||||
}
|
||||
|
||||
public String getView() {
|
||||
return view;
|
||||
}
|
||||
|
||||
public void setView(String view) {
|
||||
this.view = view;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public List<DictModel> getInclude() {
|
||||
return include;
|
||||
}
|
||||
|
||||
public void setInclude(List<DictModel> include) {
|
||||
this.include = include;
|
||||
}
|
||||
|
||||
public Object getConstant() {
|
||||
return constant;
|
||||
}
|
||||
|
||||
public void setConstant(Object constant) {
|
||||
this.constant = constant;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Integer getOrder() {
|
||||
return order;
|
||||
}
|
||||
|
||||
public void setOrder(Integer order) {
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回一个map有两个key
|
||||
* <P>key ---> Property JSON的key
|
||||
* <P>prop --> JSON object
|
||||
* @return
|
||||
*/
|
||||
public abstract Map<String,Object> getPropertyJson();
|
||||
|
||||
public JSONObject getCommonJson() {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("type", type);
|
||||
if(include!=null && include.size()>0) {
|
||||
json.put("enum", include);
|
||||
}
|
||||
if(constant!=null) {
|
||||
json.put("const", constant);
|
||||
}
|
||||
if(title!=null) {
|
||||
json.put("title", title);
|
||||
}
|
||||
if(order!=null) {
|
||||
json.put("order", order);
|
||||
}
|
||||
if(view==null) {
|
||||
json.put("view", "input");
|
||||
}else {
|
||||
json.put("view", view);
|
||||
}
|
||||
if(disabled) {
|
||||
String str = "{\"widgetattrs\":{\"disabled\":true}}";
|
||||
JSONObject ui = JSONObject.parseObject(str);
|
||||
json.put("ui", ui);
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,87 @@
|
||||
package org.jeecg.common.util.jsonschema;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* JsonSchema 模式类
|
||||
* < http://json-schema.org/draft-07/schema# >
|
||||
*/
|
||||
public class JsonSchemaDescrip implements Serializable{
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 7682073117441544718L;
|
||||
|
||||
|
||||
private String $schema = "http://json-schema.org/draft-07/schema#";
|
||||
|
||||
/**
|
||||
* 用它给我们的模式提供了标题。
|
||||
*/
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 关于模式的描述。
|
||||
*/
|
||||
private String description;
|
||||
|
||||
/**
|
||||
*type 关键字在我们的 JSON 数据上定义了第一个约束:必须是一个 JSON 对象。 可以直接设置成object
|
||||
*/
|
||||
private String type;
|
||||
|
||||
private List<String> required;
|
||||
|
||||
|
||||
public List<String> getRequired() {
|
||||
return required;
|
||||
}
|
||||
|
||||
public void setRequired(List<String> required) {
|
||||
this.required = required;
|
||||
}
|
||||
|
||||
public String get$schema() {
|
||||
return $schema;
|
||||
}
|
||||
|
||||
public void set$schema(String $schema) {
|
||||
this.$schema = $schema;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public JsonSchemaDescrip() {}
|
||||
|
||||
public JsonSchemaDescrip(List<String> required) {
|
||||
this.description="我是一个jsonschema description";
|
||||
this.title="我是一个jsonschema title";
|
||||
this.type="object";
|
||||
this.required = required;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,70 @@
|
||||
package org.jeecg.common.util.jsonschema;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class JsonschemaUtil {
|
||||
|
||||
/**
|
||||
* 生成JsonSchema
|
||||
*
|
||||
* @param descrip
|
||||
* @param propertyList
|
||||
* @return
|
||||
*/
|
||||
public static JSONObject getJsonSchema(JsonSchemaDescrip descrip, List<CommonProperty> propertyList) {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("$schema", descrip.get$schema());
|
||||
obj.put("type", descrip.getType());
|
||||
obj.put("title", descrip.getTitle());
|
||||
|
||||
List<String> requiredArr = descrip.getRequired();
|
||||
obj.put("required", requiredArr);
|
||||
|
||||
JSONObject properties = new JSONObject();
|
||||
for (CommonProperty commonProperty : propertyList) {
|
||||
Map<String, Object> map = commonProperty.getPropertyJson();
|
||||
properties.put(map.get("key").toString(), map.get("prop"));
|
||||
}
|
||||
obj.put("properties", properties);
|
||||
//鬼知道这里为什么报错 org.jeecg.modules.system.model.DictModel cannot be cast to org.jeecg.modules.system.model.DictModel
|
||||
//log.info("---JSONSchema--->"+obj.toJSONString());
|
||||
return obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成JsonSchema 用于子对象
|
||||
* @param title 子对象描述
|
||||
* @param requiredArr 子对象必填属性名集合
|
||||
* @param propertyList 子对象属性集合
|
||||
* @return
|
||||
*/
|
||||
public static JSONObject getSubJsonSchema(String title,List<String> requiredArr,List<CommonProperty> propertyList) {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("type", "object");
|
||||
obj.put("view", "tab");
|
||||
obj.put("title", title);
|
||||
|
||||
if(requiredArr==null) {
|
||||
requiredArr = new ArrayList<String>();
|
||||
}
|
||||
obj.put("required", requiredArr);
|
||||
|
||||
JSONObject properties = new JSONObject();
|
||||
for (CommonProperty commonProperty : propertyList) {
|
||||
Map<String, Object> map = commonProperty.getPropertyJson();
|
||||
properties.put(map.get("key").toString(), map.get("prop"));
|
||||
}
|
||||
obj.put("properties", properties);
|
||||
//log.info("---JSONSchema--->"+obj.toString());
|
||||
return obj;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,140 @@
|
||||
package org.jeecg.common.util.jsonschema.validate;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.jeecg.common.system.vo.DictModel;
|
||||
import org.jeecg.common.util.jsonschema.CommonProperty;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
public class NumberProperty extends CommonProperty {
|
||||
|
||||
private static final long serialVersionUID = -558615331436437200L;
|
||||
|
||||
/**
|
||||
* 倍数
|
||||
* 验证实例是否为此数值的倍数
|
||||
* “multipleOf”的值必须是一个数字,严格大于0。
|
||||
*/
|
||||
private Integer multipleOf;
|
||||
|
||||
/**
|
||||
* 小于等于
|
||||
* “maximum”的值必须是一个数字,表示数字实例的包含上限。
|
||||
* 如果实例是数字,则仅当实例小于或等于“最大”时,此关键字才会生效。
|
||||
*/
|
||||
private Integer maxinum;
|
||||
|
||||
/**
|
||||
* 小于
|
||||
* “exclusiveMaximum”的值必须是数字,表示数字实例的独占上限。
|
||||
* 如果实例是数字,则实例仅在其值严格小于(不等于)“exclusiveMaximum”时才有效。
|
||||
*/
|
||||
private Integer exclusiveMaximum;
|
||||
|
||||
/**
|
||||
* 大于等于
|
||||
*/
|
||||
private Integer minimum;
|
||||
|
||||
/**
|
||||
* 大于等于
|
||||
*/
|
||||
private Integer exclusiveMinimum;
|
||||
|
||||
public Integer getMultipleOf() {
|
||||
return multipleOf;
|
||||
}
|
||||
|
||||
public void setMultipleOf(Integer multipleOf) {
|
||||
this.multipleOf = multipleOf;
|
||||
}
|
||||
|
||||
public Integer getMaxinum() {
|
||||
return maxinum;
|
||||
}
|
||||
|
||||
public void setMaxinum(Integer maxinum) {
|
||||
this.maxinum = maxinum;
|
||||
}
|
||||
|
||||
public Integer getExclusiveMaximum() {
|
||||
return exclusiveMaximum;
|
||||
}
|
||||
|
||||
public void setExclusiveMaximum(Integer exclusiveMaximum) {
|
||||
this.exclusiveMaximum = exclusiveMaximum;
|
||||
}
|
||||
|
||||
public Integer getMinimum() {
|
||||
return minimum;
|
||||
}
|
||||
|
||||
public void setMinimum(Integer minimum) {
|
||||
this.minimum = minimum;
|
||||
}
|
||||
|
||||
public Integer getExclusiveMinimum() {
|
||||
return exclusiveMinimum;
|
||||
}
|
||||
|
||||
public void setExclusiveMinimum(Integer exclusiveMinimum) {
|
||||
this.exclusiveMinimum = exclusiveMinimum;
|
||||
}
|
||||
|
||||
public NumberProperty() {}
|
||||
|
||||
/**
|
||||
* 构造器
|
||||
* @param key 字段名
|
||||
* @param title 字段备注
|
||||
* @param type number和integer
|
||||
*/
|
||||
public NumberProperty(String key,String title,String type) {
|
||||
this.key = key;
|
||||
this.type = type;
|
||||
this.title = title;
|
||||
this.view = "number";
|
||||
}
|
||||
|
||||
/**
|
||||
* 列表类型的走这个构造器 字典里存储的都是字符串 没法走这个构造器
|
||||
* @param key
|
||||
* @param type
|
||||
* @param view list-checkbox-radio
|
||||
* @param include
|
||||
*/
|
||||
public NumberProperty(String key,String title,String view,List<DictModel> include) {
|
||||
this.type = "integer";
|
||||
this.key = key;
|
||||
this.view = view;
|
||||
this.title = title;
|
||||
this.include = include;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String,Object> getPropertyJson() {
|
||||
Map<String,Object> map = new HashMap<>();
|
||||
map.put("key",getKey());
|
||||
JSONObject prop = getCommonJson();
|
||||
if(multipleOf!=null) {
|
||||
prop.put("multipleOf",multipleOf);
|
||||
}
|
||||
if(maxinum!=null) {
|
||||
prop.put("maxinum",maxinum);
|
||||
}
|
||||
if(exclusiveMaximum!=null) {
|
||||
prop.put("exclusiveMaximum",exclusiveMaximum);
|
||||
}
|
||||
if(minimum!=null) {
|
||||
prop.put("minimum",minimum);
|
||||
}
|
||||
if(exclusiveMinimum!=null) {
|
||||
prop.put("exclusiveMinimum",exclusiveMinimum);
|
||||
}
|
||||
map.put("prop",prop);
|
||||
return map;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,76 @@
|
||||
package org.jeecg.common.util.jsonschema.validate;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.jeecg.common.util.jsonschema.CommonProperty;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
public class PopupProperty extends CommonProperty {
|
||||
|
||||
private static final long serialVersionUID = -3200493311633999539L;
|
||||
|
||||
private String code;
|
||||
|
||||
private String destFields;
|
||||
|
||||
private String orgFields;
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getDestFields() {
|
||||
return destFields;
|
||||
}
|
||||
|
||||
public void setDestFields(String destFields) {
|
||||
this.destFields = destFields;
|
||||
}
|
||||
|
||||
public String getOrgFields() {
|
||||
return orgFields;
|
||||
}
|
||||
|
||||
public void setOrgFields(String orgFields) {
|
||||
this.orgFields = orgFields;
|
||||
}
|
||||
|
||||
public PopupProperty() {}
|
||||
|
||||
public PopupProperty(String key,String title,String code,String destFields,String orgFields) {
|
||||
this.view = "popup";
|
||||
this.type = "string";
|
||||
this.key = key;
|
||||
this.title = title;
|
||||
this.code = code;
|
||||
this.destFields=destFields;
|
||||
this.orgFields=orgFields;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getPropertyJson() {
|
||||
Map<String,Object> map = new HashMap<>();
|
||||
map.put("key",getKey());
|
||||
JSONObject prop = getCommonJson();
|
||||
if(code!=null) {
|
||||
prop.put("code",code);
|
||||
}
|
||||
if(destFields!=null) {
|
||||
prop.put("destFields",destFields);
|
||||
}
|
||||
if(orgFields!=null) {
|
||||
prop.put("orgFields",orgFields);
|
||||
}
|
||||
map.put("prop",prop);
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,101 @@
|
||||
package org.jeecg.common.util.jsonschema.validate;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.jeecg.common.system.vo.DictModel;
|
||||
import org.jeecg.common.util.jsonschema.CommonProperty;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
public class StringProperty extends CommonProperty {
|
||||
|
||||
private static final long serialVersionUID = -3200493311633999539L;
|
||||
|
||||
private Integer maxLength;
|
||||
|
||||
private Integer minLength;
|
||||
|
||||
/**
|
||||
* 根据ECMA 262正则表达式方言,该字符串应该是有效的正则表达式。
|
||||
*/
|
||||
private String pattern;
|
||||
|
||||
public Integer getMaxLength() {
|
||||
return maxLength;
|
||||
}
|
||||
|
||||
|
||||
public void setMaxLength(Integer maxLength) {
|
||||
this.maxLength = maxLength;
|
||||
}
|
||||
|
||||
public Integer getMinLength() {
|
||||
return minLength;
|
||||
}
|
||||
|
||||
public void setMinLength(Integer minLength) {
|
||||
this.minLength = minLength;
|
||||
}
|
||||
|
||||
public String getPattern() {
|
||||
return pattern;
|
||||
}
|
||||
|
||||
public void setPattern(String pattern) {
|
||||
this.pattern = pattern;
|
||||
}
|
||||
|
||||
public StringProperty() {}
|
||||
|
||||
/**
|
||||
* 一般字符串类型走这个构造器
|
||||
* @param key 字段名
|
||||
* @param title 字段备注
|
||||
* @param view 展示控件
|
||||
* @param maxLength 数据库字段最大长度
|
||||
*/
|
||||
public StringProperty(String key,String title,String view,Integer maxLength) {
|
||||
this.maxLength = maxLength;
|
||||
this.key = key;
|
||||
this.view = view;
|
||||
this.title = title;
|
||||
this.type = "string";
|
||||
}
|
||||
|
||||
/**
|
||||
* 列表类型的走这个构造器
|
||||
* @param key 字段名
|
||||
* @param title 字段备注
|
||||
* @param view 展示控件 list-checkbox-radio
|
||||
* @param maxLength 数据库字段最大长度
|
||||
* @param include 数据字典
|
||||
*/
|
||||
public StringProperty(String key,String title,String view,Integer maxLength,List<DictModel> include) {
|
||||
this.maxLength = maxLength;
|
||||
this.key = key;
|
||||
this.view = view;
|
||||
this.title = title;
|
||||
this.type = "string";
|
||||
this.include = include;
|
||||
}
|
||||
@Override
|
||||
public Map<String, Object> getPropertyJson() {
|
||||
Map<String,Object> map = new HashMap<>();
|
||||
map.put("key",getKey());
|
||||
JSONObject prop = getCommonJson();
|
||||
if(maxLength!=null) {
|
||||
prop.put("maxLength",maxLength);
|
||||
}
|
||||
if(minLength!=null) {
|
||||
prop.put("minLength",minLength);
|
||||
}
|
||||
if(pattern!=null) {
|
||||
prop.put("pattern",pattern);
|
||||
}
|
||||
map.put("prop",prop);
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,572 @@
|
||||
package org.jeecg.common.util;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.sql.Date;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
*
|
||||
* @Author 张代浩
|
||||
*
|
||||
*/
|
||||
public class oConvertUtils {
|
||||
public static boolean isEmpty(Object object) {
|
||||
if (object == null) {
|
||||
return (true);
|
||||
}
|
||||
if ("".equals(object)) {
|
||||
return (true);
|
||||
}
|
||||
if ("null".equals(object)) {
|
||||
return (true);
|
||||
}
|
||||
return (false);
|
||||
}
|
||||
|
||||
public static boolean isNotEmpty(Object object) {
|
||||
if (object != null && !object.equals("") && !object.equals("null")) {
|
||||
return (true);
|
||||
}
|
||||
return (false);
|
||||
}
|
||||
|
||||
public static String decode(String strIn, String sourceCode, String targetCode) {
|
||||
String temp = code2code(strIn, sourceCode, targetCode);
|
||||
return temp;
|
||||
}
|
||||
|
||||
public static String StrToUTF(String strIn, String sourceCode, String targetCode) {
|
||||
strIn = "";
|
||||
try {
|
||||
strIn = new String(strIn.getBytes("ISO-8859-1"), "GBK");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return strIn;
|
||||
|
||||
}
|
||||
|
||||
private static String code2code(String strIn, String sourceCode, String targetCode) {
|
||||
String strOut = null;
|
||||
if (strIn == null || (strIn.trim()).equals("")) {
|
||||
return strIn;
|
||||
}
|
||||
try {
|
||||
byte[] b = strIn.getBytes(sourceCode);
|
||||
for (int i = 0; i < b.length; i++) {
|
||||
System.out.print(b[i] + " ");
|
||||
}
|
||||
strOut = new String(b, targetCode);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
return strOut;
|
||||
}
|
||||
|
||||
public static int getInt(String s, int defval) {
|
||||
if (s == null || s == "") {
|
||||
return (defval);
|
||||
}
|
||||
try {
|
||||
return (Integer.parseInt(s));
|
||||
} catch (NumberFormatException e) {
|
||||
return (defval);
|
||||
}
|
||||
}
|
||||
|
||||
public static int getInt(String s) {
|
||||
if (s == null || s == "") {
|
||||
return 0;
|
||||
}
|
||||
try {
|
||||
return (Integer.parseInt(s));
|
||||
} catch (NumberFormatException e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static int getInt(String s, Integer df) {
|
||||
if (s == null || s == "") {
|
||||
return df;
|
||||
}
|
||||
try {
|
||||
return (Integer.parseInt(s));
|
||||
} catch (NumberFormatException e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static Integer[] getInts(String[] s) {
|
||||
Integer[] integer = new Integer[s.length];
|
||||
if (s == null) {
|
||||
return null;
|
||||
}
|
||||
for (int i = 0; i < s.length; i++) {
|
||||
integer[i] = Integer.parseInt(s[i]);
|
||||
}
|
||||
return integer;
|
||||
|
||||
}
|
||||
|
||||
public static double getDouble(String s, double defval) {
|
||||
if (s == null || s == "") {
|
||||
return (defval);
|
||||
}
|
||||
try {
|
||||
return (Double.parseDouble(s));
|
||||
} catch (NumberFormatException e) {
|
||||
return (defval);
|
||||
}
|
||||
}
|
||||
|
||||
public static double getDou(Double s, double defval) {
|
||||
if (s == null) {
|
||||
return (defval);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
/*public static Short getShort(String s) {
|
||||
if (StringUtil.isNotEmpty(s)) {
|
||||
return (Short.parseShort(s));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}*/
|
||||
|
||||
public static int getInt(Object object, int defval) {
|
||||
if (isEmpty(object)) {
|
||||
return (defval);
|
||||
}
|
||||
try {
|
||||
return (Integer.parseInt(object.toString()));
|
||||
} catch (NumberFormatException e) {
|
||||
return (defval);
|
||||
}
|
||||
}
|
||||
|
||||
public static Integer getInt(Object object) {
|
||||
if (isEmpty(object)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return (Integer.parseInt(object.toString()));
|
||||
} catch (NumberFormatException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static int getInt(BigDecimal s, int defval) {
|
||||
if (s == null) {
|
||||
return (defval);
|
||||
}
|
||||
return s.intValue();
|
||||
}
|
||||
|
||||
public static Integer[] getIntegerArry(String[] object) {
|
||||
int len = object.length;
|
||||
Integer[] result = new Integer[len];
|
||||
try {
|
||||
for (int i = 0; i < len; i++) {
|
||||
result[i] = new Integer(object[i].trim());
|
||||
}
|
||||
return result;
|
||||
} catch (NumberFormatException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String getString(String s) {
|
||||
return (getString(s, ""));
|
||||
}
|
||||
|
||||
/**
|
||||
* 转义成Unicode编码
|
||||
* @param s
|
||||
* @return
|
||||
*/
|
||||
/*public static String escapeJava(Object s) {
|
||||
return StringEscapeUtils.escapeJava(getString(s));
|
||||
}*/
|
||||
|
||||
public static String getString(Object object) {
|
||||
if (isEmpty(object)) {
|
||||
return "";
|
||||
}
|
||||
return (object.toString().trim());
|
||||
}
|
||||
|
||||
public static String getString(int i) {
|
||||
return (String.valueOf(i));
|
||||
}
|
||||
|
||||
public static String getString(float i) {
|
||||
return (String.valueOf(i));
|
||||
}
|
||||
|
||||
public static String getString(String s, String defval) {
|
||||
if (isEmpty(s)) {
|
||||
return (defval);
|
||||
}
|
||||
return (s.trim());
|
||||
}
|
||||
|
||||
public static String getString(Object s, String defval) {
|
||||
if (isEmpty(s)) {
|
||||
return (defval);
|
||||
}
|
||||
return (s.toString().trim());
|
||||
}
|
||||
|
||||
public static long stringToLong(String str) {
|
||||
Long test = new Long(0);
|
||||
try {
|
||||
test = Long.valueOf(str);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
return test.longValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本机IP
|
||||
*/
|
||||
public static String getIp() {
|
||||
String ip = null;
|
||||
try {
|
||||
InetAddress address = InetAddress.getLocalHost();
|
||||
ip = address.getHostAddress();
|
||||
|
||||
} catch (UnknownHostException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断一个类是否为基本数据类型。
|
||||
*
|
||||
* @param clazz
|
||||
* 要判断的类。
|
||||
* @return true 表示为基本数据类型。
|
||||
*/
|
||||
private static boolean isBaseDataType(Class clazz) throws Exception {
|
||||
return (clazz.equals(String.class) || clazz.equals(Integer.class) || clazz.equals(Byte.class) || clazz.equals(Long.class) || clazz.equals(Double.class) || clazz.equals(Float.class) || clazz.equals(Character.class) || clazz.equals(Short.class) || clazz.equals(BigDecimal.class) || clazz.equals(BigInteger.class) || clazz.equals(Boolean.class) || clazz.equals(Date.class) || clazz.isPrimitive());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param request
|
||||
* IP
|
||||
* @return IP Address
|
||||
*/
|
||||
public static String getIpAddrByRequest(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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 本机IP
|
||||
* @throws SocketException
|
||||
*/
|
||||
public static String getRealIp() throws SocketException {
|
||||
String localip = null;// 本地IP,如果没有配置外网IP则返回它
|
||||
String netip = null;// 外网IP
|
||||
|
||||
Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
|
||||
InetAddress ip = null;
|
||||
boolean finded = false;// 是否找到外网IP
|
||||
while (netInterfaces.hasMoreElements() && !finded) {
|
||||
NetworkInterface ni = netInterfaces.nextElement();
|
||||
Enumeration<InetAddress> address = ni.getInetAddresses();
|
||||
while (address.hasMoreElements()) {
|
||||
ip = address.nextElement();
|
||||
if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {// 外网IP
|
||||
netip = ip.getHostAddress();
|
||||
finded = true;
|
||||
break;
|
||||
} else if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {// 内网IP
|
||||
localip = ip.getHostAddress();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (netip != null && !"".equals(netip)) {
|
||||
return netip;
|
||||
} else {
|
||||
return localip;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* java去除字符串中的空格、回车、换行符、制表符
|
||||
*
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public static String replaceBlank(String str) {
|
||||
String dest = "";
|
||||
if (str != null) {
|
||||
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
|
||||
Matcher m = p.matcher(str);
|
||||
dest = m.replaceAll("");
|
||||
}
|
||||
return dest;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断元素是否在数组内
|
||||
*
|
||||
* @param substring
|
||||
* @param source
|
||||
* @return
|
||||
*/
|
||||
public static boolean isIn(String substring, String[] source) {
|
||||
if (source == null || source.length == 0) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < source.length; i++) {
|
||||
String aSource = source[i];
|
||||
if (aSource.equals(substring)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Map对象
|
||||
*/
|
||||
public static Map<Object, Object> getHashMap() {
|
||||
return new HashMap<Object, Object>();
|
||||
}
|
||||
|
||||
/**
|
||||
* SET转换MAP
|
||||
*
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public static Map<Object, Object> SetToMap(Set<Object> setobj) {
|
||||
Map<Object, Object> map = getHashMap();
|
||||
for (Iterator iterator = setobj.iterator(); iterator.hasNext();) {
|
||||
Map.Entry<Object, Object> entry = (Map.Entry<Object, Object>) iterator.next();
|
||||
map.put(entry.getKey().toString(), entry.getValue() == null ? "" : entry.getValue().toString().trim());
|
||||
}
|
||||
return map;
|
||||
|
||||
}
|
||||
|
||||
public static boolean isInnerIP(String ipAddress) {
|
||||
boolean isInnerIp = false;
|
||||
long ipNum = getIpNum(ipAddress);
|
||||
/**
|
||||
* 私有IP:A类 10.0.0.0-10.255.255.255 B类 172.16.0.0-172.31.255.255 C类 192.168.0.0-192.168.255.255 当然,还有127这个网段是环回地址
|
||||
**/
|
||||
long aBegin = getIpNum("10.0.0.0");
|
||||
long aEnd = getIpNum("10.255.255.255");
|
||||
long bBegin = getIpNum("172.16.0.0");
|
||||
long bEnd = getIpNum("172.31.255.255");
|
||||
long cBegin = getIpNum("192.168.0.0");
|
||||
long cEnd = getIpNum("192.168.255.255");
|
||||
isInnerIp = isInner(ipNum, aBegin, aEnd) || isInner(ipNum, bBegin, bEnd) || isInner(ipNum, cBegin, cEnd) || ipAddress.equals("127.0.0.1");
|
||||
return isInnerIp;
|
||||
}
|
||||
|
||||
private static long getIpNum(String ipAddress) {
|
||||
String[] ip = ipAddress.split("\\.");
|
||||
long a = Integer.parseInt(ip[0]);
|
||||
long b = Integer.parseInt(ip[1]);
|
||||
long c = Integer.parseInt(ip[2]);
|
||||
long d = Integer.parseInt(ip[3]);
|
||||
|
||||
long ipNum = a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d;
|
||||
return ipNum;
|
||||
}
|
||||
|
||||
private static boolean isInner(long userIp, long begin, long end) {
|
||||
return (userIp >= begin) && (userIp <= end);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将下划线大写方式命名的字符串转换为驼峰式。
|
||||
* 如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。</br>
|
||||
* 例如:hello_world->helloWorld
|
||||
*
|
||||
* @param name
|
||||
* 转换前的下划线大写方式命名的字符串
|
||||
* @return 转换后的驼峰式命名的字符串
|
||||
*/
|
||||
public static String camelName(String name) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
// 快速检查
|
||||
if (name == null || name.isEmpty()) {
|
||||
// 没必要转换
|
||||
return "";
|
||||
} else if (!name.contains("_")) {
|
||||
// 不含下划线,仅将首字母小写
|
||||
//update-begin--Author:zhoujf Date:20180503 for:TASK #2500 【代码生成器】代码生成器开发一通用模板生成功能
|
||||
//update-begin--Author:zhoujf Date:20180503 for:TASK #2500 【代码生成器】代码生成器开发一通用模板生成功能
|
||||
return name.substring(0, 1).toLowerCase() + name.substring(1).toLowerCase();
|
||||
//update-end--Author:zhoujf Date:20180503 for:TASK #2500 【代码生成器】代码生成器开发一通用模板生成功能
|
||||
}
|
||||
// 用下划线将原始字符串分割
|
||||
String camels[] = name.split("_");
|
||||
for (String camel : camels) {
|
||||
// 跳过原始字符串中开头、结尾的下换线或双重下划线
|
||||
if (camel.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
// 处理真正的驼峰片段
|
||||
if (result.length() == 0) {
|
||||
// 第一个驼峰片段,全部字母都小写
|
||||
result.append(camel.toLowerCase());
|
||||
} else {
|
||||
// 其他的驼峰片段,首字母大写
|
||||
result.append(camel.substring(0, 1).toUpperCase());
|
||||
result.append(camel.substring(1).toLowerCase());
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将下划线大写方式命名的字符串转换为驼峰式。
|
||||
* 如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。</br>
|
||||
* 例如:hello_world,test_id->helloWorld,testId
|
||||
*
|
||||
* @param name
|
||||
* 转换前的下划线大写方式命名的字符串
|
||||
* @return 转换后的驼峰式命名的字符串
|
||||
*/
|
||||
public static String camelNames(String names) {
|
||||
if(names==null||names.equals("")){
|
||||
return null;
|
||||
}
|
||||
StringBuffer sf = new StringBuffer();
|
||||
String[] fs = names.split(",");
|
||||
for (String field : fs) {
|
||||
field = camelName(field);
|
||||
sf.append(field + ",");
|
||||
}
|
||||
String result = sf.toString();
|
||||
return result.substring(0, result.length() - 1);
|
||||
}
|
||||
|
||||
//update-begin--Author:zhoujf Date:20180503 for:TASK #2500 【代码生成器】代码生成器开发一通用模板生成功能
|
||||
/**
|
||||
* 将下划线大写方式命名的字符串转换为驼峰式。(首字母写)
|
||||
* 如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。</br>
|
||||
* 例如:hello_world->HelloWorld
|
||||
*
|
||||
* @param name
|
||||
* 转换前的下划线大写方式命名的字符串
|
||||
* @return 转换后的驼峰式命名的字符串
|
||||
*/
|
||||
public static String camelNameCapFirst(String name) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
// 快速检查
|
||||
if (name == null || name.isEmpty()) {
|
||||
// 没必要转换
|
||||
return "";
|
||||
} else if (!name.contains("_")) {
|
||||
// 不含下划线,仅将首字母小写
|
||||
return name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
|
||||
}
|
||||
// 用下划线将原始字符串分割
|
||||
String camels[] = name.split("_");
|
||||
for (String camel : camels) {
|
||||
// 跳过原始字符串中开头、结尾的下换线或双重下划线
|
||||
if (camel.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
// 其他的驼峰片段,首字母大写
|
||||
result.append(camel.substring(0, 1).toUpperCase());
|
||||
result.append(camel.substring(1).toLowerCase());
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
//update-end--Author:zhoujf Date:20180503 for:TASK #2500 【代码生成器】代码生成器开发一通用模板生成功能
|
||||
|
||||
/**
|
||||
* 将驼峰命名转化成下划线
|
||||
* @param para
|
||||
* @return
|
||||
*/
|
||||
public static String camelToUnderline(String para){
|
||||
if(para.length()<3){
|
||||
return para.toLowerCase();
|
||||
}
|
||||
StringBuilder sb=new StringBuilder(para);
|
||||
int temp=0;//定位
|
||||
//从第三个字符开始 避免命名不规范
|
||||
for(int i=2;i<para.length();i++){
|
||||
if(Character.isUpperCase(para.charAt(i))){
|
||||
sb.insert(i+temp, "_");
|
||||
temp+=1;
|
||||
}
|
||||
}
|
||||
return sb.toString().toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机数
|
||||
* @param place 定义随机数的位数
|
||||
*/
|
||||
public static String randomGen(int place) {
|
||||
String base = "qwertyuioplkjhgfdsazxcvbnmQAZWSXEDCRFVTGBYHNUJMIKLOP0123456789";
|
||||
StringBuffer sb = new StringBuffer();
|
||||
Random rd = new Random();
|
||||
for(int i=0;i<place;i++) {
|
||||
sb.append(base.charAt(rd.nextInt(base.length())));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取类的所有属性,包括父类
|
||||
*
|
||||
* @param object
|
||||
* @return
|
||||
*/
|
||||
public static Field[] getAllFields(Object object) {
|
||||
Class<?> clazz = object.getClass();
|
||||
List<Field> fieldList = new ArrayList<>();
|
||||
while (clazz != null) {
|
||||
fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
|
||||
clazz = clazz.getSuperclass();
|
||||
}
|
||||
Field[] fields = new Field[fieldList.size()];
|
||||
fieldList.toArray(fields);
|
||||
return fields;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,60 @@
|
||||
package org.jeecg.common.util.superSearch;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
|
||||
/**
|
||||
* 判断类型,追加查询规则
|
||||
*
|
||||
* @Author Scott
|
||||
* @Date 2019年02月14日
|
||||
*/
|
||||
public class ObjectParseUtil {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param queryWrapper QueryWrapper
|
||||
* @param name 字段名字
|
||||
* @param rule 查询规则
|
||||
* @param value 查询条件值
|
||||
*/
|
||||
public static void addCriteria(QueryWrapper<?> queryWrapper, String name, QueryRuleEnum rule, Object value) {
|
||||
if (value == null || rule == null) {
|
||||
return;
|
||||
}
|
||||
switch (rule) {
|
||||
case GT:
|
||||
queryWrapper.gt(name, value);
|
||||
break;
|
||||
case GE:
|
||||
queryWrapper.ge(name, value);
|
||||
break;
|
||||
case LT:
|
||||
queryWrapper.lt(name, value);
|
||||
break;
|
||||
case LE:
|
||||
queryWrapper.le(name, value);
|
||||
break;
|
||||
case EQ:
|
||||
queryWrapper.eq(name, value);
|
||||
break;
|
||||
case NE:
|
||||
queryWrapper.ne(name, value);
|
||||
break;
|
||||
case IN:
|
||||
queryWrapper.in(name, (Object[]) value);
|
||||
break;
|
||||
case LIKE:
|
||||
queryWrapper.like(name, value);
|
||||
break;
|
||||
case LEFT_LIKE:
|
||||
queryWrapper.likeLeft(name, value);
|
||||
break;
|
||||
case RIGHT_LIKE:
|
||||
queryWrapper.likeRight(name, value);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,60 @@
|
||||
package org.jeecg.common.util.superSearch;
|
||||
|
||||
import org.jeecg.common.util.oConvertUtils;
|
||||
|
||||
/**
|
||||
* Query 规则 常量
|
||||
* @Author Scott
|
||||
* @Date 2019年02月14日
|
||||
*/
|
||||
public enum QueryRuleEnum {
|
||||
|
||||
GT(">","大于"),
|
||||
GE(">=","大于等于"),
|
||||
LT("<","小于"),
|
||||
LE("<=","小于等于"),
|
||||
EQ("=","等于"),
|
||||
NE("!=","不等于"),
|
||||
IN("IN","包含"),
|
||||
LIKE("LIKE","全模糊"),
|
||||
LEFT_LIKE("LEFT_LIKE","左模糊"),
|
||||
RIGHT_LIKE("RIGHT_LIKE","右模糊"),
|
||||
SQL_RULES("EXTEND_SQL","自定义SQL片段");
|
||||
|
||||
private String value;
|
||||
|
||||
private String msg;
|
||||
|
||||
QueryRuleEnum(String value, String msg){
|
||||
this.value = value;
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
public void setMsg(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public static QueryRuleEnum getByValue(String value){
|
||||
if(oConvertUtils.isEmpty(value)) {
|
||||
return null;
|
||||
}
|
||||
for(QueryRuleEnum val :values()){
|
||||
if (val.getValue().equals(value)){
|
||||
return val;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
package org.jeecg.common.util.superSearch;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class QueryRuleVo {
|
||||
|
||||
private String field;
|
||||
private String rule;
|
||||
private String val;
|
||||
}
|
||||
@ -0,0 +1,78 @@
|
||||
package org.jeecg.modules.system.entity;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 菜单权限规则表
|
||||
* </p>
|
||||
*
|
||||
* @Author huangzhilin
|
||||
* @since 2019-03-29
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Accessors(chain = true)
|
||||
public class SysPermissionDataRule implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
@TableId(type = IdType.UUID)
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* 对应的菜单id
|
||||
*/
|
||||
private String permissionId;
|
||||
|
||||
/**
|
||||
* 规则名称
|
||||
*/
|
||||
private String ruleName;
|
||||
|
||||
/**
|
||||
* 字段
|
||||
*/
|
||||
private String ruleColumn;
|
||||
|
||||
/**
|
||||
* 条件
|
||||
*/
|
||||
private String ruleConditions;
|
||||
|
||||
/**
|
||||
* 规则值
|
||||
*/
|
||||
private String ruleValue;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
* 创建人
|
||||
*/
|
||||
private String createBy;
|
||||
|
||||
/**
|
||||
* 修改时间
|
||||
*/
|
||||
private Date updateTime;
|
||||
|
||||
/**
|
||||
* 修改人
|
||||
*/
|
||||
private String updateBy;
|
||||
}
|
||||
Reference in New Issue
Block a user