Browse Source

区分角色登录

lrf 5 days ago
parent
commit
76a53ed134
2 changed files with 10 additions and 4 deletions
  1. 2 0
      src/entity/frame/loginRecord.entity.ts
  2. 8 4
      src/service/frame/LoginRecord.service.ts

+ 2 - 0
src/entity/frame/loginRecord.entity.ts

@@ -5,6 +5,8 @@ import { BaseModel } from '../../frame/BaseModel';
 export class LoginRecord extends BaseModel {
   @Column({ comment: '用户id' })
   user_id: number;
+  @Column({ comment: '用户角色:Admin;User' })
+  role: string;
   @Column({ comment: 'token', nullable: true })
   token: string
   @Column({ comment: '最后使用时间', nullable: true })

+ 8 - 4
src/service/frame/LoginRecord.service.ts

@@ -28,17 +28,19 @@ export class LoginRecordService {
     if (!userInfo) return;
     const user_id = get(userInfo, 'id', get(userInfo, '_id'));
     if (!user_id) return;
+    const role = get(userInfo, 'role');
+    if (!role) return;
     const last_time = dayjs().format(this.timeFormat);
     const expire_time = dayjs()
       .add(this.expire_step, 's')
       .format(this.timeFormat);
     const req = this.ctx.request;
     const last_ip = get(req, 'header.x-forwarded-for', req.ip);
-    const data: any = { token, last_time, expire_time, last_ip };
-    const num = await this.model.count({ where: { user_id } });
+    const data: any = { token, last_time, expire_time, last_ip, role };
+    const num = await this.model.count({ where: { user_id, role } });
     if (num > 0) {
       // 有过登录记录,那就直接更新token,last_time,expire_time,last_ip就行
-      const q1: any = { user_id }
+      const q1: any = { user_id, role }
       await this.model.update(q1, data);
     } else {
       // 没有记录,那就创建
@@ -73,7 +75,9 @@ export class LoginRecordService {
     if (!userInfo) return;
     const user_id = get(userInfo, 'id', get(userInfo, '_id'));
     if (!user_id) return;
-    const q1: any = { user_id }
+    const role = get(userInfo, 'role')
+    if (!role) return
+    const q1: any = { user_id, role }
     const data = await this.model.findOne({ where: q1 });
     if (!data) throw new ServiceError(ErrorCode.NOT_LOGIN);
     const utoken = get(data, 'token');