月度年度报表后台job
沈斌
2016-12-27 c744d24393b7fdb169654c499da8bd7e7d77f609
环境数月报自动生成生成并发送邮件功能的完善
3 files modified
42 ■■■■ changed files
config.js 3 ●●●●● patch | view | raw | blame | history
package.json 4 ●●● patch | view | raw | blame | history
reporter.js 35 ●●●● patch | view | raw | blame | history
config.js
@@ -3,6 +3,9 @@
 */
module.exports = {
    mongo: {
        uri: 'mongodb://moral_user:m2o0r1a6l_passw0rd@121.40.92.176:27017/moral_db'
    },
    smtpOption: {
        host: 'smtp.qq.com',
        port: 465,
package.json
@@ -6,6 +6,8 @@
    "nodemailer-smtp-transport": "~2.7.2",
    "retry": "~0.10.0",
    "request": "~2.79.0",
    "webshot": "~0.18.0"
    "webshot": "~0.18.0",
    "mongodb": "~2.2.16",
    "moment": "~2.17.1"
  }
}
reporter.js
@@ -9,10 +9,37 @@
var webshot = require('webshot');
var fs = require("fs");
var config = require('./config');
var mongoClient = require('mongodb').MongoClient;
var moment = require('moment');
request('http://env.7drlb.com/report/user/5766a035f08504e7cd3fb33e/year/2016/month/11/view', function (error, response, body) {
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 = '5766a035f08504e7cd3fb33e_2016_11.png';
            var filename = userID + '_' + year + '_' + month + '.png';
        webshot(body, filename, {
            renderDelay: 3000,
            siteType:'html',
@@ -51,8 +78,8 @@
                    });
                });
            };
            send_email("it01@moral.org.cn", '七星博士环境监测月度统计表', '2016-11月度报告');
                send_email(email, '七星博士环境监测月度统计表', '2016-11月度报告');
        });
    }
});
};