zs 1 年之前
父节点
当前提交
f0a63b68ec
共有 2 个文件被更改,包括 26 次插入3 次删除
  1. 4 3
      src/controller/system/user.controller.ts
  2. 22 0
      src/service/system/user.service.ts

+ 4 - 3
src/controller/system/user.controller.ts

@@ -14,7 +14,8 @@ export class UserController extends BaseController {
   @Validate()
   @ApiResponse({ type: CVO_user })
   async create(@Body() data: CDTO_user) {
-    const dbData = await this.service.create(data);
+    const arr = await this.service.createBefore(data);
+    const dbData = await this.service.create(arr);
     const result = new CVO_user(dbData);
     return result;
   }
@@ -40,12 +41,11 @@ export class UserController extends BaseController {
     return result;
   }
 
-
   @Get('/detail/:id')
   @ApiResponse({ type: FVO_user })
   async detail(@Param('id') id: string) {
     const data = await this.service.detail(id);
-     const result = new FVO_user(data);
+    const result = new FVO_user(data);
     return result;
   }
 
@@ -54,6 +54,7 @@ export class UserController extends BaseController {
   @ApiResponse({ type: UVAO_user })
   async update(@Param('id') id: string, @Body() body: UDTO_user) {
     const result = await this.service.updateOne(id, body);
+    await this.service.updateAfter(id, body);
     return result;
   }
 

+ 22 - 0
src/service/system/user.service.ts

@@ -1,18 +1,40 @@
 import { Provide } from '@midwayjs/decorator';
+import { GetModel } from 'free-midway-component';
 import { InjectEntityModel } from '@midwayjs/typegoose';
 import { ReturnModelType } from '@typegoose/typegoose';
 import { BaseService } from 'free-midway-component';
 import { User } from '../../entity/system/user.entity';
+import { upperFirst } from 'lodash';
 type modelType = ReturnModelType<typeof User>;
 @Provide()
 export class UserService extends BaseService<modelType> {
   @InjectEntityModel(User)
   model: modelType;
 
+  // 创建前
+  async createBefore(data) {
+    const { role } = data;
+    if (role && role.length > 1) data.status = '0';
+    else data.status = '1';
+    return data;
+  }
+
+  // 修改后
+  async updateAfter(id, body) {
+    const arr = await this.detail(id);
+    const { status } = body;
+    if (status === '1' && arr.role_type !== 'User') {
+      const model = GetModel(upperFirst(arr.role_type));
+      const user = await model.findOne({ user: id }).lean();
+      await model.updateOne({ _id: user._id }, { status: '1' });
+    }
+  }
+
   async detail(id) {
     const arr = await this.model.findById(id).lean();
     let role_type;
     if (arr && arr.role && arr.role.length > 1) role_type = arr.role[1];
+    else role_type = arr.role[0];
     return { ...arr, role_type };
   }
 }