1
- import { Injectable , HttpException , HttpStatus } from '@nestjs/common' ;
1
+ import { Injectable , HttpException , HttpStatus , Logger } from '@nestjs/common' ;
2
2
import { LoginDto } from '../../controllers/login/dto/login.dto' ;
3
3
import { AccountEntity } from '../../entities/account.entity' ;
4
4
import { InjectRepository } from '@nestjs/typeorm' ;
@@ -10,6 +10,7 @@ import { LoginVo } from '../../controllers/login/vo/login.vo';
10
10
11
11
@Injectable ( )
12
12
export class LoginService {
13
+ private logger : Logger = new Logger ( LoginService . name ) ;
13
14
constructor (
14
15
@InjectRepository ( AccountEntity )
15
16
private readonly accountRepository : Repository < AccountEntity > ,
@@ -33,50 +34,46 @@ export class LoginService {
33
34
let sqlPassword : string | undefined ;
34
35
let findAccount : AccountEntity | undefined ;
35
36
if ( isMobilePhone ( username , 'zh-CN' ) ) {
36
- const findResult : AccountEntity | undefined = await getConnection ( )
37
+ const findResult : Pick < AccountEntity , 'password' > | undefined = await getConnection ( )
37
38
. createQueryBuilder ( AccountEntity , 'account' )
38
39
. select ( [ ] )
39
40
. addSelect ( 'account.password' , 'password' )
40
41
. where ( '(account.mobile = :mobile)' , { mobile : username } )
41
42
. getRawOne ( ) ;
42
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
43
43
sqlPassword = findResult ?. password ;
44
44
findAccount = await this . accountRepository . findOne ( { where : { mobile : username } } ) ;
45
45
} else if ( isEmail ( username ) ) {
46
- const findResult : AccountEntity | undefined = await getConnection ( )
46
+ const findResult : Pick < AccountEntity , 'password' > | undefined = await getConnection ( )
47
47
. createQueryBuilder ( AccountEntity , 'account' )
48
48
. select ( [ ] )
49
49
. addSelect ( 'account.password' , 'password' )
50
50
. where ( '(account.email = :email)' , { email : username } )
51
51
. getRawOne ( ) ;
52
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
53
52
sqlPassword = findResult ?. password ;
54
53
findAccount = await this . accountRepository . findOne ( { where : { email : username } } ) ;
55
54
} else {
56
- const findResult : AccountEntity | undefined = await getConnection ( )
55
+ const findResult : Pick < AccountEntity , 'password' > | undefined = await getConnection ( )
57
56
. createQueryBuilder ( AccountEntity , 'account' )
58
57
. select ( [ ] )
59
58
. addSelect ( 'account.password' , 'password' )
60
59
. where ( '(account.username = :username)' , { username } )
61
60
. getRawOne ( ) ;
62
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
63
61
sqlPassword = findResult ?. password ;
64
62
findAccount = await this . accountRepository . findOne ( { where : { username } } ) ;
65
63
}
66
64
if ( sqlPassword && this . toolsService . checkPassword ( password , sqlPassword ) && findAccount ) {
67
65
const lastLogin = this . accountLastLoginRepository . create ( {
68
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
69
- accountId : findAccount ! . id ,
66
+ accountId : findAccount . id ,
70
67
lastLoginIp : ipAddress ,
71
68
} ) ;
72
69
await this . accountLastLoginRepository . save ( lastLogin ) ;
73
- console . log ( findAccount , '当前用户' ) ;
70
+ this . logger . log ( '当前用户' , findAccount ) ;
74
71
return Object . assign ( findAccount , { token : this . toolsService . generateToken ( findAccount ) } ) ;
75
72
} else {
76
73
throw new HttpException ( '用户名或密码错误' , HttpStatus . OK ) ;
77
74
}
78
75
} catch ( e ) {
79
- console . log ( e , '?' ) ;
76
+ this . logger . error ( '用户名或密码错误' , e ) ;
80
77
throw new HttpException ( '用户名或密码错误' , HttpStatus . OK ) ;
81
78
}
82
79
}
0 commit comments