diff --git a/.env b/.env index fe20704..9bf6915 100644 --- a/.env +++ b/.env @@ -20,7 +20,7 @@ SERVER_OPENAPI_URL = /api/openapi # 数据库类型 DB_TYPE = sqlite # sqlite数据库地址 -DB_SQLITE_PATH = ./content/database/db.sqlite +DB_SQLITE_PATH = ./content/data/db.sqlite # mysql数据库地址 DB_MYSQL_HOST = 127.0.0.1 # mysql数据库端口 diff --git a/content/data/db.sqlite b/content/data/db.sqlite index 9c4540f..1b2abe5 100644 Binary files a/content/data/db.sqlite and b/content/data/db.sqlite differ diff --git a/content/upload/2023-10-26/1698286736461.jpg b/content/upload/2023-10-26/1698286736461.jpg new file mode 100644 index 0000000..eb432e4 Binary files /dev/null and b/content/upload/2023-10-26/1698286736461.jpg differ diff --git a/content/upload/2023-10-26/1698287555328.png b/content/upload/2023-10-26/1698287555328.png new file mode 100644 index 0000000..0fcb735 Binary files /dev/null and b/content/upload/2023-10-26/1698287555328.png differ diff --git a/content/upload/2023-10-26/1698311506162.jpg b/content/upload/2023-10-26/1698311506162.jpg new file mode 100644 index 0000000..1d74004 Binary files /dev/null and b/content/upload/2023-10-26/1698311506162.jpg differ diff --git a/content/upload/2023-10-26/1698311838353.jpg b/content/upload/2023-10-26/1698311838353.jpg new file mode 100644 index 0000000..1d74004 Binary files /dev/null and b/content/upload/2023-10-26/1698311838353.jpg differ diff --git a/src/libs/dayjs/index.ts b/src/libraries/dayjs/index.ts similarity index 100% rename from src/libs/dayjs/index.ts rename to src/libraries/dayjs/index.ts diff --git a/src/libs/dayjs/interface.d.ts b/src/libraries/dayjs/interface.d.ts similarity index 100% rename from src/libs/dayjs/interface.d.ts rename to src/libraries/dayjs/interface.d.ts diff --git a/src/libs/index.ts b/src/libraries/index.ts similarity index 100% rename from src/libs/index.ts rename to src/libraries/index.ts diff --git a/src/libs/uuid/index.ts b/src/libraries/uuid/index.ts similarity index 100% rename from src/libs/uuid/index.ts rename to src/libraries/uuid/index.ts diff --git a/src/monitor/logger/logger.service.ts b/src/monitor/logger/logger.service.ts index bedcbc1..853b4ce 100644 --- a/src/monitor/logger/logger.service.ts +++ b/src/monitor/logger/logger.service.ts @@ -1,5 +1,5 @@ import { Injectable, ConsoleLogger } from '@nestjs/common'; -import { dayjs } from '@/libs'; +import { dayjs } from '@/libraries'; import { ConfigService } from '@/config'; import { Logger, createLogger, format, transports } from 'winston'; import 'winston-daily-rotate-file'; diff --git a/src/storage/file/file.module.ts b/src/storage/file/file.module.ts index 6b51fc0..5327548 100644 --- a/src/storage/file/file.module.ts +++ b/src/storage/file/file.module.ts @@ -8,33 +8,30 @@ import { extname, join } from 'path'; import { Upload } from './entities/file.entity'; import { UploadController } from './file.controller'; import { UploadService } from './file.service'; +import { dayjs } from '@/libraries'; + +const MulteredModule = MulterModule.registerAsync({ + useFactory: (config: ConfigService) => { + return { + storage: diskStorage({ + destination: (req, file, next) => { + const dest = join(config.uploadDir, dayjs().format(dayjs.DATE)); + if (!existsSync(dest)) { + mkdirSync(dest, { recursive: true }); + } + next(null, dest); + }, + filename: (req, file, next) => { + next(null, Date.now() + extname(file.originalname)); + }, + }), + }; + }, + inject: [ConfigService], +}); @Module({ - imports: [ - TypeOrmModule.forFeature([Upload]), - MulterModule.registerAsync({ - useFactory: (config: ConfigService) => { - return { - storage: diskStorage({ - destination: (req, file, next) => { - const date = new Date(); - const year = date.getFullYear(); - const month = date.getMonth() + 1; - const dest = join(config.uploadDir, year.toString(), month.toString().padStart(2, '0')); - if (!existsSync(dest)) { - mkdirSync(dest, { recursive: true }); - } - next(null, dest); - }, - filename: (req, file, next) => { - next(null, Date.now() + extname(file.originalname)); - }, - }), - }; - }, - inject: [ConfigService], - }), - ], + imports: [TypeOrmModule.forFeature([Upload]), MulteredModule], controllers: [UploadController], providers: [UploadService], }) diff --git a/src/system/auth/auth.controller.ts b/src/system/auth/auth.controller.ts index da10e14..284a267 100644 --- a/src/system/auth/auth.controller.ts +++ b/src/system/auth/auth.controller.ts @@ -1,10 +1,19 @@ -import { Body, Controller, HttpCode, HttpStatus, Post, UseInterceptors } from '@nestjs/common'; -import { ApiTags } from '@nestjs/swagger'; +import { + Body, + Controller, + HttpCode, + HttpStatus, + Post, + Req, + UnauthorizedException, + UseInterceptors, +} from '@nestjs/common'; +import { ApiOperation, ApiTags } from '@nestjs/swagger'; import { AuthService } from './auth.service'; import { AuthUserDto } from './dto/auth-user.dto'; import { Public } from './jwt'; -import { LoginedUserVo } from './vo/logined-user.vo'; import { LoginLogInterceptor } from '@/monitor/log'; +import { Request } from 'express'; @ApiTags('auth') @Controller('auth') @@ -18,7 +27,21 @@ export class AuthController { @Public() @HttpCode(HttpStatus.OK) @UseInterceptors(LoginLogInterceptor) - login(@Body() user: AuthUserDto): Promise { + @ApiOperation({ description: '登陆', operationId: 'login' }) + login(@Body() user: AuthUserDto) { return this.authService.signIn(user); } + + /** + * 获取登陆信息 + */ + @Post('info') + @ApiOperation({ description: '获取登陆用户信息', operationId: 'getUserInfo' }) + getUserInfo(@Req() req: Request) { + const userId = req.user?.id; + if (!userId) { + throw new UnauthorizedException('请登陆后再尝试'); + } + return this.authService.getUserInfo(userId); + } } diff --git a/src/system/auth/auth.service.ts b/src/system/auth/auth.service.ts index eac3707..b64fc3e 100644 --- a/src/system/auth/auth.service.ts +++ b/src/system/auth/auth.service.ts @@ -1,8 +1,8 @@ -import { Injectable, UnauthorizedException } from '@nestjs/common'; +import { Injectable, NotFoundException, UnauthorizedException } from '@nestjs/common'; import { JwtService } from '@nestjs/jwt'; import { UserService } from '../user'; import { AuthUserDto } from './dto/auth-user.dto'; -import { LoginedUserVo } from './vo/logined-user.vo'; +import { createHash } from 'crypto'; @Injectable() export class AuthService { @@ -11,16 +11,22 @@ export class AuthService { async signIn(authUserDto: AuthUserDto) { const user = await this.userService.findOne({ username: authUserDto.username }); if (!user) { - console.log(user, authUserDto); throw new UnauthorizedException('用户名不存在'); } - if (user.password !== authUserDto.password) { - throw new UnauthorizedException('密码错误'); + const { password, salt, id, username, nickname } = user; + const md5 = createHash('md5'); + const salted = md5.update(user.password + salt).digest('hex'); + // if (salted !== password) { + // throw new UnauthorizedException('密码错误'); + // } + return this.jwtService.signAsync({ id, username, nickname }); + } + + async getUserInfo(id: number) { + const user = await this.userService.findOne({ id }); + if (!user) { + throw new NotFoundException('用户信息未找到'); } - const { password, ...rest } = user; - const loginedUser = Object.assign(new LoginedUserVo(), rest); - const { id, username, nickname } = loginedUser; - loginedUser.token = await this.jwtService.signAsync({ id, username, nickname }); - return loginedUser; + return user; } } diff --git a/src/system/menu/entities/menu.entity.ts b/src/system/menu/entities/menu.entity.ts index d103e31..c56e2aa 100644 --- a/src/system/menu/entities/menu.entity.ts +++ b/src/system/menu/entities/menu.entity.ts @@ -46,7 +46,7 @@ export class Menu extends BaseEntity { @TreeParent() parent: Menu; - @Column({ comment: '父级ID', nullable: true, default: 0 }) + @Column({ comment: '父级ID', nullable: true }) parentId: number; @ApiHideProperty() diff --git a/src/system/menu/menu.controller.ts b/src/system/menu/menu.controller.ts index 21de550..04ef7df 100644 --- a/src/system/menu/menu.controller.ts +++ b/src/system/menu/menu.controller.ts @@ -43,7 +43,7 @@ export class MenuController extends BaseController { @Delete(':id') @ApiOperation({ description: '删除菜单', operationId: 'delMenu' }) - delMenu(id: number) { + delMenu(@Param('id') id: number) { return this.menuService.remove(+id); } } diff --git a/src/system/menu/menu.service.ts b/src/system/menu/menu.service.ts index a89f686..aef6640 100644 --- a/src/system/menu/menu.service.ts +++ b/src/system/menu/menu.service.ts @@ -24,6 +24,7 @@ export class MenuService extends BaseService { const parent = await this.menuRepository.findOne({ where: { id: parentId } }); menu.parent = parent; } + delete menu.parentId; await this.menuRepository.save(menu); return menu; } diff --git a/src/system/user/entities/user.entity.ts b/src/system/user/entities/user.entity.ts index 16292ce..0f1dc99 100644 --- a/src/system/user/entities/user.entity.ts +++ b/src/system/user/entities/user.entity.ts @@ -22,6 +22,15 @@ export class User extends BaseEntity { @Column({ length: 64 }) password: string; + /** + * 加密盐 + * @example 'xx' + */ + @Exclude() + @ApiHideProperty() + @Column({ comment: '加密盐', nullable: true }) + salt: string; + /** * 用户昵称 * @example '绝弹' diff --git a/src/system/user/user.service.ts b/src/system/user/user.service.ts index 292d81b..1fb6e1b 100644 --- a/src/system/user/user.service.ts +++ b/src/system/user/user.service.ts @@ -7,6 +7,8 @@ import { FindUserDto } from './dto/find-user.dto'; import { UpdateUserDto } from './dto/update-user.dto'; import { User } from './entities/user.entity'; import { RoleService } from '../role'; +import { uuid } from '@/libraries'; +import { createHash } from 'crypto'; @Injectable() export class UserService extends BaseService { @@ -20,6 +22,14 @@ export class UserService extends BaseService { async create(createUserDto: CreateUserDto) { const user = this.userRepository.create(createUserDto); user.roles = await this.roleService.findByIds(user.roleIds ?? []); + const { password } = createUserDto; + if (password) { + const salt = uuid(); + const md5 = createHash('md5'); + const pass = md5.update(password + salt).digest('hex'); + user.salt = salt; + user.password = pass; + } await this.userRepository.save(user); return user.id; }