这里就是引入相关的model层写好的,然后根据业务逻辑去调用下,还是以银行卡为例
'use strict'; const { Service } = require('egg'); class BankcardService extends Service { constructor(ctx) { super(ctx); this.Bankcard = this.ctx.model.Bankcard; } async add(name, prefix, bankId) { const { ctx, Bankcard } = this; let result = await Bankcard.hasPrefix(prefix); if (result) { ctx.throw('卡号前缀已存在'); } result = await Bankcard.add(name, prefix, bankId); if (!result) { ctx.throw('添加卡号失败'); } return result; } async getAllLimit(name, prefix, bankId, page, limit) { const { ctx, Bankcard } = this; const result = await Bankcard.getAllLimit(name, prefix, bankId, { page, limit, }); if (!result) { ctx.throw('暂无数据'); } return result; } async set(id, name, prefix, bankId, isDelete) { const { ctx, Bankcard } = this; const result = await Bankcard.set(id, name, prefix, bankId, isDelete); if (result === null) { ctx.throw('更新失败'); } return result; } } module.exports = BankcardService; Controller restful API接口只要在相应的controller层定义相关的方法,egg程序就能够根据restful api去解析。
Method Path Route Name Controller.ActionGET /posts posts app.controllers.posts.index
GET /posts/new new_post app.controllers.posts.new
GET /posts/:id post app.controllers.posts.show
GET /posts/:id/edit edit_post app.controllers.posts.edit
POST /posts posts app.controllers.posts.create
PUT /posts/:id post app.controllers.posts.update
DELETE /posts/:id post app.controllers.posts.destroy
参见:https://eggjs.org/zh-cn/basics/router.html
非restful API接口这里主要是针对于楼上的情况,进行一个补充,比如说用户,除了这些,他还有登录,登出等等操作,那这个就需要单独在router中制定了, 这里笔者封装了一个resource方法,来解析restful api的函数接口,具体如下:
'use strict'; /** * @param {Egg.Application} app - egg application */ module.exports = app => { const { router, controller } = app; router.get('http://www.likecs.com/', controller.home.index); router.post('/user/login', controller.user.login); router.post('/user/logout', controller.user.logout); router.post('/user/info', controller.user.getUserInfo); router.post('/file/upload', controller.file.upload); router.post('/file/getall', controller.file.getAllByIds); router.post('/organization/by-type', controller.organization.getAllByType); router.post('/statistics/calculate', controller.statistics.calculate); function resource(path) { const pathArr = path.split('http://www.likecs.com/'); // 删掉第一个空白的 pathArr.shift(); let controllers = controller; for (const val of pathArr) { controllers = controllers[val]; } router.resources(path, path, controllers); } resource('/alarm'); resource('/bank'); resource('/bankcard'); resource('/mobile'); resource('/organization'); resource('/user'); resource('/virtual'); resource('/website'); resource('/file'); resource('/alarmCategory'); };这里还是以银行卡为例
'use strict'; const { Controller } = require('egg'); class BankCardController extends Controller { async index() { const { ctx, service } = this; const { name, prefix, bankId, page, pageSize } = ctx.request.query; const { list, ...rest } = await service.bankcard.getAllLimit( name, prefix, bankId, Number(page), Number(pageSize) ); const data = list.map(item => { const { role } = ctx.session.userinfo; let btnList = []; if (role === 'admin') { btnList = ['detail', 'modify', 'delete']; } return { btnList, ...item, }; }); ctx.success({ list: data, ...rest }); } async create() { const { ctx, service } = this; const { name, prefix, bankId } = ctx.request.body; ctx.validate( { name: { type: 'string', required: true }, prefix: { type: 'string', required: true }, bankId: { type: 'string', required: true }, }, { name, prefix, bankId } ); const result = await service.bankcard.add(name, prefix, bankId); ctx.success(result); } // async destory() { // const { ctx, service } = this; // const { method } = ctx; // this.ctx.body = '删除'; // } async update() { const { ctx, service } = this; const { id } = ctx.params; const { name, prefix, bankId, isDelete } = ctx.request.body; const result = await service.bankcard.set( id, name, prefix, bankId, isDelete ); ctx.success(result); } async show() { const { ctx, service } = this; const { method } = ctx; this.ctx.body = '查询'; } async new() { const { ctx, service } = this; const { method } = ctx; this.ctx.body = '创建页面'; } async edit() { const { ctx, service } = this; const { method } = ctx; this.ctx.body = '修改页面'; } } module.exports = BankCardController;至此,打通这样一个从model到service再到controller的流程,
eggjs中的定时任务schedule