初始化提交

This commit is contained in:
2021-01-20 18:26:47 +08:00
commit 2cedd64270
70 changed files with 3423 additions and 0 deletions

View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>business.chaoran</groupId>
<artifactId>cooperop-app</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>business.chaoran</groupId>
<artifactId>cooperop-app-common</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--ASM使用-->
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>reflectasm</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,17 @@
package business.cooperop.common.constant;
/**
* 系统基础常量
*
* @author FXY
* <p/>
* 2018年1月23日
*/
public class Base {
public static final String ROLE_ADMIN = "admin";
public static final String CURRENT_USER = "current_user";
public static final String CURRENT_COMPANY = "current_company";
}

View File

@ -0,0 +1,106 @@
package business.cooperop.common.constant;
/**
* api接口返回 code和message
*
* @author FXY
* <p>
* 2018年1月23日
*/
public enum ResultCode {
/* 成功状态码 */
SUCCESS(0, "成功"),
ERROR(1, "失败"),
/* 参数错误10001-19999 */
PARAM_IS_INVALID(10001, "参数无效"),
PARAM_IS_BLANK(10002, "参数为空"),
PARAM_TYPE_BIND_ERROR(10003, "参数类型错误"),
PARAM_NOT_COMPLETE(10004, "参数缺失"),
/* 用户错误20001-29999*/
USER_NOT_LOGGED_IN(20001, "用户未登录"),
USER_LOGIN_ERROR(20002, "账号或密码错误"),
USER_ACCOUNT_FORBIDDEN(20003, "账号已被禁用"),
USER_NOT_EXIST(20004, "用户不存在"),
USER_HAS_EXISTED(20005, "用户已存在"),
USER_Register_ERROR(20006, "用户注册错误"),
/* 业务错误30001-39999 */
SPECIFIED_QUESTIONED_USER_NOT_EXIST(30001, "某业务出现问题"),
//菜单
MENU_ROOT_ELEMENT_NUM_MAX(30002, "一级菜单数量达到最大"),
MENU_TERMINAL_ELEMENT_NUM_MAX(30003, "二级菜单数量达到最大"),
MENU_LEVEL_PROMOTION(30004, "微信规定一级菜单最多3个二级菜单最多7个。最高二级层次菜单"),
MENU_ROOT_ELEMENT_CLOSED(30005, "对应的一级菜单未打开"),
//模板消息
TEMPLATE_MESSAGE_ACTIVE_NUM_MAX(30006, "模板消息激活数量已达最大"),
/* 系统错误40001-49999 */
SYSTEM_INNER_ERROR(40001, "系统繁忙,请稍后重试"),
/* 数据错误50001-599999 */
RESULE_DATA_NONE(50001, "数据未找到"),
DATA_IS_WRONG(50002, "数据有误"),
DATA_ALREADY_EXISTED(50003, "数据已存在"),
/* 接口错误60001-69999 */
INTERFACE_INNER_INVOKE_ERROR(60001, "内部系统接口调用异常"),
INTERFACE_OUTTER_INVOKE_ERROR(60002, "外部系统接口调用异常"),
INTERFACE_FORBID_VISIT(60003, "该接口禁止访问"),
INTERFACE_ADDRESS_INVALID(60004, "接口地址无效"),
INTERFACE_REQUEST_TIMEOUT(60005, "接口请求超时"),
INTERFACE_EXCEED_LOAD(60006, "接口负载过高"),
/* 权限错误70001-79999 */
PERMISSION_NO_ACCESS(70001, "无访问权限"),
/* 文件上传 */
UPLOAD_ERROR(80001, "上传失败"),
SESSION_TIME_OUT(90001, "Session超时");
private Integer code;
private String message;
ResultCode(Integer code, String message) {
this.code = code;
this.message = message;
}
public Integer code() {
return this.code;
}
public String message() {
return this.message;
}
public static String getMessage(String name) {
for (ResultCode item : ResultCode.values()) {
if (item.name().equals(name)) {
return item.message;
}
}
return name;
}
public static Integer getCode(String name) {
for (ResultCode item : ResultCode.values()) {
if (item.name().equals(name)) {
return item.code;
}
}
return null;
}
@Override
public String toString() {
return this.name();
}
}

View File

@ -0,0 +1,45 @@
package business.cooperop.common.exception;
import business.cooperop.common.constant.ResultCode;
/**
* 自定义异常
*
* @author FXY
* <p>
* 2018年1月23日
*/
public abstract class BaseException extends RuntimeException {
private static final long serialVersionUID = 3506744187536228284L;
private Integer errCode;
private String errMsg;
public Integer getErrCode() {
return errCode;
}
public void setErrCode(Integer errCode) {
this.errCode = errCode;
}
public String getErrMsg() {
return errMsg;
}
public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}
public BaseException(Integer errCode, String errMsg) {
this.errCode = errCode;
this.errMsg = errMsg;
}
public BaseException(ResultCode resultCode){
this.errCode=resultCode.code();
this.errMsg=resultCode.message();
}
}

View File

