demo.js 919 Bytes
/*
 * @Author: limin
 * @Date: 2019-11-21 11:19:56
 * @Last Modified by: limin
 * @Last Modified time: 2019-11-21 17:12:59
 */

const Controller = require("egg").Controller;
module.exports = app =>
  class DemoController extends Controller {
    constructor(ctx) {
      super(ctx);
      this.session = ctx.session;
      this.request = ctx.request;
      this.DemoService = ctx.service.demo;
      this.ServerResponseCode = ctx.response.ServerResponseCode;
      this.ServerResponse = ctx.response.ServerResponse;
    }

    // hello
    async hello() {
      //调用 redis 缓存服务 , 否则调用 servie 从数据库读取
      // 如果不用 缓存 直接 调用 servie  this.DemoService.hello(this.request.body) 即可
      const data = await this.CacheService.get("test", () =>
        this.DemoService.hello(this.request.body)
      );
      this.ctx.body = this.ServerResponse.Success(data);
    }
  };