lrf 4 주 전
부모
커밋
25f7e18cb2

+ 24 - 4
src/controller/frame/Login.controller.ts

@@ -5,6 +5,8 @@ import { JwtService } from '@midwayjs/jwt';
 import { LoginRecordService } from '../../service/frame/LoginRecord.service';
 import { LoginRecordService } from '../../service/frame/LoginRecord.service';
 import { RF } from '../../response/CustomerResponse';
 import { RF } from '../../response/CustomerResponse';
 import { Validate } from '@midwayjs/validate';
 import { Validate } from '@midwayjs/validate';
+import { UserService } from '../../service/system/user.service';
+import { get } from 'lodash';
 
 
 @Controller('/login')
 @Controller('/login')
 export class LoginController {
 export class LoginController {
@@ -16,21 +18,39 @@ export class LoginController {
   jwtSecret;
   jwtSecret;
   @Inject()
   @Inject()
   jwtService: JwtService;
   jwtService: JwtService;
+
+  @Inject()
+  userService: UserService;
   /**
   /**
    * 账密登录
    * 账密登录
    * @param data 用户名和密码
    * @param data 用户名和密码
    * @param type 用户类型
    * @param type 用户类型
    */
    */
-  @Post('/:type')
-  async toLogin(@Body() data, @Param('type') type: string) {
-    const user = await this.loginService.loginByAccount(data, LoginType[type]);
-    if (user) user.role = type;
+  @Post('/Admin')
+  async toAdminLogin(@Body() data) {
+    const user: any = await this.loginService.adminLogin(data);
+    if (user) user.role = LoginType.Admin;
+    let vo = new LoginVO(user);
+    vo = JSON.parse(JSON.stringify(vo));
+    // user数据写成
+    const token = await this.jwtService.sign(vo, this.jwtSecret);
+    // 创建/更新登录信息
+    await this.loginRecordService.create(token);
+    return RF.success(token);
+  }
+
+  @Post('/User')
+  async toUserLogin(@Body() data) {
+    const user: any = await this.loginService.userLogin(data);
+    if (user) user.role = LoginType.User;
     let vo = new LoginVO(user);
     let vo = new LoginVO(user);
     vo = JSON.parse(JSON.stringify(vo));
     vo = JSON.parse(JSON.stringify(vo));
     // user数据写成
     // user数据写成
     const token = await this.jwtService.sign(vo, this.jwtSecret);
     const token = await this.jwtService.sign(vo, this.jwtSecret);
     // 创建/更新登录信息
     // 创建/更新登录信息
     await this.loginRecordService.create(token);
     await this.loginRecordService.create(token);
+    // 登录次数+1
+    await this.userService.loginCountAddOne(get(user, 'id'))
     return RF.success(token);
     return RF.success(token);
   }
   }
 
 

+ 3 - 4
src/entity/frame/loginRecord.entity.ts

@@ -1,9 +1,8 @@
-import { Entity, Column } from "typeorm";
-import { BaseModel } from "../../frame/BaseModel";
+import { Entity, Column } from 'typeorm';
+import { BaseModel } from '../../frame/BaseModel';
 
 
 @Entity('loginRecord', { comment: '登陆记录' })
 @Entity('loginRecord', { comment: '登陆记录' })
 export class LoginRecord extends BaseModel {
 export class LoginRecord extends BaseModel {
-
   @Column({ comment: '用户id' })
   @Column({ comment: '用户id' })
   user_id: number;
   user_id: number;
   @Column({ comment: 'token', nullable: true })
   @Column({ comment: 'token', nullable: true })
@@ -14,4 +13,4 @@ export class LoginRecord extends BaseModel {
   expire_time: string
   expire_time: string
   @Column({ comment: '最后使用ip', nullable: true })
   @Column({ comment: '最后使用ip', nullable: true })
   last_ip: string
   last_ip: string
-}
+}

+ 10 - 10
src/entity/system/user.entity.ts

@@ -2,16 +2,16 @@ import { BaseModel } from '../../frame/BaseModel';
 import { Column, Entity } from 'typeorm';
 import { Column, Entity } from 'typeorm';
 @Entity('user', { comment: '用户表' })
 @Entity('user', { comment: '用户表' })
 export class User extends BaseModel {
 export class User extends BaseModel {
-  @Column({ type: 'varchar', nullable: true, comment: '用户昵称' })
-  nick_name: string;
-  @Column({ type: 'varchar', comment: '账号', unique: true })
-  account: string;
+  @Column({ type: 'varchar', nullable: true, comment: '用户' })
+  username: string;
+  @Column({ type: 'varchar', nullable: true, comment: '电子邮箱' })
+  email: string;
   @Column({ type: 'varchar', select: false, comment: '密码' })
   @Column({ type: 'varchar', select: false, comment: '密码' })
   password: string;
   password: string;
-  @Column({ type: 'varchar', comment: '电话' })
-  tel: string;
-  @Column({ type: 'json', nullable: true, comment: '角色id数组' })
-  role: any;
-  @Column({ type: 'varchar', default: '0', comment: '是否使用: 0:使用;1:禁用' })
-  is_use: string;
+  @Column({ type: 'varchar', select: false, comment: '注册ip' })
+  register_ip: string;
+  @Column({ type: 'int', select: false, default: '1', comment: '登录次数' })
+  login_count: number
+  @Column({ type: 'varchar', select: false, default: '0', comment: '使用状态' })
+  is_use: number
 }
 }

+ 1 - 0
src/frame/Options.ts

@@ -17,6 +17,7 @@ export interface ResultOptions {
 
 
 export enum LoginType {
 export enum LoginType {
   Admin = 'Admin',
   Admin = 'Admin',
+  User = 'User',
 }
 }
 /**登录后token返回参数 */
 /**登录后token返回参数 */
 export class LoginVO {
 export class LoginVO {

+ 18 - 0
src/service/frame/Login.service.ts

@@ -36,6 +36,24 @@ export class LoginService {
     return user;
     return user;
   }
   }
 
 
+  async adminLogin(data) {
+    const user = await this.adminModel.createQueryBuilder('t').where('t.account = :account', { account: data.account }).addSelect('t.password').getOne();
+    if (!user) throw new ServiceError(ErrorCode.USER_NOT_FOUND);
+    await this.checkAccountCanLogin(user, LoginType.Admin);
+    const result = bcrypt.compareSync(data.password, user.password);
+    if (!result) throw new ServiceError(ErrorCode.BAD_PASSWORD);
+    return user;
+  }
+
+  async userLogin(data) {
+    const user = await this.userModel.createQueryBuilder('t').where('t.username = :username', { account: data.username }).addSelect('t.password').getOne();
+    if (!user) throw new ServiceError(ErrorCode.USER_NOT_FOUND);
+    await this.checkAccountCanLogin(user, LoginType.Admin);
+    const result = bcrypt.compareSync(data.password, user.password);
+    if (!result) throw new ServiceError(ErrorCode.BAD_PASSWORD);
+    return user;
+  }
+
   /**
   /**
    * 检查用户是否可以登录(从用户本身和角色检查)
    * 检查用户是否可以登录(从用户本身和角色检查)
    * @param user 用户信息
    * @param user 用户信息

+ 9 - 2
src/service/system/user.service.ts

@@ -14,8 +14,15 @@ export class UserService extends BaseService {
   /**查重 */
   /**查重 */
   async checkInDB(data: object) {
   async checkInDB(data: object) {
     // account查重,虽然数据库有unique约束,但是尽量不触发,因为异常捕获不好解析
     // account查重,虽然数据库有unique约束,但是尽量不触发,因为异常捕获不好解析
-    const account = get(data, 'account');
-    const num = await this.model.count({ where: { account } });
+    const username = get(data, 'username');
+    const num = await this.model.count({ where: { username } });
     if (num > 0) throw new ServiceError(ErrorCode.ACCOUNT_IS_EXISTS);
     if (num > 0) throw new ServiceError(ErrorCode.ACCOUNT_IS_EXISTS);
   }
   }
+
+  async loginCountAddOne(id) {
+    const user = await this.model.findOne({ where: { id } });
+    if (!user) return
+    const loginCount = get(user, 'login_count', 0)
+    await this.model.update({ id }, { login_count: loginCount + 1 });
+  }
 }
 }