ueditor.js 2.37 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 domain = 'http://172.16.6.240:8088/platform';

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
        };

    }else{
        res.json({
                code: "501",
                message: "用户失效,请重新登录"
            });
    }

    console.log(req.files);
    if (req.is("multipart/form-data")) {
        req.body.file = fs.createReadStream(req.files.upfile.path);
        req.body.bucket="goodsimg";
        request.post({
                url: domain + '/fileupload/upload',
                formData: req.body
        }, function optionalCallback(error, httpResponse, body) {
            // {"code":200,"data":"http://img12.static.yhbimg.com/goodsimg/2016/03/10/20/0258a569ab956e81339c7212bb23767c2d.jpg","message":"上传成功","status":true}
            // {"originalName":"demo.jpg","name":"demo.jpg","url":"upload\/demo.jpg","size":"99697","type":".jpg","state":"SUCCESS"}
            // {"originalName":"018da7232374448d10a565fdfe6211d078.jpg","name":"","url":"","type":".jpg","state":"SUCCESS"}
            if (!error && httpResponse.statusCode == 200) {
                var json = JSON.parse(body);
                var result={};
                result.originalName=json.data.substring(json.data.lastIndexOf('/')+1);
                result.name=result.originalName;
                result.url=json.data;
                result.type=json.data.substring(json.data.lastIndexOf('.'));
                result.state=json.message=="上传成功"?"SUCCESS":"FAIL";
                res.send(JSON.stringify(result));
            }else{
                res.send({code:"500",message:"接口异常"});
            }
        });

    } else {
        res.json({
            code: "500",
            message: "请求类型错误"
        });
    }
}