45 lines
1007 B
JavaScript
45 lines
1007 B
JavaScript
'use strict';
|
||
|
||
const Service = require('egg').Service;
|
||
|
||
/**
|
||
* 通用方法调度服务
|
||
* method 固定为 workbench.business,name 为变量入参
|
||
*/
|
||
class SystemService extends Service {
|
||
|
||
/**
|
||
* 调度入口
|
||
* @param {string} name - 操作名称
|
||
* @param {object} data - 请求数据
|
||
*/
|
||
async dispatch(name, data) {
|
||
const { ctx } = this;
|
||
|
||
// method 固定为 workbench.business,调用 workbench service
|
||
if (ctx.service.workbench) {
|
||
return await this._callService(ctx.service.workbench, name, data);
|
||
}
|
||
|
||
return {
|
||
success: false,
|
||
message: `workbench 服务未实现`,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 调用 service 实例上的指定方法
|
||
*/
|
||
async _callService(serviceInstance, name, data) {
|
||
if (!serviceInstance || typeof serviceInstance[name] !== 'function') {
|
||
return {
|
||
success: false,
|
||
message: `方法 ${name} 不存在`,
|
||
};
|
||
}
|
||
return await serviceInstance[name](data);
|
||
}
|
||
}
|
||
|
||
module.exports = SystemService;
|