| 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509 |
2
2
2
2
2
45
45
45
45
45
45
45
1
105
45
105
1
1
2
6
2
4
2
2
4
4
5
5
5
1
1
4
4
3
1
1
1
46
2
44
11
11
4
2
2
2
3
3
12
1
11
1
2
1
10
4
1
3
3
6
2
3
3
2
1
1
5
2
2
3
17
17
17
1
16
6
10
2
2
97
6
91
5
2
5
5
4
5
6
6
6
6
3
3
3
2
2
3
3
2
3
3
3
2
2
3
23
22
23
19
16
16
16
3
51
31
20
3
20
19
20
20
20
5
1
1
5
4
5
5
5
5
5
5
5
5
5
5
2
2
1
2
2
2
7
7
3
3
4
1
1
1
4
7
2
7
7
7
1
2
2
2
3
1
1
1
2
| /**
* Controller 基类
* @return {[type]} [description]
*/
var fs = require('fs');
var path = require('path');
var url = require('url');
module.exports = Class(function() {
'use strict';
return {
/**
* 初始化执行方法
* @param {[type]} http [description]
* @return {[type]} [description]
*/
init: function(http) {
this.http = http;
this.view = null;
//将http数据打到模版里
this.assign('http', this.http);
//将配置信息打到模版里
this.assign('config', C());
//设置变量别名
this.set = this.assign;
//success别名
this.ok = this.success;
//error别名
this.fail = this.error;
},
/**
* 获取客户端的ip
* @return {[type]} [description]
*/
ip: function() {
return this.http.ip();
},
/**
* 实例化View类
* @return {[type]} [description]
*/
initView: function() {
if (!this.view) {
this.view = thinkRequire('View')(this.http);
}
return this.view;
},
/**
* 是否是GET请求
* @return {Boolean} [description]
*/
isGet: function() {
return this.http.method === 'GET';
},
/**
* 是否是POST请求
* @return {Boolean} [description]
*/
isPost: function() {
return this.http.method === 'POST';
},
/**
* 是否是特定METHOD请求
* @param {[type]} method [description]
* @return {Boolean} [description]
*/
isMethod: function(method) {
return this.http.method === method.toUpperCase();
},
/**
* 是否是AJAX请求
* @return {Boolean} [description]
*/
isAjax: function(method) {
//请求类型判断
if (method && this.http.method !== method.toUpperCase()) {
return false;
}
return this.header('x-requested-with') === 'XMLHttpRequest';
},
/**
* 是否是websocket请求
* @return {Boolean} [description]
*/
isWebSocket: function(){
return !!this.http.websocket;
},
/**
* 是否是命令行模式
* @return {Boolean} [description]
*/
isCli: function(){
return APP_MODE === 'cli';
},
/**
* 是否是jsonp接口
* @return {Boolean} [description]
*/
isJsonp: function(name){
name = name || C('url_callback_name');
return !!this.get(name);
},
/**
* token功能
* @return {[type]} [description]
*/
token: function(token){
var tokenName = C('token_name');
var self = this;
if (token) {
return this.session(tokenName).then(function(value){
return value === token;
})
}else{
return this.session(tokenName).then(function(token){
if (token) {
return token;
}
token = thinkRequire('Session').uid(32);
return self.session(tokenName, token).then(function(){
return token;
})
})
}
},
/**
* 获取QUERY参数
* @param {[type]} name [description]
* @return {[type]} [description]
*/
get: function(name) {
if (name === undefined) {
return this.http.get;
}
return this.http.get[name] || '';
},
/**
* 获取POST参数
* @param {[type]} name [description]
* @return {[type]} [description]
*/
post: function(name) {
var http = this.http;
return name ? (http.post[name] || '') : http.post;
},
/**
* 获取参数
* @param {[type]} name [description]
* @return {[type]} [description]
*/
param: function(name) {
if (name === undefined) {
var post = this.post();
return !isEmpty(post) ? post : this.get();
}
return this.post(name) || this.get(name);
},
/**
* 获取上传的文件
* @param {[type]} name [description]
* @return {[type]} [description]
*/
file: function(name) {
var http = this.http;
return name ? (http.file[name] || {}) : http.file;
},
/**
* header操作
* @param {[type]} name [description]
* @param {[type]} value [description]
* @return {[type]} [description]
*/
header: function(name, value) {
if (name === undefined) {
return this.http.headers;
}else if (isObject(name)) {
for (var key in name) {
this.header(key, name[key]);
}
return this;
}else if (value !== undefined) {
if (name === 'Content-Type') {
return this.type(value);
}
this.http.setHeader(name, value);
return this;
}else{
return this.http.getHeader(name);
}
},
/**
* 获取userAgent
* @return {[type]} [description]
*/
userAgent: function(){
return this.http.headers['user-agent'] || '';
},
/**
* 获取referrer
* @return {[type]} [description]
*/
referer: function(host){
var referer = this.http.headers.referer || this.http.headers.referrer || '';
if (!referer || !host) {
return referer;
}
var info = url.parse(referer);
return info.hostname;
},
/**
* cookie操作
* @param {[type]} name [description]
* @param {[type]} value [description]
* @param {[type]} options [description]
* @return {[type]} [description]
*/
cookie: function(name, value, options) {
if (value !== undefined) {
this.http.setCookie(name, value, options);
return this;
}
return name === undefined ? this.http.cookie : (this.http.cookie[name] || '');
},
/**
* session
* 如果是get操作,则返回一个promise
* @param {[type]} name [description]
* @param {[type]} value [description]
* @return {[type]} [description]
*/
session: function(name, value) {
thinkRequire('Session').start(this.http);
var instance = this.http.session;
if (name === undefined) {
return instance.rm();
}
if (value !== undefined) {
return instance.set(name, value);
}
return instance.get(name);
},
/**
* 跳转,返回一个pendding promise阻止后面继续执行
* @param {[type]} url [description]
* @param {[type]} code [description]
* @return {[type]} [description]
*/
redirect: function(url, code) {
this.http.redirect(url, code);
return getDefer().promise;
},
/**
* 赋值变量到模版
* @param {[type]} name [description]
* @param {[type]} value [description]
* @return {[type]} [description]
*/
assign: function(name, value) {
if (arguments.length <= 1) {
return this.initView().assign(name);
}
return this.initView().assign(name, value);
},
/**
* 获取解析后的模版内容
* @param {[type]} templateFile [description]
* @param {[type]} content [description]
* @return {[type]} [description]
*/
fetch: function(templateFile) {
return this.initView().fetch(templateFile);
},
/**
* 输出模版内容
* @param {[type]} templateFile [description]
* @param {[type]} charset [description]
* @param {[type]} contentType [description]
* @param {[type]} content [description]
* @return {[type]} [description]
*/
display: function(templateFile, charset, contentType) {
return this.initView().display(templateFile, charset, contentType);
},
/**
* 调用另一个controll里的aciton
* 可以跨分组
* A('Admin/Test/index')
* @param {[type]} action [description]
* @return {[type]} [description]
*/
action: function(action, data) {
//自动补group
action = action.replace(/\//g, ':');
if (action.split(':').length === 2) {
action = this.http.group + ':' + action;
}
return A(action, this.http, data);
},
/**
* jsonp格式输出
* @param {[type]} data [description]
* @param {[type]} jsonp [description]
* @return {[type]} [description]
*/
jsonp: function(data) {
this.type(C('json_content_type'));
var callback = this.get(C('url_callback_name'));
//过滤callback值里的非法字符
callback = callback.replace(/[^\w\.]/g, '');
if (callback) {
data = callback + '(' + (data !== undefined ? JSON.stringify(data) : '') + ')';
this.end(data);
} else {
this.end(data);
}
},
/**
* json格式输出
* @param {[type]} data [description]
* @return {[type]} [description]
*/
json: function(data){
this.type(C('json_content_type'));
return this.end(data);
},
/**
* 设置http响应状态码
* @param {[type]} status [description]
* @return {[type]} [description]
*/
status: function(status) {
var res = this.http.res;
if (!res.headersSent) {
res.statusCode = status || 404;
}
return this;
},
/**
* 阻止访问
* @param {[type]} status [description]
* @return {[type]} [description]
*/
deny: function(status){
var res = this.http.res;
if (!res.headersSent) {
res.statusCode = status || 403;
this.http.end();
}
return getDefer().promise;
},
/**
* 输出内容
* 自动JSON.stringify
* 自定将数字等转化为字符串
* @param {[type]} obj [description]
* @return {[type]} [description]
*/
echo: function(obj, encoding) {
//自动发送Content-Type的header
if (C('auto_send_content_type')) {
this.type(C('tpl_content_type'));
}
return this.http.echo(obj, encoding);
},
/**
* 结束输出,输出完成时一定要调用这个方法
* @param {[type]} obj [description]
* @return {[type]} [description]
*/
end: function(obj, encoding) {
if (obj !== undefined) {
var self = this;
return this.echo(obj, encoding).then(function(){
self.http.end();
});
}
this.http.end();
},
/**
* 发送Content-Type
* @param {[type]} type [description]
* @return {[type]} [description]
*/
type: function(ext){
if (this.http.cthIsSend || !ext) {
return;
}
if (ext.indexOf('/') === -1) {
ext = require('mime').lookup(ext);
}
if (ext.toLowerCase().indexOf('charset=') === -1) {
ext += '; charset=' + C('encoding');
}
//Content-Type Header has been Send
this.http.cthIsSend = true;
this.http.setHeader('Content-Type', ext);
return this;
},
/**
* 下载文件
* @return Promise [description]
*/
download: function(file, contentType, filename) {
if (isString(contentType) && contentType.indexOf('.') > -1) {
filename = contentType;
contentType = '';
}
if (!contentType || contentType.indexOf('/') === -1) {
contentType = require('mime').lookup(contentType || file);
}
var http = this.http;
var fileStream = fs.createReadStream(file);
var deferred = getDefer();
this.type(contentType);
http.setHeader('Content-Disposition', 'attachment; filename="' + (filename || path.basename(file)) + '"');
fileStream.pipe(http.res);
fileStream.on('end', function() {
http.end();
deferred.resolve();
});
return deferred.promise;
},
/**
* 正常json数据输出
* @param {[type]} data [description]
* @return {[type]} [description]
*/
success: function(data){
var obj = getObject([C('error_no_key'), C('error_msg_key')], [0, '']);
if (data !== undefined) {
obj.data = data;
}
this.type(C('json_content_type'));
this.end(obj);
return getDefer().promise;
},
/**
* 异常json数据数据
* @param {[type]} errno [description]
* @param {[type]} errmsg [description]
* @param {[type]} extra [description]
* @return {[type]} [description]
*/
error: function(errno, errmsg, data){
var obj;
if (isObject(errno)) {
data = errmsg;
obj = extend({}, errno);
}else{
if (!isNumber(errno)) {
data = errmsg;
errmsg = errno;
errno = C('error_no_default_value');
}
obj = getObject([C('error_no_key'), C('error_msg_key')], [errno, errmsg || 'error']);
}
if (data !== undefined) {
obj.data = data;
}
this.type(C('json_content_type'));
this.end(obj);
return getDefer().promise;
},
/**
* 关闭数据库连接
* @return {[type]} [description]
*/
closeDb: function(){
thinkRequire('Model').close();
},
/**
* 发送执行时间
* @param {[type]} name [description]
* @return {[type]} [description]
*/
sendTime: function(name){
return this.http.sendTime(name);
},
/**
* 对数据进行过滤
* @param {[type]} data [description]
* @param {[type]} type [description]
* @return {[type]} [description]
*/
filter: function() {
var filter = thinkRequire('Filter').filter;
return filter.apply(null, arguments);
},
/**
* 校验一个值是否合法
* @param {[type]} data [description]
* @param {[type]} validType [description]
* @return {[type]} [description]
*/
valid: function(data, validType) {
//单个值检测,只返回是否正常
if (validType !== undefined) {
data = [{
value: data,
valid: validType
}];
var result = thinkRequire('Valid')(data);
return isEmpty(result);
}
return thinkRequire('Valid')(data);
}
};
}); |