月度年度报表后台job
徐荣
2016-12-28 8d8620417789550cce223fe48daee20e3b7b4c4a
增加发送年度报表邮件功能
29 files added
85 ■■■■■ changed files
5766a035f08504e7cd3fb33e_2016.png patch | view | raw | blame | history
5766a035f08504e7cd3fb33e_2016_12.png patch | view | raw | blame | history
57b4117ecd5f86740fe26cb1_2016.png patch | view | raw | blame | history
57b4117ecd5f86740fe26cb1_2016_12.png patch | view | raw | blame | history
57bba70a20e7219a71d0b2ec_2016.png patch | view | raw | blame | history
57bba70a20e7219a71d0b2ec_2016_12.png patch | view | raw | blame | history
57e55428b2c5ee2d4c8e9cab_2016.png patch | view | raw | blame | history
57e55428b2c5ee2d4c8e9cab_2016_12.png patch | view | raw | blame | history
57ed2f60b2c5ee2d4c8e9cb3_2016.png patch | view | raw | blame | history
57ed2f60b2c5ee2d4c8e9cb3_2016_12.png patch | view | raw | blame | history
582a0fbb76b2ace74a05354e_2016.png patch | view | raw | blame | history
582a0fbb76b2ace74a05354e_2016_12.png patch | view | raw | blame | history
582d6af8677961ce6b055880_2016.png patch | view | raw | blame | history
582d6af8677961ce6b055880_2016_12.png patch | view | raw | blame | history
582fe2a320cdec097af3db74_2016.png patch | view | raw | blame | history
582fe2a320cdec097af3db74_2016_12.png patch | view | raw | blame | history
5832ec6620cdec097af3db78_2016.png patch | view | raw | blame | history
5832ec6620cdec097af3db78_2016_12.png patch | view | raw | blame | history
5834691d20cdec097af3db7a_2016.png patch | view | raw | blame | history
5834691d20cdec097af3db7a_2016_12.png patch | view | raw | blame | history
5837a148dab0a10f44fc6848_2016.png patch | view | raw | blame | history
5837a148dab0a10f44fc6848_2016_12.png patch | view | raw | blame | history
5837fc72dab0a10f44fc684a_2016.png patch | view | raw | blame | history
5837fc72dab0a10f44fc684a_2016_12.png patch | view | raw | blame | history
5844f53b1023fcd1353c1897_2016.png patch | view | raw | blame | history
5844f53b1023fcd1353c1897_2016_12.png patch | view | raw | blame | history
5852086143ae539e2895ba6c_2016.png patch | view | raw | blame | history
5852086143ae539e2895ba6c_2016_12.png patch | view | raw | blame | history
reporter_year.js 85 ●●●●● patch | view | raw | blame | history
5766a035f08504e7cd3fb33e_2016.png
5766a035f08504e7cd3fb33e_2016_12.png
57b4117ecd5f86740fe26cb1_2016.png
57b4117ecd5f86740fe26cb1_2016_12.png
57bba70a20e7219a71d0b2ec_2016.png
57bba70a20e7219a71d0b2ec_2016_12.png
57e55428b2c5ee2d4c8e9cab_2016.png
57e55428b2c5ee2d4c8e9cab_2016_12.png
57ed2f60b2c5ee2d4c8e9cb3_2016.png
57ed2f60b2c5ee2d4c8e9cb3_2016_12.png
582a0fbb76b2ace74a05354e_2016.png
582a0fbb76b2ace74a05354e_2016_12.png
582d6af8677961ce6b055880_2016.png
582d6af8677961ce6b055880_2016_12.png
582fe2a320cdec097af3db74_2016.png
582fe2a320cdec097af3db74_2016_12.png
5832ec6620cdec097af3db78_2016.png
5832ec6620cdec097af3db78_2016_12.png
5834691d20cdec097af3db7a_2016.png
5834691d20cdec097af3db7a_2016_12.png
5837a148dab0a10f44fc6848_2016.png
5837a148dab0a10f44fc6848_2016_12.png
5837fc72dab0a10f44fc684a_2016.png
5837fc72dab0a10f44fc684a_2016_12.png
5844f53b1023fcd1353c1897_2016.png
5844f53b1023fcd1353c1897_2016_12.png
5852086143ae539e2895ba6c_2016.png
5852086143ae539e2895ba6c_2016_12.png
reporter_year.js
New file
@@ -0,0 +1,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 + '/myyear/' + year + '/view';
    request(url, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            var filename = userID + '_' + year + '.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月度报告');
            });
        }
    });
};