upload.js 1.7 KB
var request = require('request');
var _ = require('lodash');
var fs = require('fs');
var ipaddr = require('ipaddr.js');
var domain = require('../../config/common.js').domain;
//var domain = 'http://172.16.6.227:8083/yohobuy-platform-web'; //马力
// var domain = 'http://172.16.6.162:8088/platform';

var env = process.env.NODE_ENV || 'development';

exports.uploadFile = function(req, res) {
    var headers = {};
    if (req.session && req.session.user) {
        var ipObject = ipaddr.process(req.ip).octets
        var ip = ipObject ? ipObject.join('.') : req.ip;

        headers = {
            'x-user-id': req.session.user.auth.pid,
            'x-user-name': req.session.user.auth.account,
            'x-site-type': 1,
            'x-client-ip': ip
        };

    }
    if (req.is("multipart/form-data")) {
        req.body.file = fs.createReadStream(req.files[req.body.filename].path);
        if (req.body.__type == "batch-import") {
            request.post({
                url: domain + '/batch/import',
                formData: req.body,
                headers: headers
            }, function optionalCallback(err, httpResponse, body) {
                var json = JSON.parse(body);
                json.status = true;
                res.json(json);
            });
        } else {
            request.post({
                url: domain + '/fileupload/upload',
                formData: req.body
            }, function optionalCallback(err, httpResponse, body) {
                var json = JSON.parse(body);
                json.status = true;
                res.json(json);
            });
        }
    } else {
        res.json({
            code: "500",
            message: "请求类型错误"
        });
    }
}