diff --git a/lib/Lib/Behavior/CheckRouteBehavior.class.js b/lib/Lib/Behavior/CheckRouteBehavior.class.js index f5079d9f..43d05199 100644 --- a/lib/Lib/Behavior/CheckRouteBehavior.class.js +++ b/lib/Lib/Behavior/CheckRouteBehavior.class.js @@ -104,7 +104,7 @@ module.exports = Behavior(function(){ if (item) { return item; }; - }) + }); this.http.action = Dispatcher.getAction(pathname.pop()); this.http.controller = Dispatcher.getController(pathname.pop()); this.http.group = Dispatcher.getGroup(pathname.pop()); diff --git a/lib/Lib/Core/Controller.class.js b/lib/Lib/Core/Controller.class.js index 1ecadb70..17b1ee0b 100644 --- a/lib/Lib/Core/Controller.class.js +++ b/lib/Lib/Core/Controller.class.js @@ -263,13 +263,7 @@ module.exports = Class(function(){ if (C('auto_send_content_type') && !this.http.cthIsSend) { this.header("Content-Type", C("tpl_content_type")); }; - if (is_array(obj) || is_object(obj)) { - obj = JSON.stringify(obj); - }; - if (!is_string(obj) && !(obj instanceof Buffer)) { - obj += ""; - }; - this.http.res.write(obj, encoding || C('encoding')); + this.http.echo(obj, encoding); }, /** * 结束输出,输出完成时一定要调用这个方法 diff --git a/lib/Lib/Core/Http.class.js b/lib/Lib/Core/Http.class.js index bf2f3aa6..7cca0826 100644 --- a/lib/Lib/Core/Http.class.js +++ b/lib/Lib/Core/Http.class.js @@ -146,6 +146,7 @@ module.exports = Class(function(){ setHeader: function(name, value){ this.res.setHeader(name, value); }, + _cookie: {}, /** * 设置cookie * @param {[type]} name [description] @@ -153,6 +154,14 @@ module.exports = Class(function(){ * @param {[type]} options [description] */ setCookie: function(name, value, options){ + if (name === true) { + var cookies = Object.values(this._cookie); + if (cookies.length) { + this.setHeader("Set-Cookie", cookies); + this._cookie = {}; + }; + return true; + }; options = options || {}; if (typeof options == 'number') { options = { @@ -177,7 +186,7 @@ module.exports = Class(function(){ }; options = extend(defaultOptions, options || {}); value = cookie.serialize(name, value, options); - this.setHeader("Set-Cookie", value); + this._cookie[name] = value; }, /** * url跳转 @@ -199,11 +208,28 @@ module.exports = Class(function(){ var time = Date.now() - this.startTime; this.setHeader("X-" + name, time + "ms"); }, + /** + * 输出内容 + * @param {[type]} obj [description] + * @param {[type]} encoding [description] + * @return {[type]} [description] + */ + echo: function(obj, encoding){ + this.setCookie(true); + if (is_array(obj) || is_object(obj)) { + obj = JSON.stringify(obj); + }; + if (!is_string(obj) && !(obj instanceof Buffer)) { + obj += ""; + }; + this.res.write(obj, encoding || C('encoding')); + }, /** * 结束URL * @return {[type]} [description] */ end: function(){ + this.setCookie(true); tag("before_res_end", this); this.res.end(); } diff --git a/lib/Lib/Core/View.class.js b/lib/Lib/Core/View.class.js index 80dd2883..34a21813 100644 --- a/lib/Lib/Core/View.class.js +++ b/lib/Lib/Core/View.class.js @@ -74,7 +74,7 @@ module.exports = Class(function(){ if (C('show_exec_time')) { this.http.sendTime("Exec-Time"); }; - this.http.res.write(content || "", C('encoding')); + this.http.echo(content || "", C('encoding')); }, /** * 获取模版文件内容