-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from coverjs/add-account-login
Add account login
- Loading branch information
Showing
24 changed files
with
241 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,29 @@ | ||
# Cover Admin Service | ||
|
||
Cover Admin 中后台服务端 | ||
|
||
## 如何运行 | ||
|
||
- 安装依赖 | ||
|
||
```bash | ||
npm install | ||
``` | ||
|
||
- 同步数据库 | ||
|
||
```bash | ||
pnpm db:pm | ||
``` | ||
|
||
- 生成Prisma客户端 首次执行需要确保能访问外网 | ||
|
||
```bash | ||
pnpm db:pg | ||
``` | ||
|
||
- 启动开发环境服务 | ||
|
||
```bash | ||
pnpm dev | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { encryptPassword } from 'uni-nest'; | ||
|
||
export const User = [ | ||
{ | ||
username: 'admin', | ||
password: encryptPassword('admin', '1118'), | ||
salt: '1118', | ||
nickname: 'hacxy', | ||
email: 'admin@admin.com' | ||
} | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { PrismaClient } from '@prisma/client'; | ||
import { User } from './seed-data'; | ||
|
||
export const initDatabase = async () => { | ||
const prisma = new PrismaClient(); | ||
// // 创建用户数据 | ||
await prisma.user.createMany({ data: User }); | ||
// // 创建角色数据 | ||
// await prisma.role.createMany({ data: seedData.Role, skipDuplicates: true }); | ||
// // 创建菜单数据 | ||
// await prisma.user.createMany({ data: seedData.User, skipDuplicates: true }); | ||
// initData() | ||
// .then(async () => { | ||
// await prisma.$disconnect(); | ||
// }) | ||
// .catch(async (e) => { | ||
// console.error(e); | ||
// await prisma.$disconnect(); | ||
// process.exit(1); | ||
// }); | ||
}; | ||
|
||
initDatabase(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const JWT_SECRET_ENV_KEY = 'JWT_SECRET'; | ||
export const TOKEN_EXPIRES_ENV_KEY = 'TOKEN_EXPIRES'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const ADMIN_ERROR_CODE = { | ||
USERNAME_OR_PASSWORD_INCORRECT: { | ||
code: 1000, | ||
msg: '用户名或密码不正确' | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { UniBusinessException } from 'uni-nest'; | ||
import { ADMIN_ERROR_CODE } from './business.error.code'; | ||
|
||
// 定义业务异常 | ||
export class BusinessException extends UniBusinessException { | ||
static throwUsernameOrPasswordIncorrect(): void { | ||
throw new BusinessException(ADMIN_ERROR_CODE.USERNAME_OR_PASSWORD_INCORRECT); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Body, Controller } from '@nestjs/common'; | ||
import { AccountService } from './account.service'; | ||
import { AccountDto } from './dto/account.dto'; | ||
import { ApiTags, Method, UniDefine } from 'uni-nest'; | ||
import { AccountLoginVo } from './dto/account.vo'; | ||
|
||
@ApiTags('账号管理') | ||
@Controller('account') | ||
export class AccountController { | ||
constructor(private readonly accountService: AccountService) {} | ||
|
||
@UniDefine({ | ||
path: 'login', | ||
method: Method.Post, | ||
summary: '登录', | ||
isPublic: true, | ||
body: { | ||
type: AccountDto | ||
}, | ||
response: { | ||
type: AccountLoginVo | ||
} | ||
}) | ||
create(@Body() createAccountDto: AccountDto) { | ||
return this.accountService.login(createAccountDto); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AccountService } from './account.service'; | ||
import { AccountController } from './account.controller'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
|
||
@Module({ | ||
controllers: [AccountController], | ||
providers: [AccountService, JwtService] | ||
}) | ||
export class AccountModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { AccountDto } from './dto/account.dto'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { JWT_SECRET_ENV_KEY, TOKEN_EXPIRES_ENV_KEY } from '../../constants'; | ||
import { PrismaService } from '../../common/prisma/prisma.service'; | ||
import { encryptPassword } from 'uni-nest'; | ||
import { BusinessException } from '../../exceptions/business.exceptions'; | ||
|
||
@Injectable() | ||
export class AccountService { | ||
constructor( | ||
private readonly configService: ConfigService, | ||
private readonly jwtService: JwtService, | ||
private readonly prismaService: PrismaService | ||
) {} | ||
async login(account: AccountDto) { | ||
const { username, password } = account; | ||
const userInfo = await this.prismaService.user.findUnique({ | ||
where: { | ||
username | ||
} | ||
}); | ||
|
||
if (userInfo && userInfo.password === encryptPassword(password, userInfo.salt)) { | ||
const { password, ...res } = userInfo; | ||
const token = this.jwtService.sign(res, { | ||
secret: this.configService.get(JWT_SECRET_ENV_KEY), | ||
expiresIn: this.configService.get(TOKEN_EXPIRES_ENV_KEY) | ||
}); | ||
return { token }; | ||
} | ||
BusinessException.throwUsernameOrPasswordIncorrect(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { ApiProperty } from 'uni-nest'; | ||
|
||
export class AccountDto { | ||
@ApiProperty({ | ||
description: '用户名', | ||
example: 'admin' | ||
}) | ||
username: string; | ||
|
||
@ApiProperty({ | ||
description: '密码', | ||
example: 'admin' | ||
}) | ||
password: string; | ||
|
||
@ApiProperty({ | ||
description: '登录类型', | ||
example: 'account', | ||
enum: ['account', 'mobile'] | ||
}) | ||
type: 'account' | 'mobile'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { ApiProperty } from 'uni-nest'; | ||
|
||
export class AccountLoginVo { | ||
@ApiProperty({ description: 'token' }) | ||
token: string; | ||
} |
Oops, something went wrong.