import { ALL , Body , Controller , Get , Inject , Post } from '@midwayjs/decorator' ; import { BaseController } from '../../../core/base_controller' ; import { Jwt } from '../../../util/jwt' ; import { LoginDto , LogoutDto } from '../dto/auth' ; import { UserService } from '../service/user' ; @ Controller ( '/auth' , { description : '授权管理' } ) export class AuthController extends BaseController { @ Inject ( ) private readonly jwt : Jwt ; @ Inject ( ) private readonly userService : UserService ; @ Post ( '/login' , { description : '用户登录' } ) public async login ( @ Body ( ALL ) body ) { const result = await this . userService . login ( body ) ; this . success ( result ) ; } @ Get ( '/info' , { description : '获取登录信息' } ) public async getInfo ( ) { const { _id } = this . ctx . state . user ; const user = await this . userService . findById ( _id ) ; delete this . ctx . state . user . iat ; delete this . ctx . state . user . exp ; const token = this . jwt . sign ( this . ctx . state . user ) ; this . success ( { token , user } ) ; } @ Post ( '/logout' , { description : '用户登出' } ) public async logout ( @ Body ( ALL ) body ) { await this . jwt . revoke ( body . token || this . ctx . state . token ) ; this . success ( ) ; } }