-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
78 lines (66 loc) · 1.84 KB
/
app.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
// import _ from 'lodash';
import Koa from 'koa';
import Router from 'koa-router';
import koaStatic from 'koa-static';
import bodyparser from 'koa-bodyparser';
import Pug from 'koa-pug';
import {CronJob} from 'cron';
import config from './config';
import {logger} from './lib/utils';
import {getContactData, getWechatData, showQRcode, sendMsg, heartBeatDetect, getContactList} from './controllers/index';
const TZ = 'Asia/Shanghai';
const app = new Koa();
const router = new Router();
new Pug({
viewPath: `${__dirname}/views`,
helperPath: [
{ _: require('lodash') },
{ moment: require('moment') }
],
noCache: true,
app
});
router.get('/', async (ctx) => {
ctx.body = 'Welcome to Wechat-Robot!';
ctx.render('index', {}, true);
});
router.get('/health', async (ctx) => {
ctx.body = 'OK';
});
router.get('/sendMsg', async (ctx) => {
let query = ctx.query;
let {nickname, msg} = query;
let result = await sendMsg(nickname, msg);
ctx.body = result;
});
router.get('/heartBeat', async (ctx) => {
ctx.body = await heartBeatDetect();
});
router.get('/data', async (ctx) => {
ctx.body = await getWechatData();
});
router.get('/contactList', async (ctx) => {
ctx.body = getContactList();
});
router.get('/contactList/update', async (ctx) => {
await getContactData();
ctx.body = getContactList();
});
showQRcode().then(() => {
logger.info('QRcode scan success!');
app
.use(bodyparser())
.use(koaStatic(`${__dirname}/static`))
.use(router.routes())
.listen(config.port);
console.log(`listening on port ${config.port}`);
// 心跳检测
new CronJob('*/20 * * * * *', () => {
(async () => {
let result = await heartBeatDetect();
logger.info(`心跳检测 ${result ? '成功' : '失败'} ${new Date()}`);
})();
}, null, true, TZ);
}, (err) => {
console.log(`QRcode scan failed in "${err}"`);
});