...
|
...
|
@@ -11,42 +11,53 @@ var oldUploadUrl = 'http://upload.static.yohobuy.com'; |
|
|
var errBackMsg = {
|
|
|
code: "500",
|
|
|
message: "接口异常"
|
|
|
};
|
|
|
};
|
|
|
|
|
|
//var domain = 'http://172.16.6.227:8088/platform'; //马力
|
|
|
// var domain = 'http://172.16.6.162:8088/platform';
|
|
|
//var domain = 'http://172.16.6.240:8088/platform';
|
|
|
|
|
|
exports.uploadFile = function(req, res) {
|
|
|
exports.uploadFile = function (req, res) {
|
|
|
var headers = {};
|
|
|
if (req.session && req.session.user) {
|
|
|
var ipObject = ipaddr.process(req.ip).octets
|
|
|
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{
|
|
|
}
|
|
|
else {
|
|
|
res.json({
|
|
|
code: "501",
|
|
|
message: "用户失效,请重新登录"
|
|
|
});
|
|
|
}
|
|
|
|
|
|
if (req.is("multipart/form-data")) {
|
|
|
console.log(req.body);
|
|
|
var url=[];
|
|
|
for(var key in req.body){
|
|
|
url.push(key+"="+req.body[key]);
|
|
|
var url = [];
|
|
|
for (var key in req.body) {
|
|
|
url.push(key + "=" + req.body[key]);
|
|
|
}
|
|
|
//req.body.file = fs.createReadStream(req.files[req.body.filename].path);
|
|
|
var files = req.files[req.body.filename];
|
|
|
//判断是单张传还是多张传
|
|
|
var flag = Object.prototype.toString.call(files) === '[object Array]';
|
|
|
//如果是单张,则数组化
|
|
|
if (!flag) {
|
|
|
files = [];
|
|
|
files.push(req.files[req.body.filename]);
|
|
|
}
|
|
|
req.body.files = [];
|
|
|
req.body.fileNames = [];
|
|
|
for (var index = 0; index < files.length; index++) {
|
|
|
req.body.files[index] = fs.createReadStream(files[index].path);
|
|
|
req.body.fileNames[index] = files[index].name;
|
|
|
}
|
|
|
req.body.file = fs.createReadStream(req.files[req.body.filename].path);
|
|
|
if (req.body.__type == "batch-import") {
|
|
|
request.post({
|
|
|
url: domain + '/batch/import?'+url.join("&"),
|
|
|
url: domain + '/batch/import?' + url.join("&"),
|
|
|
formData: req.body,
|
|
|
headers: headers
|
|
|
}, function optionalCallback(error, httpResponse, body) {
|
...
|
...
|
@@ -56,33 +67,37 @@ exports.uploadFile = function(req, res) { |
|
|
if (!error && httpResponse.statusCode == 200) {
|
|
|
var json = JSON.parse(body);
|
|
|
json.status = true;
|
|
|
// console.log(json);
|
|
|
res.json(json);
|
|
|
} else {
|
|
|
res.json(errBackMsg);
|
|
|
}
|
|
|
});
|
|
|
} else {
|
|
|
|
|
|
//如果是goodsimg(商品)的图片,灰度到老系统的上传地址
|
|
|
if(req.body.bucket === 'goodsimg') {
|
|
|
req.body.filename = req.files[req.body.filename].path;
|
|
|
if (req.body.bucket === 'goodsimg') {
|
|
|
//req.body.filename = req.files[req.body.filename].path;
|
|
|
request.post({
|
|
|
url: oldUploadUrl,
|
|
|
formData:{
|
|
|
fileData:req.body.file,
|
|
|
project:req.body.bucket
|
|
|
formData: {
|
|
|
fileData: req.body.files,
|
|
|
project: req.body.bucket
|
|
|
}
|
|
|
},function(error,resd,body) {
|
|
|
if(!error && resd.statusCode == 200) {
|
|
|
}, function (error, resd, body) {
|
|
|
if (!error && resd.statusCode == 200) {
|
|
|
var ret = JSON.parse(body);
|
|
|
if(ret.code === 200) {
|
|
|
var imgUrl = ret.data.imagesList[0];
|
|
|
if (ret.code === 200) {
|
|
|
//var imgUrl = ret.data.imagesList[0];
|
|
|
var imgs = ret.data.imagesList || [];
|
|
|
var datas = [];
|
|
|
for (var i = 0; i < imgs.length; i++) {
|
|
|
datas.push(joinimg(imgs[i]))
|
|
|
}
|
|
|
res.json({
|
|
|
code:200,
|
|
|
data:joinimg(imgUrl),
|
|
|
message:'上传成功',
|
|
|
status:true
|
|
|
code: 200,
|
|
|
data: datas,
|
|
|
names: req.body.fileNames,
|
|
|
message: '上传成功',
|
|
|
status: true
|
|
|
});
|
|
|
return;
|
|
|
}
|
...
|
...
|
@@ -90,12 +105,11 @@ exports.uploadFile = function(req, res) { |
|
|
res.json(errBackMsg);
|
|
|
});
|
|
|
} else {
|
|
|
|
|
|
req.body.file = fs.createReadStream(req.files[req.body.filename].path);
|
|
|
request.post({
|
|
|
url: domain + '/fileupload/upload',
|
|
|
formData: req.body
|
|
|
}, function optionalCallback(error, httpResponse, body) {
|
|
|
console.log(body);
|
|
|
if (!error && httpResponse.statusCode == 200) {
|
|
|
var json = JSON.parse(body);
|
|
|
json.status = true;
|
...
|
...
|
@@ -112,7 +126,7 @@ exports.uploadFile = function(req, res) { |
|
|
message: "请求类型错误"
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 组合全路径的图片地址
|
...
|
...
|
@@ -120,12 +134,12 @@ exports.uploadFile = function(req, res) { |
|
|
function joinimg(url) {
|
|
|
var urlArr = url.split('/');
|
|
|
var domain = 'static.yhbimg.com/goodsimg';
|
|
|
var num = urlArr[urlArr.length-1].substr(1,1);
|
|
|
var url = domain + url;
|
|
|
if(num == 1) {
|
|
|
return 'http://img11.' + url;
|
|
|
var num = urlArr[urlArr.length - 1].substr(1, 1);
|
|
|
var url1 = domain + url;
|
|
|
if (num == 1) {
|
|
|
return 'http://img11.' + url1;
|
|
|
} else {
|
|
|
return 'http://img12.'+ url;
|
|
|
return 'http://img12.' + url1;
|
|
|
}
|
|
|
}
|
|
|
|
...
|
...
|
@@ -153,7 +167,7 @@ function joinimg(url) { |
|
|
exports.ueditor = function(req, res) {
|
|
|
var headers = {};
|
|
|
if (req.session && req.session.user) {
|
|
|
var ipObject = ipaddr.process(req.ip).octets
|
|
|
var ipObject = ipaddr.process(req.ip).octets;
|
|
|
var ip = ipObject ? ipObject.join('.') : req.ip;
|
|
|
|
|
|
headers = {
|
...
|
...
|
@@ -169,8 +183,6 @@ exports.ueditor = function(req, res) { |
|
|
message: "用户失效,请重新登录"
|
|
|
});
|
|
|
}
|
|
|
|
|
|
console.log(req.files);
|
|
|
if (req.is("multipart/form-data")) {
|
|
|
req.body.file = fs.createReadStream(req.files.upfile.path);
|
|
|
req.body.bucket="goodsimg";
|
...
|
...
|
|