import { Body, Controller, Del, Get, Inject, Param, Post, } from '@midwayjs/core'; import { AdminService } from '../../service/system/admin.service'; import { RF } from '../../response/CustomerResponse'; import { Page, Query } from '../../decorator/page.decorator'; import * as bcrypt from 'bcryptjs'; import { get } from 'lodash'; @Controller('/admin') export class AdminController { @Inject() service: AdminService; @Post('/') async create(@Body() body) { await this.service.checkInDB(body); // 处理密码 const passowrd = get(body, 'password'); if (passowrd) { const salt = bcrypt.genSaltSync(10); const hash = bcrypt.hashSync(passowrd, salt); Object.assign(body, { password: hash }); } const data = await this.service.create(body); return RF.success(data); } @Get('/') async query(@Query() query, @Page() page) { const result = await this.service.query(query, page); return RF.success(result); } @Get('/:id') async fetch(@Param('id') id: number) { const result = await this.service.fetch({ id }); return RF.success(result); } @Post('/:id') async update(@Param('id') id: number, @Body() body) { // 删除account字段,不允许更改 delete body.account; const result = await this.service.update({ id }, body); return RF.success(result); } @Del('/:id') async delete(@Param('id') id: number) { await this.service.delete({ id }); return RF.success(); } }