工具类操作
This commit is contained in:
parent
879cc4f8df
commit
05a3bda985
85
src/main/kotlin/com/xlf/dromstarkotlin/utils/BaseResponse.kt
Normal file
85
src/main/kotlin/com/xlf/dromstarkotlin/utils/BaseResponse.kt
Normal file
|
@ -0,0 +1,85 @@
|
|||
package com.frontleaves.general.utils
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude
|
||||
import org.springframework.http.HttpStatus
|
||||
import java.io.Serializable
|
||||
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
class BaseResponse constructor(val output: String, val code: Int, val message: String, val data: Any?) :
|
||||
Serializable {
|
||||
constructor(httpStatus: HttpStatus) : this(
|
||||
httpStatus.toString(),
|
||||
httpStatus.value(),
|
||||
httpStatus.reasonPhrase,
|
||||
null
|
||||
)
|
||||
|
||||
constructor(httpStatus: HttpStatus, data: Any?) : this(
|
||||
httpStatus.toString(),
|
||||
httpStatus.value(),
|
||||
httpStatus.reasonPhrase,
|
||||
data
|
||||
)
|
||||
|
||||
constructor(httpStatus: HttpStatus, message: String) : this(
|
||||
httpStatus.toString(),
|
||||
httpStatus.value(),
|
||||
message,
|
||||
null
|
||||
)
|
||||
|
||||
constructor(httpStatus: HttpStatus, message: String, data: Any?) : this(
|
||||
httpStatus.toString(),
|
||||
httpStatus.value(),
|
||||
message,
|
||||
data
|
||||
)
|
||||
|
||||
constructor(httpStatus: HttpStatus, code: Int, message: String) : this(
|
||||
httpStatus.toString(),
|
||||
code,
|
||||
message,
|
||||
null
|
||||
)
|
||||
|
||||
constructor(httpStatus: HttpStatus, code: Int, message: String, data: Any?) : this(
|
||||
httpStatus.toString(),
|
||||
code,
|
||||
message,
|
||||
data
|
||||
)
|
||||
|
||||
constructor(code: Int, message: String) : this(
|
||||
HttpStatus.OK.toString(),
|
||||
code, message,
|
||||
null
|
||||
)
|
||||
|
||||
constructor(code: Int, message: String, data: Any?) : this(
|
||||
HttpStatus.OK.toString(),
|
||||
code,
|
||||
message,
|
||||
data
|
||||
)
|
||||
|
||||
constructor(message: String, data: Any?) : this(
|
||||
HttpStatus.OK.toString(),
|
||||
HttpStatus.OK.value(),
|
||||
message,
|
||||
data
|
||||
)
|
||||
|
||||
constructor(data: Any?) : this(
|
||||
HttpStatus.OK.toString(),
|
||||
HttpStatus.OK.value(),
|
||||
HttpStatus.OK.reasonPhrase,
|
||||
data
|
||||
)
|
||||
|
||||
constructor() : this(
|
||||
HttpStatus.OK.toString(),
|
||||
HttpStatus.OK.value(),
|
||||
HttpStatus.OK.reasonPhrase,
|
||||
null
|
||||
)
|
||||
}
|
154
src/main/kotlin/com/xlf/dromstarkotlin/utils/ErrorCode.kt
Normal file
154
src/main/kotlin/com/xlf/dromstarkotlin/utils/ErrorCode.kt
Normal file
|
@ -0,0 +1,154 @@
|
|||
package com.frontleaves.general.utils
|
||||
|
||||
import org.springframework.http.HttpStatus
|
||||
|
||||
enum class ErrorCode(val output: String, val code: Int, val message: String, val httpStatus: HttpStatus) {
|
||||
MISSING_PATH_VALUE(
|
||||
"MissingPathValue",
|
||||
40010,
|
||||
"URL参数缺失",
|
||||
HttpStatus.BAD_REQUEST
|
||||
),
|
||||
MISSING_PARAMETER(
|
||||
"MissingParameter",
|
||||
40011,
|
||||
"Param参数缺失",
|
||||
HttpStatus.BAD_REQUEST
|
||||
),
|
||||
MISSING_REQUEST_BODY(
|
||||
"MissingRequestBody",
|
||||
40012,
|
||||
"RequestBody参数缺失",
|
||||
HttpStatus.BAD_REQUEST
|
||||
),
|
||||
USER_NOT_FOUNDED(
|
||||
"UserNotFounded",
|
||||
40014,
|
||||
"没有找到用户",
|
||||
HttpStatus.BAD_REQUEST
|
||||
),
|
||||
CONFIG_ERROR(
|
||||
"ConfigError",
|
||||
40017,
|
||||
"配置错误",
|
||||
HttpStatus.BAD_REQUEST
|
||||
),
|
||||
WHITELIST_IS_NOT_CONFIGURED(
|
||||
"WhitelistIsNotConfigured",
|
||||
40018, "未配置白名单",
|
||||
HttpStatus.BAD_REQUEST
|
||||
),
|
||||
BLACKLIST_IS_NOT_CONFIGURED(
|
||||
"BlacklistIsNotConfigured",
|
||||
40019, "未配置黑名单",
|
||||
HttpStatus.BAD_REQUEST
|
||||
),
|
||||
TYPE_OBJECT_MISMATCH(
|
||||
"TypeObjectMismatch",
|
||||
40020, "类型不匹配",
|
||||
HttpStatus.BAD_REQUEST
|
||||
),
|
||||
QPS_LIMITATION_GLOBAL(
|
||||
"QPSLimitationGlobal",
|
||||
40023,
|
||||
"全局QPS限制,请过10分钟后操作",
|
||||
HttpStatus.BAD_REQUEST
|
||||
),
|
||||
QPS_LIMITATION_VISIT(
|
||||
"QPSLimitationVisit",
|
||||
40024,
|
||||
"QPS限制,请过1分钟后操作",
|
||||
HttpStatus.BAD_REQUEST
|
||||
),
|
||||
UNABLE_TO_GENERATE_VERIFY_KEY(
|
||||
"UnableToGenerateVerifyKey",
|
||||
40025,
|
||||
"无法生成验证密钥(VerifyKey)",
|
||||
HttpStatus.BAD_REQUEST
|
||||
),
|
||||
YOU_ARE_NOT_LOGIN(
|
||||
"YouAreNotLogin",
|
||||
40110,
|
||||
"您未登录",
|
||||
HttpStatus.UNAUTHORIZED
|
||||
),
|
||||
TYPE_MISMATCH(
|
||||
"TypeMismatch",
|
||||
40310,
|
||||
"参数 [action] 错误",
|
||||
HttpStatus.FORBIDDEN
|
||||
),
|
||||
USER_EXIST(
|
||||
"UserExist",
|
||||
40311,
|
||||
"用户已存在不能创建",
|
||||
HttpStatus.FORBIDDEN
|
||||
),
|
||||
USER_BANED(
|
||||
"UserBaned",
|
||||
40312,
|
||||
"用户已被封禁",
|
||||
HttpStatus.FORBIDDEN
|
||||
),
|
||||
TOKEN_NOT_EXIST(
|
||||
"TokenNotExist",
|
||||
40313,
|
||||
"Cookie中Token不存在",
|
||||
HttpStatus.FORBIDDEN
|
||||
),
|
||||
TOKEN_VERIFY_FAILED(
|
||||
"TokenVerifyFailed",
|
||||
40314,
|
||||
"Token校验失败",
|
||||
HttpStatus.FORBIDDEN
|
||||
),
|
||||
TOKEN_NOT_FOUNDED(
|
||||
"TokenNotFounded",
|
||||
40315,
|
||||
"Token不存在",
|
||||
HttpStatus.FORBIDDEN
|
||||
),
|
||||
WRONG_PASSWORD(
|
||||
"WrongPassword",
|
||||
40316,
|
||||
"密码不正确",
|
||||
HttpStatus.FORBIDDEN
|
||||
),
|
||||
NOT_APIKEY(
|
||||
"NotApikey",
|
||||
40317,
|
||||
"apikey不正确",
|
||||
HttpStatus.FORBIDDEN
|
||||
),
|
||||
METHOD_NOT_ALLOWED(
|
||||
"MethodNotAllowed",
|
||||
40510,
|
||||
"请求方法不允许",
|
||||
HttpStatus.METHOD_NOT_ALLOWED
|
||||
),
|
||||
DATABASE_UPDATE_OPERATION_FAILED(
|
||||
"DatabaseUpdateOperationFailed",
|
||||
50010,
|
||||
"覆写数据库内容操作失败",
|
||||
HttpStatus.INTERNAL_SERVER_ERROR
|
||||
),
|
||||
DATABASE_INSERT_OPERATION_FAILED(
|
||||
"DatabaseInsertOperationFailed",
|
||||
50011,
|
||||
"插入数据库内容操作失败",
|
||||
HttpStatus.INTERNAL_SERVER_ERROR
|
||||
),
|
||||
DATABASE_DELETE_OPERATION_FAILED(
|
||||
"DatabaseDeleteOperationFailed",
|
||||
50012,
|
||||
"删除数据库内容操作失败",
|
||||
HttpStatus.INTERNAL_SERVER_ERROR
|
||||
),
|
||||
HTTP_MESSAGE_NOT_READABLE_EXCEPTION(
|
||||
"HttpMessageNotReadableException",
|
||||
50013,
|
||||
"Http消息不可读异常",
|
||||
HttpStatus.INTERNAL_SERVER_ERROR
|
||||
);
|
||||
|
||||
}
|
44
src/main/kotlin/com/xlf/dromstarkotlin/utils/Processing.kt
Normal file
44
src/main/kotlin/com/xlf/dromstarkotlin/utils/Processing.kt
Normal file
|
@ -0,0 +1,44 @@
|
|||
package com.xlf.dromstarkotlin.utils
|
||||
|
||||
import java.util.Date
|
||||
import java.util.Random
|
||||
|
||||
object Processing {
|
||||
|
||||
/**
|
||||
* 创建固定长度随机字符串
|
||||
*/
|
||||
fun createRandomString(length: Int) = createRandomString(length, length)
|
||||
|
||||
/**
|
||||
* 创建范围长度随机字符串
|
||||
*/
|
||||
fun createRandomString(min: Int, max: Int): String {
|
||||
return ""
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 Token 令牌
|
||||
*/
|
||||
fun createToken(): String {
|
||||
var token = (Date().time/1000).toString()
|
||||
val random = Random()
|
||||
// 判定随机范围
|
||||
var i: Int
|
||||
var j = 0
|
||||
while (j++ < 3) {
|
||||
val x: Int = if(j == 1 || j == 2) 6 else 10
|
||||
token += "-"
|
||||
i = 0
|
||||
while (i < x) {
|
||||
if (random.nextBoolean()) {
|
||||
token += random.nextInt(10)
|
||||
} else {
|
||||
token += random.nextInt(97, 123).toChar()
|
||||
}
|
||||
i++
|
||||
}
|
||||
}
|
||||
return token
|
||||
}
|
||||
}
|
140
src/main/kotlin/com/xlf/dromstarkotlin/utils/ResultUtil.kt
Normal file
140
src/main/kotlin/com/xlf/dromstarkotlin/utils/ResultUtil.kt
Normal file
|
@ -0,0 +1,140 @@
|
|||
package com.frontleaves.general.utils
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.ResponseEntity
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.*
|
||||
|
||||
class ResultUtil constructor(val output: String, val code: Int, val message: String, val data: Any?) {
|
||||
companion object {
|
||||
/**
|
||||
* ## 无参正确返回
|
||||
*
|
||||
* 返回状态码为 200,返回信息为 OK,返回数据为 null
|
||||
*
|
||||
* @return BaseResponse
|
||||
*/
|
||||
fun success(): ResponseEntity<BaseResponse> {
|
||||
return ResponseEntity
|
||||
.status(200)
|
||||
.body(BaseResponse())
|
||||
}
|
||||
|
||||
/**
|
||||
* ## 正确返回
|
||||
*
|
||||
* 返回状态码为 200,返回信息为 [message],返回数据为 null
|
||||
*
|
||||
* @return BaseResponse
|
||||
*/
|
||||
fun success(message: String, httpServletRequest: HttpServletRequest): ResponseEntity<BaseResponse> {
|
||||
println("${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())} [Log] <200>Success | Message:$message | URI:${httpServletRequest.requestURI}")
|
||||
return ResponseEntity
|
||||
.status(200)
|
||||
.body(BaseResponse(message, null))
|
||||
}
|
||||
|
||||
fun success(output: String, message: String, data: Any?, httpServletRequest: HttpServletRequest): ResponseEntity<BaseResponse> {
|
||||
println("${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())} [Log] <200>$output | Message:$message | URI:${httpServletRequest.requestURI}")
|
||||
return ResponseEntity
|
||||
.status(200)
|
||||
.body(BaseResponse(output, 200, message, data))
|
||||
}
|
||||
|
||||
/**
|
||||
* ## 正确返回
|
||||
*
|
||||
* 返回状态码为 200,返回信息为 [message],返回数据为 [data]
|
||||
*
|
||||
* @return BaseResponse
|
||||
*/
|
||||
fun success(message: String, data: Any?, httpServletRequest: HttpServletRequest): ResponseEntity<BaseResponse> {
|
||||
println("${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())} [Log] <200>Success | Message:$message | URI:${httpServletRequest.requestURI}")
|
||||
return ResponseEntity
|
||||
.status(200)
|
||||
.body(BaseResponse(message, data))
|
||||
}
|
||||
|
||||
/**
|
||||
* ## 正确返回
|
||||
*
|
||||
* 返回状态码为 200,返回信息为 OK,返回数据为 [data]
|
||||
*
|
||||
* @return BaseResponse
|
||||
*/
|
||||
fun success(data: Any?, httpServletRequest: HttpServletRequest): ResponseEntity<BaseResponse> {
|
||||
return success("OK", data, httpServletRequest)
|
||||
}
|
||||
|
||||
/**
|
||||
* ## 错误返回
|
||||
*
|
||||
* 返回数据 [errorCode]
|
||||
*
|
||||
* 返回内容依据 [errorCode] 内枚举为准,返回数据为 null
|
||||
*
|
||||
* @return BaseResponse
|
||||
*/
|
||||
fun error(errorCode: ErrorCode, httpServletRequest: HttpServletRequest): ResponseEntity<BaseResponse> {
|
||||
println("${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())} [Log] <${errorCode.code}>${errorCode.output} | Message:${errorCode.message} | URI:${httpServletRequest.requestURI}")
|
||||
return ResponseEntity
|
||||
.status(errorCode.httpStatus)
|
||||
.body(BaseResponse(errorCode.output, errorCode.code, errorCode.message, null))
|
||||
}
|
||||
|
||||
/**
|
||||
* ## 错误返回
|
||||
*
|
||||
* 返回数据 [errorCode]
|
||||
*
|
||||
* 返回内容依据 [errorCode] 内枚举为准,描述信息为 [message] ,返回数据为 null
|
||||
*
|
||||
* @return BaseResponse
|
||||
*/
|
||||
fun error(errorCode: ErrorCode, message: String, httpServletRequest: HttpServletRequest): ResponseEntity<BaseResponse> {
|
||||
println("${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())} [Log] <${errorCode.code}>${errorCode.output} | Message:$message | URI:${httpServletRequest.requestURI}")
|
||||
return ResponseEntity
|
||||
.status(errorCode.httpStatus)
|
||||
.body(BaseResponse(errorCode.output, errorCode.code, message, null))
|
||||
}
|
||||
|
||||
/**
|
||||
* ## 错误返回
|
||||
*
|
||||
* 返回数据 [errorCode]
|
||||
*
|
||||
* 返回内容依据 [errorCode] 内枚举为准,描述信息为 [message] ,返回数据为 [data]
|
||||
*
|
||||
* @return BaseResponse
|
||||
*/
|
||||
fun error(errorCode: ErrorCode, data: Any?, httpServletRequest: HttpServletRequest): ResponseEntity<BaseResponse> {
|
||||
println("${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())} [Log] <${errorCode.code}>${errorCode.output} | Message:${errorCode.message} | URI:${httpServletRequest.requestURI}")
|
||||
return ResponseEntity
|
||||
.status(errorCode.httpStatus)
|
||||
.body(BaseResponse(errorCode.output, errorCode.code, errorCode.message, data))
|
||||
}
|
||||
|
||||
fun redirect(link:String, httpServletRequest: HttpServletRequest): ResponseEntity<BaseResponse> {
|
||||
println("${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())} [Log] <302>Redirect | Message:Redirect to $link | URI:${httpServletRequest.requestURI}")
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.FOUND)
|
||||
.header("Location", link)
|
||||
.body(BaseResponse("Redirect", 302, "重定向", null))
|
||||
}
|
||||
|
||||
fun redirect(output: String, message: String, data: Any?, link: String?, httpServletRequest: HttpServletRequest): ResponseEntity<BaseResponse> {
|
||||
println("${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())} [Log] <200>$output | Message:$message, Redirect to $link | URI:${httpServletRequest.requestURI}")
|
||||
return if (link != null) {
|
||||
ResponseEntity
|
||||
.status(HttpStatus.FOUND)
|
||||
.header("Location", link)
|
||||
.body(BaseResponse(output, 302, message, data))
|
||||
} else {
|
||||
ResponseEntity
|
||||
.status(HttpStatus.OK)
|
||||
.body(BaseResponse(output, 200, message, data))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user