月度年度报表后台job
徐荣
2017-01-04 0f269e2a147d330741b516b7cea156cd7c01dabe
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
/**
 * Created by bin.shen on 26/12/2016.
 */
 
var nodemailer = require("nodemailer");
var smtpTransport = require('nodemailer-smtp-transport');
var retry = require('retry');
var request = require('request');
var webshot = require('webshot');
var fs = require("fs");
var config = require('./config');
var mongoClient = require('mongodb').MongoClient;
var moment = require('moment');
 
mongoClient.connect(config.mongo.uri, function(err, db) {
    if (err) {
        console.log(err.message);
        return;
    }
    console.log('Connecting to Mongo DB at ' + config.mongo.uri);
 
    var current = moment();
    var year = current.format("YYYY");
    var month = current.format("MM");
 
    db.collection("users").find().toArray(function(err, docs) {
        docs.forEach(function(doc) {
            var userID = doc._id;
            var email = doc.email;
            console.log(year + ' | ' + month + ' | ' + userID + ' | ' + email);
            sendReport(userID, year, month, email);
        });
    });
 
    db.close();
});
 
var sendReport = function(userID, year, month, email) {
    var url = 'http://env.7drlb.com/report/user/' + userID + '/year/' + year + '/month/' + month + '/view';
    request(url, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            var filename = userID + '_' + year + '_' + month + '.png';
            webshot(body, filename, {
                renderDelay: 3000,
                siteType:'html',
                screenSize: {
                    width: 1000,
                    height: 2000
                },
                shotSize: {
                    width: 1000,
                    height: 'all'
                }
            }, function(err) {
                var filepath = __dirname + '/' + filename;
                var imageFile = fs.readFileSync(filepath);
                var send_email = function(email, subject, content, level) {
                    var operation = retry.operation(config.retryOption);
                    operation.attempt(function(currentAttempt) {
                        var to = email;
                        var html = '<img src="data:image/png;base64,' + imageFile.toString("base64") + '" width=1000 height=2000>';
                        var transport = nodemailer.createTransport(smtpTransport(config.smtpOption));
                        var mailOptions = {
                            from: config.smtpOption.auth.user,
                            to: to,
                            subject:subject,
                            html:html,
                            attachments: [{
                                path: filepath,
                                filename: filename,
                                content: content
                            }]
                        };
                        transport.sendMail(mailOptions, function(err, doc){
                            if (operation.retry(err)) {
                                return;
                            }
                        });
                    });
                };
                send_email(email, '七星博士环境监测月度统计表', '2016-11月度报告');
            });
        }
    });
};