初始化

This commit is contained in:
jimlee
2023-01-10 16:52:33 +08:00
parent 78fa2be0de
commit c7633de700
17 changed files with 2784 additions and 1 deletions

View File

@@ -0,0 +1,23 @@
package com.pomelotea.hoperun.sign
import org.slf4j.LoggerFactory
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import java.text.SimpleDateFormat
import java.util.*
/**
*
* @version 0.0.1
* @author jimlee
* date 2022-07-15 10:53
* 启动入口
**/
@SpringBootApplication
open class DakaApplication {
}
private val logger = LoggerFactory.getLogger("APPLICATION-STARTER")
fun main(args: Array<String>) {
SpringApplication.run(DakaApplication::class.java, *args)
logger.info("hoperun打卡服务启动成功:{}", SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(Date()))
}

View File

@@ -0,0 +1,433 @@
package com.pomelotea.hoperun.sign.api
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.alibaba.fastjson.TypeReference
import com.pomelotea.hoperun.sign.config.HoperunUserConfig
import com.pomelotea.hoperun.sign.config.UserConfig
import okhttp3.FormBody
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.text.SimpleDateFormat
import java.time.Duration
import java.time.LocalDate
import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
/**
*
* @version 0.0.1
* @author jimlee
* date 2022-07-15 11:01
* hoperun打卡服务接口
**/
@RestController
@RequestMapping("/api/daka")
class HoperunSignController(
private val hoperunUserConfig: HoperunUserConfig
) {
companion object {
const val DAKA_URL = "http://pom.hoperun.com:8187/attm/attence/recordAttendance"
const val MONTH_ATT_URL = "http://pom.hoperun.com:8187/attm/calendar/monthAtt"
const val LOGIN_URL = "http://pom.hoperun.com:8187/attm/login/login"
val client = OkHttpClient()
.newBuilder()
.connectTimeout(Duration.ofSeconds(10))
.callTimeout(Duration.ofSeconds(10))
// .addInterceptor(LogInterceptor())
.build()
val sessionMap: MutableMap<String, String?> = HashMap()
val expireMap: MutableMap<String, Long> = HashMap()
fun getNowDateyyyy_MM(): String {
val localDate = LocalDate.now()
return "${localDate.year}-${pad(localDate.monthValue)}"
}
fun getLastDateyyyy_MM(): String {
val localDate = LocalDate.now().minusMonths(1)
return "${localDate.year}-${pad(localDate.monthValue)}"
}
fun getNowDateyyyy_MM_dd(): String {
val localDate = LocalDate.now()
return "${localDate.year}-${pad(localDate.monthValue)}-${pad(localDate.dayOfMonth)}"
}
private fun pad(num: Int): String {
return if (num < 10) "0$num" else "$num"
}
}
@GetMapping("/username/{employeeNo}")
fun getUsername(@PathVariable employeeNo: String): WebResult<LoginResponse?> {
val userConfig = getUserConfig(employeeNo)
if (userConfig == null) {
return WebResult.getFailed("登陆失败")
}
return WebResult.getSuccess(
LoginResponse(
userConfig.username,
userConfig.device,
userConfig.project_id,
userConfig.projectname,
"杭州市"
)
)
}
@GetMapping("/last/{employeeNo}")
fun getLast5DaysRecord(@PathVariable employeeNo: String): WebResult<Any?> {
val jsessionId = getJsessionIdAutoLogin(employeeNo) ?: return WebResult.getFailed("登陆失败")
return WebResult.getSuccess(monthAtt(employeeNo, jsessionId))
}
@PostMapping("/endTime")
fun endTime(@RequestBody request: DakaRequest): WebResult<Any?> {
val userConfig = hoperunUserConfig.userConfigMap.get(request.employeeNo)
if (userConfig?.device == null) {
return WebResult.getFailed("用户没有配置的deviceUA")
}
val jsessionId = getJsessionIdAutoLogin(request.employeeNo)
if (jsessionId == null) {
return WebResult.getFailed("登陆失败")
}
val date = if (request.date == "今天") SimpleDateFormat("yyyy-MM-dd").format(Date()) else request.date
val dakaRequest = Request.Builder()
.url(DAKA_URL)
.post(
JSON.toJSONString(
endTimeHoperunDakaRequest(
request.employeeNo,
date,
request.time
)
).toRequestBody("application/json;charset=utf-8".toMediaTypeOrNull())
)
.addHeader("Cookie", "JSESSIONID=$jsessionId")
.build()
val result: String? = client.newCall(dakaRequest).execute().body?.string()
return WebResult.getSuccess(JSONObject.parseObject(result, DakaResponse::class.java))
}
@PostMapping("/beginTime")
fun beginTime(@RequestBody request: DakaRequest): WebResult<Any?> {
val userConfig = hoperunUserConfig.userConfigMap.get(request.employeeNo)
if (userConfig?.device == null) {
return WebResult.getFailed("用户没有配置的deviceUA")
}
val jsessionId = getJsessionIdAutoLogin(request.employeeNo)
if (jsessionId == null) {
return WebResult.getFailed("登陆失败")
}
val date = if (request.date == "今天") SimpleDateFormat("yyyy-MM-dd").format(Date()) else request.date
val dakaRequest = Request.Builder()
.url(DAKA_URL)
.post(
JSON.toJSONString(
beginTimeHoperunDakaRequest(
request.employeeNo,
date,
request.time
)
).toRequestBody("application/json;charset=utf-8".toMediaTypeOrNull())
)
.addHeader("Cookie", "JSESSIONID=$jsessionId")
.build()
val result: String? = client.newCall(dakaRequest).execute().body?.string()
return WebResult.getSuccess(JSONObject.parseObject(result, DakaResponse::class.java))
}
private fun getJsessionIdAutoLogin(employeeNo: String): String? {
if (sessionMap.get(employeeNo) == null || expireMap.get(employeeNo) == null || expireMap.get(employeeNo)!! < System.currentTimeMillis()) {
login(employeeNo)
}
return sessionMap.get(employeeNo)
}
private fun getUsernameAutoLogin(employeeNo: String): String? {
val userConfig = hoperunUserConfig.userConfigMap.get(employeeNo)
if (userConfig?.username == null) {
login(employeeNo)
}
return hoperunUserConfig.userConfigMap.get(employeeNo)?.username
}
private fun getUserConfig(employeeNo: String): UserConfig? {
val userConfig = hoperunUserConfig.userConfigMap.get(employeeNo)
if (userConfig?.username == null) {
login(employeeNo)
}
return hoperunUserConfig.userConfigMap.get(employeeNo)
}
/**
* 查询近两个月的
*/
private fun monthAtt(employeeNo: String, jsessionId: String): List<MonthAttLog> {
val monthAttResult: MutableList<MonthAttLog> = ArrayList()
val monthAttList: List<MonthAttLog> = queryMonthAttData(employeeNo, jsessionId, getNowDateyyyy_MM() + "-01")
// 如果dateType = 1的结果小于3条查询上月
val monthAttLogs = monthAttList.sortedByDescending { it.yearmonth }.filter { it.dateType == "1" }
.filter { it.yearmonth!!.compareTo(getNowDateyyyy_MM_dd()) <= 0 }
monthAttResult.addAll(monthAttLogs)
val lastMonthAttList: List<MonthAttLog> =
queryMonthAttData(employeeNo, jsessionId, getLastDateyyyy_MM() + "-01")
val lastMonthAttLogs = lastMonthAttList.sortedByDescending { it.yearmonth }.filter { it.dateType == "1" }
monthAttResult.addAll(lastMonthAttLogs)
return monthAttResult
}
private fun queryMonthAttData(employeeNo: String, jsessionId: String, yearmonth: String): List<MonthAttLog> {
val monthAttRequest = Request.Builder()
.url(MONTH_ATT_URL)
.post(
FormBody.Builder()
.add("staff_code", padEmployeeNumber(employeeNo))
.add("yearmonth", yearmonth)
.build()
)
.addHeader("Cookie", "JSESSIONID=$jsessionId")
.build()
val result: String? = client.newCall(monthAttRequest).execute().body?.string()
return JSONObject.parseObject(
JSONObject.parseObject(result).getString("data"),
object : TypeReference<List<MonthAttLog>?>() {})
}
private fun login(employeeNo: String) {
val jsessionId: String?
val loginRequest = Request.Builder()
.url(LOGIN_URL)
.post(
FormBody.Builder()
.add("login_id", padEmployeeNumber(employeeNo))
.add("password", "123456")
.build()
)
.build()
val response = client.newCall(loginRequest).execute()
jsessionId = response.request.url.pathSegments[2].substring(19)
sessionMap.put(employeeNo, jsessionId)
expireMap.put(employeeNo, System.currentTimeMillis() + 300000)
// 读取员工姓名
if (hoperunUserConfig.userConfigMap[employeeNo]?.username == null) {
setUserConfig(employeeNo)
}
}
private fun setUserConfig(employeeNo: String) {
// 获取deviceua
val loginRequest = Request.Builder()
.url("http://pom.hoperun.com:8187/attp/login/login.do")
.post(
JSON.toJSONString(
mapOf(
"login_id" to padEmployeeNumber(employeeNo),
"password" to "123456",
"roleType" to "0"
)
).toRequestBody("application/json;charset=utf-8".toMediaTypeOrNull())
)
.build()
val response = client.newCall(loginRequest).execute()
val jsessionId = response.headers("Set-Cookie")[0].substring(11, 43)
val attendancesDetailRequest = Request.Builder()
.url("http://pom.hoperun.com:8187/attp/attendances/queryAttendancesDetail")
.post(
JSON.toJSONString(
mapOf(
"beginDate" to getLastDateyyyy_MM() + "-21",
"endDate" to getNowDateyyyy_MM_dd(),
"staffCode" to padEmployeeNumber(employeeNo)
)
).toRequestBody("application/json;charset=utf-8".toMediaTypeOrNull())
)
.addHeader("Cookie", "JSESSIONID=$jsessionId")
.build()
val attendancesDetailResponse = client.newCall(attendancesDetailRequest).execute()
val bodyString = attendancesDetailResponse.body?.string()
val dakaJsonArray = JSONObject.parseArray(bodyString)
val dakaInfo = dakaJsonArray.getJSONObject(0)
val dakaList = dakaInfo.getJSONArray("list")
var lastDakaInfo = dakaList.getJSONObject(dakaList.size - 1)
val userConfig: UserConfig = hoperunUserConfig.userConfigMap.get(employeeNo) ?: UserConfig()
if (lastDakaInfo.getString("begin_time") == null) {
for (i in dakaList.size - 1 downTo 0) {
lastDakaInfo = dakaList.getJSONObject(i)
if (lastDakaInfo.getString("actual_area_end") != null) {
break
}
}
}
val username: String = lastDakaInfo.getString("staff_name")
userConfig.username = username
if (userConfig.device == null) {
if (lastDakaInfo.getString("actual_area_end") != null) {
val area: String = lastDakaInfo.getString("actual_area_end")
userConfig.device = area.substring(area.lastIndexOf("Qing") + 13)
} else {
userConfig.device = null
}
}
if (userConfig.projectcode == null) {
userConfig.projectcode = lastDakaInfo.getString("project_id")
}
if (userConfig.projectname == null) {
userConfig.projectname = lastDakaInfo.getString("projectname")
}
if (userConfig.project_id == null) {
userConfig.project_id = lastDakaInfo.getString("project_id")
}
hoperunUserConfig.addUserConfig(
employeeNo, userConfig
)
}
private fun beginTimeHoperunDakaRequest(
employeeNo: String,
yearmonth: String,
begin_time: String?
): HoperunDakaRequest {
val ua = hoperunUserConfig.getUA(employeeNo)
val userConfig: UserConfig = getUserConfig(employeeNo)!!
val hoperunDakaRequest = HoperunDakaRequest(
staff_code = padEmployeeNumber(employeeNo),
yearmonth = yearmonth,
userConfig.project_id!!,
userConfig.projectname!!,
userConfig.projectcode!!,
actualArea = ua
)
hoperunDakaRequest.begin_time = begin_time
return hoperunDakaRequest
}
private fun endTimeHoperunDakaRequest(
employeeNo: String,
yearmonth: String,
end_time: String?
): HoperunDakaRequest {
val ua = hoperunUserConfig.getUA(employeeNo)
val userConfig: UserConfig = getUserConfig(employeeNo)!!
val hoperunDakaRequest = HoperunDakaRequest(
staff_code = padEmployeeNumber(employeeNo),
yearmonth = yearmonth,
userConfig.project_id!!,
userConfig.projectname!!,
userConfig.projectcode!!,
actualArea = ua
)
hoperunDakaRequest.end_time = end_time
return hoperunDakaRequest
}
fun padEmployeeNumber(employeeNumber: String): String {
return when (employeeNumber.length) {
5 -> "0000" + employeeNumber
4 -> "00000" + employeeNumber
else -> employeeNumber
}
}
}
data class DakaRequest(
val employeeNo: String,
val date: String,
val time: String
)
data class DakaResponse(
var result: String? = null,
var comment: String? = null,
var data: String? = null
)
class WebResult<T> protected constructor() : java.io.Serializable {
var data: T? = null
var code: Int? = null
var success = false
var message: String? = null
@Suppress("UNUSED")
var timestamp = System.currentTimeMillis()
companion object {
fun <T> getSuccess(data: T): WebResult<T> {
return getWebResult(true, data, "success", 0)
}
fun <T> getFailed(message: String = "failed"): WebResult<T?> {
return getWebResult(false, null, message, -1)
}
fun <T> getWebResult(success: Boolean, data: T, message: String?, code: Int?): WebResult<T> {
val webResult = WebResult<T>()
webResult.success = success
webResult.data = data
webResult.code = code
webResult.message = message
return webResult
}
}
}
data class MonthAttLog(
var area_id: String? = null,
var area_id_begin: String? = null,
var area_id_end: String? = null,
var attState: String? = null,
var att_type: String? = null,
var begin_time: String? = null,
var dateType: String? = null,
var departmentcode: String? = null,
var end_time: String? = null,
var project_id: String? = null,
var projectcode: String? = null,
var staff_code: String? = null,
var yearmonth: String? = null
)
data class HoperunDakaRequest(
val staff_code: String,
val yearmonth: String,
var project_id: String = "U2103S000078",
var projectname: String = "JRKF-银河资产对接合作平台贷项目",
var projectcode: String = "U2103S000078",
val area_id: String = "杭州市",
val actualArea: String,
var begin_time: String? = null,
var end_time: String? = null
)
data class LoginResponse(
var username: String? = null,
var device: String? = null,
var project_id: String? = null,
var projectname: String? = null,
var area: String? = null
)

