Controller.js 3.93 KB
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:err.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={};
        }
        var json_str = JSON.stringify(model);
        var cx = /[\u00A0\u2028\u2029\uFEFF]/g;

		if (cx.test(json_str)) {
			console.log("------------------")
			json_str = json_str.replace(cx, function () {
				return "";
			});
		}   
		ViewModel="<script>var ViewModel="+json_str+";</script>";
        var src="";

        	
//        if(process.env.NODE_ENV==="production"||process.env.NODE_ENV==="gray"){
//        	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;