Skip to content

服务层

这里面主要是调用服务器端的东西,还有掉用数据库之类的。

要是前后端不分离。都写在一个项目里。这个肯定需要

但要是前后端分离,这个模块可有可无

bash

const { Service } = require('ee-core');
const Services = require('ee-core/services');

/**
 * 示例服务
 * @class
 */
class ExampleService extends Service {

  constructor(ctx) {
    super(ctx);
  }

  /**
   * test
   */
  async test (args, event) {
    let obj = {
      status:'ok',
      params: args
    }

    // 调用其它service
    Services.get('framework').test('egg');

    // 主动向前端发请求
    // channel 前端ipc.on(),监听的路由
    const channel = "controller.example.something"
    // controller 传入 event
    // IpcMainInvokeEvent
    event.reply(channel, {age:21})
    // IpcMainEvent
    event.sender.send(`${channel}`, {age:21})

    return obj;
  }
}

ExampleService.toString = () => '[class ExampleService]';
module.exports = ExampleService;