forked from georgehuan1994/DingDing-Automatic-Clock-in
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDingDing-Automatic-Clock-in.js
575 lines (448 loc) · 15.3 KB
/
DingDing-Automatic-Clock-in.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/*
* @Author: George Huan
* @Date: 2020-08-03 09:30:30
* @LastEditTime: 2021-03-20 15:00:56
* @Description: DingDing-Automatic-Clock-in (Run on AutoJs)
* @URL: https://github.com/georgehuan1994/DingDing-Automatic-Clock-in
*/
const ACCOUNT = "钉钉账号"
const PASSWORD = "钉钉密码"
const EMAILL_ADDRESS = "用于接收打卡结果的邮箱地址"
const BUNDLE_ID_DD = "com.alibaba.android.rimet"
const BUNDLE_ID_XMSF = "com.xiaomi.xmsf"
const BUNDLE_ID_MAIL = "com.netease.mail"
const BUNDLE_ID_TASKER = "net.dinglisch.android.taskerm"
const NAME_OF_EMAILL_APP = "网易邮箱大师"
const NAME_OF_ATTENDANCE_MACHINE = "前台大门" // 考勤机名称
const LOWER_BOUND = 1 * 60 * 1000 // 最小等待时间:1min
const UPPER_BOUND = 5 * 60 * 1000 // 最大等待时间:5min
// 执行时的屏幕亮度(0-255),需要"修改系统设置"权限
const SCREEN_BRIGHTNESS = 20
// 是否过滤通知
const NOTIFICATIONS_FILTER = false;
// BundleId白名单
const BUNDLE_ID_WHITE_LIST = [BUNDLE_ID_DD,BUNDLE_ID_XMSF,BUNDLE_ID_MAIL,BUNDLE_ID_TASKER, ]
const WEEK_DAY = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday",]
// 公司的钉钉CorpId,获取方法见 2020-09-24 更新日志。如果只加入了一家公司,可以不填
const CORP_ID = ""
const ACTION_LOCK_SCREEN = "autojs.intent.action.LOCK_SCREEN"
// =================== ↓↓↓ 主线程:监听通知 ↓↓↓ ====================
var suspend = false
var needWaiting = true
var currentDate = new Date()
// 检查无障碍权限
auto.waitFor("normal")
// 检查Autojs版本
requiresAutojsVersion("4.1.0")
// 创建运行日志
console.setGlobalLogConfig({
file: "/sdcard/脚本/Archive/" + getCurrentDate() + "-log.txt"
});
// 监听本机通知
events.observeNotification()
events.on("notification", function(n) {
notificationHandler(n)
});
toastLog("监听中,请在日志中查看记录的通知及其内容")
// =================== ↑↑↑ 主线程:监听通知 ↑↑↑ =====================
/**
* @description 处理通知
*/
function notificationHandler(n) {
var bundleId = n.getPackageName() // 获取通知包名
var abstract = n.tickerText // 获取通知摘要
var text = n.getText() // 获取通知文本
// 过滤BundleId白名单之外的应用所发出的通知
if (!filterNotification(bundleId, abstract, text)) {
return;
}
// 监听摘要为 "定时打卡" 的通知,不一定要从 Tasker 中发出通知,日历、定时器等App均可实现
if (abstract == "定时打卡" && !suspend) {
needWaiting = true
threads.shutDownAll()
threads.start(function(){
doClock()
})
return;
}
// 监听文本为 "打卡" 的通知
if ((bundleId == BUNDLE_ID_MAIL || bundleId == BUNDLE_ID_XMSF) && text == "打卡") {
needWaiting = false
threads.shutDownAll()
threads.start(function(){
doClock()
})
return;
}
// 监听文本为 "考勤结果" 的通知
if ((bundleId == BUNDLE_ID_MAIL || bundleId == BUNDLE_ID_XMSF) && (text == "Re: 考勤结果" || text == "考勤结果")) {
threads.shutDownAll()
threads.start(function(){
sendEmail("考勤结果", getStorageData("dingding", "clockResult"))
})
return;
}
// 监听文本为 "暂停" 的通知
if ((bundleId == BUNDLE_ID_MAIL || bundleId == BUNDLE_ID_XMSF) && text == "暂停") {
suspend = true
console.warn("暂停定时打卡")
threads.shutDownAll()
threads.start(function(){
sendEmail("操作成功", "已暂停定时打卡功能")
})
return;
}
// 监听文本为 "恢复" 的通知
if ((bundleId == BUNDLE_ID_MAIL || bundleId == BUNDLE_ID_XMSF) && text == "恢复") {
suspend = false
console.warn("恢复定时打卡")
threads.shutDownAll()
threads.start(function(){
sendEmail("操作成功", "已恢复定时打卡功能")
})
return;
}
if (text == null) {
return;
}
// 监听钉钉返回的考勤结果
if (bundleId == BUNDLE_ID_DD && text.indexOf("考勤打卡") >= 0) {
setStorageData("dingding", "clockResult", text)
threads.shutDownAll()
threads.start(function(){
sendEmail("考勤结果", text)
})
return;
}
}
/**
* @description 打卡流程
*/
function doClock() {
currentDate = new Date()
console.log("本地时间:" + getCurrentDate() + " " + getCurrentTime())
console.log("开始打卡流程!")
brightScreen() // 唤醒屏幕
unlockScreen() // 解锁屏幕
holdOn() // 随机等待
signIn() // 自动登录
handleLate() // 处理迟到
attendKaoqin() // 考勤打卡
if (currentDate.getHours() <= 12)
clockIn() // 上班打卡
else
clockOut() // 下班打卡
lockScreen() // 关闭屏幕
}
/**
* @description 发送邮件流程
* @param {*} title 邮件主题
* @param {*} message 邮件正文
*/
function sendEmail(title, message) {
console.log("开始发送邮件流程!")
brightScreen() // 唤醒屏幕
unlockScreen() // 解锁屏幕
app.sendEmail({
email: [EMAILL_ADDRESS],
subject: title,
text: message
})
console.log("选择邮件应用")
waitForActivity("com.android.internal.app.ChooserActivity")// 等待选择应用界面弹窗出现,如果设置了默认应用就注释掉
if (null != textMatches(NAME_OF_EMAILL_APP).findOne(1000)) {
btn_email = textMatches(NAME_OF_EMAILL_APP).findOnce().parent()
btn_email.click()
}
else {
console.error("没有找到" + NAME_OF_EMAILL_APP)
lockScreen()
return;
}
waitForActivity("com.netease.mobimail.activity.MailComposeActivity")
id("send").findOne().click()
console.log("正在发送邮件...")
home()
sleep(1000)
lockScreen() // 关闭屏幕
}
/**
* @description 唤醒设备
*/
function brightScreen() {
console.log("唤醒设备")
device.setBrightnessMode(0) // 手动亮度模式
device.setBrightness(SCREEN_BRIGHTNESS)
device.wakeUpIfNeeded() // 唤醒设备
device.keepScreenOn() // 保持亮屏
console.info("设备已唤醒")
sleep(1000) // 等待屏幕亮起
if (!device.isScreenOn()) {
console.warn("设备未唤醒,重试")
device.wakeUpIfNeeded()
brightScreen()
}
sleep(1000)
}
/**
* @description 解锁屏幕
*/
function unlockScreen() {
console.log("解锁屏幕")
gesture(320,[540,device.height * 0.9],[540,device.height * 0.1]) // 上滑解锁
sleep(1000) // 等待解锁动画完成
home()
sleep(1000) // 等待返回动画完成
if (isDeviceLocked()) {
console.error("上滑解锁失败,请调整gesture参数,或使用其他解锁方案!")
exit()
}
console.info("屏幕已解锁")
}
/**
* @description 随机等待
*/
function holdOn(){
if (!needWaiting) {
return;
}
var randomTime = random(LOWER_BOUND, UPPER_BOUND)
toastLog(Math.floor(randomTime / 1000) + "秒后启动" + app.getAppName(BUNDLE_ID_DD) + "...")
sleep(randomTime)
}
/**
* @description 启动并登陆钉钉
*/
function signIn() {
app.launchPackage(BUNDLE_ID_DD)
console.log("正在启动" + app.getAppName(BUNDLE_ID_DD) + "...")
sleep(10000) // 等待钉钉启动
if (currentPackage() == BUNDLE_ID_DD &&
currentActivity() == "com.alibaba.android.user.login.SignUpWithPwdActivity") {
console.info("账号未登录")
var account = id("et_phone_input").findOne()
account.setText(ACCOUNT)
console.log("输入账号")
var password = id("et_pwd_login").findOne()
password.setText(PASSWORD)
console.log("输入密码")
var btn_login = id("btn_next").findOne()
btn_login.click()
console.log("正在登陆...")
sleep(3000)
}
if (currentPackage() == BUNDLE_ID_DD &&
currentActivity() != "com.alibaba.android.user.login.SignUpWithPwdActivity") {
console.info("账号已登录")
sleep(1000)
}
}
/**
* @description 处理迟到打卡
*/
function handleLate(){
if (null != textMatches("迟到打卡").clickable(true).findOne(1000)) {
btn_late = textMatches("迟到打卡").clickable(true).findOnce()
btn_late.click()
console.warn("迟到打卡")
}
if (null != descMatches("迟到打卡").clickable(true).findOne(1000)) {
btn_late = descMatches("迟到打卡").clickable(true).findOnce()
btn_late.click()
console.warn("迟到打卡")
}
}
/**
* @description 使用 URL Scheme 进入考勤界面
*/
function attendKaoqin(){
var url_scheme = "dingtalk://dingtalkclient/page/link?url=https://attend.dingtalk.com/attend/index.html"
if(CORP_ID != "") {
url_scheme = url_scheme + "?corpId=" + CORP_ID
}
var a = app.intent({
action: "VIEW",
data: url_scheme,
//flags: [Intent.FLAG_ACTIVITY_NEW_TASK]
});
app.startActivity(a);
console.log("正在进入考勤界面...")
sleep(6000)
if (null != textMatches("申请").clickable(true).findOne(3000)) {
console.info("已进入考勤界面")
sleep(1000)
}
}
/**
* @description 上班打卡
*/
function clockIn() {
console.log("上班打卡...")
if (null != textContains("休息").findOne(1000) || null != descContains("休息").findOne(1000)) {
console.info("今日休息")
home()
sleep(1000)
return;
}
if (null != textContains("已打卡").findOne(1000)) {
console.info("已打卡")
toast("已打卡")
home()
sleep(1000)
return;
}
console.log("等待连接到考勤机:" + NAME_OF_ATTENDANCE_MACHINE + "...")
sleep(2000)
if (null != textContains("未连接").findOne(1000)) {
console.error("未连接考勤机,重新进入考勤界面!")
attendKaoqin()
}
textContains(NAME_OF_ATTENDANCE_MACHINE).waitFor()
console.info("已连接考勤机:" + NAME_OF_ATTENDANCE_MACHINE)
sleep(1000)
if (null != textMatches("上班打卡").clickable(true).findOne(1000)) {
btn_clockin = textMatches("上班打卡").clickable(true).findOnce()
btn_clockin.click()
console.log("按下打卡按钮")
}
else {
click(device.width / 2, device.height * 0.560)
console.log("点击打卡按钮坐标")
}
sleep(1000)
handleLate() // 处理迟到打卡
home()
sleep(1000)
}
/**
* @description 下班打卡
*/
function clockOut() {
console.log("下班打卡...")
if (null != textContains("休息").findOne(1000) || null != descContains("休息").findOne(1000)) {
console.info("今日休息")
home()
sleep(1000)
return;
}
if (null != textContains("更新打卡").findOne(1000)) {
if (null != textContains("早退").findOne(1000)) {
toastLog("早退,更新打卡记录")
}
else {
home()
sleep(1000)
return;
}
}
console.log("等待连接到考勤机:" + NAME_OF_ATTENDANCE_MACHINE + "...")
sleep(2000)
if (null != textContains("未连接").findOne(1000)) {
console.error("未连接考勤机,重新进入考勤界面!")
attendKaoqin()
}
textContains(NAME_OF_ATTENDANCE_MACHINE).waitFor()
console.info("已连接考勤机:" + NAME_OF_ATTENDANCE_MACHINE)
sleep(1000)
if (null != textMatches("下班打卡").clickable(true).findOne(1000)) {
btn_clockout = textMatches("下班打卡").clickable(true).findOnce()
btn_clockout.click()
console.log("按下打卡按钮")
sleep(1000)
}
if (null != textContains("早退打卡").clickable(true).findOne(1000)) {
className("android.widget.Button").text("早退打卡").clickable(true).findOnce().parent().click()
console.warn("早退打卡")
}
home()
sleep(1000)
}
/**
* @description 锁屏
*/
function lockScreen(){
console.log("关闭屏幕")
// 锁屏方案1:Root
// Power()
// 锁屏方案2:No Root
press(Math.floor(device.width / 2), Math.floor(device.height * 0.973), 1000) // 小米的快捷手势:长按Home键锁屏
// 万能锁屏方案:向Tasker发送广播,触发系统锁屏动作。配置方法见 2021-03-09 更新日志
// app.sendBroadcast({action: ACTION_LOCK_SCREEN});
device.setBrightnessMode(1) // 自动亮度模式
device.cancelKeepingAwake() // 取消设备常亮
if (isDeviceLocked()) {
console.info("屏幕已关闭")
}
else {
console.error("屏幕未关闭,请尝试其他锁屏方案")
}
}
// ===================== ↓↓↓ 功能函数 ↓↓↓ =======================
function dateDigitToString(num){
return num < 10 ? '0' + num : num
}
function getCurrentTime(){
var currentDate = new Date()
var hours = dateDigitToString(currentDate.getHours())
var minute = dateDigitToString(currentDate.getMinutes())
var second = dateDigitToString(currentDate.getSeconds())
var formattedTimeString = hours + ':' + minute + ':' + second
return formattedTimeString
}
function getCurrentDate(){
var currentDate = new Date()
var year = dateDigitToString(currentDate.getFullYear())
var month = dateDigitToString(currentDate.getMonth() + 1)
var date = dateDigitToString(currentDate.getDate())
var week = currentDate.getDay()
var formattedDateString = year + '-' + month + '-' + date + '-' + WEEK_DAY[week]
return formattedDateString
}
// 通知过滤器
function filterNotification(bundleId, abstract, text) {
var check = BUNDLE_ID_WHITE_LIST.some(function(item) {return bundleId == item})
if (!NOTIFICATIONS_FILTER || check) {
console.verbose(bundleId)
console.verbose(abstract)
console.verbose(text)
console.verbose("---------------------------")
return true
}
else {
return false
}
}
// 保存本地数据
function setStorageData(name, key, value) {
const storage = storages.create(name) // 创建storage对象
storage.put(key, value)
}
// 读取本地数据
function getStorageData(name, key) {
const storage = storages.create(name)
if (storage.contains(key)) {
return storage.get(key, "")
}
// 默认返回undefined
}
// 删除本地数据
function delStorageData(name, key) {
const storage = storages.create(name)
if (storage.contains(key)) {
storage.remove(key)
}
}
// 获取应用版本号
function getPackageVersion(bundleId) {
importPackage(android.content)
var pckMan = context.getPackageManager()
var packageInfo = pckMan.getPackageInfo(bundleId, 0)
return packageInfo.versionName
}
// 屏幕是否为锁定状态
function isDeviceLocked() {
importClass(android.app.KeyguardManager)
importClass(android.content.Context)
var km = context.getSystemService(Context.KEYGUARD_SERVICE)
return km.isKeyguardLocked()
}