lrf 6 months ago
parent
commit
1ca4bb3f47

+ 11 - 1
src/controller/system/admin.controller.ts

@@ -1,11 +1,13 @@
 import { Body, Controller, Del, Get, Inject, Param, Post, Put, Query } from '@midwayjs/core';
 import { AdminService } from '../../service/system/admin.service';
-import { omit, pick } from 'lodash';
+import { get, omit, pick } from 'lodash';
 import { ApiResponse, ApiTags } from '@midwayjs/swagger';
 import { BaseController } from '../../frame/BaseController';
 import { ErrorCode, ServiceError } from '../../error/service.error';
 import { Validate } from '@midwayjs/validate';
 import { CVO_admin, FVO_admin, QVO_admin, UVAO_admin } from '../../interface/system/admin.interface';
+import * as bcrypt from 'bcryptjs';
+
 const namePrefix = '管理用户';
 @ApiTags(['管理用户表'])
 @Controller('/admin', { tagName: namePrefix })
@@ -37,6 +39,13 @@ export class HomeController implements BaseController {
   @Validate()
   @ApiResponse({ type: CVO_admin })
   async create(@Body() data: object) {
+    // 处理密码
+    const passowrd = get(data, 'password');
+    if (passowrd) {
+      const salt = bcrypt.genSaltSync(10);
+      const hash = bcrypt.hashSync(passowrd, salt);
+      Object.assign(data, { password: hash });
+    }
     const result = await this.service.create(data);
     return result;
   }
@@ -47,6 +56,7 @@ export class HomeController implements BaseController {
   @ApiResponse({ type: UVAO_admin })
   async update(@Param('id') id: number, @Body() data: object) {
     if (!id) throw new ServiceError(ErrorCode.ID_NOT_FOUND);
+    data = omit(data, ['password']);
     const result = await this.service.update({ id }, data);
     return result;
   }

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

@@ -2,11 +2,13 @@ import { Body, Controller, Del, Get, Inject, Param, Post, Query } from '@midwayj
 import { BaseController } from '../../frame/BaseController';
 import { ApiQuery, ApiResponse, ApiTags } from '@midwayjs/swagger';
 import { Validate } from '@midwayjs/validate';
-import { omit, pick } from 'lodash';
+import { get, omit, pick } from 'lodash';
 import { ErrorCode, ServiceError } from '../../error/service.error';
 import { UserService } from '../../service/system/user.service';
 import { CVO_user, FVO_user, QVO_user, UVAO_user } from '../../interface/system/user.interface';
 import { RoleService } from '../../service/system/role.service';
+import * as bcrypt from 'bcryptjs';
+
 const namePrefix = '平台用户';
 @ApiTags(['平台用户'])
 @Controller('/user', { tagName: namePrefix })
@@ -65,6 +67,13 @@ export class UserController implements BaseController {
     await this.service.createExamine(data);
     await this.service.checkPhone(data);
     await this.service.checkEmail(data);
+    // 处理密码
+    const passowrd = get(data, 'password');
+    if (passowrd) {
+      const salt = bcrypt.genSaltSync(10);
+      const hash = bcrypt.hashSync(passowrd, salt);
+      Object.assign(data, { password: hash });
+    }
     const dbData = await this.service.create(data);
     const result = new CVO_user(dbData);
     return result;
@@ -76,8 +85,9 @@ export class UserController implements BaseController {
   @ApiResponse({ type: UVAO_user })
   async update(@Param('id') id: number, @Body() data: object) {
     if (!id) throw new ServiceError(ErrorCode.ID_NOT_FOUND);
-    await this.service.checkPhone(data)
-    await this.service.checkEmail(data)
+    await this.service.checkPhone(data);
+    await this.service.checkEmail(data);
+    data = omit(data, ['password']);
     const result = await this.service.update({ id }, data);
     return result;
   }

+ 1 - 15
src/entity/system/admin.entity.ts

@@ -1,6 +1,5 @@
 import { Column, Entity } from 'typeorm';
 import { BaseModel } from '../../frame/BaseModel';
-import * as bcrypt from 'bcryptjs';
 // 管理员
 @Entity('admin', { comment: '管理员用户' })
 export class Admin extends BaseModel {
@@ -8,20 +7,7 @@ export class Admin extends BaseModel {
   nick_name: string;
   @Column({ type: 'character varying', comment: '账号', unique: true })
   account: string;
-  @Column({
-    type: 'character varying',
-    select: false,
-    comment: '密码',
-    transformer: {
-      from: value => value,
-      // to:进入数据库,需要加密
-      to: value => {
-        const salt = bcrypt.genSaltSync(10);
-        const hash = bcrypt.hashSync(value, salt);
-        return hash;
-      },
-    },
-  })
+  @Column({ type: 'character varying', select: false, comment: '密码' })
   password: string;
   @Column({ type: 'jsonb', nullable: true, comment: '角色id数组', default: [] })
   role: string[];

+ 1 - 14
src/entity/system/user.entity.ts

@@ -12,20 +12,7 @@ export class User extends BaseModel {
   account: string;
   @Column({ type: 'jsonb', nullable: true, comment: '所属产业' })
   industry: Array<any>;
-  @Column({
-    type: 'character varying',
-    select: false,
-    comment: '密码',
-    transformer: {
-      from: value => value,
-      // to:进入数据库,需要加密
-      to: value => {
-        const salt = bcrypt.genSaltSync(10);
-        const hash = bcrypt.hashSync(value, salt);
-        return hash;
-      },
-    },
-  })
+  @Column({ type: 'character varying', select: false, comment: '密码' })
   password: string;
   @Column({ type: 'character varying', comment: '性别' })
   gender: string;

+ 7 - 1
src/service/login.service.ts

@@ -213,7 +213,13 @@ export class LoginService {
     else model = this.userModel;
     const user = await model.findOne({ where: { id: Equal(data.id) } });
     if (!user) new ServiceError(ErrorCode.USER_NOT_FOUND);
-    await model.update({ id: data.id }, { password: data.password });
+    // 处理密码
+    let password = get(data, 'password');
+    if (password) {
+      const salt = bcrypt.genSaltSync(10);
+      password = bcrypt.hashSync(password, salt);
+    }
+    await model.update({ id: data.id }, { password });
   }
 
   // 需要改的时候再用