🆗 由于默认密码变更,已无法后台登录,改为使用jsessionID登录

🆗 兼容新的打卡
🆗 增加自动打卡开关
This commit is contained in:
jimlee
2023-03-22 18:10:03 +08:00
parent f69ec33050
commit becdffe1d4
13 changed files with 681 additions and 253 deletions

View File

@@ -3,81 +3,47 @@ package com.pomelotea.hoperun.sign.api
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import com.alibaba.fastjson.TypeReference
import com.pomelotea.hoperun.sign.api.model.AttendancesDetail
import com.pomelotea.hoperun.sign.api.model.AttendancesDetailResponse
import com.pomelotea.hoperun.sign.common.*
import com.pomelotea.hoperun.sign.config.HoperunUserConfig
import com.pomelotea.hoperun.sign.config.HoperunUserConfig.deviceMap
import com.pomelotea.hoperun.sign.config.HoperunUserConfig.getUserConfig
import com.pomelotea.hoperun.sign.config.HoperunUserConfig.userConfigMap
import com.pomelotea.hoperun.sign.config.UserConfig
import com.pomelotea.hoperun.sign.scheduler.AutoDakaScheduler
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 org.jsoup.Jsoup
import org.springframework.web.bind.annotation.*
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
* update: 2023/3/22
* hoperun打卡服务接口
**/
@RestController
@RequestMapping("/api/daka")
class HoperunSignController(
private val hoperunUserConfig: HoperunUserConfig
) {
class HoperunSignController {
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"
}
init {
AutoDakaScheduler()
}
@GetMapping("/username/{employeeNo}")
fun getUsername(@PathVariable employeeNo: String): WebResult<LoginResponse?> {
val userConfig = getUserConfig(employeeNo)
if (userConfig == null) {
return WebResult.getFailed("登陆失败")
}
@GetMapping("/username/{employeeNo}/{jsessionId}/{checked}")
fun getUsername(@PathVariable employeeNo: String, @PathVariable jsessionId: String, @PathVariable checked: Boolean): WebResult<LoginResponse?> {
getJsessionIdAutoLogin(employeeNo, jsessionId)
val userConfig = getUserConfig(employeeNo) ?: return WebResult.getFailed("登陆失败")
userConfig.autoDaka = checked
userConfig.device = deviceMap[employeeNo]
return WebResult.getSuccess(
LoginResponse(
userConfig.username,
@@ -89,19 +55,26 @@ class HoperunSignController(
)
}
@GetMapping("/auto/{employeeNo}/{checked}")
fun autoDaka(@PathVariable employeeNo: String, @PathVariable checked: Boolean): WebResult<Any?> {
val userConfig = getUserConfig(employeeNo) ?: return WebResult.getFailed("需要重新登录")
userConfig.autoDaka = checked
return WebResult.getSuccess(checked)
}
@GetMapping("/last/{employeeNo}")
fun getLast5DaysRecord(@PathVariable employeeNo: String): WebResult<Any?> {
val jsessionId = getJsessionIdAutoLogin(employeeNo) ?: return WebResult.getFailed("登陆失败")
val jsessionId = sessionMap.get(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)
val userConfig = userConfigMap.get(request.employeeNo)
if (userConfig?.device == null) {
return WebResult.getFailed("用户没有配置的deviceUA")
}
val jsessionId = getJsessionIdAutoLogin(request.employeeNo)
val jsessionId = sessionMap.get(request.employeeNo)
if (jsessionId == null) {
return WebResult.getFailed("登陆失败")
}
@@ -126,14 +99,11 @@ class HoperunSignController(
@PostMapping("/beginTime")
fun beginTime(@RequestBody request: DakaRequest): WebResult<Any?> {
val userConfig = hoperunUserConfig.userConfigMap.get(request.employeeNo)
val userConfig = userConfigMap.get(request.employeeNo)
if (userConfig?.device == null) {
return WebResult.getFailed("用户没有配置的deviceUA")
}
val jsessionId = getJsessionIdAutoLogin(request.employeeNo)
if (jsessionId == null) {
return WebResult.getFailed("登陆失败")
}
val jsessionId = sessionMap.get(request.employeeNo)
val date = if (request.date == "今天") SimpleDateFormat("yyyy-MM-dd").format(Date()) else request.date
val dakaRequest = Request.Builder()
.url(DAKA_URL)
@@ -152,21 +122,13 @@ class HoperunSignController(
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)
private fun getJsessionIdAutoLogin(employeeNo: String, jsessionId: String): String? {
if (sessionMap.get(employeeNo) == null) {
login(employeeNo, jsessionId)
}
return sessionMap.get(employeeNo)
}
private fun getUserConfig(employeeNo: String): UserConfig? {
val userConfig = hoperunUserConfig.userConfigMap.get(employeeNo)
if (userConfig?.username == null) {
login(employeeNo)
}
return hoperunUserConfig.userConfigMap.get(employeeNo)
}
/**
* 查询近两个月的
*/
@@ -201,150 +163,64 @@ class HoperunSignController(
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)
private fun login(employeeNo: String, jsessionId: String) {
// 读取员工姓名
if (hoperunUserConfig.userConfigMap[employeeNo]?.username == null) {
setUserConfig(employeeNo)
if (userConfigMap[employeeNo]?.username == null) {
setUserConfig(employeeNo, jsessionId)
sessionMap.put(employeeNo, jsessionId)
}
}
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)
private fun setUserConfig(employeeNo: String, jsessionId: String) {
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())
)
.url("http://pom.hoperun.com:8187/attm/calendar/monthAtt?staff_code=${padEmployeeNumber(employeeNo)}&yearmonth=${getNowDateyyyy_MM()}-01")
.get()
.addHeader("Cookie", "JSESSIONID=$jsessionId")
.addHeader("User-Agent", "Qing/0.9.113;iOS 16.3.1;Apple;iPhone13,2;deviceId:a8baf66f-fdeb-4f4d-b1e5-9fafcd5045b6;deviceName:iOS;clientId:10200;os:iOS 16.3.1;brand:Apple;model:iPhone13,2;lang:zh-CN;fontNum:0;fontScale:1.0;ver:10.7.14;Mozilla/5.0 (iPhone; CPU iPhone OS 16_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148")
.addHeader("accept", "*/*")
.addHeader("Origin", "http://pom.hoperun.com:8187")
.addHeader("Referer", "http://pom.hoperun.com:8187/attm/attence/getInfo")
.addHeader("content-type", "application/x-www-form-urlencoded; charset=UTF-8")
.build()
val attendancesDetailResponse = client.newCall(attendancesDetailRequest).execute()
val bodyString = attendancesDetailResponse.body?.string()
val dakaJsonArray = JSONObject.parseArray(bodyString)
val dakaInfo = dakaJsonArray.getJSONObject(dakaJsonArray.size - 1)
val dakaList = dakaInfo.getJSONArray("list")
var lastDakaInfo = dakaList.getJSONObject(dakaList.size - 1)
val userConfig: UserConfig = hoperunUserConfig.userConfigMap.get(employeeNo) ?: UserConfig()
val attendancesDetailResp = client.newCall(attendancesDetailRequest).execute()
val bodyString = attendancesDetailResp.body?.string()
val attendancesDetailResponse = JSONObject.parseObject(bodyString, AttendancesDetailResponse::class.java)
val dataList: List<AttendancesDetail> = JSONObject.parseObject(attendancesDetailResponse.data, object : TypeReference<List<AttendancesDetail>>() {})
val lastDakaInfo: AttendancesDetail = dataList
.filter { it.project_id != "-1" }
.findLast { it.yearmonth == "2023-03-21" }!!
val userConfig: UserConfig = 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 if (lastDakaInfo.getString("actual_area_begin") != null) {
val area: String = lastDakaInfo.getString("actual_area_begin")
userConfig.device = area.substring(area.lastIndexOf("Qing") + 13)
} else {
userConfig.device = null
}
}
if (userConfig.projectcode == null) {
userConfig.projectcode = dakaInfo.getString("pro_id")
userConfig.projectcode = lastDakaInfo.projectcode
}
if (userConfig.projectname == null) {
userConfig.projectname = dakaInfo.getString("pro_name")
userConfig.projectname = lastDakaInfo.projectname
}
if (userConfig.project_id == null) {
userConfig.project_id = dakaInfo.getString("pro_id")
userConfig.project_id = lastDakaInfo.project_id
}
hoperunUserConfig.addUserConfig(
// username 要从主页的html元素中获取
val indexRequest = Request.Builder()
.url("http://pom.hoperun.com:8187/attm/attence/getInfo")
.get()
.addHeader("Cookie", "JSESSIONID=$jsessionId")
.addHeader("User-Agent", "Qing/0.9.113;iOS 16.3.1;Apple;iPhone13,2;deviceId:a8baf66f-fdeb-4f4d-b1e5-9fafcd5045b6;deviceName:iOS;clientId:10200;os:iOS 16.3.1;brand:Apple;model:iPhone13,2;lang:zh-CN;fontNum:0;fontScale:1.0;ver:10.7.14;Mozilla/5.0 (iPhone; CPU iPhone OS 16_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148")
.addHeader("accept", "*/*")
.addHeader("Origin", "http://pom.hoperun.com:8187")
.addHeader("Referer", "http://pom.hoperun.com:8187/attm/attence/getInfo")
.addHeader("content-type", "application/x-www-form-urlencoded; charset=UTF-8")
.build()
val indexResp = client.newCall(indexRequest).execute()
val indexHtml = indexResp.body?.string()
val doc = Jsoup.parse(indexHtml!!)
userConfig.username = doc.select("#attendance-detail-content > div.container.none-padding > div > div:nth-child(1) > div:nth-child(4)").text()
userConfig.employeeNo = employeeNo
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
}
}
}

View File

@@ -0,0 +1,30 @@
package com.pomelotea.hoperun.sign.api.model
/**
*
* @version 0.0.1
* @author jimlee
* date 2023-03-22 13:52
*
**/
data class AttendancesDetailResponse(
val data: String,
val success: Boolean = false
)
data class AttendancesDetail(
val area_id: String? = null,
val area_id_begin: String? = null,
val area_id_end: String? = null,
val attState: String? = null,
val att_type: String? = null,
val begin_time: String? = null,
val dateType: String? = null,
val departmentcode: String? = null,
val end_time: String? = null,
val project_id: String? = null,
val projectcode: String? = null,
val projectname: String? = null,
val staff_code: String? = null,
val yearmonth: String? = null,
)