初始化提交
This commit is contained in:
25
common/core/.gitignore
vendored
Normal file
25
common/core/.gitignore
vendored
Normal file
@ -0,0 +1,25 @@
|
||||
/target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/build/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
40
common/core/pom.xml
Normal file
40
common/core/pom.xml
Normal file
@ -0,0 +1,40 @@
|
||||
<?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>
|
||||
|
||||
<groupId>business.chaoran</groupId>
|
||||
<artifactId>core</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>core</name>
|
||||
<description>Demo Core project for Spring Boot</description>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<parent>
|
||||
<groupId>business.chaoran</groupId>
|
||||
<artifactId>common</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!--Swagger2 - RESTful API文档-->
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger2</artifactId>
|
||||
<version>2.9.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger-ui</artifactId>
|
||||
<version>2.9.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@ -0,0 +1,167 @@
|
||||
package com.springboot.cloud.common.core.entity.vo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.springboot.cloud.common.core.exception.BaseException;
|
||||
import com.springboot.cloud.common.core.exception.ErrorType;
|
||||
import com.springboot.cloud.common.core.exception.SystemErrorType;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
@ApiModel(description = "rest请求的返回模型,所有rest正常都返回该类的对象")
|
||||
@Getter
|
||||
public class Result<T> {
|
||||
|
||||
public static final String SUCCESSFUL_CODE = "000000";
|
||||
public static final String SUCCESSFUL_MESG = "处理成功";
|
||||
|
||||
@ApiModelProperty(value = "处理结果code", required = true)
|
||||
private String code;
|
||||
@ApiModelProperty(value = "处理结果描述信息")
|
||||
private String msg;
|
||||
@ApiModelProperty(value = "请求结果生成时间戳")
|
||||
private Instant time;
|
||||
@ApiModelProperty(value = "处理结果数据信息")
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
private T data;
|
||||
|
||||
public Result() {
|
||||
this.time = ZonedDateTime.now().toInstant();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param errorType
|
||||
*/
|
||||
public Result(ErrorType errorType) {
|
||||
this.code = errorType.getCode();
|
||||
this.msg = errorType.getMesg();
|
||||
this.time = ZonedDateTime.now().toInstant();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param errorType
|
||||
* @param data
|
||||
*/
|
||||
public Result(ErrorType errorType, T data) {
|
||||
this(errorType);
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 内部使用,用于构造成功的结果
|
||||
*
|
||||
* @param code
|
||||
* @param msg
|
||||
* @param data
|
||||
*/
|
||||
private Result(String code, String msg, T data) {
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
this.data = data;
|
||||
this.time = ZonedDateTime.now().toInstant();
|
||||
}
|
||||
|
||||
/**
|
||||
* 快速创建成功结果并返回结果数据
|
||||
*
|
||||
* @param data
|
||||
* @return Result
|
||||
*/
|
||||
public static Result success(Object data) {
|
||||
return new Result<>(SUCCESSFUL_CODE, SUCCESSFUL_MESG, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 快速创建成功结果
|
||||
*
|
||||
* @return Result
|
||||
*/
|
||||
public static Result success() {
|
||||
return success(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 系统异常类没有返回数据
|
||||
*
|
||||
* @return Result
|
||||
*/
|
||||
public static Result fail() {
|
||||
return new Result(SystemErrorType.SYSTEM_ERROR);
|
||||
}
|
||||
|
||||
/**
|
||||
* 系统异常类没有返回数据
|
||||
*
|
||||
* @param baseException
|
||||
* @return Result
|
||||
*/
|
||||
public static Result fail(BaseException baseException) {
|
||||
return fail(baseException, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 系统异常类并返回结果数据
|
||||
*
|
||||
* @param data
|
||||
* @return Result
|
||||
*/
|
||||
public static Result fail(BaseException baseException, Object data) {
|
||||
return new Result<>(baseException.getErrorType(), data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 系统异常类并返回结果数据
|
||||
*
|
||||
* @param errorType
|
||||
* @param data
|
||||
* @return Result
|
||||
*/
|
||||
public static Result fail(ErrorType errorType, Object data) {
|
||||
return new Result<>(errorType, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 系统异常类并返回结果数据
|
||||
*
|
||||
* @param errorType
|
||||
* @return Result
|
||||
*/
|
||||
public static Result fail(ErrorType errorType) {
|
||||
return Result.fail(errorType, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 系统异常类并返回结果数据
|
||||
*
|
||||
* @param data
|
||||
* @return Result
|
||||
*/
|
||||
public static Result fail(Object data) {
|
||||
return new Result<>(SystemErrorType.SYSTEM_ERROR, data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 成功code=000000
|
||||
*
|
||||
* @return true/false
|
||||
*/
|
||||
@JsonIgnore
|
||||
public boolean isSuccess() {
|
||||
return SUCCESSFUL_CODE.equals(this.code);
|
||||
}
|
||||
|
||||
/**
|
||||
* 失败
|
||||
*
|
||||
* @return true/false
|
||||
*/
|
||||
@JsonIgnore
|
||||
public boolean isFail() {
|
||||
return !isSuccess();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,32 @@
|
||||
package com.springboot.cloud.common.core.exception;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class BaseException extends RuntimeException {
|
||||
/**
|
||||
* 异常对应的错误类型
|
||||
*/
|
||||
private final ErrorType errorType;
|
||||
|
||||
/**
|
||||
* 默认是系统异常
|
||||
*/
|
||||
public BaseException() {
|
||||
this.errorType = SystemErrorType.SYSTEM_ERROR;
|
||||
}
|
||||
|
||||
public BaseException(ErrorType errorType) {
|
||||
this.errorType = errorType;
|
||||
}
|
||||
|
||||
public BaseException(ErrorType errorType, String message) {
|
||||
super(message);
|
||||
this.errorType = errorType;
|
||||
}
|
||||
|
||||
public BaseException(ErrorType errorType, String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
this.errorType = errorType;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
package com.springboot.cloud.common.core.exception;
|
||||
|
||||
public interface ErrorType {
|
||||
/**
|
||||
* 返回code
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String getCode();
|
||||
|
||||
/**
|
||||
* 返回mesg
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String getMesg();
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
package com.springboot.cloud.common.core.exception;
|
||||
|
||||
/**
|
||||
* Created by zhoutaoo on 2018/6/2.
|
||||
*/
|
||||
public class ServiceException extends BaseException {
|
||||
|
||||
//TODO 对业务异常的返回码进行校验,规范到一定范围内
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
package com.springboot.cloud.common.core.exception;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public enum SystemErrorType implements ErrorType {
|
||||
|
||||
SYSTEM_ERROR("-1", "系统异常"),
|
||||
SYSTEM_BUSY("000001", "系统繁忙,请稍候再试"),
|
||||
|
||||
GATEWAY_NOT_FOUND_SERVICE("010404", "服务未找到"),
|
||||
GATEWAY_ERROR("010500", "网关异常"),
|
||||
GATEWAY_CONNECT_TIME_OUT("010002", "网关超时"),
|
||||
|
||||
ARGUMENT_NOT_VALID("020000", "请求参数校验不通过"),
|
||||
INVALID_TOKEN("020001", "无效token"),
|
||||
UPLOAD_FILE_SIZE_LIMIT("020010", "上传文件大小超过限制"),
|
||||
|
||||
DUPLICATE_PRIMARY_KEY("030000","唯一键冲突");
|
||||
|
||||
/**
|
||||
* 错误类型码
|
||||
*/
|
||||
private String code;
|
||||
/**
|
||||
* 错误类型描述信息
|
||||
*/
|
||||
private String mesg;
|
||||
|
||||
SystemErrorType(String code, String mesg) {
|
||||
this.code = code;
|
||||
this.mesg = mesg;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,84 @@
|
||||
package com.springboot.cloud.common.core.util;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* {"companies":[{"default":true,"authority":"1"},{"default":false,"authority":"2"}],"user_name":"admin","scope":["read"],"exp":1610100088,"authorities":["1","2"],"jti":"nt9bTYbUe8IzuEgfAz70nwaMveI","client_id":"test_client"}
|
||||
*/
|
||||
|
||||
/**
|
||||
* 用户上下文
|
||||
*/
|
||||
public class UserContextHolder {
|
||||
|
||||
private ThreadLocal<Map<String, String>> threadLocal;
|
||||
|
||||
private UserContextHolder() {
|
||||
this.threadLocal = new ThreadLocal<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建实例
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static UserContextHolder getInstance() {
|
||||
return SingletonHolder.sInstance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 静态内部类单例模式
|
||||
* 单例初使化
|
||||
*/
|
||||
private static class SingletonHolder {
|
||||
private static final UserContextHolder sInstance = new UserContextHolder();
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户上下文中放入信息
|
||||
*
|
||||
* @param map
|
||||
*/
|
||||
public void setContext(Map<String, String> map) {
|
||||
threadLocal.set(map);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上下文中的信息
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Map<String, String> getContext() {
|
||||
return threadLocal.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上下文中的用户名
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getUsername() {
|
||||
return Optional.ofNullable(threadLocal.get()).orElse(Maps.newHashMap()).get("user_name");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上下文中用户登录的公司
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getCurrentCompany() {
|
||||
return Optional.ofNullable(threadLocal.get()).orElse(Maps.newHashMap()).get("company");
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空上下文
|
||||
*/
|
||||
public void clear() {
|
||||
threadLocal.remove();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user