upload.js
6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
var request = require('request');
var _ = require('lodash');
var fs = require('fs');
var http = require('http');
var config=global.global.Register;
var Utils={
objectToUrl:function(items){
var urls=[];
for(var name in items){
if(items.hasOwnProperty(name)&&items[name]){
// if(items[name]!=="undefined"){
urls.push(name+"="+items[name]);
// }
}
}
return urls.join('&');
},
createHttpHeader:function(req){
if (req.session && req.session.user) {
return {
'x-user-id': req.session.user.auth.pid,
'x-user-name': req.session.user.auth.account,
'x-site-type': 1,
'x-client-ip': req.ip,
'x-shop-id':req.session.user.auth.shopId
};
}
},
joinImg:function(url) {
var urlArr = url.split('/');
var domain = 'static.yhbimg.com/goodsimg';
var num = urlArr[urlArr.length - 1].substr(1, 1);
var url1 = domain + url;
if (num == 1) {
return 'http://img11.' + url1;
} else {
return 'http://img12.' + url1;
}
}
}
module.exports={
namespace:"common",
apis:{
multipart:function(req, callback){
var result={code: "500",message: "接口异常"};
var httpHeader=Utils.createHttpHeader(req);
if(!httpHeader){
result = { code: "501", message: "用户失效,请重新登录" };
return callback(null, result);
}
var body=req.body;
if(body.__type == "batch-import"){
var url=Utils.objectToUrl(body);
body.file = fs.createReadStream(req.files[body.filename].path);
request.post({
url: config.domain + '/batch/import?' + url,
formData: body,
headers: httpHeader
}, function optionalCallback(error, httpResponse, rebody) {
if (!error && httpResponse.statusCode == 200) {
result = JSON.parse(rebody);
result.status = true;
}
return callback(null, result);
});
}else{
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;
}
if (req.body.bucket === 'goodsimg') {
request.post({
url: "http://upload.static.yohobuy.com",
formData: {
fileData: req.body.files,
project: req.body.bucket
}
}, function (error, httpResponse, rebody) {
if (!error && httpResponse.statusCode == 200) {
var ret = JSON.parse(rebody);
var imgs = ret.data.imagesList || [];
var datas = [];
for (var i = 0; i < imgs.length; i++) {
datas.push(Utils.joinImg(imgs[i]))
}
result={
code: 200,
data: datas[0],
datas:datas,
names: req.body.fileNames,
message: '上传成功',
status: true
};
}
return callback(null, result);
});
} else {
req.body.file = fs.createReadStream(req.files[req.body.filename].path);
request.post({
url: config.domain + '/fileupload/upload',
formData: req.body
}, function optionalCallback(error, httpResponse, rebody) {
if (!error && httpResponse.statusCode == 200) {
result = JSON.parse(rebody);
result.status = true;
}
return callback(null, result);
});
}
}
},
baiduUeditor:function(req, callback){
var result={code: "500",message: "接口异常"};
var httpHeader=Utils.createHttpHeader(req);
if(!httpHeader){
result={code: "501",message: "用户失效,请重新登录"};
return callback(null, result);
}
req.body.files = [fs.createReadStream(req.files.upfile.path)];
req.body.bucket="goodsimg";
request.post({
url: "http://upload.static.yohobuy.com",
formData: {
fileData: req.body.files,
project: req.body.bucket
}
}, function optionalCallback(error, httpResponse, body) {
if (!error && httpResponse.statusCode == 200) {
var ret = JSON.parse(body);
if (ret.code === 200) {
var imgUrl = ret.data.imagesList[0];
imgUrl=Utils.joinImg(imgUrl);
result={};
if(imgUrl){
result.originalName=Date.now();
result.name=Date.now();
result.url=imgUrl;
result.type=imgUrl.substring(imgUrl.lastIndexOf('.'));
result.state="SUCCESS";
}else{
result.state="FAIL";
}
}
}
return callback(null, result);
});
},
downExcel:function(req, callback){
var result = request.get(config.domain + "/common/" + req.params.name + ".xlsx");
return callback(null, result);
},
exportExcel:function(req, callback){
var headers=Utils.createHttpHeader(req);
headers['content-type']='application/json';
var result = request({
method: 'POST',
url: config.domain + '/batch/export',
headers: headers,
body: req.query,
json: true
});
return callback(null, result);
},
proxy:function(req, callback){
var httpHeader = Utils.createHttpHeader(req);
if(!httpHeader){
return null;
}
httpHeader['Content-Type'] = 'application/json';
var result=request({
headers:headers,
url:global.config.domain + req.query.url,
body:JSON.stringify(req.body)
});
return callback(null, result)
},
download:function(req, callback){
var result = request.get(config.domain + "/batch/download?path=" + req.query.path);
return callback(null, result);
}
}
}