rest controller

think.controller.rest 继承自 think.controller.base,用来处理 Rest 接口。

使用 ES6 的语法继承该类
  1. export default class extends think.controller.rest {
  2. }
使用普通方式继承该类
  1. module.exports = think.controller('rest', {
  2. })

属性

controller._isRest

标识此 controller 对应的是 Rest 接口。如果在 init 方法里将该属性设置为 false,那么该 controller 不再是一个 Rest 接口。

controller._method

获取 method 方式。默认从 http method 中获取,但有些客户端不支持发送 DELETE, PUT 类型的请求,所以可以设置为从 GET 参数里获取。

  1. export default class extends think.controller.rest {
  2. init(http){
  3. super.init(http);
  4. //设置 _method,表示从 GET 参数获取 _method 字段的值
  5. //如果没有取到,则从 http method 中获取
  6. this._method = '_method';
  7. }
  8. }

controller.resource

当前 Rest 对应的 Resource 名称。

controller.id

资源 ID

controller.modelInstance

资源对应 model 的实例。

方法

controller.__before()

可以在魔术方法 __before 中进行字段过滤、分页、权限校验等功能。

  1. export default class extends think.controller.rest{
  2. __before(){
  3. //过滤 password 字段
  4. this.modelInstance.field('password', true);
  5. }
  6. }

controller.getAction()

获取资源数据,如果有 id,拉取一条,否则拉取列表。

  1. //方法实现,可以根据需要修改
  2. export default class extends think.controller.rest {
  3. async getAction(){
  4. let data;
  5. if (this.id) {
  6. let pk = await this.modelInstance.getPk();
  7. data = await this.modelInstance.where({[pk]: this.id}).find();
  8. return this.success(data);
  9. }
  10. data = await this.modelInstance.select();
  11. return this.success(data);
  12. }
  13. }

controller.postAction()

添加数据

  1. //方法实现,可以根据需要修改
  2. export default class extends think.controller.rest {
  3. async postAction(){
  4. let pk = await this.modelInstance.getPk();
  5. let data = this.post();
  6. delete data[pk];
  7. if(think.isEmpty(data)){
  8. return this.fail('data is empty');
  9. }
  10. let insertId = await this.modelInstance.add(data);
  11. return this.success({id: insertId});
  12. }
  13. }

controller.deleteAction()

删除数据

  1. //方法实现,可以根据需要修改
  2. export default class extends think.controller.rest {
  3. async deleteAction(){
  4. if (!this.id) {
  5. return this.fail('params error');
  6. }
  7. let pk = await this.modelInstance.getPk();
  8. let rows = await this.modelInstance.where({[pk]: this.id}).delete();
  9. return this.success({affectedRows: rows});
  10. }
  11. }

controller.putAction()

更新数据

  1. //方法实现,可以根据需要修改
  2. export default class extends think.controller.rest {
  3. async putAction(){
  4. if (!this.id) {
  5. return this.fail('params error');
  6. }
  7. let pk = await this.modelInstance.getPk();
  8. let data = this.post();
  9. delete data[pk];
  10. if (think.isEmpty(data)) {
  11. return this.fail('data is empty');
  12. }
  13. let rows = await this.modelInstance.where({[pk]: this.id}).update(data);
  14. return this.success({affectedRows: rows});
  15. }
  16. }

controller.__call()

找不到方法时调用

  1. export default class extends think.controller.rest {
  2. __call(){
  3. return this.fail(think.locale('ACTION_INVALID', this.http.action, this.http.url));
  4. }
  5. }

原文: https://thinkjs.org/zh-cn/doc/2.2/api_controller_rest.html