打卡任务开发完成

This commit is contained in:
xx572959496 2024-12-05 16:43:05 +08:00
parent a1ddf2add6
commit 1c7e4dfa54
4 changed files with 100 additions and 41 deletions

View File

@ -1,54 +1,95 @@
package com.dx.union;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.text.csv.CsvReader;
import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.List;
import java.util.Properties;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
/**
*
*/
public class CheckLogin {
private static final Logger log = LogManager.getLogger(Main.class);
private static final Logger log = LogManager.getLogger(CheckLogin.class);
private static final char[] HEX_CHARS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
private static final String DEFAULT_FILE_PATH = "/tmp/users.csv";
public static void main(String[] args) {
try {
log.info("==== 开始运行打卡任务 ====");
String morning = DateUtil.today() + " 09:00:00";
String afternoon = DateUtil.today() + " 18:00:00";
DateTime morningDate = DateUtil.parse(morning);
DateTime afternoonDate = DateUtil.parse(afternoon);
DateTime now = DateUtil.date();
DateTime beginOfDay = DateUtil.beginOfDay(now);
DateTime endOfDay = DateUtil.endOfDay(now);
log.info("==== 当前时间:{} ====", now.toString());
boolean isMorning = DateUtil.isIn(now, beginOfDay, morningDate);
boolean isAfterNoon = DateUtil.isIn(now, afternoonDate, endOfDay);
if (!isMorning && !isAfterNoon) {
log.info("==== 现在不是打卡时间哦! ====");
return;
}
int singMark = isMorning ? 0 : 1;
log.info("==== 现在要打 {} 卡 ====", isMorning ? "上班" : "下班");
String filePath = args.length > 0 ? args[0] : DEFAULT_FILE_PATH;
if (!FileUtil.exist(filePath)) {
log.error("未自定义账号文件路径(运行命令行添加自定义账号文件路径 例如java -jar xxx.jar /use/local/user.csv默认文件路径 {} 读取为空", DEFAULT_FILE_PATH);
return;
}
final CsvReader reader = CsvUtil.getReader();
// Properties properties = getProperties();
// String csv_file_path = properties.getProperty("csv_file_path");
// log.info("csv_file_path:{}", csv_file_path);
//假设csv文件在classpath目录下
final List<User> userList = reader.read(
ResourceUtil.getUtf8Reader("users.csv"), User.class);
log.info("读取到 {} 个需打卡账号", userList.size());
userList.forEach(user -> {
log.info("开始打卡 {} 的账号,密码为:{}", user.getUsername(), user.getPassword());
boolean isSignSuccess = sign(user);
});
ResourceUtil.getUtf8Reader(filePath), User.class);
int size = userList.size();
log.info("共有 {} 个需打卡账号", size);
userList.forEach(forEachWithIndex((user, index) -> {
boolean isSignSuccess = sign(user, singMark);
if (isSignSuccess) {
log.info("==== {} 账号打卡成功 ====", user.getUsername());
} else {
log.error("==== {} 账号打卡失败 ====", user.getUsername());
}
if (index + 1 == size) {
return;
}
// 随机 1-5分钟之后执行下一个人的打卡
// 1 * 60 * 1000
BigDecimal randomMinute = NumberUtil.round(RandomUtil.randomFloat(1, 6),2);
log.info("==== {}分钟后 执行下个人的打卡任务 ====", randomMinute);
ThreadUtil.safeSleep(NumberUtil.mul(randomMinute, 60, 1000));
}));
} catch (Exception e) {
log.error(e);
} finally {
log.info("==== 打卡任务结束 ====");
}
}
public static boolean sign(User user) {
public static boolean sign(User user, Integer signMark) {
boolean result = false;
try {
String password = encrypt2ToMD5("weaver@2024");
System.out.println(password);
String username = "WBzhangmiao";
log.info("开始{}打卡 {} 的账号,密码为:{}", signMark == 0 ? "上班" : "下班", user.getUsername(), user.getPassword());
String password = encrypt2ToMD5(user.getPassword());
String username = user.getUsername();
log.info("加密密码为:{}", password);
String body = HttpRequest.post("https://login.oa.unionpay.com/idsapi/portal/Login")
.form("username", username)
.form("password", password)
@ -57,14 +98,29 @@ public class CheckLogin {
.form("loginType", "normal")
.form("code", "")
.execute().body();
JSONObject bodyJson = JSONUtil.parseObj(body);
String token = bodyJson.getJSONObject("data").getStr("token");
String thirdBody = HttpRequest.get("http://oms.oa.unionpay.com/prod-api/checkTokenThenLogin?personType=0&token=" + token).execute().body();
JSONObject thirdBodyJson = JSONUtil.parseObj(thirdBody);
String thirdToken = thirdBodyJson.getStr("localToken");
String resultBody = HttpRequest.post("http://oms.oa.unionpay.com/prod-api/atds/ds/1").header("Authorization", "Bearer " + thirdToken).execute().body();
System.out.println(resultBody);
result = true;
log.info("认证系统返回报文\t {}", body);
JSONObject data = JSONUtil.parseObj(body).getJSONObject("data");
if (data != null) {
String token = data.getStr("token");
log.info("认证系统返回token\t {}", token);
String thirdBody = HttpRequest
.get("http://oms.oa.unionpay.com/prod-api/checkTokenThenLogin?personType=0&token=" + token)
.execute().body();
JSONObject thirdBodyJson = JSONUtil.parseObj(thirdBody);
log.info("签到系统返回报文\t {}", thirdBodyJson);
String thirdToken = thirdBodyJson.getStr("localToken");
log.info("签到系统返回token\t {}", thirdToken);
String resultBody = HttpRequest.post("http://oms.oa.unionpay.com/prod-api/atds/ds/" + signMark)
.header("Authorization", "Bearer " + thirdToken)
.execute().body();
log.info("签到接口返回报文\t {}", resultBody);
JSONObject signJson = JSONUtil.parseObj(resultBody);
if ("200".equals(signJson.getStr("code"))) {
String signDateTime = DateUtil.formatDateTime(signJson.getDate("data"));
result = true;
log.info("{} 打卡时间\t {}", user.getUsername(), signDateTime);
}
}
} catch (Exception e) {
log.error("sign error", e);
}
@ -100,10 +156,24 @@ public class CheckLogin {
return result.toString();
}
public static Properties getProperties() throws IOException {
Properties properties = new Properties();
properties.load(ResourceUtil.getStream("config.properties"));
return properties;
/**
* 利用BiConsumer实现foreach循环支持index
*
* @param biConsumer
* @param <T>
* @return
*/
public static <T> Consumer<T> forEachWithIndex(BiConsumer<T, Integer> biConsumer) {
/*这里说明一下我们每次传入forEach都是一个重新实例化的Consumer对象在lambada表达式中我们无法对int进行++操作,
我们模拟AtomicInteger对象写个getAndIncrement方法不能直接使用AtomicInteger哦*/
class IncrementInt{
int i = 0;
public int getAndIncrement(){
return i++;
}
}
IncrementInt incrementInt = new IncrementInt();
return t -> biConsumer.accept(t, incrementInt.getAndIncrement());
}
}

View File

@ -1,7 +0,0 @@
package com.dx.union;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}

View File

@ -1 +0,0 @@
csv_file_path=D:/file/source/

View File

@ -1,3 +0,0 @@
用户名,密码
111,222
1111,2222
1 用户名 密码
2 111 222
3 1111 2222