This repository has been archived by the owner on Jun 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.js
373 lines (328 loc) · 10.1 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
auto.waitFor();
let currentTime = new Date(); //当前时间不戳
//读取配置文件
let file = files.read("./config.json");
let config = JSON.parse(file);
let sbHour = config.sbHour;
let sbMinute = config.sbMinute;
let sbSecond = config.sbSecond;
let xbHour = config.xbHour;
let xbMinute = config.xbMinute;
let xbSecond = config.xbSecond;
let menu = config.workTitle;
let sbEarly = config.sbEarly;
let xbDelay = config.xbDelay;
let txApiKey = config.txApiKey;
let barkPusherKey = config.barkPusherKey;
let barkPusherURL = config.barkPusherURL;
let dingPassword = config.dingPassword;
minNum = config.randomMin;
maxNum = config.randomMax;
let sbTime = sbHour * 3600 + sbMinute * 60 + sbSecond;
let xbTime = xbHour * 3600 + xbMinute * 60 + xbSecond;
let thisTime =
currentTime.getHours() * 60 * 60 +
currentTime.getMinutes() * 60 +
currentTime.getSeconds();
// 1上班 2下班 0不打
let bc = 0;
let $$init = {
start() {
return wakeUp();
function fullChain() {
openDingtalk();
loginDingtalk();
if (fastSign()) {
return true;
}
inKaoqin();
signIn();
}
function wakeUp() {
while (!device.isScreenOn()) {
toastLog("努力点亮手机");
device.wakeUpIfNeeded();
sleep(1000);
swipe(500, 2000, 500, 1000, 220);
}
//注册退出事件
events.on("exit", function () {
home();
});
detectTimer();
}
function detectTimer() {
if (thisTime < sbTime) {
//上班时间一小时以内
bc = 1;
console.log("打上班卡");
fullChain();
} else if (thisTime > xbTime) {
//下班时间5分钟以上
bc = 2;
console.log("打下班卡");
fullChain();
} else {
//不用打卡直接设置时间
bc = 3;
postMessage('不用打卡啊', 3)
console.log("不用打卡");
}
return;
}
function setTimer() {
let timee = "";
let str = "";
if (thisTime < sbTime) {
//打当天上班卡时间设置
let date = getAvailableDate(new Date() - 86400 * 1000);
str = date + " " + sbHour + ":" + sbMinute + ":" + sbSecond;
timee = Date.parse(str) - sbEarly * 1000 - randomNum() * 60 * 1000;
}
if (thisTime < xbTime) {
//打当天下班卡时间设置
let date = getAvailableDate(new Date() - 86400 * 1000);
str = date + " " + xbHour + ":" + xbMinute + ":" + xbSecond;
timee = Date.parse(str) + xbDelay * 1000 + randomNum() * 60 * 1000;
}
if (thisTime > xbTime) {
//打下一次上班卡时间设置
let date = getAvailableDate(new Date() - 0);
str = date + " " + sbHour + ":" + sbMinute + ":" + sbSecond;
timee = Date.parse(str) - sbEarly * 1000 - randomNum() * 60 * 1000;
}
let timer = require("./modules/ext-timers.js")(runtime, this);
timer.addDisposableTask({
path: "main.js",
date: timee,
});
//创建存储
return dateFormat("Y-mm-dd HH:MM:SS", new Date(timee))
}
function randomNum() {
return random(minNum, maxNum);
}
function getAvailableDate(cTime) {
let d = new Date(cTime + 86400 * 1000);
let ish = isHoliday(dateFormat("Y-mm-dd", d));
if (ish) {
//如果当天是假日,则明天
return getAvailableDate(cTime + 86400 * 1000);
}
return dateFormat("Y/mm/dd", new Date(cTime + 86400 * 1000));
}
function openDingtalk() {
if (currentPackage() == "com.alibaba.android.rimet") {
toastLog("当前在钉钉里");
return;
}
toastLog("启动钉钉APPing");
let res = app.launchApp("钉钉");
if (!res) {
postMessage("没有找到可以打开的钉钉", 2);
}
}
function loginDingtalk() {
sleep(2000);
if (!(textContains("登录").findOnce())) {
toastLog("钉钉已登录或登录成功,跳过此环节");
return true;
}
toastLog("尝试登录钉钉");
let passwordInput = id("et_pwd_login").findOne(1000);
if (passwordInput) {
toastLog("找到密码输入框");
let res = passwordInput.setText(dingPassword);
if (!res) {
toastLog("输入失败");
}
let btn = id("btn_next").findOne(1000);
btn.click();
sleep(3000);
loginDingtalk();
} else {
toastLog("寻找密码输入按钮");
let passwordLogin = textContains("密码登录").findOne(1000);
if (!passwordLogin) {
return false;
}
passwordLogin.click();
loginDingtalk();
}
return false;
}
function fastSign() {
toastLog("等待10秒的极速打卡");
//等待10秒的极速打卡
let tt = textContains("查看打卡结果").findOnce(3000);
if (tt) {
postMessage("极速打卡成功,哦耶", 1);
return true;
}
toastLog("未检测到极速打卡通知,开始正常打卡");
return false;
}
//进入考勤页面
function inKaoqin() {
//这10秒包括了启动钉钉所需的时间
workBtn = text(menu).findOne(10000);
if (!workBtn) {
//可能不在主界面,重启钉钉
toastLog("钉钉不在主界面可能,重启钉钉");
shutdownDingtalk();
openDingtalk();
workBtn = text(menu).findOne(10000);
}
workBtn.parent().parent().click();
toastLog("进入工作台");
dkBtn = text("考勤打卡").findOne(10000);
if (!dkBtn) {
postMessage("打开了控制台却没有找到考勤打卡按钮", 2);
return;
}
dkBtn.click();
toastLog("进入打卡页");
sleep(3000);
}
//点击打卡
function signIn() {
if (bc !== 3) {
textContains("上班").waitFor();
if (text("外勤打卡").exists() || text("迟到打卡").exists()) {
postMessage(
"当前处于外勤打卡或迟到打卡状态,已停止任务,请自己处理。", 2
);
return;
} else if (bc === 2 && text("更新打卡").exists()) {
postMessage("已经打过卡了,再见", 1);
return;
} else if (bc == 1 && textContains("已打卡").exists()) {
postMessage("已经打过卡了,再见", 1);
return;
}
let text1 = "";
if (bc == 1) {
text1 = "上班打卡";
} else if (bc == 2) {
text1 = "下班打卡";
} else {
postMessage("多余任务,无需打卡", 3);
return;
}
toastLog("当前类型是" + bc + ",点击按钮" + text1);
dcard = text(text1).findOne(10000);
if (!dcard) {
postMessage("没有找到打卡相关按钮,可能是时间设置出错了吧。", 2);
return;
}
result = dcard.click();
if (result) {
postMessage("检测到打卡成功消息,打卡成功", 1);
} else {
postMessage("未检测到打卡成功消息或打卡失败", 2);
}
} else {
postMessage("未检测到能打的班次,取消打卡", 3);
}
}
function postMessage(result, status) {
let messge;
nextime = setTimer()
if (typeof result === "boolean") {
if (result) {
//打卡成功,推送
message = "检测到打卡成功消息,打卡成功";
} else {
message = "未检测到打卡成功消息或打卡失败";
}
} else {
message = result;
}
toastLog(message);
let date = dateFormat("Y-mm-dd", new Date());
let bb = "非打卡时间"
if(bc == 1) {
bb = "上班"
}else if(bc == 2) {
bb = "下班"
}
try{
if (barkPusherKey && barkPusherURL) {
http.post(barkPusherURL + "/push", {
device_key: barkPusherKey,
title: date + ' ' + bb + ' 打卡结果',
body: message + "\n下次时间:" + nextime,
ext_params: {
group: "打卡"
},
category: "AutoJS",
});
}
}catch(e){
toastLog("pushover失败,无所谓咯", e)
}
}
function isHoliday(dater) {
//返回今天是不是节假日
if (!txApiKey) {
return false;
}
url =
"http://api.tianapi.com/txapi/jiejiari/index?key=" +
txApiKey +
"&date=" +
dater;
let res = http.get(url, {});
let data = res.body.json()["newslist"][0];
let ish = data["isnotwork"];
return !!ish;
}
function dateFormat(fmt, date) {
let ret;
const opt = {
"Y+": date.getFullYear().toString(), // 年
"m+": (date.getMonth() + 1).toString(), // 月
"d+": date.getDate().toString(), // 日
"H+": date.getHours().toString(), // 时
"M+": date.getMinutes().toString(), // 分
"S+": date.getSeconds().toString(), // 秒
// 有其他格式化字符需求可以继续添加,必须转化成字符串
};
for (let k in opt) {
ret = new RegExp("(" + k + ")").exec(fmt);
if (ret) {
fmt = fmt.replace(
ret[1],
ret[1].length == 1 ? opt[k] : padStart(opt[k], ret[1].length, "0")
);
}
}
return fmt;
}
function padStart(string, targetLength, padString) {
while (string.length < targetLength) {
string = padString + string;
}
return string;
}
function shutdownDingtalk() {
let packageName = app.getPackageName("钉钉");
app.openAppSetting(packageName);
text("钉钉").waitFor();
let is_sure = textContains("结束").findOne();
if (is_sure.enabled()) {
click("结束");
textContains("确定").findOne();
click("确定");
log(app.getAppName(packageName) + "应用已被关闭");
sleep(1000);
} else {
log(app.getAppName(packageName) + "应用不能被正常关闭或不在后台运行");
}
}
},
bind() {
return this;
},
}.bind();
$$init.start();