diff --git a/src/main/kotlin/com/pomelotea/hoperun/sign/api/HoperunDakaController.kt b/src/main/kotlin/com/pomelotea/hoperun/sign/api/HoperunDakaController.kt index 512a8ea..de73251 100644 --- a/src/main/kotlin/com/pomelotea/hoperun/sign/api/HoperunDakaController.kt +++ b/src/main/kotlin/com/pomelotea/hoperun/sign/api/HoperunDakaController.kt @@ -13,6 +13,7 @@ import com.pomelotea.hoperun.sign.config.HoperunUserConfig.userConfigMap import com.pomelotea.hoperun.sign.config.UserConfig import com.pomelotea.hoperun.sign.scheduler.AutoDakaScheduler import com.pomelotea.hoperun.sign.scheduler.AutoDakaScheduler.Companion.dakaQueue +import com.pomelotea.hoperun.sign.scheduler.AutoRenewSessionScheduler import okhttp3.FormBody import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.Request @@ -35,8 +36,11 @@ import java.util.* @RequestMapping("/api/daka") class HoperunSignController { + val LOGIN_URL = "http://pom.hoperun.com:8187/attm/login/login" + init { AutoDakaScheduler() + AutoRenewSessionScheduler() val yxl = UserConfig( project_id = "U2103S000112", projectcode = "U2103S000112", @@ -46,14 +50,22 @@ class HoperunSignController { userConfigMap["16638"] = yxl } - @GetMapping("/username/{employeeNo}/{jsessionId}/{checked}") +// @GetMapping("/username/{employeeNo}/{checked}/{jsessionId}") + @GetMapping("/username/{employeeNo}/{checked}") fun getUsername( @PathVariable employeeNo: String, - @PathVariable jsessionId: String, @PathVariable checked: Boolean ): WebResult { - getJsessionIdAutoLogin(employeeNo, jsessionId) - val userConfig = getUserConfig(employeeNo) ?: return WebResult.getFailed("登陆失败") +/* + if (jsessionId != null) { + getJsessionIdAutoLogin(employeeNo, jsessionId) + }*/ + + val userConfig = getUserConfig(employeeNo).let { + oldLogin(employeeNo) + getUserConfig(employeeNo) + } + userConfig ?: return WebResult.getFailed("登陆失败") userConfig.autoDaka = checked userConfig.device = deviceMap[employeeNo] return WebResult.getSuccess( @@ -138,6 +150,7 @@ class HoperunSignController { if (sessionMap.get(employeeNo) == null) { login(employeeNo, jsessionId) } + sessionMap[employeeNo] = jsessionId return sessionMap.get(employeeNo) } @@ -186,10 +199,26 @@ class HoperunSignController { // 读取员工姓名 if (userConfigMap[employeeNo]?.username == null) { setUserConfig(employeeNo, jsessionId) - sessionMap.put(employeeNo, jsessionId) } } + private fun oldLogin(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) + setUserConfigOld(employeeNo) + } + private fun setUserConfig(employeeNo: String, jsessionId: String) { val attendancesDetailRequest = Request.Builder() .url("http://pom.hoperun.com:8187/attm/calendar/monthAtt?staff_code=${padEmployeeNumber(employeeNo)}&yearmonth=${getNowDateyyyy_MM()}-01") @@ -252,6 +281,77 @@ class HoperunSignController { ) } + + private fun setUserConfigOld(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(dakaJsonArray.size - 1) + val dakaList = dakaInfo.getJSONArray("list") + var lastDakaInfo = dakaList.getJSONObject(dakaList.size - 1) + 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") + } + if (userConfig.projectname == null) { + userConfig.projectname = dakaInfo.getString("pro_name") + } + if (userConfig.project_id == null) { + userConfig.project_id = dakaInfo.getString("pro_id") + } + userConfigMap[employeeNo] = userConfig + } } diff --git a/src/main/kotlin/com/pomelotea/hoperun/sign/config/HoperunUserConfig.kt b/src/main/kotlin/com/pomelotea/hoperun/sign/config/HoperunUserConfig.kt index 2498c3c..ba4d26c 100644 --- a/src/main/kotlin/com/pomelotea/hoperun/sign/config/HoperunUserConfig.kt +++ b/src/main/kotlin/com/pomelotea/hoperun/sign/config/HoperunUserConfig.kt @@ -11,7 +11,7 @@ object HoperunUserConfig { val deviceMap = mapOf( "16638" to "Android 12;Redmi;M2007J3SC;deviceId:OAIDe7fa6084205e9a22d8f6f71bc91893ff;deviceName:Android", - "9119" to "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" + "9119" to "iOS 16.4.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" ) val userConfigMap: MutableMap = HashMap() diff --git a/src/main/kotlin/com/pomelotea/hoperun/sign/scheduler/AutoRenewSessionScheduler.kt b/src/main/kotlin/com/pomelotea/hoperun/sign/scheduler/AutoRenewSessionScheduler.kt new file mode 100644 index 0000000..2074581 --- /dev/null +++ b/src/main/kotlin/com/pomelotea/hoperun/sign/scheduler/AutoRenewSessionScheduler.kt @@ -0,0 +1,79 @@ +package com.pomelotea.hoperun.sign.scheduler + +import com.pomelotea.hoperun.sign.common.client +import com.pomelotea.hoperun.sign.common.sessionMap +import okhttp3.Request +import org.jsoup.Jsoup +import java.io.BufferedWriter +import java.io.File +import java.io.OutputStreamWriter +import java.util.* +import java.util.concurrent.Executors +import java.util.concurrent.ScheduledExecutorService +import java.util.concurrent.TimeUnit + +/** + * + * @version 0.0.1 + * @author jimlee + * date 2023-03-23 08:54 + * + **/ +class AutoRenewSessionScheduler { + + val outFile = File("./renewsession.log") + val bw: BufferedWriter = BufferedWriter(OutputStreamWriter(outFile.outputStream())) + + val schedulerThreadPool: ScheduledExecutorService = Executors.newScheduledThreadPool(1) + + init { + schedulerThreadPool.schedule({renewSession()}, 2, TimeUnit.MINUTES) + } + + private fun renewSession() { + val iterator = sessionMap.iterator() + while (iterator.hasNext()) { + val item = iterator.next() + item.value?.let { + val result = index(it) + if (result != null) { + bw.write("[RENEW-SESSION]:SUCCESS:USER:$result") + bw.newLine() + bw.flush() + } else { + bw.write("[REMOVE-SESSION]:USER:$result") + bw.newLine() + bw.flush() + iterator.remove() + } + } + } + schedulerThreadPool.schedule({renewSession()}, (30 + Random().nextInt(30)).toLong(), TimeUnit.MINUTES) + } + + private fun index(jsessionId: String): String? { + 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 username = try { + Jsoup.parse(indexHtml!!).select("#attendance-detail-content > div.container.none-padding > div > div:nth-child(1) > div:nth-child(4)") + .text() + } catch (e: Exception) { + null + } + return username + } + +} \ No newline at end of file diff --git a/src/main/resources/static/index.0.html b/src/main/resources/static/index.0.html index 1af7b84..9372e45 100644 --- a/src/main/resources/static/index.0.html +++ b/src/main/resources/static/index.0.html @@ -37,9 +37,9 @@
-
+
自动打卡开关
diff --git a/src/main/resources/static/index.html b/src/main/resources/static/index.html index 31811c8..0a4afd6 100644 --- a/src/main/resources/static/index.html +++ b/src/main/resources/static/index.html @@ -1 +1 @@ -打卡
打卡记录
\ No newline at end of file +打卡
打卡记录
\ No newline at end of file diff --git a/src/main/resources/static/script.js b/src/main/resources/static/script.js index 8b60e63..4a52a7b 100644 --- a/src/main/resources/static/script.js +++ b/src/main/resources/static/script.js @@ -29,13 +29,13 @@ if ($('#login-span').css('display') !== 'none') { } -let jsessonIdInput = document.querySelector('#jsessionId'); +//let jsessonIdInput = document.querySelector('#jsessionId'); let autoDaka = document.querySelector("#toggle--switch") autoDaka.checked = window.localStorage.getItem("autodaka") document.querySelector('#login').addEventListener("click", () => { window.localStorage.setItem("employeeNo", employeeNumberInput.value); - window.localStorage.setItem("jsessionId", jsessonIdInput.value); + // window.localStorage.setItem("jsessionId", jsessonIdInput.value); window.localStorage.setItem("autodaka", autoDaka.checked); loginAndLoadRecord(); }) @@ -44,12 +44,12 @@ function loginAndLoadRecord() { $.ajax({ method: "get", - url: "/api/daka/username/" + window.localStorage.getItem("employeeNo") + "/" + window.localStorage.getItem("jsessionId") + "/" + window.localStorage.getItem("autodaka") , + url: "/api/daka/username/" + window.localStorage.getItem("employeeNo") + "/" + window.localStorage.getItem("autodaka"), success: function (result) { if (result.success) { // $("#login-span").css("display", "none"); $("#login").css("display", "none"); - $("#jsessionId").css("display", "none"); + //$("#jsessionId").css("display", "none"); $("#employee-number").css("display", "none"); $('#username').text(result.data.username); window.localStorage.setItem("username", result.data.username);