From dbe6bf44ca7def33c98bf3cb0c70159eebe422cb Mon Sep 17 00:00:00 2001 From: welefen Date: Mon, 7 Jul 2014 14:34:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AF=B9RESTFUL=E7=9A=84?= =?UTF-8?q?=E6=94=AF=E6=8C=81,=20fix=20#82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/Conf/config.js | 4 +- lib/Lib/Behavior/CheckRouteBehavior.js | 16 +++- lib/Lib/Core/App.js | 4 + lib/Lib/Extend/Controller/RestController.js | 93 ++++++++++++++++++++- 4 files changed, 110 insertions(+), 7 deletions(-) diff --git a/lib/Conf/config.js b/lib/Conf/config.js index aa62e864..465f35de 100644 --- a/lib/Conf/config.js +++ b/lib/Conf/config.js @@ -19,7 +19,7 @@ module.exports = { post_max_fields: 1000, //最大表单数 post_max_fields_size: 2 * 1024, //单个表单最大值 - app_group_list: ['Home', 'Admin'], //分组列表 + app_group_list: ['Home', 'Admin', 'Restful'], //分组列表 default_group: 'Home', //默认分组 default_controller: 'Index', //默认模块 default_action: 'index', //默认Action @@ -37,6 +37,8 @@ module.exports = { autoload_path: {}, //autoload查找的path,用于thinkRequire加载自定义库的时候查找 create_server_fn: '', //自定义create server全局函数名,可以在Common/common.js里实现 + restful_group: 'Restful', //RESTFUL API默认分组 + load_ext_config: [], //加载额外的配置文件 CONF_PATH load_ext_file: [], //加载额外的文件 COMMON_PATH diff --git a/lib/Lib/Behavior/CheckRouteBehavior.js b/lib/Lib/Behavior/CheckRouteBehavior.js index 7d3695db..f4f6d01d 100644 --- a/lib/Lib/Behavior/CheckRouteBehavior.js +++ b/lib/Lib/Behavior/CheckRouteBehavior.js @@ -142,7 +142,7 @@ module.exports = Behavior(function(){ * @param {[type]} route [description] * @return {[type]} [description] */ - getRoute: function(route){ + getRoute: function(route, matches){ if (isObject(route)) { //对应的请求类型 for(var method in route){ @@ -153,6 +153,18 @@ module.exports = Behavior(function(){ } return; } + var routeUpper = route.toUpperCase(); + //RESTFUL API + if (routeUpper === 'RESTFUL' || routeUpper.indexOf('RESTFUL:') === 0) { + var group = route.split(':')[1] || C('restful_group'); + route = group + '/' + matches[1] + '/' + this.http.method.toLowerCase() + '?resource=' + matches[1]; + if (matches[2]) { + route += '&id=' + matches[2]; + } + //设置变量到http对象上,方便后续使用 + this.http.isRestful = true; + return route; + } return route; }, /** @@ -163,7 +175,7 @@ module.exports = Behavior(function(){ * @return {[type]} [description] */ parseRegExp: function(matches, route, pathname){ - route = this.getRoute(route); + route = this.getRoute(route, matches); if (!route) { return false; } diff --git a/lib/Lib/Core/App.js b/lib/Lib/Core/App.js index 83af1f35..4c9805b9 100644 --- a/lib/Lib/Core/App.js +++ b/lib/Lib/Core/App.js @@ -26,6 +26,10 @@ App.getBaseController = function(http){ */ App.getCallController = function(http){ 'use strict'; + //如果是RESTFUL API,则调用RestController + if (http.isRestful) { + return thinkRequire('RestController')(http); + } var config = C('call_controller'); if (!config) { return; diff --git a/lib/Lib/Extend/Controller/RestController.js b/lib/Lib/Extend/Controller/RestController.js index 0b8eae7a..d1711964 100644 --- a/lib/Lib/Extend/Controller/RestController.js +++ b/lib/Lib/Extend/Controller/RestController.js @@ -5,11 +5,96 @@ module.exports = Controller(function(){ 'use strict'; return { - __before: function(){ - + init: function(http){ + this.super('init', http); + //资源名 + this.resource = this.get('resource'); + //资源id + this.id = this.get('id') | 0; + //实例化对应的模型 + this.model = D(this.resource); }, - __call: function(){ - this.end('method is not allowed'); + /** + * 获取 + * @return {[type]} [description] + */ + getAction: function(){ + var self = this; + if (this.id) { + return this.model.getPk().then(function(pk){ + return self.model.where(getObject(pk, self.id)).find(); + }).then(function(data){ + return self.success(data); + }).catch(function(err){ + return self.error(err.message); + }) + } + return this.model.select().then(function(data){ + return self.success(data); + }).catch(function(err){ + return self.error(err.message); + }); + }, + /** + * 新建 + * @return {[type]} [description] + */ + postAction: function(){ + var self = this; + return this.model.getPk().then(function(pk){ + var data = self.post(); + if (isEmpty(data)) { + return self.error('data is empty'); + } + delete data[pk]; + return self.model.add(data); + }).then(function(){ + return self.success(); + }).catch(function(err){ + return self.error(err.message); + }); + }, + /** + * 删除 + * @return {[type]} [description] + */ + deleteAction: function(){ + if (!this.id) { + return this.error('params error'); + } + var self = this; + return this.model.getPk().then(function(pk){ + return self.model.where(getObject(pk, self.id)).delete(); + }).then(function(){ + return self.success(); + }).catch(function(err){ + return self.error(err.message); + }); + }, + /** + * 更新 + * @return {[type]} [description] + */ + putAction: function(){ + if (!this.id) { + return this.error('params error'); + } + var self = this; + return this.model.getPk().then(function(pk){ + var data = self.post(); + if (isEmpty(data)) { + return self.error('data is empty'); + } + delete data[pk]; + return self.model.where(getObject(pk, self.id)).update(data); + }).then(function(){ + return self.success(); + }).catch(function(err){ + return self.error(err.message); + }); + }, + __call: function(action){ + return this.error('action `' + action + '` is not allowed'); } } }) \ No newline at end of file