base_controller.js 533 Bytes
Newer Older
wuhao's avatar
wuhao committed
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
// app/core/base_controller.js
const { Controller } = require('egg');
class BaseController extends Controller {
  get user() {
    return this.ctx.session.user;
  }

  success(data) {
    this.ctx.status = 200;
    this.ctx.body = {
      code: 0,
      success: true,
      data,
    };
  }

  fail(msg) {
    this.ctx.status = 200;
    this.ctx.body = {
      code: 1,
      success: false,
      msg,
    };
  }

  notFound(msg) {
    msg = msg || 'not found';
    this.ctx.throw(404, msg);
  }
}
module.exports = BaseController;