Controller.js
3.88 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
var path=require('path');
var util = require('util');
var md5 = require('md5');
var Emitter=require('events');
/*重写控制器*/
var _ = require('lodash');
var fs=require('fs');
var config = JSON.parse(fs.readFileSync('./package.json').toString());
var toString= Object.prototype.toString;
var Controller=function(interfaces){
this.interfaces=interfaces;
this.routers=[];
Emitter.call(this);
};
util.inherits(Controller,Emitter);
/*解析 req 规则*/
Controller.prototype.__parseReq=function(req){
return{
defRes:req.xhr?"json":"render"
}
}
Controller.prototype.__define=function(method,router,view,apis,callback,clientmodel,clientsrc){
var me=this,isObj=false;
if(typeof apis=="string"){
apis=[apis];
isObj=true;
}
/*参数验证*/
var guid=md5(router+":"+method)
me.on(guid,function(){
var args=[].slice.call(arguments, 0);
var req = args[0], res = args[1];
var local = { __session__: req.session, $extend: {} };
/*如果接口不存在 就实现express 原来的方法*/
if(!apis.length){
var model={};
if(callback){
model=callback.apply(local, args);
}
if(view){
res.render(view,local,me.client(res,clientsrc,model,clientmodel));
}
return;
}
args.push(function (err, interfaces, names){
if (err) {
res.json({code:500,message:"接口异常"});
return;
}
//callback
var model={};
if (typeof callback == "function") {
model=callback.apply(local,interfaces.concat(args));
}else{
interfaces.forEach(function(item,index){
if(isObj){
model=item;
}else{
model[names[index]]=item;
}
});
}
if(res.__complete__){
return;
}
if (model&&model.readable) {
model.pipe(res);
}else if (view && method == "get" && !req.xhr) {
res.render(view, local, me.client(res, clientsrc, model,clientmodel));
} else {
res.json(model);
}
});
//调用接口获取数据
me.interfaces.require.apply(me.interfaces,[apis].concat(args));
});
me.routers.push({
guid:guid,
url:router,
method:method
});
//console.log(me.routers);
}
Controller.prototype.vue=function(router,views,apis,callback){
var me=this;
if(!(typeof views=="string"&&views.indexOf('.')>-1)){
callback=apis;
apis=views;
views=null;
}
if(typeof apis =='function'){
callback=apis;
apis=[];
}
apis=apis||[];
me.__define("get",router,"common.Vue",apis,callback,"vue",views);
}
Controller.prototype.get=function(router,views,apis,callback){
var me=this;
if(!(typeof views=="string"&&views.indexOf('.')>-1)){
callback=apis;
apis=views;
views=null;
}
if(typeof apis =='function'){
callback=apis;
apis=[];
}
apis=apis||[];
me.__define("get",router,views,apis,callback,"jquery",views);
}
Controller.prototype.post=function(router,apis,callback){
var me=this;
if(typeof apis=="function"){
callback=apis;
apis=[];
}
me.__define("post",router,null,apis,callback);
}
Controller.prototype.client=function(res,views,model,clientmodel){
return function(err, html){
var ViewModel="";
if(!model){
model={};
}
ViewModel="<script>var ViewModel="+JSON.stringify(model)+";</script>";
var src="";
if(process.env.NODE_ENV==="production"){
src="http://cdn.yoho.cn/"+config.name+"/"+config.version;
html=html.replace(/\/static\/index\.min\.css/g,function($1){
return src+"/index.min.css";
})
}else{
src="/static";
}
var script=['<script type="text/javascript" src="'+src+'/'+clientmodel+'/libs.js"></script>',
'<script type="text/javascript" src="'+src+'/'+clientmodel+'/'+ views+'.js"></script>'];
res.send(html+ViewModel+script.join(''));
}
}
module.exports= Controller;