View File

@@ -0,0 +1,59 @@
package com.pomelotea.hoperun.sign.config
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Configuration
/**
*
* @version 0.0.1
* @author jimlee
* date 2022-07-18 09:56
* 用户配置
**/
@Configuration
@ConfigurationProperties("hoperun")
open class HoperunUserConfig {
val userConfigMap: MutableMap<String, UserConfig> = HashMap()
var address: String = "浙江省杭州市西湖区万塘路18号黄龙时代广场B座"
var longitueHead: String = "120.131"
var latitudeHead: String = "30.279"
var longitueShort: String = "120.136679"
var latitudeShort: String = "30.279766"
var qingUa: String = "Qing/0.9.101"
fun getUA(emplotyeeNo: String): String {
return address +
"$longitueHead${random(11)}," +
"$latitudeHead${random(12)};" +
"$longitueShort," +
"$latitudeShort;" +
"$qingUa;" +
(userConfigMap.get(emplotyeeNo)!!.device ?: "")
}
fun addUserConfig(emplotyeeNo: String, userConfig: UserConfig) {
userConfigMap.put(emplotyeeNo, userConfig)
}
fun random(place: Int): Long {
var random = 0L
var index = 1
var divisor = 1
while (index <= place) {
random += (Math.random() * 10).toInt() * divisor
divisor *= 10
index++
}
return if (random < 0) -random else random
}
}
data class UserConfig(
var username: String? = null,
var device: String? = null,
var project_id: String? = null,
var projectname: String? = null,
var projectcode: String? = null
)

