async getData() { try { this.ctx.logger.info('getData'); this.ctx.helper.test(); this.ctx.body = await this.ctx.service.foo.bar(); this.ctx.proxy.foo.bar(); } catch (e) { const body: RequestObjectBody = this.ctx.request.body; this.app.logger.info(e.name, body.foo); } }
test(this: IHelper) { this.test2(); }
// 删除所选角色(条件id[]) async removes() { const { ctx, service } = this // 组装参数 // const payload = ctx.queries.id const { id } = ctx.request.body // {id: "5a452a44ab122b16a0231b42,5a452a3bab122b16a0231b41"} const payload = id.split(',') || [] // 调用 Service 进行业务处理 const result = await service.role.removes(payload) // 设置响应内容和响应状态码 ctx.helper.success({ctx}) }
// POST /api/demo public async testRedis() { const { app, ctx } = this; await app.redis.set('test', ctx.request.body.test); const data = await app.redis.get('test'); ctx.helper.rest(data); }
// GET /api/users?limit=5&offset=5 6~10 public async index() { const { ctx } = this; const query = { limit: parseInt(ctx.query.limit) || 10, offset: parseInt(ctx.query.offset) || 0, }; const data = await ctx.service.user.list(query); ctx.helper.rest({ ...data, // { "count": 2, "rows": [{...}, {...}] } }, 'ok', 0); }
// DELETE /api/posts/:id public async destroy() { const ctx = this.ctx; const id = parseInt(ctx.params.id) || 0; const user_id = parseInt(ctx.request.body.user_id); await ctx.service.post.destroy({ id, user_id }); ctx.helper.rest({ id, user_id }); }
// 创建用户 async create() { const { ctx, service } = this // 校验参数 ctx.validate(this.UserCreateTransfer) // 组装参数 const payload = ctx.request.body || {} // 调用 Service 进行业务处理 const res = await service.user.create(payload) // 设置响应内容和响应状态码 ctx.helper.success({ctx, res}) }
async updateModel(data) { // 修改数据,将会根据主键 ID 查找,并更新 const { ctx, app } = this; data.createtime = ctx.helper.currentDateTime(); const result = await app.mysql.update('tb_users', data); return { error_code: result.affectedRows > 0 ? 0 : 1, msg: result.affectedRows > 0 ? '修改成功' : '修改失败', }; }
// 获取所有文件(分页/模糊) async index() { const { ctx, service } = this // 组装参数 const payload = ctx.query // 调用 Service 进行业务处理 const res = await service.upload.index(payload) // 设置响应内容和响应状态码 ctx.helper.success({ctx, res}) }
// 添加图片描述 async extra() { const { ctx, service } = this // 组装参数 const { id } = ctx.params // 传入要修改的文档ID const payload = ctx.request.body || {} await service.upload.extra(id, payload) // 设置响应内容和响应状态码 ctx.helper.success({ctx}) }
// GET /api/users/:id public async show() { const ctx = this.ctx; const data = await ctx.service.user.find(parseInt(ctx.params.id) || 0); ctx.helper.rest(data); // {...} }
// 删除单个用户 async destroy() { const { ctx, service } = this // 校验参数 const { id } = ctx.params // 调用 Service 进行业务处理 await service.user.destroy(id) // 设置响应内容和响应状态码 ctx.helper.success({ctx}) }
// 用户登入 async login() { const { ctx, service } = this // 校验参数 ctx.validate(this.UserLoginTransfer) // 组装参数 const payload = ctx.request.body || {} // 调用 Service 进行业务处理 const res = await service.userAccess.login(payload) // 设置响应内容和响应状态码 ctx.helper.success({ctx, res}) }
// 获取单个文件 async show() { const { ctx, service } = this // 组装参数 const { id } = ctx.params // 调用 Service 进行业务处理 const res = await service.upload.show(id) // 设置响应内容和响应状态码 ctx.helper.success({ctx, res}) }