forked from rujor/false
-
Notifications
You must be signed in to change notification settings - Fork 0
/
haierV2.js
669 lines (645 loc) · 23.2 KB
/
haierV2.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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/*
海尔智家
活动专区-智慧小屋
自己捉mps.haiersmarthomes.com的包
把token和clientId填到haierAccount里,多账号换行隔开
export haierAccount="token#clientId"
一天一两次
cron: 44 0,18 * * *
*/
const $ = new Env("海尔智家");
let envSplitor = ['\n']
let userCookie = ($.isNode() ? process.env.haierAccount : $.getdata('haierAccount')) || '';
let userList = []
let userIdx = 0
let userCount = 0
let extraTaskList = ['T0181','T0182','T0183']
let appId = 'MB-UZHSH-0001'
let appVersion = '7.12.0'
let appKey = '5dfca8714eb26e3a776e58a8273c8752'
let defaultUA = 'Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/19A346 NebulaSDK/1.8.100112 Nebula WK PSDType(1) mPaaSClient/(null) App/Uplus Nebula'
let Referer = 'https://zjrs.haier.net/'
let Origin = 'https://zjrs.haier.net'
///////////////////////////////////////////////////////////////////
class UserInfo {
constructor(str) {
this.index = ++userIdx
this.name = this.index
this.valid = false
let info = str.split('#')
this.token = info[0]
this.clientId = info[1]
}
async taskApi(paramIn={}) {
let paramOut = {}
try {
let host = paramIn.url.replace('//','/').split('/')[1]
let urlObject = {
url: paramIn.url,
headers: {
'Host': host,
'Connection': 'keep-alive',
'accountToken': this.token,
'accessToken': this.token,
'clientId': this.clientId,
'appId': appId,
'timestamp': Date.now(),
'appVersion': appVersion,
'User-Agent': defaultUA,
'Referer': Referer,
'Origin': Origin,
'appKey': appKey,
},
timeout: 5000,
}
if(paramIn.body) {
urlObject.headers['Content-Type'] = paramIn['Content-Type'] || 'application/json;charset=utf-8'
if(typeof paramIn.body === 'object') {
if(urlObject.headers['Content-Type'].includes('json')) {
urlObject.body = JSON.stringify(paramIn.body)
} else {
urlObject.body = $.json2str(paramIn.body,'&')
}
} else {
urlObject.body = paramIn.body
}
}
if(paramIn.urlObjectParam) Object.assign(urlObject,paramIn.urlObjectParam)
if(paramIn.headerParam) Object.assign(urlObject.headers,paramIn.headerParam)
paramOut = Object.assign({},await httpRequest(paramIn.method,urlObject))
if(paramOut.err) {
paramOut.statusCode = paramOut?.err?.response?.statusCode
} else {
paramOut.statusCode = paramOut?.resp?.statusCode
if(paramOut?.resp?.body) {
try {
paramOut.result = JSON.parse(paramOut.resp.body)
} catch (e) {
paramOut.result = paramOut.resp.body
}
}
}
} catch(e) {
console.log(e)
} finally {
return Promise.resolve(paramOut);
}
}
async queryTask() {
let paramOut = {}
try {
let urlParam = {
method: 'post',
url: `https://mps.haiersmarthomes.com/api-gw/wisdomHouseActivity/task/queryTask`,
body: {
publishType: 2,
sourceClient: 1,
}
}
paramOut = Object.assign({},await this.taskApi(urlParam))
if(typeof paramOut?.result === 'object') {
let result = paramOut.result
if(result.retCode == '00000') {
this.star = result.data.wisdomStarBalance
for(let task of result.data.taskList) {
console.log(`任务[${task.taskCode}][${task.taskTitle}] -- ${task.taskStatus==1?'已':'未'}完成`)
if(task.taskStatus!=1) {
await this.doTask(task);
}
}
} else {
console.log(`查询任务列表失败:${result.retInfo}`)
}
} else {
console.log(`[queryTask]没有返回json数据`)
}
} catch(e) {
console.log(e)
} finally {
return Promise.resolve(paramOut);
}
}
async queryStar() {
let paramOut = {}
try {
let urlParam = {
method: 'post',
url: `https://mps.haiersmarthomes.com/api-gw/wisdomHouseActivity/task/queryTask`,
body: {
publishType: 2,
sourceClient: 1,
}
}
paramOut = Object.assign({},await this.taskApi(urlParam))
if(typeof paramOut?.result === 'object') {
let result = paramOut.result
if(result.retCode == '00000') {
this.star = result.data.wisdomStarBalance
console.log(`智慧星:${this.star}`)
while(this.star >= 20) {
await this.lotteryPrize();
}
} else {
console.log(`查询任务列表失败:${result.retInfo}`)
}
} else {
console.log(`[queryTask]没有返回json数据`)
}
} catch(e) {
console.log(e)
} finally {
return Promise.resolve(paramOut);
}
}
async doTask(task) {
let paramOut = {}
try {
let urlParam = {
method: 'post',
url: `https://mps.haiersmarthomes.com/api-gw/wisdomHouseActivity/task/doTask`,
body: {
taskCode: task.taskCode,
sourceClient: 1,
}
}
paramOut = Object.assign({},await this.taskApi(urlParam))
if(typeof paramOut?.result === 'object') {
let result = paramOut.result
if(result.retCode == '00000') {
console.log(`完成任务[${task.taskCode}]成功:${result.retInfo}`)
} else {
console.log(`完成任务[${task.taskCode}]失败:${result.retInfo}`)
}
} else {
console.log(`[doTask]没有返回json数据`)
}
} catch(e) {
console.log(e)
} finally {
return Promise.resolve(paramOut);
}
}
async lotteryPrize() {
let paramOut = {}
try {
let urlParam = {
method: 'post',
url: `https://mps.haiersmarthomes.com/api-gw/wisdomHouseActivity/lottery/lotteryPrize`,
body: {}
}
paramOut = Object.assign({},await this.taskApi(urlParam))
if(typeof paramOut?.result === 'object') {
let result = paramOut.result
if(result.retCode == '00000') {
this.star -= 20;
console.log(`抽奖获得${result.data.prizeExtra.prizeName}`)
} else {
console.log(`抽奖失败:${result.retInfo}`)
}
} else {
console.log(`[lotteryPrize]没有返回json数据`)
}
} catch(e) {
console.log(e)
} finally {
return Promise.resolve(paramOut);
}
}
async signIn() {
let paramOut = {}
try {
let urlParam = {
method: 'post',
url: `https://mps.haiersmarthomes.com/api-gw/wisdomHouseActivity/sign/signIn`,
body: {}
}
paramOut = Object.assign({},await this.taskApi(urlParam))
if(typeof paramOut?.result === 'object') {
let result = paramOut.result
if(result.retCode == '00000') {
if(result.data.firstSign) {
console.log(`签到成功`)
}
} else {
console.log(`签到失败:${result.retInfo}`)
}
} else {
console.log(`[signIn]没有返回json数据`)
}
} catch(e) {
console.log(e)
} finally {
return Promise.resolve(paramOut);
}
}
async getUserPointsAndWallet() {
let paramOut = {}
try {
let urlParam = {
method: 'post',
url: `https://zj.haier.net/zjapi/zjBaseServer/signDetail/getUserPointsAndWallet`,
body: {}
}
paramOut = Object.assign({},await this.taskApi(urlParam))
if(typeof paramOut?.result === 'object') {
let result = paramOut.result
if(result.retCode=='00000') {
console.log(`海贝:${result.data.haiBeiTotal}`)
console.log(`红包:${result.data.wallet}`)
} else {
console.log(`查询余额失败: ${result.retInfo}`)
}
} else {
console.log(`[getUserPointsAndWallet]没有返回json数据`)
}
} catch(e) {
console.log(e)
} finally {}
}
async appDailySign() {
let paramOut = {}
try {
let urlParam = {
method: 'post',
url: `https://zj.haier.net/zjapi/zjBaseServer/daily/sign`,
body: {}
}
paramOut = Object.assign({},await this.taskApi(urlParam))
if(typeof paramOut?.result === 'object') {
let result = paramOut.result
if(result.retCode == '00000') {
if(result.data.first) {
console.log(`签到成功,获得${result.data.haibeiCount}海贝`)
}
} else {
console.log(`签到失败:${result.retInfo}`)
}
} else {
console.log(`[signIn]没有返回json数据`)
}
} catch(e) {
console.log(e)
} finally {
return Promise.resolve(paramOut);
}
}
async userTask() {
let paramOut = {}
try {
console.log(`\n============= 账号[${this.index}] =============`)
await this.signIn();
await this.queryTask();
console.log('------------ 做隐藏任务 ------------')
for(let taskCode of extraTaskList) {
await this.doTask({taskCode})
}
console.log('------------------------------------')
await this.queryStar();
await this.appDailySign();
await this.getUserPointsAndWallet();
} catch(e) {
console.log(e)
} finally {
return Promise.resolve(paramOut);
}
}
}
!(async () => {
if (typeof $request !== "undefined") {
await GetRewrite()
}else {
if(!(await checkEnv())) return;
for(let user of userList) {
await user.userTask();
}
await $.showmsg();
}
})()
.catch((e) => console.log(e))
.finally(() => $.done())
///////////////////////////////////////////////////////////////////
async function GetRewrite() {
}
async function checkEnv() {
if(userCookie) {
let splitor = envSplitor[0];
for(let sp of envSplitor) {
if(userCookie.indexOf(sp) > -1) {
splitor = sp;
break;
}
}
for(let userCookies of userCookie.split(splitor)) {
if(userCookies) userList.push(new UserInfo(userCookies))
}
userCount = userList.length
} else {
console.log('未找到CK')
return;
}
console.log(`共找到${userCount}个账号`)
return true
}
////////////////////////////////////////////////////////////////////
async function httpRequest(method,url) {
return new Promise((resolve) => {
$.send(method, url, async (err, req, resp) => {
resolve({err,req,resp})
})
});
}
////////////////////////////////////////////////////////////////////
function Env(name,env) {
"undefined" != typeof process && JSON.stringify(process.env).indexOf("GITHUB") > -1 && process.exit(0);
return new class {
constructor(name,env) {
this.name = name
this.notifyStr = ''
this.startTime = (new Date).getTime()
Object.assign(this,env)
console.log(`${this.name} 开始运行:\n`)
}
isNode() {
return "undefined" != typeof module && !!module.exports
}
isQuanX() {
return "undefined" != typeof $task
}
isSurge() {
return "undefined" != typeof $httpClient && "undefined" == typeof $loon
}
isLoon() {
return "undefined" != typeof $loon
}
getdata(t) {
let e = this.getval(t);
if (/^@/.test(t)) {
const[, s, i] = /^@(.*?)\.(.*?)$/.exec(t),
r = s ? this.getval(s) : "";
if (r)
try {
const t = JSON.parse(r);
e = t ? this.lodash_get(t, i, "") : e
} catch (t) {
e = ""
}
}
return e
}
setdata(t, e) {
let s = !1;
if (/^@/.test(e)) {
const[, i, r] = /^@(.*?)\.(.*?)$/.exec(e),
o = this.getval(i),
h = i ? "null" === o ? null : o || "{}" : "{}";
try {
const e = JSON.parse(h);
this.lodash_set(e, r, t),
s = this.setval(JSON.stringify(e), i)
} catch (e) {
const o = {};
this.lodash_set(o, r, t),
s = this.setval(JSON.stringify(o), i)
}
}
else {
s = this.setval(t, e);
}
return s
}
getval(t) {
return this.isSurge() || this.isLoon() ? $persistentStore.read(t) : this.isQuanX() ? $prefs.valueForKey(t) : this.isNode() ? (this.data = this.loaddata(), this.data[t]) : this.data && this.data[t] || null
}
setval(t, e) {
return this.isSurge() || this.isLoon() ? $persistentStore.write(t, e) : this.isQuanX() ? $prefs.setValueForKey(t, e) : this.isNode() ? (this.data = this.loaddata(), this.data[e] = t, this.writedata(), !0) : this.data && this.data[e] || null
}
send(m, t, e = (() => {})) {
if(m != 'get' && m != 'post' && m != 'put' && m != 'delete') {
console.log(`无效的http方法:${m}`);
return;
}
if(m == 'get' && t.headers) {
delete t.headers["Content-Type"];
delete t.headers["Content-Length"];
} else if(t.body && t.headers) {
if(!t.headers["Content-Type"]) t.headers["Content-Type"] = "application/x-www-form-urlencoded";
}
if(this.isSurge() || this.isLoon()) {
if(this.isSurge() && this.isNeedRewrite) {
t.headers = t.headers || {};
Object.assign(t.headers, {"X-Surge-Skip-Scripting": !1});
}
let conf = {
method: m,
url: t.url,
headers: t.headers,
timeout: t.timeout,
data: t.body
};
if(m == 'get') delete conf.data
$axios(conf).then(t => {
const {
status: i,
request: q,
headers: r,
data: o
} = t;
e(null, q, {
statusCode: i,
headers: r,
body: o
});
}).catch(err => console.log(err))
} else if (this.isQuanX()) {
t.method = m.toUpperCase(), this.isNeedRewrite && (t.opts = t.opts || {}, Object.assign(t.opts, {
hints: !1
})),
$task.fetch(t).then(t => {
const {
statusCode: i,
request: q,
headers: r,
body: o
} = t;
e(null, q, {
statusCode: i,
headers: r,
body: o
})
}, t => e(t))
} else if (this.isNode()) {
this.got = this.got ? this.got : require("got");
const {
url: s,
...i
} = t;
this.instance = this.got.extend({
followRedirect: false
});
this.instance[m](s, i).then(t => {
const {
statusCode: i,
request: q,
headers: r,
body: o
} = t;
e(null, q, {
statusCode: i,
headers: r,
body: o
})
}, t => {
const {
message: s,
request: q,
response: i
} = t;
e(s, q, i)
})
}
}
time(t,x=null) {
let xt = x ? new Date(x) : new Date
let e = {
"M+": xt.getMonth() + 1,
"d+": xt.getDate(),
"h+": xt.getHours(),
"m+": xt.getMinutes(),
"s+": xt.getSeconds(),
"q+": Math.floor((xt.getMonth() + 3) / 3),
S: xt.getMilliseconds()
};
/(y+)/.test(t) && (t = t.replace(RegExp.$1, (xt.getFullYear() + "").substr(4 - RegExp.$1.length)));
for (let s in e)
new RegExp("(" + s + ")").test(t) && (t = t.replace(RegExp.$1, 1 == RegExp.$1.length ? e[s] : ("00" + e[s]).substr(("" + e[s]).length)));
return t
}
async showmsg() {
if(!this.notifyStr) return;
let notifyBody = this.name + " 运行通知\n\n" + this.notifyStr
if($.isNode()){
var notify = require('./sendNotify');
console.log('\n============== 推送 ==============')
await notify.sendNotify(this.name, notifyBody);
} else {
this.msg(notifyBody);
}
}
logAndNotify(str) {
console.log(str)
this.notifyStr += str
this.notifyStr += '\n'
}
logAndNotifyWithTime(str) {
let t = '['+this.time('hh:mm:ss.S')+']'+str
console.log(t)
this.notifyStr += t
this.notifyStr += '\n'
}
logWithTime(str) {
console.log('['+this.time('hh:mm:ss.S')+']'+str)
}
msg(e = t, s = "", i = "", r) {
const o = t => {
if (!t)
return t;
if ("string" == typeof t)
return this.isLoon() ? t : this.isQuanX() ? {
"open-url": t
}
: this.isSurge() ? {
url: t
}
: void 0;
if ("object" == typeof t) {
if (this.isLoon()) {
let e = t.openUrl || t.url || t["open-url"],
s = t.mediaUrl || t["media-url"];
return {
openUrl: e,
mediaUrl: s
}
}
if (this.isQuanX()) {
let e = t["open-url"] || t.url || t.openUrl,
s = t["media-url"] || t.mediaUrl;
return {
"open-url": e,
"media-url": s
}
}
if (this.isSurge()) {
let e = t.url || t.openUrl || t["open-url"];
return {
url: e
}
}
}
};
this.isMute || (this.isSurge() || this.isLoon() ? $notification.post(e, s, i, o(r)) : this.isQuanX() && $notify(e, s, i, o(r)));
let h = ["", "============== 系统通知 =============="];
h.push(e),
s && h.push(s),
i && h.push(i),
console.log(h.join("\n"))
}
getMin(a,b){
return ((a<b) ? a : b)
}
getMax(a,b){
return ((a<b) ? b : a)
}
padStr(num,length,padding='0') {
let numStr = String(num)
let numPad = (length>numStr.length) ? (length-numStr.length) : 0
let retStr = ''
for(let i=0; i<numPad; i++) {
retStr += padding
}
retStr += numStr
return retStr;
}
json2str(obj,c,encodeUrl=false) {
let ret = []
for(let keys of Object.keys(obj).sort()) {
let v = obj[keys]
if(v && encodeUrl) v = encodeURIComponent(v)
ret.push(keys+'='+v)
}
return ret.join(c);
}
str2json(str,decodeUrl=false) {
let ret = {}
for(let item of str.split('&')) {
if(!item) continue;
let idx = item.indexOf('=')
if(idx == -1) continue;
let k = item.substr(0,idx)
let v = item.substr(idx+1)
if(decodeUrl) v = decodeURIComponent(v)
ret[k] = v
}
return ret;
}
randomString(len,charset='abcdef0123456789') {
let str = '';
for (let i = 0; i < len; i++) {
str += charset.charAt(Math.floor(Math.random()*charset.length));
}
return str;
}
randomList(a) {
let idx = Math.floor(Math.random()*a.length)
return a[idx]
}
wait(t) {
return new Promise(e => setTimeout(e, t))
}
done(t = {}) {
const e = (new Date).getTime(),
s = (e - this.startTime) / 1e3;
console.log(`\n${this.name} 运行结束,共运行了 ${s} 秒!`)
if(this.isSurge() || this.isQuanX() || this.isLoon()) $done(t)
}
}(name,env)
}