View File

@@ -0,0 +1,331 @@
package com.pomelotea.hoperun.sign.error
import com.fasterxml.jackson.databind.ObjectMapper
import com.pomelotea.hoperun.sign.api.WebResult
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController
import org.springframework.boot.web.servlet.error.ErrorAttributes
import org.springframework.core.Ordered
import org.springframework.core.annotation.Order
import org.springframework.http.HttpStatus
import org.springframework.validation.BindException
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.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.client.HttpServerErrorException
import org.springframework.web.servlet.NoHandlerFoundException
import org.springframework.web.servlet.view.RedirectView
import java.net.InetAddress
import java.net.UnknownHostException
import java.text.MessageFormat
import javax.servlet.ServletOutputStream
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
/**
*
* @version 0.0.1
* @author jimlee
* date 2022-07-18 10:51
* 全局异常处理器
**/
@Order(Ordered.HIGHEST_PRECEDENCE)
@ControllerAdvice
@RequestMapping("/error")
open class ExtendExceptionHandler(errorAttributes: ErrorAttributes) : AbstractErrorController(errorAttributes) {
/**
* 参数断言异常
* @param request HttpServletRequest 请求
* @param response HttpServletResponse 响应
* @param ex Exception 服务异常
* @throws Exception 方法抛出异常
*/
@ExceptionHandler(IllegalArgumentException::class)
@Throws(Exception::class)
fun illegalArgumentException(request: HttpServletRequest, response: HttpServletResponse, ex: Exception) {
log.error(
MessageFormat.format(
"!!! request uri:{0} from {1} assert exception:{2}",
request.requestURI,
getIpAddr(request),
ex.message
), ex
)
out(
response,
"${if (ex.message != null && ex.message!!.length > 40) ex.message!!.substring(0, 40) else ex.message}",
false,
HttpStatus.INTERNAL_SERVER_ERROR.value(),
ex.javaClass.name
)
}
/**
* 空指针异常
* @param request HttpServletRequest 请求
* @param response HttpServletResponse 响应
* @param ex Exception 服务异常
* @throws Exception 方法抛出异常
*/
@ExceptionHandler(NullPointerException::class)
@Throws(Exception::class)
fun nullPointerException(request: HttpServletRequest, response: HttpServletResponse, ex: Exception) {
log.error(
MessageFormat.format(
"!!! request uri:{0} from {1} null pointor exception:{2}",
request.requestURI,
getIpAddr(request),
ex.message
), ex
)
out(
response,
"服务器开小差了:${if (ex.message != null && ex.message!!.length > 40) ex.message!!.substring(0, 40) else ex.message}",
false,
HttpStatus.INTERNAL_SERVER_ERROR.value(),
ex.javaClass.name
)
}
/**
* 其他异常
* @param request HttpServletRequest 请求
* @param response HttpServletResponse 响应
* @param ex Exception 服务异常
* @throws Exception 方法抛出异常
*/
@ExceptionHandler(Exception::class)
@Throws(Exception::class)
fun unknownException(request: HttpServletRequest, response: HttpServletResponse, ex: Exception) {
log.error(
MessageFormat.format(
"!!! request uri:{0} from {1} unknown exception:{2}",
request.requestURI,
getIpAddr(request),
ex.message
), ex
)
out(
response,
"服务器错误:${if (ex.message != null && ex.message!!.length > 40) ex.message!!.substring(0, 40) else ex.message}",
false,
HttpStatus.INTERNAL_SERVER_ERROR.value(),
ex.javaClass.name
)
}
/**
* 500状态异常
* @param request HttpServletRequest 请求
* @param response HttpServletResponse 响应
* @param ex Exception 服务异常
* @throws Exception 方法抛出异常
*/
@ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(HttpServerErrorException.InternalServerError::class)
@Throws(Exception::class)
fun serverError(request: HttpServletRequest, response: HttpServletResponse, ex: Exception) {
log.error(
MessageFormat.format(
"!!! request uri:{0} from {1} server error[500] exception:{2}",
request.requestURI,
getIpAddr(request),
ex.message
), ex
)
out(
response,
"服务器内部错误:${if (ex.message != null && ex.message!!.length > 40) ex.message!!.substring(0, 40) else ex.message}",
false,
HttpStatus.INTERNAL_SERVER_ERROR.value(),
ex.javaClass.name
)
}
/**
* 404状态异常
* @param request HttpServletRequest 请求
* @param response HttpServletResponse 响应
* @param ex Exception 服务异常
* @throws Exception 方法抛出异常
*/
@ResponseStatus(code = HttpStatus.NOT_FOUND)
@ExceptionHandler(NoHandlerFoundException::class)
@Throws(Exception::class)
fun notFound(request: HttpServletRequest, response: HttpServletResponse, ex: Exception) {
log.error(
MessageFormat.format(
"!!! request uri:{0} from {1} not found mapping url exception:{2}",
request.requestURI,
getIpAddr(request),
ex.message
), ex
)
out(
response,
"404 Not Fount:${if (ex.message != null && ex.message!!.length > 40) ex.message!!.substring(0, 40) else ex.message}",
false,
HttpStatus.NOT_FOUND.value(),
ex.javaClass.name
)
}
/**
* 请求校验缺少必要参数
* @param request HttpServletRequest 请求
* @param response HttpServletResponse 响应
* @param ex Exception 服务异常
* @throws Exception 方法抛出异常
*/
@ResponseStatus(code = HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException::class)
@Throws(Exception::class)
fun methodArgumentNotValidException(request: HttpServletRequest, response: HttpServletResponse, ex: MethodArgumentNotValidException) {
val msg = StringBuilder()
ex.bindingResult.fieldErrors.forEach {
msg.append(
"[${it.field}]:${it.defaultMessage}"
)
}
out(
response,
msg.toString(),
false,
HttpStatus.NOT_FOUND.value(),
ex.javaClass.name
)
}
/**
* 参数绑定请求实体错误
* @param request HttpServletRequest 请求
* @param response HttpServletResponse 响应
* @param ex Exception 服务异常
* @throws Exception 方法抛出异常
*/
@ResponseStatus(code = HttpStatus.BAD_REQUEST)
@ExceptionHandler(BindException::class)
@Throws(Exception::class)
fun bindException(request: HttpServletRequest, response: HttpServletResponse, ex: BindException) {
val msg = StringBuilder()
ex.bindingResult.fieldErrors.forEach {
msg.append(
"[${it.field}]:${it.defaultMessage}"
)
}
out(
response,
msg.toString(),
false,
HttpStatus.NOT_FOUND.value(),
ex.javaClass.name
)
}
@RequestMapping
@ResponseBody
@Throws(Exception::class)
fun handleErrors(request: HttpServletRequest, response: HttpServletResponse, e: Exception) {
val status = getStatus(request)
if (status === HttpStatus.NOT_FOUND) {
notFound(request, response, e)
} else if (status === HttpStatus.INTERNAL_SERVER_ERROR) {
serverError(request, response, e)
} else unknownException(request, response, e)
}
@GetMapping
@ResponseBody
@Throws(Exception::class)
fun defaultGetError(request: HttpServletRequest, response: HttpServletResponse, e: Exception) {
handleErrors(request, response, e)
}
@ResponseStatus(code = HttpStatus.NOT_FOUND)
@RequestMapping(produces = ["text/html"])
@Throws(Exception::class)
fun handleHtml(request: HttpServletRequest, response: HttpServletResponse): RedirectView {
return RedirectView("/404")
}
companion object {
private val log: Logger = LoggerFactory.getLogger(ExtendExceptionHandler::class.java)
/**
* 全局异常错误输出
* @param response 响应对象
* @param msg 消息
* @param success 状态
* @param code 响应码
* @param data 响应实体
*/
@JvmStatic
fun out(response: HttpServletResponse, msg: String?, success: Boolean, code: Int?, data: Any? = null) {
val out: ServletOutputStream
try {
out = response.outputStream
response.reset()
response.characterEncoding = "UTF-8"
response.contentType = "application/json;charset=utf-8"
response.status = HttpStatus.OK.value()
out.write(ObjectMapper().writeValueAsBytes(WebResult.getWebResult(success, data, msg, code)))
} catch (e: Exception) {
log.error(e.toString() + "响应错误")
}
}
/**
* 获取请求IP地址
* @param request 请求
* @return String? ip地址
*/
fun getIpAddr(request: HttpServletRequest): String? {
var ipAddress: String?
return try {
ipAddress = request.getHeader("x-forwarded-for")
if (ipAddress == null || ipAddress.length == 0 || "unknown".equals(ipAddress, ignoreCase = true)) {
ipAddress = request.getHeader("Proxy-Client-IP")
}
if (ipAddress == null || ipAddress.length == 0 || "unknown".equals(ipAddress, ignoreCase = true)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP")
}
if (ipAddress == null || ipAddress.length == 0 || "unknown".equals(ipAddress, ignoreCase = true)) {
ipAddress = request.remoteAddr
// 本机访问, 获取网卡地址
if (ipAddress == "127.0.0.1" || ipAddress == "0:0:0:0:0:0:0:1") {
try {
ipAddress = InetAddress.getLocalHost().hostAddress
if (ipAddress != null) {
ipAddress += "(local)"
}
} catch (e: UnknownHostException) {
log.error("获取本机网卡IP失败", e)
}
}
}
if (ipAddress != null) {
if (ipAddress.contains(",")) {
ipAddress.split(",".toRegex()).toTypedArray()[0]
} else {
ipAddress
}
} else {
log.error("获取请求IP为空")
null
}
} catch (e: Exception) {
log.error("获取用户IP失败", e)
null
}
}
}
}