-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
93 lines (90 loc) · 2.57 KB
/
index.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
const config = require('./config')
const redis = require('redis')
const bot = require('./bot')
const client = require('./redis')
const https = require('https')
class HosCheck
{
constructor (cb) {
this.cb = cb
};
getContent () {
return new Promise((resolve, reject) => {
https.get(config.url, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
resolve(JSON.parse(data))
});
}).on("error", (err) => {
console.warn("Error: " + err.message);
reject(err)
})
})
};
async getContentCache () {
let rds = await this.getRedis(config.keys.content);
if (rds) {return JSON.parse(rds); }
let resp = await this.getContent()
if (!resp.hasError) {
this.setRedis(config.keys.content, resp)
this.setRedisExpire(config.keys.content, 600);
return resp;
}
throw {msg: "获取内容失败", resp}
};
getRedis (key) {
return new Promise((resolve, reject) => {
client.get(key, (err, res) => {
resolve(res)
})
})
};
setRedis (key, content) {
let res = typeof content == 'object' ? JSON.stringify(content) : content;
client.set(key, res);
};
setRedisExpire (key, sec) {
client.expire(key, sec);
};
async getLastDate () {
let resp = await this.getContentCache();
let shiftSchedule = resp.data.shiftSchedule
let lastSchedule = shiftSchedule[shiftSchedule.length - 1]
let lastDate = lastSchedule.date;
return lastDate
};
async check () {
let lastDate = await this.getLastDate();
let lastDateRedis = await this.getRedis(config.keys.lastDate)
if (lastDate != lastDateRedis) {
this.setRedis(config.keys.lastDate, lastDate);
return this.cb(lastDate);
}
return true;
};
sendDateNotice (date) {
return bot.send({
"msgtype": "text",
"text": {
"content": `您关注的医生可预约 ${date} 的号,请尽快完成预约`
},
"at": {
"atMobiles": [
config.admin,
],
"isAtAll": false
}
})
};
}
const hc = new HosCheck(date => {
return hc.sendDateNotice(date)
})
hc.check().then(() => {
setTimeout( () => {
process.exit(0)
}, 3000)
});