diff --git a/src/main/java/com/xiaolfeng/dormstar/services/DayScheduleService.java b/src/main/java/com/xiaolfeng/dormstar/services/DayScheduleService.java index 2d09ca3..cfa73eb 100644 --- a/src/main/java/com/xiaolfeng/dormstar/services/DayScheduleService.java +++ b/src/main/java/com/xiaolfeng/dormstar/services/DayScheduleService.java @@ -1,10 +1,10 @@ package com.xiaolfeng.dormstar.services; import com.fasterxml.jackson.databind.ObjectMapper; -import com.xiaolfeng.dormstar.entities.DrcomEntity; import com.xiaolfeng.dormstar.entities.DrcomLoginEntity; import com.xiaolfeng.dormstar.entities.UserEntity; import com.xiaolfeng.dormstar.mapper.UserMapper; +import com.xiaolfeng.dormstar.utils.SwitchingPeriod; import lombok.RequiredArgsConstructor; import okhttp3.OkHttpClient; import okhttp3.Request; @@ -13,7 +13,6 @@ import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.io.IOException; -import java.text.SimpleDateFormat; import java.time.LocalTime; import java.time.ZoneId; import java.util.Date; @@ -30,47 +29,11 @@ import java.util.regex.Pattern; public class DayScheduleService { private final UserMapper userMapper; private Request login; - private Request loginBody; - - @Scheduled(cron = "0 0 7 * * ?") - public void morningAutoLogin() { - UserEntity[] userEntities = userMapper.getAllUser(); - Random random = new Random(); - int getLong = random.nextInt(userEntities.length); - this.login = new Request.Builder() - .url("http://10.1.99.100:801/eportal/portal/login?callback=dr1003&login_method=1&user_account=,0," + userEntities[getLong].getUid() + "@" + userEntities[getLong].getType() + "&user_password=" + userEntities[getLong].getPassword() + "&jsVersion=4.1.3&terminal_type=1&lang=zh-cn&v=8101&lang=zh") - .build(); - this.loginBody = new Request.Builder() - .url("http://10.1.99.100/drcom/chkstatus?callback=dr1002&jsVersion=4.X&v=1117&lang=zh") - .build(); - - // Start - OkHttpClient client = new OkHttpClient(); - // 检查是否可以访问内网 - try (Response ignored = client.newCall(loginBody).execute()) { - // 处理登录 - try (Response responseLogin = client.newCall(login).execute()) { - if (responseLogin.body() != null) { - String getData = responseLogin.body().string(); - Matcher matcher = Pattern.compile("dr1003\\(([^)]+)\\)").matcher(getData); - String data = null; - if (matcher.find()) { - data = matcher.group(1); - } - ObjectMapper objectMapper = new ObjectMapper(); - DrcomLoginEntity drocm = objectMapper.readValue(data, DrcomLoginEntity.class); - System.out.println(new Date().getTime() + " [INFO] " + drocm.getMessage()); - } - } catch (IOException ignoredSecond) { - System.out.println(new SimpleDateFormat().format("yyyy-mm-dd HH:ii:ss") + " [WARNING] be unable to log in"); - } - } catch (IOException ignoredFirst) { - System.out.println(new SimpleDateFormat().format("yyyy-mm-dd HH:ii:ss") + " [WARNING] Not in the campus network, please manually manage the network"); - } - } + private Request loginDefault; @Scheduled(fixedDelay = 60000) public void executePeriodicTask() { + Date nowDate = new Date(); UserEntity[] userEntities = userMapper.getAllUser(); Random random = new Random(); int getLong = random.nextInt(userEntities.length); @@ -79,8 +42,11 @@ public class DayScheduleService { this.login = new Request.Builder() .url("http://10.1.99.100:801/eportal/portal/login?callback=dr1003&login_method=1&user_account=,0," + userEntities[getLong].getUid() + "@" + userEntities[getLong].getType() + "&user_password=" + userEntities[getLong].getPassword() + "&jsVersion=4.1.3&terminal_type=1&lang=zh-cn&v=8101&lang=zh") .build(); + this.loginDefault = new Request.Builder() + .url("http://10.1.99.100:801/eportal/portal/login?callback=dr1003&login_method=1&user_account=,0," + userEntities[getLong].getUid() + "&user_password=" + userEntities[getLong].getPassword() + "&jsVersion=4.1.3&terminal_type=1&lang=zh-cn&v=8101&lang=zh") + .build(); } - this.loginBody = new Request.Builder() + Request loginBody = new Request.Builder() .url("http://10.1.99.100/drcom/chkstatus?callback=dr1002&jsVersion=4.X&v=1117&lang=zh") .build(); @@ -100,21 +66,65 @@ public class DayScheduleService { } catch (IOException ignoreFirst) { // 检查是否可以访问内网 try (Response ignored = client.newCall(loginBody).execute()) { - // 处理登录 - try (Response responseLogin = client.newCall(login).execute()) { - if (responseLogin.body() != null) { - String getData = responseLogin.body().string(); - Matcher matcher = Pattern.compile("dr1003\\(([^)]+)\\)").matcher(getData); - String data = null; - if (matcher.find()) { - data = matcher.group(1); + // 检测时间 + if (SwitchingPeriod.checkTime(nowDate.getTime())) { + // 处理登录(网络段) + try (Response responseLogin = client.newCall(login).execute()) { + if (responseLogin.body() != null) { + String getData = responseLogin.body().string(); + Matcher matcher = Pattern.compile("dr1003\\(([^)]+)\\)").matcher(getData); + String data = null; + if (matcher.find()) { + data = matcher.group(1); + } + ObjectMapper objectMapper = new ObjectMapper(); + DrcomLoginEntity drocm = objectMapper.readValue(data, DrcomLoginEntity.class); + System.out.println(new Date().getTime() + " [INFO] " + drocm.getMessage()); } - ObjectMapper objectMapper = new ObjectMapper(); - DrcomLoginEntity drocm = objectMapper.readValue(data, DrcomLoginEntity.class); - System.out.println(new Date().getTime() + " [INFO] " + drocm.getMessage()); + } catch (IOException ignore) { + System.out.println(new Date().getTime() + " [FAILED] login failed"); + } + } else { + // 处理登录(非网络段23点) + try (Response responseLogin = client.newCall(loginDefault).execute()) { + if (responseLogin.body() != null) { + String getData = responseLogin.body().string(); + Matcher matcher = Pattern.compile("dr1003\\(([^)]+)\\)").matcher(getData); + String data = null; + if (matcher.find()) { + data = matcher.group(1); + } + ObjectMapper objectMapper = new ObjectMapper(); + DrcomLoginEntity drocm = objectMapper.readValue(data, DrcomLoginEntity.class); + // 处理登出后登录 + if ("Authentication fail".equals(drocm.getMessage())) { + Request removeLogin = new Request.Builder() + .url("http://10.1.99.100:801/eportal/portal/mac/unbind?callback=dr1002&user_account=" + userEntities[getLong].getUid() + "&jsVersion=4.1.3&v=1721&lang=zh") + .build(); + Response removeLoginFunction = client.newCall(removeLogin).execute(); + removeLoginFunction.close(); + + // 重新登录 + try (Response responseLoginSecond = client.newCall(login).execute()) { + if (responseLoginSecond.body() != null) { + String getDatas = responseLoginSecond.body().string(); + Matcher matchers = Pattern.compile("dr1003\\(([^)]+)\\)").matcher(getDatas); + data = null; + if (matchers.find()) { + data = matchers.group(1); + } + drocm = objectMapper.readValue(data, DrcomLoginEntity.class); + System.out.println(new Date().getTime() + " [INFO] " + drocm.getMessage()); + } + } catch (IOException ignore) { + System.out.println(new Date().getTime() + " [FAILED] login failed"); + } + } + System.out.println(new Date().getTime() + " [INFO] " + drocm.getMessage()); + } + } catch (IOException ignore) { + System.out.println(new Date().getTime() + " [FAILED] login failed"); } - } catch (IOException e) { - throw new RuntimeException(e); } } catch (IOException ignoreSecond) { System.out.println(new Date().getTime() + " [WARNING] Not in the campus network, please manually manage the network"); @@ -123,77 +133,4 @@ public class DayScheduleService { } } } - - @Scheduled(cron = "0 0 23 * * 0-4") - public void theWorkDay() { - this.theDayChangeNetwork(); - } - - @Scheduled(cron = "0 30 23 * * 5-6") - public void theWeekendDay() { - this.theDayChangeNetwork(); - } - - public void theDayChangeNetwork() { - UserEntity[] userEntities = userMapper.getAllUser(); - Random random = new Random(); - int getLong = random.nextInt(userEntities.length); - this.login = new Request.Builder() - .url("http://10.1.99.100:801/eportal/portal/login?callback=dr1003&login_method=1&user_account=,0," + userEntities[getLong].getUid() + "&user_password=061823zcw&jsVersion=4.1.3&terminal_type=1&lang=zh-cn&v=6795&lang=zh") - .build(); - this.loginBody = new Request.Builder() - .url("http://10.1.99.100/drcom/chkstatus?callback=dr1002&jsVersion=4.X&v=1117&lang=zh") - .build(); - - // Start - OkHttpClient client = new OkHttpClient(); - // 获取当前登录信息 - try (Response response = client.newCall(loginBody).execute()) { - // 处理信息 - if (response.body() != null) { - // 获取数据 - String getData = response.body().string(); - Matcher matcher = Pattern.compile("dr1002\\(([^)]+)\\)").matcher(getData); - String data = null; - if (matcher.find()) { - data = matcher.group(1); - } - DrcomEntity drcom = new ObjectMapper().readValue(data, DrcomEntity.class); - Matcher getUser = Pattern.compile("^[0-9]+").matcher(drcom.getUid()); - String user = getUser.group(1); - // 解除当前登录 - Request removeLogin = new Request.Builder() - .url("http://10.1.99.100:801/eportal/portal/mac/unbind?callback=dr1002&user_account=" + user + "&jsVersion=4.1.3&v=1721&lang=zh") - .build(); - try (Response responseRemoveLogin = client.newCall(removeLogin).execute()) { - if (responseRemoveLogin.body() != null) { - // 处理无锡学院登录 - try (Response responseDefault = client.newCall(login).execute()) { - if (responseDefault.body() != null) { - String getDataDefault = responseDefault.body().string(); - Matcher matcherDefault = Pattern.compile("dr1003\\(([^)]+)\\)").matcher(getDataDefault); - String dataDefault = null; - if (matcher.find()) { - dataDefault = matcherDefault.group(1); - } - ObjectMapper objectMapper = new ObjectMapper(); - DrcomLoginEntity drocm = objectMapper.readValue(dataDefault, DrcomLoginEntity.class); - System.out.println(new Date().getTime() + "[INFO] " + drocm.getMessage()); - } - } catch (IOException ignored) { - System.out.println(new SimpleDateFormat().format("yyyy-mm-dd HH:ii:ss") + " [WARNING] Not in the campus network, please manually manage the network"); - } - } else { - System.out.println("[ERROR] Object server exception"); - } - } catch (IOException ignored) { - System.out.println(new SimpleDateFormat().format("yyyy-mm-dd HH:ii:ss") + " [WARNING] Not in the campus network, please manually manage the network"); - } - } else { - System.out.println("[ERROR] Object server exception"); - } - } catch (IOException ignored) { - System.out.println(new SimpleDateFormat().format("yyyy-mm-dd HH:ii:ss") + " [WARNING] Not in the campus network, please manually manage the network"); - } - } } diff --git a/src/main/java/com/xiaolfeng/dormstar/utils/SwitchingPeriod.java b/src/main/java/com/xiaolfeng/dormstar/utils/SwitchingPeriod.java new file mode 100644 index 0000000..938bfc9 --- /dev/null +++ b/src/main/java/com/xiaolfeng/dormstar/utils/SwitchingPeriod.java @@ -0,0 +1,34 @@ +package com.xiaolfeng.dormstar.utils; + +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + +/** + * @author 筱锋xiao_lfeng + * @version v1.0.0 + */ +public class SwitchingPeriod { + /** + * + * + * @param timestamp 时间戳 + * @return 返回是否在可登录时间段 + */ + public static boolean checkTime(long timestamp) { + Date getDate = new Date(timestamp); + // 检查星期 + Calendar calendar = Calendar.getInstance(); + calendar.setTime(getDate); + + // 获取时间 + int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); + // 检查日期 + if (dayOfWeek > Calendar.SUNDAY && dayOfWeek < Calendar.WEDNESDAY) { + // 时间范围 + return 2300 >= Integer.parseInt(new SimpleDateFormat("HHmm").format(getDate)); + } else { + return 2330 >= Integer.parseInt(new SimpleDateFormat("HHmm").format(getDate)); + } + } +}