Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
polysoft-nafmii
/
polysoft-parent
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit f2fdb50b
authored
Apr 23, 2021
by
zhoujinghao
Browse Files
Options
Browse Files
Tag
Download
Plain Diff
Merge branch 'master' into dev
2 parents
16b50dd1
17adc532
Hide whitespace changes
Inline
Side-by-side
Showing
21 changed files
with
799 additions
and
213 deletions
nafmii-common/pom.xml
nafmii-common/src/main/java/org/nafmii/config/JsonSerializerConfig.java
nafmii-common/src/main/java/org/nafmii/config/RedisConfig.java
nafmii-common/src/main/java/org/nafmii/constant/HttpClientHeaderEnum.java
nafmii-common/src/main/java/org/nafmii/constant/MessageEnum.java
nafmii-common/src/main/java/org/nafmii/exception/ApiException.java
nafmii-common/src/main/java/org/nafmii/exception/Asserts.java
nafmii-common/src/main/java/org/nafmii/exception/BusinessException.java
nafmii-common/src/main/java/org/nafmii/exception/BusinessExceptionHandler.java
nafmii-common/src/main/java/org/nafmii/exception/GlobalExceptionHandler.java
nafmii-common/src/main/java/org/nafmii/param/OrderBy.java
nafmii-common/src/main/java/org/nafmii/param/PageQuery.java
nafmii-common/src/main/java/org/nafmii/result/IErrorCode.java
nafmii-common/src/main/java/org/nafmii/result/Result.java
nafmii-common/src/main/java/org/nafmii/result/ResultCode.java
nafmii-common/src/main/java/org/nafmii/utils/BeanTransferUtils.java
nafmii-common/src/main/java/org/nafmii/utils/FileUtils.java
nafmii-common/src/main/java/org/nafmii/utils/PagedResult.java
nafmii-common/src/main/java/org/nafmii/utils/RedisUtils.java
nafmii-common/src/main/java/org/nafmii/utils/RequestUtils.java
nafmii-common/src/main/java/org/nafmii/utils/SpringUtils.java
nafmii-common/pom.xml
View file @
f2fdb50
...
...
@@ -14,10 +14,25 @@
<description>
Demo project for Spring Boot
</description>
<properties>
<java.version>
1.8
</java.version>
<httpClient.version>
4.5.3
</httpClient.version>
<fastjson.version>
1.2.58
</fastjson.version>
<ojdbc.version>
10.2.0.4
</ojdbc.version>
</properties>
<dependencies>
<!--swagger 相关依赖-->
<dependency>
<groupId>
io.springfox
</groupId>
<artifactId>
springfox-swagger2
</artifactId>
<version>
2.8.0
</version>
</dependency>
<dependency>
<groupId>
io.springfox
</groupId>
<artifactId>
springfox-swagger-ui
</artifactId>
<version>
2.8.0
</version>
</dependency>
<dependency>
<groupId>
net.logstash.logback
</groupId>
<artifactId>
logstash-logback-encoder
</artifactId>
</dependency>
...
...
@@ -71,6 +86,95 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-web
</artifactId>
</dependency>
<!--redis-->
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-data-redis
</artifactId>
</dependency>
<!-- mybatis plus 代码生成器 -->
<dependency>
<groupId>
com.baomidou
</groupId>
<artifactId>
mybatis-plus-core
</artifactId>
<version>
3.3.0
</version>
</dependency>
<dependency>
<groupId>
com.baomidou
</groupId>
<artifactId>
mybatis-plus-generator
</artifactId>
<version>
3.3.0
</version>
</dependency>
<dependency>
<groupId>
com.baomidou
</groupId>
<artifactId>
mybatis-plus-boot-starter
</artifactId>
<version>
3.3.0
</version>
</dependency>
<dependency>
<groupId>
com.baomidou
</groupId>
<artifactId>
mybatis-plus-extension
</artifactId>
<version>
3.3.0
</version>
</dependency>
<!-- 代码生成器模板 -->
<dependency>
<groupId>
org.apache.velocity
</groupId>
<artifactId>
velocity-engine-core
</artifactId>
<version>
2.2
</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>
org.projectlombok
</groupId>
<artifactId>
lombok
</artifactId>
</dependency>
<!-- http client -->
<dependency>
<groupId>
commons-httpclient
</groupId>
<artifactId>
commons-httpclient
</artifactId>
<version>
3.1
</version>
</dependency>
<dependency>
<groupId>
org.apache.httpcomponents
</groupId>
<artifactId>
httpmime
</artifactId>
<version>
${httpClient.version}
</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>
com.alibaba
</groupId>
<artifactId>
fastjson
</artifactId>
<version>
${fastjson.version}
</version>
</dependency>
<dependency>
<groupId>
commons-beanutils
</groupId>
<artifactId>
commons-beanutils
</artifactId>
<version>
1.9.3
</version>
</dependency>
<!--poi文件的导入导出-->
<dependency>
<groupId>
org.apache.poi
</groupId>
<artifactId>
poi
</artifactId>
<version>
3.9
</version>
</dependency>
<dependency>
<groupId>
org.apache.poi
</groupId>
<artifactId>
poi-ooxml
</artifactId>
<version>
3.9
</version>
</dependency>
<dependency>
<groupId>
org.apache.poi
</groupId>
<artifactId>
poi-ooxml-schemas
</artifactId>
<version>
3.9
</version>
</dependency>
<!-- 反射 -->
<dependency>
<groupId>
cn.hutool
</groupId>
<artifactId>
hutool-all
</artifactId>
</dependency>
<dependency>
<groupId>
commons-io
</groupId>
<artifactId>
commons-io
</artifactId>
<version>
2.4
</version>
</dependency>
</dependencies>
<build>
...
...
nafmii-common/src/main/java/org/nafmii/config/JsonSerializerConfig.java
0 → 100644
View file @
f2fdb50
package
org
.
nafmii
.
config
;
import
com.fasterxml.jackson.annotation.JsonInclude
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.module.SimpleModule
;
import
com.fasterxml.jackson.databind.ser.std.ToStringSerializer
;
import
org.springframework.boot.jackson.JsonComponent
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.http.converter.json.Jackson2ObjectMapperBuilder
;
import
javax.annotation.PostConstruct
;
import
java.util.TimeZone
;
@JsonComponent
public
class
JsonSerializerConfig
{
@Bean
public
ObjectMapper
jacksonObjectMapper
(
Jackson2ObjectMapperBuilder
builder
)
{
ObjectMapper
objectMapper
=
builder
.
createXmlMapper
(
false
).
build
();
objectMapper
.
setSerializationInclusion
(
JsonInclude
.
Include
.
ALWAYS
);
SimpleModule
module
=
new
SimpleModule
();
module
.
addSerializer
(
Long
.
class
,
ToStringSerializer
.
instance
);
module
.
addSerializer
(
Long
.
TYPE
,
ToStringSerializer
.
instance
);
objectMapper
.
registerModule
(
module
);
return
objectMapper
;
}
@PostConstruct
public
void
loadTimeZone
()
{
TimeZone
.
setDefault
(
TimeZone
.
getTimeZone
(
"GMT+8"
));
}
}
nafmii-common/src/main/java/org/nafmii/config/RedisConfig.java
0 → 100644
View file @
f2fdb50
package
org
.
nafmii
.
config
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.data.redis.connection.RedisConnectionFactory
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.data.redis.serializer.RedisSerializer
;
import
org.springframework.data.redis.serializer.StringRedisSerializer
;
import
org.springframework.stereotype.Component
;
@Component
public
class
RedisConfig
{
@SuppressWarnings
(
"rawtypes"
)
@Bean
public
RedisTemplate
redisTemplate
(
RedisConnectionFactory
factory
){
RedisTemplate
<
String
,
Object
>
redisTemplate
=
new
RedisTemplate
<>();
RedisSerializer
stringSerializer
=
new
StringRedisSerializer
();
redisTemplate
.
setConnectionFactory
(
factory
);
redisTemplate
.
setKeySerializer
(
stringSerializer
);
redisTemplate
.
setValueSerializer
(
stringSerializer
);
redisTemplate
.
setHashKeySerializer
(
stringSerializer
);
redisTemplate
.
setHashValueSerializer
(
stringSerializer
);
return
redisTemplate
;
}
}
nafmii-common/src/main/java/org/nafmii/constant/HttpClientHeaderEnum.java
0 → 100644
View file @
f2fdb50
package
org
.
nafmii
.
constant
;
/**
* @author by zhangbr
* @date 2020/4/8.
*/
public
enum
HttpClientHeaderEnum
{
/**
* 系统自定义请求header参数
*/
USER_HEADER_USER_ID
(
"userId"
,
"用户ID"
),
USER_HEADER_OLD_USER_ID
(
"oldUserId"
,
"老用户ID"
),
USER_HEADER_USER_TOKEN
(
"token"
,
"用户token"
),
USER_HEADER_MODE
(
"mode"
,
"设备:PC,APP,PAD"
),
;
HttpClientHeaderEnum
(
String
code
,
String
name
){
this
.
code
=
code
;
this
.
name
=
name
;
}
private
String
name
;
private
String
code
;
public
String
getName
()
{
return
name
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
public
String
getCode
()
{
return
code
;
}
public
void
setCode
(
String
code
)
{
this
.
code
=
code
;
}
public
static
boolean
checkHasCode
(
String
key
)
{
for
(
HttpClientHeaderEnum
c
:
HttpClientHeaderEnum
.
values
())
{
if
(
c
.
getCode
().
toLowerCase
().
equals
(
key
.
toLowerCase
()))
{
return
true
;
}
}
return
false
;
}
}
nafmii-common/src/main/java/org/nafmii/constant/MessageEnum.java
0 → 100644
View file @
f2fdb50
package
org
.
nafmii
.
constant
;
public
enum
MessageEnum
{
/**
* 通用提示枚举
*/
SUCCESS
(
0L
,
"操作成功!"
),
FAIL
(-
100L
,
"操作失败,请稍后再试!"
),
FAIL_PARAM
(-
101L
,
"参数不可为空!"
),
FAIL_PARAM_TYPE
(-
102L
,
"参数类型错误"
),
FAIL_PARAM_VALIDATE
(-
103L
,
"参数校验错误"
),
NO_LOGIN
(
401L
,
"会话超时,请重新登录!"
),
HTTP_CLIENT
(-
104L
,
"http请求失败,请稍后再试!"
),
SELECT_HAS_NO
(-
105L
,
"信息不存在或已被删除,请选择有效数据!"
),
NO_SUPPORT_ACTION
(-
106L
,
"不支持此操作!"
),
EXCEL_EXPORT_ERROR
(-
107L
,
"Excel数据导出失败!"
),
FEIGN_CLIENT_DECODE
(-
108L
,
"远程接口解码失败!"
),
MAX_FILE_SIZE
(-
109L
,
"上传文件超过最大限制!"
),
FILE_TYPE_ERROR
(-
110L
,
"文件类型不允许上传!"
),
FILE_UPLOAD_FAIL
(-
111L
,
"文件上传失败!"
),
FILE_DOWN_FAIL
(-
112L
,
"文件不存在或已被删除!"
),
;
private
Long
code
;
private
String
remarks
;
MessageEnum
(
Long
code
,
String
remarks
){
this
.
code
=
code
;
this
.
remarks
=
remarks
;
}
public
Long
getCode
()
{
return
code
;
}
public
String
getRemarks
()
{
return
remarks
;
}
}
nafmii-common/src/main/java/org/nafmii/exception/ApiException.java
deleted
100644 → 0
View file @
16b50dd
package
org
.
nafmii
.
exception
;
import
org.nafmii.result.IErrorCode
;
/**
* 自定义API异常
* Created by macro on 2020/2/27.
*/
public
class
ApiException
extends
RuntimeException
{
private
IErrorCode
errorCode
;
public
ApiException
(
IErrorCode
errorCode
)
{
super
(
errorCode
.
getMessage
());
this
.
errorCode
=
errorCode
;
}
public
ApiException
(
String
message
)
{
super
(
message
);
}
public
ApiException
(
Throwable
cause
)
{
super
(
cause
);
}
public
ApiException
(
String
message
,
Throwable
cause
)
{
super
(
message
,
cause
);
}
public
IErrorCode
getErrorCode
()
{
return
errorCode
;
}
}
nafmii-common/src/main/java/org/nafmii/exception/Asserts.java
deleted
100644 → 0
View file @
16b50dd
package
org
.
nafmii
.
exception
;
import
org.nafmii.result.IErrorCode
;
/**
* 断言处理类,用于抛出各种API异常
* Created by macro on 2020/2/27.
*/
public
class
Asserts
{
public
static
void
fail
(
String
message
)
{
throw
new
ApiException
(
message
);
}
public
static
void
fail
(
IErrorCode
errorCode
)
{
throw
new
ApiException
(
errorCode
);
}
}
nafmii-common/src/main/java/org/nafmii/exception/BusinessException.java
0 → 100644
View file @
f2fdb50
package
org
.
nafmii
.
exception
;
import
javax.servlet.http.HttpServletResponse
;
public
class
BusinessException
extends
RuntimeException
{
private
static
final
long
serialVersionUID
=
1L
;
private
Integer
statusCode
;
public
BusinessException
()
{
super
();
this
.
statusCode
=
HttpServletResponse
.
SC_INTERNAL_SERVER_ERROR
;
}
public
BusinessException
(
String
message
)
{
super
(
message
);
this
.
statusCode
=
HttpServletResponse
.
SC_INTERNAL_SERVER_ERROR
;
}
public
BusinessException
(
Integer
statusCode
,
String
message
)
{
super
(
message
);
this
.
statusCode
=
statusCode
;
}
public
BusinessException
(
Long
statusCode
,
String
message
)
{
super
(
message
);
this
.
statusCode
=
Integer
.
parseInt
(
statusCode
.
toString
());
}
public
Integer
getStatusCode
()
{
return
statusCode
;
}
public
void
setStatusCode
(
Integer
statusCode
)
{
this
.
statusCode
=
statusCode
;
}
}
nafmii-common/src/main/java/org/nafmii/exception/BusinessExceptionHandler.java
0 → 100644
View file @
f2fdb50
package
org
.
nafmii
.
exception
;
import
lombok.extern.slf4j.Slf4j
;
import
org.nafmii.constant.MessageEnum
;
import
org.nafmii.result.Result
;
import
org.springframework.core.annotation.Order
;
import
org.springframework.http.converter.HttpMessageNotReadableException
;
import
org.springframework.validation.BindingResult
;
import
org.springframework.web.bind.MethodArgumentNotValidException
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.multipart.MaxUploadSizeExceededException
;
import
javax.websocket.DecodeException
;
@Order
(
Integer
.
MIN_VALUE
)
@ControllerAdvice
@Slf4j
public
class
BusinessExceptionHandler
{
/**
* 默认异常处理
*/
@ExceptionHandler
(
value
=
Exception
.
class
)
@ResponseBody
public
Result
defaultErrorHandler
(
Exception
e
)
{
log
.
error
(
e
.
getMessage
(),
e
);
return
new
Result
(
MessageEnum
.
FAIL
.
getCode
(),
e
.
getMessage
());
}
/**
* 自定义业务异常处理
* @param e BusinessException
* @return ApiResponse
*/
@ExceptionHandler
(
value
=
BusinessException
.
class
)
@ResponseBody
public
Result
businessErrorHandler
(
BusinessException
e
)
{
log
.
error
(
e
.
getMessage
(),
e
);
return
new
Result
(
e
.
getStatusCode
(),
e
.
getMessage
());
}
/**
* 入参异常处理
*/
@ExceptionHandler
(
value
=
MethodArgumentNotValidException
.
class
)
@ResponseBody
public
Result
argumentErrorHandler
(
MethodArgumentNotValidException
e
)
{
BindingResult
bindingResult
=
e
.
getBindingResult
();
String
errorMessage
=
"参数不能为空!"
;
if
(
bindingResult
.
getFieldError
()
!=
null
){
errorMessage
=
bindingResult
.
getFieldError
().
getDefaultMessage
();
}
return
new
Result
(
MessageEnum
.
FAIL_PARAM
.
getCode
(),
errorMessage
);
}
/**
* 入参类型异常处理
*/
@ExceptionHandler
(
value
=
HttpMessageNotReadableException
.
class
)
@ResponseBody
public
Result
argumentErrorHandler
(
HttpMessageNotReadableException
e
)
{
log
.
error
(
e
.
getMessage
(),
e
);
return
new
Result
(
MessageEnum
.
FAIL_PARAM_TYPE
.
getCode
(),
MessageEnum
.
FAIL_PARAM_TYPE
.
getRemarks
());
}
/**
* FeignClient解码失败
*/
@ExceptionHandler
(
value
=
DecodeException
.
class
)
@ResponseBody
public
Result
argumentErrorHandler
(
DecodeException
e
)
{
log
.
error
(
e
.
getMessage
(),
e
);
return
new
Result
(
MessageEnum
.
FEIGN_CLIENT_DECODE
.
getCode
(),
MessageEnum
.
FEIGN_CLIENT_DECODE
.
getRemarks
());
}
/**
* 文件大小限制
*/
@ExceptionHandler
(
value
=
MaxUploadSizeExceededException
.
class
)
@ResponseBody
public
Result
argumentErrorHandler
(
MaxUploadSizeExceededException
e
)
{
log
.
error
(
e
.
getMessage
(),
e
);
return
new
Result
(
MessageEnum
.
MAX_FILE_SIZE
.
getCode
(),
MessageEnum
.
MAX_FILE_SIZE
.
getRemarks
());
}
}
nafmii-common/src/main/java/org/nafmii/exception/GlobalExceptionHandler.java
deleted
100644 → 0
View file @
16b50dd
package
org
.
nafmii
.
exception
;
import
org.nafmii.result.Result
;
import
org.springframework.validation.BindException
;
import
org.springframework.validation.BindingResult
;
import
org.springframework.validation.FieldError
;
import
org.springframework.web.bind.MethodArgumentNotValidException
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ResponseBody
;
/**
* 全局异常处理
* Created by macro on 2020/2/27.
*/
@ControllerAdvice
public
class
GlobalExceptionHandler
{
@ResponseBody
@ExceptionHandler
(
value
=
ApiException
.
class
)
public
Result
handle
(
ApiException
e
)
{
if
(
e
.
getErrorCode
()
!=
null
)
{
return
Result
.
failed
(
e
.
getErrorCode
());
}
return
Result
.
failed
(
e
.
getMessage
());
}
@ResponseBody
@ExceptionHandler
(
value
=
MethodArgumentNotValidException
.
class
)
public
Result
handleValidException
(
MethodArgumentNotValidException
e
)
{
BindingResult
bindingResult
=
e
.
getBindingResult
();
String
message
=
null
;
if
(
bindingResult
.
hasErrors
())
{
FieldError
fieldError
=
bindingResult
.
getFieldError
();
if
(
fieldError
!=
null
)
{
message
=
fieldError
.
getField
()+
fieldError
.
getDefaultMessage
();
}
}
return
Result
.
validateFailed
(
message
);
}
@ResponseBody
@ExceptionHandler
(
value
=
BindException
.
class
)
public
Result
handleValidException
(
BindException
e
)
{
BindingResult
bindingResult
=
e
.
getBindingResult
();
String
message
=
null
;
if
(
bindingResult
.
hasErrors
())
{
FieldError
fieldError
=
bindingResult
.
getFieldError
();
if
(
fieldError
!=
null
)
{
message
=
fieldError
.
getField
()+
fieldError
.
getDefaultMessage
();
}
}
return
Result
.
validateFailed
(
message
);
}
}
nafmii-common/src/main/java/org/nafmii/param/OrderBy.java
0 → 100644
View file @
f2fdb50
package
org
.
nafmii
.
param
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.Data
;
/**
* @author by zhangbr
* @date 2020/4/13.
*/
@Data
public
class
OrderBy
{
@ApiModelProperty
(
value
=
"排序字段"
,
example
=
"createTime"
)
private
String
column
;
@ApiModelProperty
(
value
=
"排序方式(ASC:正序,DESC:倒序,默认:ASC)"
,
example
=
"ASC"
)
private
String
type
;
}
nafmii-common/src/main/java/org/nafmii/param/PageQuery.java
0 → 100644
View file @
f2fdb50
package
org
.
nafmii
.
param
;
import
com.baomidou.mybatisplus.extension.plugins.pagination.Page
;
import
com.fasterxml.jackson.annotation.JsonIgnore
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.Data
;
import
java.util.List
;
@Data
public
class
PageQuery
{
@ApiModelProperty
(
"页码"
)
private
Integer
pageNo
=
1
;
@ApiModelProperty
(
"每页条数,默认10"
)
private
Integer
pageSize
=
10
;
@ApiModelProperty
(
value
=
"关键字"
)
private
String
key
;
@ApiModelProperty
(
value
=
"排序集合"
)
private
List
<
OrderBy
>
orderByList
;
@ApiModelProperty
(
value
=
"是否分页,默认:true"
)
private
Boolean
isPage
=
true
;
@JsonIgnore
private
Integer
maxPageSize
=
1000
;
@JsonIgnore
public
<
T
>
Page
<
T
>
getPage
(){
if
(
isPage
){
return
new
Page
<>(
pageNo
<=
0
?
1
:
pageNo
,
pageSize
<=
0
?
10
:
pageSize
);
}
return
new
Page
<>(
0
,
maxPageSize
);
}
}
nafmii-common/src/main/java/org/nafmii/result/IErrorCode.java
deleted
100644 → 0
View file @
16b50dd
package
org
.
nafmii
.
result
;
/**
* 封装API的错误码
* Created by macro on 2019/4/19.
*/
public
interface
IErrorCode
{
long
getCode
();
String
getMessage
();
}
nafmii-common/src/main/java/org/nafmii/result/Result.java
View file @
f2fdb50
package
org
.
nafmii
.
result
;
import
lombok.Data
;
import
org.nafmii.constant.MessageEnum
;
/**
* 通用返回对象
* Created by macro on 2019/4/19.
*/
@Data
public
class
Result
<
T
>
{
private
long
code
;
private
Long
code
;
private
Integer
intCode
;
private
String
message
;
private
T
data
;
public
Result
()
{
}
protected
Result
(
long
code
,
String
message
,
T
data
)
{
public
Result
(
Long
code
,
String
message
,
T
data
)
{
this
.
code
=
code
;
this
.
message
=
message
;
this
.
data
=
data
;
}
public
Result
(
Long
code
,
String
message
)
{
this
.
code
=
code
;
this
.
message
=
message
;
}
public
Result
(
Integer
intCode
,
String
message
)
{
this
.
intCode
=
intCode
;
this
.
message
=
message
;
}
/**
* 成功返回结果
*
* @param data 获取的数据
*/
public
static
<
T
>
Result
<
T
>
success
(
T
data
)
{
return
new
Result
<
T
>(
ResultCode
.
SUCCESS
.
getCode
(),
ResultCode
.
SUCCESS
.
getMessage
(),
data
);
return
new
Result
<
T
>(
MessageEnum
.
SUCCESS
.
getCode
(),
MessageEnum
.
SUCCESS
.
getRemarks
(),
data
);
}
/**
...
...
@@ -34,46 +43,23 @@ public class Result<T> {
* @param message 提示信息
*/
public
static
<
T
>
Result
<
T
>
success
(
T
data
,
String
message
)
{
return
new
Result
<
T
>(
ResultCode
.
SUCCESS
.
getCode
(),
message
,
data
);
}
/**
* 失败返回结果
* @param errorCode 错误码
*/
public
static
<
T
>
Result
<
T
>
failed
(
IErrorCode
errorCode
)
{
return
new
Result
<
T
>(
errorCode
.
getCode
(),
errorCode
.
getMessage
(),
null
);
return
new
Result
<
T
>(
MessageEnum
.
SUCCESS
.
getCode
(),
message
,
data
);
}
/**
* 失败返回结果
* @param errorCode 错误码
* @param message 错误信息
*/
public
static
<
T
>
Result
<
T
>
failed
(
IErrorCode
errorCode
,
String
message
)
{
return
new
Result
<
T
>(
errorCode
.
getCode
(),
message
,
null
);
}
/**
* 失败返回结果
* @param message 提示信息
*/
public
static
<
T
>
Result
<
T
>
failed
(
String
message
)
{
return
new
Result
<
T
>(
ResultCode
.
FAILED
.
getCode
(),
message
,
null
);
}
/**
* 失败返回结果
*/
public
static
<
T
>
Result
<
T
>
failed
()
{
return
failed
(
ResultCode
.
FAILED
);
return
new
Result
<
T
>(
MessageEnum
.
FAIL
.
getCode
(),
message
,
null
);
}
/**
* 参数验证失败返回结果
*/
public
static
<
T
>
Result
<
T
>
validateFailed
()
{
return
failed
(
ResultCode
.
VALIDATE_FAILED
);
return
new
Result
<
T
>(
MessageEnum
.
FAIL_PARAM_VALIDATE
.
getCode
(),
MessageEnum
.
FAIL_PARAM_VALIDATE
.
getRemarks
()
);
}
/**
...
...
@@ -81,44 +67,14 @@ public class Result<T> {
* @param message 提示信息
*/
public
static
<
T
>
Result
<
T
>
validateFailed
(
String
message
)
{
return
new
Result
<
T
>(
ResultCode
.
VALIDATE_FAILED
.
getCode
(),
message
,
null
);
}
return
new
Result
<
T
>(
MessageEnum
.
FAIL_PARAM_VALIDATE
.
getCode
(),
message
,
null
);
}
/**
* 未登录返回结果
*/
public
static
<
T
>
Result
<
T
>
unauthorized
(
T
data
)
{
return
new
Result
<
T
>(
ResultCode
.
UNAUTHORIZED
.
getCode
(),
ResultCode
.
UNAUTHORIZED
.
getMessage
(),
data
);
public
static
<
T
>
Result
<
T
>
unauthorized
()
{
return
new
Result
<
T
>(
MessageEnum
.
NO_LOGIN
.
getCode
(),
MessageEnum
.
NO_LOGIN
.
getRemarks
()
);
}
/**
* 未授权返回结果
*/
public
static
<
T
>
Result
<
T
>
forbidden
(
T
data
)
{
return
new
Result
<
T
>(
ResultCode
.
FORBIDDEN
.
getCode
(),
ResultCode
.
FORBIDDEN
.
getMessage
(),
data
);
}
public
long
getCode
()
{
return
code
;
}
public
void
setCode
(
long
code
)
{
this
.
code
=
code
;
}
public
String
getMessage
()
{
return
message
;
}
public
void
setMessage
(
String
message
)
{
this
.
message
=
message
;
}
public
T
getData
()
{
return
data
;
}
public
void
setData
(
T
data
)
{
this
.
data
=
data
;
}
}
nafmii-common/src/main/java/org/nafmii/result/ResultCode.java
deleted
100644 → 0
View file @
16b50dd
package
org
.
nafmii
.
result
;
/**
* 枚举了一些常用API操作码
* Created by macro on 2019/4/19.
*/
public
enum
ResultCode
implements
IErrorCode
{
SUCCESS
(
200
,
"操作成功"
),
FAILED
(
500
,
"操作失败"
),
VALIDATE_FAILED
(
404
,
"参数检验失败"
),
UNAUTHORIZED
(
401
,
"暂未登录或token已经过期"
),
FORBIDDEN
(
403
,
"没有相关权限"
);
private
long
code
;
private
String
message
;
private
ResultCode
(
long
code
,
String
message
)
{
this
.
code
=
code
;
this
.
message
=
message
;
}
public
long
getCode
()
{
return
code
;
}
public
String
getMessage
()
{
return
message
;
}
}
nafmii-common/src/main/java/org/nafmii/utils/BeanTransferUtils.java
0 → 100644
View file @
f2fdb50
package
org
.
nafmii
.
utils
;
import
com.baomidou.mybatisplus.extension.plugins.pagination.Page
;
import
org.springframework.util.CollectionUtils
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
/**
* @author by zhangbr
* @date 2020/3/26.
*/
public
class
BeanTransferUtils
{
public
static
<
T
>
T
transfer
(
Object
text
,
Class
<
T
>
clazz
)
{
T
t
;
try
{
t
=
clazz
.
newInstance
();
org
.
springframework
.
beans
.
BeanUtils
.
copyProperties
(
text
,
t
);
return
t
;
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
e
);
}
}
public
static
<
T
>
List
<
T
>
transferList
(
List
<?>
list
,
Class
<
T
>
clazz
)
{
if
(
CollectionUtils
.
isEmpty
(
list
)){
return
Collections
.
emptyList
();
}
List
<
T
>
result
=
new
ArrayList
<>();
for
(
Object
each
:
list
)
{
result
.
add
(
transfer
(
each
,
clazz
));
}
return
result
;
}
public
static
<
T
>
PagedResult
<
T
>
toPagedResult
(
List
<
T
>
list
,
Page
page
)
{
PagedResult
<
T
>
result
=
new
PagedResult
<>();
if
(
list
!=
null
&&
!
list
.
isEmpty
()){
result
.
setList
(
list
);
if
(
page
!=
null
){
result
.
setTotal
(
page
.
getTotal
());
}
else
{
result
.
setTotal
(
list
.
size
());
}
}
return
result
;
}
public
static
<
T
>
PagedResult
<
T
>
toPagedResult
(
List
<?>
list
,
Class
<
T
>
clazz
,
Page
page
)
{
PagedResult
<
T
>
result
=
new
PagedResult
<>();
if
(
page
!=
null
){
result
.
setTotal
(
page
.
getTotal
());
}
else
{
if
(
list
!=
null
){
result
.
setTotal
(
list
.
size
());
}
}
if
(
list
!=
null
&&
!
list
.
isEmpty
()){
result
.
setList
(
BeanTransferUtils
.
transferList
(
list
,
clazz
));
}
return
result
;
}
}
nafmii-common/src/main/java/org/nafmii/utils/FileUtils.java
0 → 100644
View file @
f2fdb50
package
org
.
nafmii
.
utils
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.util.StringUtils
;
import
javax.servlet.ServletOutputStream
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.IOException
;
import
java.io.InputStream
;
@Slf4j
public
class
FileUtils
{
public
static
void
downLoadFile
(
InputStream
inputStream
,
String
fileName
){
if
(
inputStream
==
null
||
StringUtils
.
isEmpty
(
fileName
)){
return
;
}
try
{
HttpServletResponse
response
=
RequestUtils
.
getResponse
();
response
.
reset
();
response
.
setContentType
(
"multipart/form-data"
);
response
.
setHeader
(
"Content-Disposition"
,
"attachment;filename="
+
new
String
(
fileName
.
getBytes
(),
"ISO-8859-1"
));
ServletOutputStream
outputStream
=
response
.
getOutputStream
();
byte
[]
buff
=
new
byte
[
1024
];
int
length
=
0
;
while
((
length
=
inputStream
.
read
(
buff
))
!=
-
1
)
{
outputStream
.
write
(
buff
,
0
,
length
);
}
if
(
outputStream
!=
null
)
{
outputStream
.
flush
();
outputStream
.
close
();
}
}
catch
(
Exception
e
)
{
log
.
error
(
e
.
getMessage
());
}
finally
{
try
{
inputStream
.
close
();
}
catch
(
IOException
e
)
{
log
.
error
(
"关闭资源出错"
+
e
.
getMessage
());
}
}
}
}
nafmii-common/src/main/java/org/nafmii/utils/PagedResult.java
0 → 100644
View file @
f2fdb50
package
org
.
nafmii
.
utils
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.Data
;
import
java.util.List
;
@Data
public
class
PagedResult
<
T
>
{
@ApiModelProperty
(
"数据"
)
private
List
<
T
>
list
;
@ApiModelProperty
(
"总条数"
)
private
long
total
;
}
nafmii-common/src/main/java/org/nafmii/utils/RedisUtils.java
0 → 100644
View file @
f2fdb50
package
org
.
nafmii
.
utils
;
import
com.alibaba.fastjson.JSONObject
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
java.util.List
;
import
java.util.concurrent.TimeUnit
;
public
class
RedisUtils
{
@SuppressWarnings
(
"unchecked"
)
private
static
RedisTemplate
<
String
,
Object
>
redisTemplate
=
SpringUtils
.
getBean
(
"redisTemplate"
,
RedisTemplate
.
class
);
public
RedisUtils
(
RedisTemplate
<
String
,
Object
>
redisTemplate
)
{
RedisUtils
.
redisTemplate
=
redisTemplate
;
}
/**
* 普通缓存获取
* @param key 键
* @return 值
*/
public
static
Object
get
(
String
key
){
return
key
==
null
?
null
:
redisTemplate
.
opsForValue
().
get
(
key
);
}
public
static
<
T
>
T
hGetObject
(
String
key
,
Class
<
T
>
clazz
){
Object
o
=
redisTemplate
.
opsForValue
().
get
(
key
);
return
JSONObject
.
parseObject
((
String
)
o
,
clazz
);
}
public
static
<
T
>
T
hGetObject
(
String
key
,
String
item
,
Class
<
T
>
clazz
){
Object
o
=
redisTemplate
.
opsForHash
().
get
(
key
,
item
);
return
JSONObject
.
parseObject
((
String
)
o
,
clazz
);
}
public
static
<
T
>
List
<
T
>
hGetList
(
String
key
,
String
item
,
Class
<
T
>
clazz
){
Object
o
=
redisTemplate
.
opsForHash
().
get
(
key
,
item
);
return
JSONObject
.
parseArray
((
String
)
o
,
clazz
);
}
/**
* 向一张hash表中放入数据,如果不存在将创建
* @param key 键
* @param item 项
* @param value 值
* @return true 成功 false失败
*/
public
static
boolean
hSet
(
String
key
,
String
item
,
Object
value
)
{
try
{
redisTemplate
.
opsForHash
().
put
(
key
,
item
,
value
);
return
true
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
false
;
}
}
public
static
boolean
hSet
(
String
key
,
Object
value
,
Long
time
,
TimeUnit
unit
)
{
try
{
redisTemplate
.
opsForValue
().
set
(
key
,
value
);
if
(
time
>
0
)
{
expire
(
key
,
time
,
unit
);
}
return
true
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
false
;
}
}
public
static
boolean
hSet
(
String
key
,
Object
value
)
{
try
{
redisTemplate
.
opsForValue
().
set
(
key
,
value
);
return
true
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
false
;
}
}
private
static
boolean
expire
(
String
key
,
long
time
,
TimeUnit
unit
){
try
{
if
(
time
>
0
){
redisTemplate
.
expire
(
key
,
time
,
unit
);
}
return
true
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
false
;
}
}
/**
* 删除hash表中的值
* @param key 键 不能为null
* @param item 项 可以使多个 不能为null
*/
public
static
void
hDel
(
String
key
,
Object
...
item
){
redisTemplate
.
opsForHash
().
delete
(
key
,
item
);
}
/**
* 删除hash表中的值
* @param key 键 不能为null
*/
public
static
void
hDel
(
String
key
){
redisTemplate
.
delete
(
key
);
}
/**
* redis自增命令
* @param key 如果key有值加1,否则新增key且初始化为0
*/
public
static
void
increment
(
String
key
,
Long
time
,
TimeUnit
unit
){
try
{
redisTemplate
.
opsForValue
().
increment
(
key
);
if
(
time
>
0
)
{
expire
(
key
,
time
,
unit
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
\ No newline at end of file
nafmii-common/src/main/java/org/nafmii/utils/RequestUtils.java
0 → 100644
View file @
f2fdb50
package
org
.
nafmii
.
utils
;
import
org.nafmii.constant.HttpClientHeaderEnum
;
import
org.nafmii.constant.MessageEnum
;
import
org.nafmii.exception.BusinessException
;
import
org.springframework.util.StringUtils
;
import
org.springframework.web.context.request.RequestAttributes
;
import
org.springframework.web.context.request.RequestContextHolder
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
public
class
RequestUtils
{
public
static
HttpServletRequest
getRequest
()
{
RequestAttributes
requestAttributes
=
RequestContextHolder
.
getRequestAttributes
();
return
(
HttpServletRequest
)
requestAttributes
.
resolveReference
(
RequestAttributes
.
REFERENCE_REQUEST
);
}
public
static
HttpServletResponse
getResponse
()
{
ServletRequestAttributes
servletRequestAttributes
=
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
HttpServletResponse
response
=
servletRequestAttributes
.
getResponse
();
return
response
;
}
public
static
Long
getUserId
(){
String
userId
=
getRequest
().
getHeader
(
HttpClientHeaderEnum
.
USER_HEADER_USER_ID
.
getCode
());
if
(!
StringUtils
.
isEmpty
(
userId
)){
try
{
return
Long
.
valueOf
(
userId
);
}
catch
(
NumberFormatException
e
)
{
return
null
;
}
}
return
null
;
}
public
static
String
getToken
(){
String
token
=
getRequest
().
getHeader
(
HttpClientHeaderEnum
.
USER_HEADER_USER_TOKEN
.
getCode
());
return
StringUtils
.
isEmpty
(
token
)
?
null
:
token
;
}
/**
* 校验是否登录,登录返回用户ID
* @return Long
*/
public
static
Long
checkLoginStatus
(){
Long
userId
=
getUserId
();
String
token
=
getToken
();
if
(
userId
==
null
||
StringUtils
.
isEmpty
(
token
)){
throw
new
BusinessException
(
MessageEnum
.
NO_LOGIN
.
getCode
(),
MessageEnum
.
NO_LOGIN
.
getRemarks
());
}
return
userId
;
}
}
nafmii-common/src/main/java/org/nafmii/utils/SpringUtils.java
0 → 100644
View file @
f2fdb50
package
org
.
nafmii
.
utils
;
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
;
import
org.springframework.web.servlet.LocaleResolver
;
import
javax.servlet.http.HttpServletRequest
;
import
java.util.Locale
;
@SuppressWarnings
(
"unchecked"
)
@Component
public
class
SpringUtils
implements
ApplicationContextAware
{
private
static
ApplicationContext
applicationContext
;
@Override
public
void
setApplicationContext
(
ApplicationContext
applicationContext
)
throws
BeansException
{
SpringUtils
.
applicationContext
=
applicationContext
;
}
private
static
<
T
>
T
getBean
(
Class
<
T
>
tClass
){
return
applicationContext
.
getBean
(
tClass
);
}
public
static
<
T
>
T
getBean
(
String
name
,
Class
<
T
>
type
)
{
return
applicationContext
.
getBean
(
name
,
type
);
}
private
static
HttpServletRequest
getCurrentReq
()
{
ServletRequestAttributes
requestAttrs
=
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
if
(
requestAttrs
==
null
)
{
return
null
;
}
return
requestAttrs
.
getRequest
();
}
public
static
String
getMessage
(
String
code
,
Object
...
args
)
{
LocaleResolver
localeResolver
=
getBean
(
LocaleResolver
.
class
);
Locale
locale
=
localeResolver
.
resolveLocale
(
getCurrentReq
());
return
applicationContext
.
getMessage
(
code
,
args
,
locale
);
}
}
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment