import { HttpException , Inject } from '@nestjs/common' ; import { Mutation , Query , Resolver } from '@nestjs/graphql' ; import { __ as t } from 'i18n' ; import { CommonResult } from '../interfaces/common-result.interface' ; import { CreateUserInput , UpdateUserInput , UserInfoData } from '../interfaces/user.interface' ; import { UserService } from '../services/user.service' ; import { PagerUtil } from '../utils/pager.util' ; @ Resolver ( ) export class UserResolver { constructor ( @ Inject ( UserService ) private readonly userService , @ Inject ( PagerUtil ) private readonly pagerUtil ) { } @ Query ( 'checkPwd' ) async checkPwd ( req , body ) { const data = await this . userService . checkPwd ( body . username , body . password ) ; return { code : 200 , message : '验证完毕' , data } ; } @ Query ( 'login' ) async login ( req , body ) { let data ; if ( isNaN ( Number ( body . password ) ) ) { data = await this . userService . login ( body . username , body . password ) ; } else if ( ! isNaN ( Number ( body . password ) ) ) { data = await this . userService . mobileLogin ( body . username , parseInt ( body . password ) ) ; } return { code : 200 , message : t ( 'Login success' ) , data : data . tokenInfo } ; } @ Query ( 'adminLogin' ) async adminLogin ( req , body ) { let data ; if ( isNaN ( Number ( body . password ) ) ) { data = await this . userService . login ( body . username , body . password ) ; } else if ( ! isNaN ( Number ( body . password ) ) ) { data = await this . userService . mobileLogin ( body . username , parseInt ( body . password ) ) ; } const userInfoData = data . userInfoData ; if ( userInfoData . username !== 'sadmin' && userInfoData . userRoles . map ( v => v . id ) . includes ( 1 ) ) { throw new HttpException ( t ( 'You are not authorized to access' ) , 401 ) ; } return { code : 200 , message : t ( 'Login success' ) , data : data . tokenInfo } ; } @ Mutation ( 'register' ) async register ( req , body ) { await this . userService . register ( body . registerUserInput ) ; return { code : 200 , message : t ( 'Registration success' ) } ; } @ Mutation ( 'createUser' ) async createUser ( req , body ) { await this . userService . createUser ( body . createUserInput ) ; return { code : 200 , message : t ( 'Create user successfully' ) } ; } @ Mutation ( 'addUserRole' ) async addUserRole ( req , body ) { await this . userService . addUserRole ( body . userId , body . roleId ) ; return { code : 200 , message : t ( 'Add user role successfully' ) } ; } @ Mutation ( 'deleteUserRole' ) async deleteUserRole ( req , body ) { await this . userService . deleteUserRole ( body . userId , body . roleId ) ; return { code : 200 , message : t ( 'Delete user role successfully' ) } ; } @ Mutation ( 'banUser' ) async banUser ( req , body ) { await this . userService . recycleOrBanUser ( body . userId , 'recycle' ) ; return { code : 200 , message : t ( 'Ban user successfully' ) } ; } @ Mutation ( 'recycleUser' ) async recycleUser ( req , body ) { await this . userService . recycleOrBanUser ( body . userId , 'recycle' ) ; return { code : 200 , message : t ( 'Delete user to recycle bin successfully' ) } ; } @ Mutation ( 'deleteRecycledUser' ) async deleteRecycledUser ( req , body ) { await this . userService . deleteUser ( body . userId ) ; return { code : 200 , message : t ( 'Delete user in the recycle bin successfully' ) } ; } @ Mutation ( 'revertBannedUser' ) async revertBannedUser ( req , body ) { await this . userService . revertBannedOrRecycledUser ( body . userId , 'banned' ) ; return { code : 200 , message : t ( 'Revert banned user successfully' ) } ; } @ Mutation ( 'revertRecycledUser' ) async revertRecycledUser ( req , body ) { await this . userService . revertBannedOrRecycledUser ( body . userId , 'recycled' ) ; return { code : 200 , message : t ( 'Revert recycled user successfully' ) } ; } @ Mutation ( 'updateUserInfoById' ) async updateUserInfo ( req , body ) { await this . userService . updateUserInfo ( body . userId , body . updateUserInput ) ; return { code : 200 , message : t ( 'Update user information successfully' ) } ; } @ Mutation ( 'updateCurrentUserInfo' ) async updateCurrentUserInfo ( req , body , context ) { await this . userService . updateUserInfo ( context . user . id , body . updateCurrentUserInput ) ; return { code : 200 , message : t ( 'Update current login user information successfully' ) } ; } @ Query ( 'findUserInfoByIds' ) async findUserInfoById ( req , body ) { const data = await this . userService . findUserInfoById ( body . userIds ) as UserInfoData [ ] ; return { code : 200 , message : t ( 'Query the specified users information successfully' ) , data } ; } @ Query ( 'findCurrentUserInfo' ) async findCurrentUserInfo ( req , body , context ) { const data = await this . userService . findUserInfoById ( context . user . id ) as UserInfoData ; return { code : 200 , message : t ( 'Query the current login user information successfully' ) , data } ; } @ Query ( 'findUsersInRole' ) async findUsersInRole ( req , body ) { const data = await this . userService . findByRoleId ( body . roleId ) ; return { code : 200 , message : t ( 'Query the user under the role successfully' ) , data } ; } @ Mutation ( 'findPassword' ) async findPassword ( req , body ) { await this . userService . findPassword ( body . mobile , body . password ) ; return { code : 200 , message : t ( 'Update user information successfully' ) } ; } }