authed.js
6.8 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
var urlencode = require('urlencode');
var passport = require('passport');
exports=module.exports=function(logger,rest,settings){
function encodeUrl(url){
return urlencode(url);
}
function encodePamars(params){
var temp=[];
for(var i=0;i<params.length;i++){
var param=params[i];
var tempParam=param.split("=")
tempParam[1]=urlencode.decode(tempParam[1], 'utf8');
tempParam[1]=tempParam[1].replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
logger.debug(tempParam[1]);
if(i<(params.length-1)){
if(/[:&+/?%#=]/g.test(tempParam[1])){
temp.push(tempParam[0]+"="+encodeUrl(tempParam[1])+"&");
}else{
temp.push(tempParam[0]+"="+tempParam[1]+"&");
}
}else{
if(/[:&+/?%#=]/g.test(tempParam[1])){
temp.push(tempParam[0]+"="+encodeUrl(tempParam[1]));
}else{
temp.push(tempParam[0]+"="+tempParam[1]);
}
}
}
return temp.join("");
}
function handlePamars(url){
var tempUrl=url;
// if(url.indexOf("?")!=-1){
// var temp=url.split("?");
// var params="";
// if(temp[1]&&temp[1].indexOf("&")!=-1){
// params=encodePamars(temp[1].split("&"));
// }else{
// params=encodePamars([temp[1]]);
// }
// return temp[0]+"?"+params;
// }else {
// return tempUrl;
// }
return tempUrl;
}
function splitServiceFromUrl(url){
var catalog=url.substring(1,url.indexOf('/',1));
var pathUrl=url.substring(url.indexOf('/',1));
var fullPath=settings.restful.url+catalog+settings.restful.version+pathUrl;
return fullPath
}
function splitUrl(fullUrl){
if(settings.prefix&&settings.prefix.length>1){
fullUrl=fullUrl.substring(settings.prefix.length-1);
}
var url=fullUrl.substring(9);
return url;
}
function searchService(services,name){
if(services[name]){
logger.debug(services[name]);
return false;
}else{
return true;
}
}
function checkReq(req,res){
var flag=true;
// if(flag&&req.headers&&!req.headers['service-catalog']){
// res.status(400);
// res.send({"errors":{},"message":"缺少必要请求参数,服务目录名称是必填项"});
// flag=false;
// }
// if(flag&&req.session.passport&&req.session.passport.user&&!req.session.passport.user.service_catalog){
// res.status(404);
// res.send({"errors":{},"message":"服务目录未加载,请重新登录"});
// flag=false;
// }
// if(flag&&searchService(req.session.passport.user.service_catalog,req.headers['service-catalog'])){
// res.status(404);
// res.send({"errors":{},"message":"服务目录未查询到请求服务,请确认参数正确"});
// flag=false;
// }
return flag;
}
function checkJson(req){
if(req.headers&&req.headers['content-type']=='application/json'){
return true;
}else{
return false;
}
}
function get(req,res,next){
var url=splitUrl(req.originalUrl);
url=handlePamars(url);
url=splitServiceFromUrl(url);
if(checkReq(req,res)){
var options={
'service_catalog':'',
'url': url,
'useUrl':true,
'params':req.body,
'callback':_cb,
'req':req,
'res':res,
'options':{},
'excludeToken':true
};
// if(checkJson(req)){
// rest.json(options);
// }else{
// rest.get(options);
// }
rest.get(options);
}
function _cb(data,response){
logger.debug('back data:',data);
if(response.statusCode<300&&!data){
res.send({
code:200,
message:'操作成功!'
});
}else {
res.send(data);
}
}
}
function post(req,res,next){
var url=splitUrl(req.originalUrl);
url=splitServiceFromUrl(url);
if(checkReq(req,res)){
var options={
'service_catalog':'',
'url': url,
'useUrl':true,
'params':req.body,
'callback':_cb,
'req':req,
'res':res,
'options':{},
'excludeToken':true
};
if(checkJson(req)){
rest.postJson(options);
}else{
rest.post(options);
}
}
function _cb(data,response){
logger.debug('back data:',data);
if(response.statusCode<300&&!data){
res.send({
code:200,
message:'操作成功!'
});
}else{
res.send(data);
}
}
}
function put(req,res,next){
var url=splitUrl(req.originalUrl);
url=splitServiceFromUrl(url);
if(checkReq(req,res)){
var options={
'service_catalog':'',
'url':url,
'useUrl':true,
'params':req.body,
'callback':_cb,
'req':req,
'res':res,
'options':{},
'excludeToken':true
};
if(checkJson(req)){
rest.putJson(options);
}else{
rest.put(options);
}
}
function _cb(data,response){
logger.debug('back data:',data);
if(response.statusCode<300&&!data){
res.send({
code:200,
message:'操作成功!'
});
}else{
res.send(data);
}
}
}
function patch(req,res,next){
var url=splitUrl(req.originalUrl);
url=splitServiceFromUrl(url);
if(checkReq(req,res)){
var options={
'service_catalog':'',
'url': url,
'useUrl':true,
'params':req.body,
'callback':_cb,
'req':req,
'res':res,
'options':{},
'excludeToken':true
};
if(checkJson(req)){
rest.patchJson(options);
}else{
rest.patch(options);
}
}
function _cb(data,response){
logger.debug('back data:',data);
res.send(data);
}
}
function head(req,res,next){
var url=splitUrl(req.originalUrl);
url=splitServiceFromUrl(url);
if(checkReq(req,res)){
var options={
'service_catalog':'',
'url': url,
'useUrl':true,
'params':req.body,
'callback':_cb,
'req':req,
'res':res,
'options':{},
'excludeToken':true
};
rest.get(options);
}
function _cb(data,response){
logger.debug('back data:',data);
res.send(data);
}
}
function del(req,res,next){
var url=splitUrl(req.originalUrl);
url=splitServiceFromUrl(url);
if(checkReq(req,res)){
var options={
'service_catalog':'',
'url': url,
'useUrl':true,
'params':req.body,
'callback':_cb,
'req':req,
'res':res,
'options':{},
'excludeToken':true
};
rest.del(options);
}
function _cb(data,response){
logger.debug('back data:',data);
if(response.statusCode<300){
res.send({'action':'delete',"message":"删除成功"});
}else{
res.send(data);
}
}
}
function doLogin(req,res,next){
passport.authenticate('local', function(err, user, info) {
if (err) { return next(err); }
if (!user) { return res.send({'error':'用户名或密码错误!'}); }
req.logIn(user, function(err) {
if (err) { return next(err); }
return res.send({'ok':'登录成功',user_info:req.session.passport.user.user_info});
});
})(req, res, next);
}
return {
'get':get,
'post':post,
'put':put,
'patch':patch,
'head':head,
'delete':del
};
};
exports['@singleton']=true;
exports['@require']=['igloo/logger','utils/rest','igloo/settings'];