-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
357 lines (303 loc) · 11.6 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
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
'use strict';
var domain = require('domain');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var cloud = require('./cloud');
var AV = require('leanengine');
var formidable = require('formidable');
var fs = require('fs');
var app = express();
var pwd = 'test';
// 设置 view 引擎
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static('public'));
// 加载云代码方法
app.use(cloud);
// 使用 LeanEngine 中间件
// (如果没有加载云代码方法请使用此方法,否则会导致部署失败,详细请阅读 LeanEngine 文档。)
// app.use(AV.Cloud);
// app.use(bodyParser.json());
// app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
// 未处理异常捕获 middleware
app.use(function (req, res, next) {
var d = null;
if (process.domain) {
d = process.domain;
} else {
d = domain.create();
}
d.add(req);
d.add(res);
d.on('error', function (err) {
console.error('uncaughtException url=%s, msg=%s', req.url, err.stack || err.message || err);
if (!res.finished) {
res.statusCode = 500;
res.setHeader('content-type', 'application/json; charset=UTF-8');
res.end('uncaughtException');
}
});
d.run(next);
});
app.get('/', function (req, res) {
var query = new AV.Query('Record');
query.addDescending('createdAt');
query.limit(1);
query.find().then(function (records) {
res.render('patern', {
_start: records.length?records[0].get('start'):'',
_end: records.length?records[0].get('end'):'',
_keyword: records.length?records[0].get('keyword'):'',
_patchKeyword: records.length?records[0].get('patchKeyword'):'',
_delKeyword: records.length?records[0].get('delKeyword'):''
});
}, function (err) {
throw err;
})
});
//新建record
app.post('/record', function (req, res) {
var form = new formidable.IncomingForm();
form.uploadDir = './temp';
form.parse(req, function (err, fields, files) {
if (err) {
fs.unlink(files['txt'].path);
throw err;
}
// 上传文件所需的密码
if (fields.pwd != pwd) {
res.send('密码错误请重新输入!');
return;
}
fs.readFile(files['txt'].path, 'utf-8', function (err, data) {
if (err) {
fs.unlink(files['txt'].path);
throw err;
}
var file = new AV.File(files['txt'].name, new Buffer(data));
var record = new AV.Object('Record');
//配置项
record.save({
start: fields.start,
end: fields.end,
keyword: fields.keyword,
patchKeyword: fields.patchKeyword,
delKeyword:fields.delKeyword,
file: file
}).then(function (record) {
fs.unlink(files['txt'].path, function (err) {
if (err) {
throw err;
}
res.redirect('/record/' + record.id);
});
}, function (err) {
fs.unlink(files['txt'].path);
throw err;
})
})
});
});
//获取record
app.get('/record/:id', function (req, res) {
var id = req.params.id;
var query = new AV.Query('Record');
if (id == 'recent') {
query.addDescending('createdAt');
query.limit(1);
query.find().then(function (records) {
res.redirect('/record/' + records[0].id)
}, function (err) {
throw err;
})
return;
}
var query = new AV.Query('Record');
query.include('file');
query.get(id).then(function (record) {
//向文件链接发送请求,目的是得到text
AV.Cloud.httpRequest({
url: record.get('file').url()
}).then(function (data) {
var keyword = record.get('keyword');
var patchKeyword = record.get('patchKeyword').replace(/([\[\]])/g, '\\$1').replace(/%date%/, '(\\d{4}-\\d{2}-\\d{2})');
var delKeyword = record.get('delKeyword').replace(/([\[\]])/g, '\\$1').replace(/%date%/, '(\\d{4}-\\d{2}-\\d{2})');
var start = new Date(record.get('start'));
var end = new Date(record.get('end'));
/*var names = (function () {
var reg = /^.+$/mg,
temp,
names = [];
//被匹配的字符串是每行一个名字
while (temp = reg.exec(record.get('names'))) {
names.push(temp[0]);
}
return names;
})()*/
// ?原本是贪婪的
var reg = new RegExp('(\\d{4}-\\d{2}-\\d{2})\\s\\d{1,2}:\\d{2}:\\d{2}\\s\\s?'+ //日期
'(?:【.*?】)?(.*?)'+ //姓名
'(?:\\(\\d+?\\))?'+ //QQ
'(?:(?:\\r\\n)|(?:\\n))'+ //换行
'((?:.|\\s)*?)'+ //说话内容,.不能匹配换行符,所以需要和\s组合起来
'(?=\\d{4}-\\d{2}-\\d{2}\\s\\d{1,2}:\\d{2}:\\d{2}\\s\\s?)', 'g'); //断言
/*var analysis = {
'2016-03-20': {
'Dophin': '<打卡>test1',
'Hugin': '<打卡>test2'
},
'2016-03-21': {
'Dophin': '<打卡>test3',
'Hugin': '<打卡>test4'
}
}*/
var analysis = {},
names = {};
var temp, //临时存放正常情况下的exec数组
patch, //临时存放补卡情况下的exec数组
del, //临时存放删卡情况下的exec数组
lastIndex;
// 每个循环正常情况会匹配到一个会话,最后一个会话会被忽略
// 如果多个对话都出现关键字,那么以最后一个为准;如果一句话内出现多个相同关键字,以第一个为准
// temp[1]为日期,temp[2]为姓名,temp[3]为内容
while (temp = reg.exec(data.text)) {
lastIndex = reg.lastIndex;
//处理特殊情况
if(temp[2] == '()' || temp[2] == '0')
continue;
names[temp[2]] = null;
//删卡
if (del = new RegExp(delKeyword).exec(temp[3])) {
analysis[del[1]] && (delete analysis[del[1]][del[2]]);
//console.log(temp[0],'====删卡操作'+del[0]+'====\n\n\n');
continue;
}
//补卡
if (patch = new RegExp(patchKeyword).exec(temp[3])) {
temp[1] = patch[1];
analysis[temp[1]] || (analysis[temp[1]] = {});
analysis[temp[1]][temp[2]] = temp[3];
//console.log(temp[0],'====补卡操作'+patch[0]+'====\n\n\n');
continue;
}
//打卡
if (temp[3].indexOf(keyword) != -1) { //意味着这个会话的含义是打卡
analysis[temp[1]] || (analysis[temp[1]] = {});
analysis[temp[1]][temp[2]] = temp[3];
//console.log(temp[0],'====打卡操作'+temp[1]+'====\n\n\n');
}
}
//对最后一个会话进行专门的处理
reg = new RegExp('(\\d{4}-\\d{2}-\\d{2})\\s\\d{1,2}:\\d{2}:\\d{2}\\s\\s?(.*?)(?:\\(\\d+?\\))?(?:(?:\\r\\n)|(?:\\n))((?:.|\\s)*)');
temp = data.text.substr(lastIndex);
temp = reg.exec(temp);
names[temp[2]] = null;
if(del = new RegExp(delKeyword).exec(temp[3])){
analysis[del[1]] && (delete analysis[del[1]][del[2]]);
//console.log(temp[0],'====删卡操作'+del[0]+'====\n\n\n');
}
else if (patch = new RegExp(patchKeyword).exec(temp[3])) { //补卡
temp[1] = patch[1];
analysis[temp[1]] || (analysis[temp[1]] = {});
analysis[temp[1]][temp[2]] = temp[3];
//console.log(temp[0],'====补卡操作'+patch[0]+'====\n\n\n');
} else if (temp[3].indexOf(keyword) != -1) { //打卡
analysis[temp[1]] || (analysis[temp[1]] = {});
analysis[temp[1]][temp[2]] = temp[3];
//console.log(temp[0],'====打卡操作'+temp[1]+'====\n\n\n');
}
res.render('display', {
_keyword: keyword,
_patchKeyword: record.get('patchKeyword'),
_delKeyword: record.get('delKeyword'),
_start: record.get('start'),
_end: record.get('end'),
names: names,
file: {
name: record.get('file').name(),
url: record.get('file').url()
},
date: {
start: start,
end: end,
days: (function () {
return Math.ceil((end - start) / (24 * 60 * 60 * 1000));
})()
},
analysis: analysis,
//格式化日期
formatDate: function (d) {
d = new Date(d);
var mon = d.getMonth() + 1, date = d.getDate();
mon = mon < 10 ? '0' + mon : mon;
date = date < 10 ? '0' + date : date;
return d.getFullYear() + "-" + mon + "-" + date;
}
});
}, function (err) {
console.log('请求错了:',err);
throw err;
});
}, function (err) {
throw err;
})
})
//更新record
app.patch('/record/:id', function (req, res) {
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields) {
if (err) {
throw err;
}
if (fields.pwd != pwd) {
res.json({code: 1, msg: '密码错误请重新输入!'})
return;
}
var post = AV.Object.createWithoutData('Record', req.params.id);
post.save({
start: fields.start,
end: fields.end,
keyword: fields.keyword,
patchKeyword: fields.patchKeyword,
delKeyword: fields.delKeyword
}).then(function () {
res.json({code: 200, msg: '保存成功'})
}, function (err) {
res.json({code: err.code, msg: err.message})
});
});
})
// 如果任何路由都没匹配到,则认为 404
// 生成一个异常让后面的 err handler 捕获
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// 如果是开发环境,则将异常堆栈输出到页面,方便开发调试
if (app.get('env') === 'development') {
app.use(function (err, req, res, next) { // jshint ignore:line
var statusCode = err.status || 500;
if (statusCode === 500) {
console.error(err.stack || err);
}
res.status(statusCode);
res.render('error', {
message: err.message || err,
error: err
});
});
}
// 如果是非开发环境,则页面只输出简单的错误信息
app.use(function (err, req, res, next) { // jshint ignore:line
res.status(err.status || 500);
res.render('error', {
message: err.message || err,
error: {}
});
});
module.exports = app;