* @description: 用户登录 * @author: 白雾茫茫丶
(
loginParams: LoginParamsDto,
clinetIp: string,
session: SessionTypes,
)
| 43 | * @author: 白雾茫茫丶 |
| 44 | */ |
| 45 | async loginSingToken( |
| 46 | loginParams: LoginParamsDto, |
| 47 | clinetIp: string, |
| 48 | session: SessionTypes, |
| 49 | ): Promise<responseResult> { |
| 50 | // 登录参数校验结果 |
| 51 | const authResult: responseResult = await this.validateUser( |
| 52 | loginParams, |
| 53 | session, |
| 54 | ); |
| 55 | // 解构参数 |
| 56 | const { data: userInfo, code } = authResult; |
| 57 | // 状态码 code === 200,则登录成功 |
| 58 | switch (code) { |
| 59 | case 200: |
| 60 | // 生成 token |
| 61 | const token = this.jwtService.sign({ |
| 62 | user_name: userInfo.user_name, |
| 63 | user_id: userInfo.user_id, |
| 64 | }); |
| 65 | // 登录成功后执行当前用户的更新操作 |
| 66 | const where: WhereOptions = { user_id: userInfo.user_id }; |
| 67 | const params = { |
| 68 | token, |
| 69 | login_last_ip: clinetIp, |
| 70 | login_last_time: new Date(), |
| 71 | }; |
| 72 | // 执行更新操作 |
| 73 | await this.userModel.update(params, { where }); |
| 74 | // 将登录次数+1 |
| 75 | await this.userModel.increment({ login_num: 1 }, { where }); |
| 76 | // 将数据保存到session |
| 77 | const currentUserInfo = await this.userModel.findOne({ |
| 78 | attributes: { |
| 79 | include: ['j.jobs_name', 'o.org_name', 'r.role_name'], |
| 80 | }, |
| 81 | // 联表查询 |
| 82 | include: [ |
| 83 | { |
| 84 | model: XmwJobs, |
| 85 | as: 'j', |
| 86 | attributes: [], |
| 87 | }, |
| 88 | { |
| 89 | model: XmwOrganization, |
| 90 | as: 'o', |
| 91 | attributes: [], |
| 92 | }, |
| 93 | { |
| 94 | model: XmwRole, |
| 95 | as: 'r', |
| 96 | attributes: [], |
| 97 | }, |
| 98 | ], |
| 99 | raw: true, |
| 100 | where, |
| 101 | }); |
| 102 | session.currentUserInfo = currentUserInfo; |