@ -0,0 +1,27 @@
package business.cooperop.common.exception;
/*
**********************************************
* DATE PERSON REASON
* 2020/10/23 FXY Created
**********************************************
*/
import business.cooperop.common.constant.ResultCode;
/**
* 默认异常实现
*/
public class DefaultException extends BaseException {
public DefaultException(Integer errCode, String errMsg) {
super(errCode, errMsg);
}
//根据枚举建立异常
public static DefaultException simple(ResultCode resultCode) {
return new DefaultException(resultCode.code(), resultCode.message());
}
}

View File

@ -0,0 +1,32 @@
package business.cooperop.common.result;
/**
* Controller参数校验 错误返回封装
*
* @author FXY
* <p>
* 2018年1月23日
*/
public class ParameterInvalidItem {
private String fieldName;
private String message;
public String getFieldName() {
return fieldName;
}
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}

View File

@ -0,0 +1,114 @@
package business.cooperop.common.result;
import business.cooperop.common.constant.ResultCode;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* api接口数据返回封装
*
* @author FXY
* <p>
* 2018年1月23日
*/
public class Result implements Serializable {
private static final long serialVersionUID = -4762928619495260423L;
private Integer code;
private String msg;
private Object data;
public Result() {
}
public Result(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public static Result success() {
Result result = new Result();
result.setResultCode(ResultCode.SUCCESS);
return result;
}
public static Result success(Object data) {
Result result = new Result();
result.setResultCode(ResultCode.SUCCESS);
result.setData(data);
return result;
}
public static Result failed() {
Result result = new Result();
result.setResultCode(ResultCode.SYSTEM_INNER_ERROR);
return result;
}
// 带消息的失败返回结果
public static Result failed(String msg) {
Result result = new Result();
result.setResultCode(ResultCode.SYSTEM_INNER_ERROR);
result.setMsg(msg);
return result;
}
public static Result error(ResultCode resultCode) {
Result result = new Result();
result.setResultCode(resultCode);
return result;
}
public static Result error(ResultCode resultCode, Object data) {
Result result = new Result();
result.setResultCode(resultCode);
result.setData(data);
return result;
}
public void setResultCode(ResultCode code) {
this.code = code.code();
this.msg = code.message();
}
public Map<String, Object> simple() {
Map<String, Object> simple = new HashMap<String, Object>();
this.data = simple;
return simple;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}

View File

@ -0,0 +1,258 @@
package business.cooperop.common.utils;
/*
**********************************************
* DATE PERSON REASON
* 2020/9/4 FXY Created
**********************************************
*/
import com.esotericsoftware.reflectasm.MethodAccess;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* 待重写
*/
public class BeanUtilsV2 {
/**
* 通过 ASM反射 速度比 Spring BeanUtils.copyProperties(source,target) 快一倍
* 类型不同可以转换
* 大小写可以忽略
* 下划线 _ 被忽略
* fxy备注需要操纵字节码服务器没有性能瓶颈的情况下慎用
*
* @param source 数据源
* @param target 目标是数据
* @param <T>
* @return
*/
public static <T> T copyPropertiesASM(Object source, Object target) {
MethodAccess sourceMethodAccess = CacheMethodAccess.getMethodAccess(source.getClass());
MethodAccess targetMethodAccess = CacheMethodAccess.getMethodAccess(target.getClass());
Map<String, String> sourceGet = CacheAsmFiledMethod.getMethod("get", source.getClass());
Map<String, String> targetSet = CacheAsmFiledMethod.getMethod("set", target.getClass());
CacheFieldMap.getFieldMap(target.getClass()).keySet().forEach((it) -> {
String sourceIndex = sourceGet.get(it);
if (sourceIndex != null) {
Object value = sourceMethodAccess.invoke(source, sourceIndex);
String setIndex = targetSet.get(it);
targetMethodAccess.invoke(target, setIndex, value);
}
});
return (T) target;
}
/**
* 模仿Spring中 BeanUtils.copyProperties(source,target)
* 目前仅支持整形到布尔类型的单向转换
* 但是
* 大小写可以忽略
* 下划线 _ 被忽略
*
* @param source
* @param target
* @param <T>
* @return
*/
public static <T> T copyProperties(Object source, Object target) {
Map<String, Field> sourceMap = CacheFieldMap.getFieldMap(source.getClass());
CacheFieldMap.getFieldMap(target.getClass()).values().forEach((it) -> {
Field field = sourceMap.get(it.getName().toLowerCase().replace("_", ""));
if (field != null) {
it.setAccessible(true);
field.setAccessible(true);
try {
if (field.getType() == Integer.class && it.getType() == Boolean.class) {
it.set(target, (Integer) field.get(source) == 1 ? true : false);
} else {
it.set(target, field.get(source));
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} finally {
//及时关闭访问权限
field.setAccessible(false);
it.setAccessible(false);
}
}
});
return (T) target;
}
/**
* @param source 源对象
* @param target 目标对象
* @param ignores 需要忽略的字段数组元素
* @param <T> 返回赋了值的目标对象
* 目前不同类型之间的转换,仅支持整形向布尔类型的单向转换
* @return
*/
public static <T> T copyProperties(Object source, Object target, String... ignores) {
Map<String, Field> sourceMap = CacheFieldMap.getFieldMap(source.getClass(), ignores);
CacheFieldMap.getFieldMap(target.getClass()).values().forEach((it) -> {
Field field = sourceMap.get(it.getName().toLowerCase().replace("_", ""));
if (field != null) {
it.setAccessible(true);
field.setAccessible(true);
try {
if (field.getType() == Integer.class && it.getType() == Boolean.class) {
it.set(target, (Integer) field.get(source) == 1 ? true : false);
} else {
it.set(target, field.get(source));
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} finally {
field.setAccessible(false);
it.setAccessible(false);
}
}
});
return (T) target;
}
/**
* Asm方式缓存变量域到map中待优化
* 可以将加锁操作换做无阻塞的并发容器
*/
private static class CacheAsmFiledMethod {
private static Map<String, Map<String, String>> cacheGetMethod = new HashMap<>();
private static Map<String, Map<String, String>> cacheSetMethod = new HashMap<>();
private static Map<String, String> getMethod(String type, Class clazz) {
MethodAccess methodAccess = CacheMethodAccess.getMethodAccess(clazz);
Map<String, Field> allFields = CacheFieldMap.getFieldMap(clazz);
Map<String, String> result = null;
if (type.equals("get")) {
result = cacheGetMethod.get(clazz.getName());
} else if (type.equals("set")) {
result = cacheSetMethod.get(clazz.getName());
}
if (result == null) {
synchronized (CacheAsmFiledMethod.class) {
if (result == null) {
Map<String, String> set = new HashMap<>();
Map<String, String> get = new HashMap<>();
allFields.values().forEach((it) -> {
//判断是否是静态
if (!Modifier.isStatic(it.getModifiers())) {
//首字母大写
char[] f = it.getName().toCharArray();
f[0] -= 32;
String fieldName = new String(f);
get.put(fieldName.toLowerCase().replace("_", ""), "get" + fieldName);
set.put(fieldName.toLowerCase().replace("_", ""), "set" + fieldName);
}
});
cacheGetMethod.put(clazz.getName(), get);
cacheSetMethod.put(clazz.getName(), set);
if (type.equals("get")) {
result = cacheGetMethod.get(clazz.getName());
} else if (type.equals("set")) {
result = cacheSetMethod.get(clazz.getName());
}
}
}
}
return result;
}
}
/**
* 缓存方法访问权限容器,加锁操作待优化
*/
private static class CacheMethodAccess {
private CacheMethodAccess() {
}
private static Map<String, MethodAccess> cache = new HashMap<>();
private static MethodAccess getMethodAccess(Class clazz) {
MethodAccess result = cache.get(clazz.getName());
if (result == null) {
synchronized (CacheMethodAccess.class) {
if (result == null) {
cache.put(clazz.getName(), MethodAccess.get(clazz));
result = cache.get(clazz.getName());
}
}
}
return result;
}
}
/**
* 缓存所有域到map中加锁操作待优化
*/
private static class CacheFieldMap {
private static Map<String, Map<String, Field>> cacheMap = new HashMap<>();
private static Map<String, Field> getFieldMap(Class clazz) {
Map<String, Field> result = cacheMap.get(clazz.getName());
if (result == null) {
synchronized (CacheFieldMap.class) {
if (result == null) {
List<Field> allFields = getAllFields(clazz);
Map<String, Field> fieldMap = allFields.stream().collect(Collectors.toMap(field -> field.getName().toLowerCase().replace("_", ""), Function.<Field>identity()));
cacheMap.put(clazz.getName(), fieldMap);
result = cacheMap.get(clazz.getName());
}
}
}
return result;
}
/**
* 每次反射完成后将数据存放在内存中
*
* @param clazz
* @param ignores
* @return
*/
private static Map<String, Field> getFieldMap(Class clazz, String... ignores) {
Map<String, Field> result = cacheMap.get(clazz.getName());
List<String> list = Arrays.asList(ignores);
if (result == null) {
synchronized (CacheFieldMap.class) {
if (result == null) {
List<Field> allFields = getAllFields(clazz);
Map<String, Field> fieldMap = allFields.stream().filter(a -> !(list.contains(a.getName()))).collect(Collectors.toMap(field -> field.getName().toLowerCase().replace("_", ""), Function.<Field>identity()));
cacheMap.put(clazz.getName(), fieldMap);
result = cacheMap.get(clazz.getName());
}
}
}
return result;
}
}
/**
* 得到包括继承而来的所有域
*
* @param clazz
* @return
*/
private static List<Field> getAllFields(Class clazz) {
List<Field> fieldList = new ArrayList<>();
while (clazz != null && !clazz.getName().toLowerCase().equals("java.lang.object")) {
fieldList.addAll(Arrays.asList(clazz.getDeclaredFields()));
clazz = clazz.getSuperclass();
}
return fieldList;
}
}