打卡任务开发完成
This commit is contained in:
parent
a1ddf2add6
commit
1c7e4dfa54
@ -1,54 +1,95 @@
|
|||||||
package com.dx.union;
|
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.io.resource.ResourceUtil;
|
||||||
import cn.hutool.core.text.csv.CsvReader;
|
import cn.hutool.core.text.csv.CsvReader;
|
||||||
import cn.hutool.core.text.csv.CsvUtil;
|
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.http.HttpRequest;
|
||||||
import cn.hutool.json.JSONObject;
|
import cn.hutool.json.JSONObject;
|
||||||
import cn.hutool.json.JSONUtil;
|
import cn.hutool.json.JSONUtil;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.math.BigDecimal;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Properties;
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class CheckLogin {
|
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 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) {
|
public static void main(String[] args) {
|
||||||
try {
|
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();
|
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(
|
final List<User> userList = reader.read(
|
||||||
ResourceUtil.getUtf8Reader("users.csv"), User.class);
|
ResourceUtil.getUtf8Reader(filePath), User.class);
|
||||||
log.info("读取到 {} 个需打卡账号", userList.size());
|
int size = userList.size();
|
||||||
userList.forEach(user -> {
|
log.info("共有 {} 个需打卡账号", size);
|
||||||
log.info("开始打卡 {} 的账号,密码为:{}", user.getUsername(), user.getPassword());
|
userList.forEach(forEachWithIndex((user, index) -> {
|
||||||
boolean isSignSuccess = sign(user);
|
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) {
|
} catch (Exception e) {
|
||||||
log.error(e);
|
log.error(e);
|
||||||
|
} finally {
|
||||||
|
log.info("==== 打卡任务结束 ====");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
public static boolean sign(User user, Integer signMark) {
|
||||||
|
|
||||||
public static boolean sign(User user) {
|
|
||||||
boolean result = false;
|
boolean result = false;
|
||||||
try {
|
try {
|
||||||
String password = encrypt2ToMD5("weaver@2024");
|
log.info("开始{}打卡 {} 的账号,密码为:{}", signMark == 0 ? "上班" : "下班", user.getUsername(), user.getPassword());
|
||||||
System.out.println(password);
|
String password = encrypt2ToMD5(user.getPassword());
|
||||||
String username = "WBzhangmiao";
|
String username = user.getUsername();
|
||||||
|
log.info("加密密码为:{}", password);
|
||||||
String body = HttpRequest.post("https://login.oa.unionpay.com/idsapi/portal/Login")
|
String body = HttpRequest.post("https://login.oa.unionpay.com/idsapi/portal/Login")
|
||||||
.form("username", username)
|
.form("username", username)
|
||||||
.form("password", password)
|
.form("password", password)
|
||||||
@ -57,14 +98,29 @@ public class CheckLogin {
|
|||||||
.form("loginType", "normal")
|
.form("loginType", "normal")
|
||||||
.form("code", "")
|
.form("code", "")
|
||||||
.execute().body();
|
.execute().body();
|
||||||
JSONObject bodyJson = JSONUtil.parseObj(body);
|
log.info("认证系统返回报文\t {}", body);
|
||||||
String token = bodyJson.getJSONObject("data").getStr("token");
|
JSONObject data = JSONUtil.parseObj(body).getJSONObject("data");
|
||||||
String thirdBody = HttpRequest.get("http://oms.oa.unionpay.com/prod-api/checkTokenThenLogin?personType=0&token=" + token).execute().body();
|
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);
|
JSONObject thirdBodyJson = JSONUtil.parseObj(thirdBody);
|
||||||
|
log.info("签到系统返回报文\t {}", thirdBodyJson);
|
||||||
String thirdToken = thirdBodyJson.getStr("localToken");
|
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();
|
log.info("签到系统返回token\t {}", thirdToken);
|
||||||
System.out.println(resultBody);
|
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;
|
result = true;
|
||||||
|
log.info("{} 打卡时间\t {}", user.getUsername(), signDateTime);
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("sign error", e);
|
log.error("sign error", e);
|
||||||
}
|
}
|
||||||
@ -100,10 +156,24 @@ public class CheckLogin {
|
|||||||
return result.toString();
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Properties getProperties() throws IOException {
|
/**
|
||||||
Properties properties = new Properties();
|
* 利用BiConsumer实现foreach循环支持index
|
||||||
properties.load(ResourceUtil.getStream("config.properties"));
|
*
|
||||||
return properties;
|
* @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());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
package com.dx.union;
|
|
||||||
|
|
||||||
public class Main {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
System.out.println("Hello world!");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
csv_file_path=D:/file/source/
|
|
@ -1,3 +0,0 @@
|
|||||||
用户名,密码
|
|
||||||
111,222
|
|
||||||
1111,2222
|
|
|
Loading…
Reference in New Issue
Block a user