refactor: 统一代码缩进与引号风格,新增CORS配置
1. 修正所有文件的缩进为4空格,将单引号统一改为双引号 2. 新增CORS_ORIGIN环境变量配置并从env读取CORS允许源 3. 新增prettier配置文件统一代码格式 4. 为main.ts添加启动日志输出
This commit is contained in:
3
.env
3
.env
@@ -12,4 +12,7 @@ DB_USERNAME=root
|
||||
DB_PASSWORD=zhxiao1124..
|
||||
DB_DATABASE=onion_tiktok
|
||||
|
||||
# CORS settings
|
||||
CORS_ORIGIN=http://localhost:5173,http://127.0.0.1:5173
|
||||
|
||||
DB_SYNCHRONIZE = true
|
||||
|
||||
@@ -12,4 +12,7 @@ DB_USERNAME=root
|
||||
DB_PASSWORD=
|
||||
DB_DATABASE=cth_tk_backend
|
||||
|
||||
# CORS settings
|
||||
CORS_ORIGIN=http://localhost:5173,http://127.0.0.1:5173
|
||||
|
||||
DB_SYNCHRONIZE = true
|
||||
|
||||
9
.prettierrc
Normal file
9
.prettierrc
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"tabWidth": 4,
|
||||
"singleQuote": false,
|
||||
"trailingComma": "all",
|
||||
"printWidth": 390,
|
||||
"eslintIntegration": true,
|
||||
"bracketSpacing": true,
|
||||
"stylelintIntegration": true
|
||||
}
|
||||
@@ -1,15 +1,15 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { AdminModule } from './modules/admin/admin.module';
|
||||
import { AuthModule } from './modules/auth/auth.module';
|
||||
import { DataModule } from './modules/data/data.module';
|
||||
import { DatabaseModule } from './modules/database/database.module';
|
||||
import { DramasModule } from './modules/dramas/dramas.module';
|
||||
import { HealthModule } from './modules/health/health.module';
|
||||
import { LogsModule } from './modules/logs/logs.module';
|
||||
import { MediaModule } from './modules/media/media.module';
|
||||
import { OrdersModule } from './modules/orders/orders.module';
|
||||
import { PlayerModule } from './modules/player/player.module';
|
||||
import { Module } from "@nestjs/common";
|
||||
import { ConfigModule } from "@nestjs/config";
|
||||
import { AdminModule } from "./modules/admin/admin.module";
|
||||
import { AuthModule } from "./modules/auth/auth.module";
|
||||
import { DataModule } from "./modules/data/data.module";
|
||||
import { DatabaseModule } from "./modules/database/database.module";
|
||||
import { DramasModule } from "./modules/dramas/dramas.module";
|
||||
import { HealthModule } from "./modules/health/health.module";
|
||||
import { LogsModule } from "./modules/logs/logs.module";
|
||||
import { MediaModule } from "./modules/media/media.module";
|
||||
import { OrdersModule } from "./modules/orders/orders.module";
|
||||
import { PlayerModule } from "./modules/player/player.module";
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
|
||||
@@ -9,10 +9,7 @@ import { LogsService } from "./modules/logs/logs.service";
|
||||
|
||||
export function bootstrapApp(app: INestApplication) {
|
||||
app.enableCors({
|
||||
origin: process.env.CORS_ORIGIN?.split(',') ?? [
|
||||
'http://localhost:5173',
|
||||
'http://127.0.0.1:5173',
|
||||
],
|
||||
origin: process.env.CORS_ORIGIN?.split(","),
|
||||
credentials: true,
|
||||
});
|
||||
app.use(RequestIdMiddleware);
|
||||
@@ -25,18 +22,13 @@ export function bootstrapApp(app: INestApplication) {
|
||||
);
|
||||
|
||||
const logsService = app.get(LogsService);
|
||||
app.useGlobalFilters(new HttpExceptionFilter(logsService));
|
||||
app.useGlobalInterceptors(
|
||||
new RequestLoggingInterceptor(logsService, app.get(Reflector)),
|
||||
new ResponseInterceptor(),
|
||||
);
|
||||
|
||||
const config = new DocumentBuilder()
|
||||
.setTitle("TikTok Short Drama Backend")
|
||||
.setDescription("RESTful API for TikTok short drama backend services.")
|
||||
.setVersion("1.0")
|
||||
.addBearerAuth()
|
||||
.build();
|
||||
app.useGlobalFilters(new HttpExceptionFilter(logsService));
|
||||
app.useGlobalInterceptors(new RequestLoggingInterceptor(logsService, app.get(Reflector)), new ResponseInterceptor());
|
||||
|
||||
const config = new DocumentBuilder().setTitle("TikTok Short Drama Backend").setDescription("RESTful API for TikTok short drama backend services.").setVersion("1.0").addBearerAuth().build();
|
||||
|
||||
const document = SwaggerModule.createDocument(app, config);
|
||||
|
||||
SwaggerModule.setup("/api/docs", app, document);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import 'dotenv/config';
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './app.module';
|
||||
import { bootstrapApp } from './bootstrap';
|
||||
import "dotenv/config";
|
||||
import { NestFactory } from "@nestjs/core";
|
||||
import { AppModule } from "./app.module";
|
||||
import { bootstrapApp } from "./bootstrap";
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
bootstrapApp(app);
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
console.log(`Application is running on: ${await app.getUrl()}`);
|
||||
}
|
||||
|
||||
void bootstrap();
|
||||
|
||||
@@ -1,151 +1,117 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
Param,
|
||||
Patch,
|
||||
Post,
|
||||
Query,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
||||
import { RequirePermissions } from '../admin/decorators/require-permissions.decorator';
|
||||
import { AdminJwtAuthGuard } from '../admin/guards/admin-jwt-auth.guard';
|
||||
import { PermissionsGuard } from '../admin/guards/permissions.guard';
|
||||
import { ConfigureEpisodesDto } from './dto/configure-episodes.dto';
|
||||
import { CreateDramaDto } from './dto/create-drama.dto';
|
||||
import { CreateEpisodeDto } from './dto/create-episode.dto';
|
||||
import { UpdateDramaDto } from './dto/update-drama.dto';
|
||||
import { UpdateEpisodeDto } from './dto/update-episode.dto';
|
||||
import { DramasService } from './dramas.service';
|
||||
import { PublishStatus } from './drama-status.enum';
|
||||
import { Body, Controller, Get, Param, Patch, Post, Query, UseGuards } from "@nestjs/common";
|
||||
import { ApiBearerAuth, ApiTags } from "@nestjs/swagger";
|
||||
import { RequirePermissions } from "../admin/decorators/require-permissions.decorator";
|
||||
import { AdminJwtAuthGuard } from "../admin/guards/admin-jwt-auth.guard";
|
||||
import { PermissionsGuard } from "../admin/guards/permissions.guard";
|
||||
import { ConfigureEpisodesDto } from "./dto/configure-episodes.dto";
|
||||
import { CreateDramaDto } from "./dto/create-drama.dto";
|
||||
import { CreateEpisodeDto } from "./dto/create-episode.dto";
|
||||
import { UpdateDramaDto } from "./dto/update-drama.dto";
|
||||
import { UpdateEpisodeDto } from "./dto/update-episode.dto";
|
||||
import { DramasService } from "./dramas.service";
|
||||
import { PublishStatus } from "./drama-status.enum";
|
||||
|
||||
@ApiTags('Dramas')
|
||||
@ApiTags("Dramas")
|
||||
@Controller()
|
||||
export class DramasController {
|
||||
constructor(private readonly dramasService: DramasService) {}
|
||||
|
||||
@Post('/api/admin/v1/dramas')
|
||||
@Post("/api/admin/v1/dramas")
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
||||
@RequirePermissions('drama:create')
|
||||
@RequirePermissions("drama:create")
|
||||
createDrama(@Body() dto: CreateDramaDto) {
|
||||
return this.dramasService.createDrama(dto);
|
||||
}
|
||||
|
||||
@Get('/api/admin/v1/dramas')
|
||||
@Get("/api/admin/v1/dramas")
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
||||
@RequirePermissions('drama:read')
|
||||
listAdminDramas(@Query('page') page?: string, @Query('pageSize') pageSize?: string) {
|
||||
@RequirePermissions("drama:read")
|
||||
listAdminDramas(@Query("page") page?: string, @Query("pageSize") pageSize?: string) {
|
||||
return this.dramasService.listAdminDramas({
|
||||
page: Number(page) || 1,
|
||||
pageSize: Number(pageSize) || 20,
|
||||
});
|
||||
}
|
||||
|
||||
@Get('/api/admin/v1/dramas/:id')
|
||||
@Get("/api/admin/v1/dramas/:id")
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
||||
@RequirePermissions('drama:read')
|
||||
getDrama(@Param('id') id: string) {
|
||||
@RequirePermissions("drama:read")
|
||||
getDrama(@Param("id") id: string) {
|
||||
return this.dramasService.getDramaOrThrow(Number(id));
|
||||
}
|
||||
|
||||
@Patch('/api/admin/v1/dramas/:id')
|
||||
@Patch("/api/admin/v1/dramas/:id")
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
||||
@RequirePermissions('drama:update')
|
||||
updateDrama(@Param('id') id: string, @Body() dto: UpdateDramaDto) {
|
||||
@RequirePermissions("drama:update")
|
||||
updateDrama(@Param("id") id: string, @Body() dto: UpdateDramaDto) {
|
||||
return this.dramasService.updateDrama(Number(id), dto);
|
||||
}
|
||||
|
||||
@Post('/api/admin/v1/dramas/:id/episodes/batch')
|
||||
@Post("/api/admin/v1/dramas/:id/episodes/batch")
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
||||
@RequirePermissions('episode:create')
|
||||
configureEpisodes(
|
||||
@Param('id') id: string,
|
||||
@Body() dto: ConfigureEpisodesDto,
|
||||
) {
|
||||
@RequirePermissions("episode:create")
|
||||
configureEpisodes(@Param("id") id: string, @Body() dto: ConfigureEpisodesDto) {
|
||||
return this.dramasService.configureEpisodes(Number(id), dto);
|
||||
}
|
||||
|
||||
@Get('/api/admin/v1/dramas/:id/episodes')
|
||||
@Get("/api/admin/v1/dramas/:id/episodes")
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
||||
@RequirePermissions('episode:update')
|
||||
listAdminEpisodes(
|
||||
@Param('id') id: string,
|
||||
@Query('page') page?: string,
|
||||
@Query('pageSize') pageSize?: string,
|
||||
) {
|
||||
@RequirePermissions("episode:update")
|
||||
listAdminEpisodes(@Param("id") id: string, @Query("page") page?: string, @Query("pageSize") pageSize?: string) {
|
||||
return this.dramasService.listAdminEpisodes(Number(id), {
|
||||
page: Number(page) || 1,
|
||||
pageSize: Number(pageSize) || 20,
|
||||
});
|
||||
}
|
||||
|
||||
@Post('/api/admin/v1/episodes')
|
||||
@Post("/api/admin/v1/episodes")
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
||||
@RequirePermissions('episode:create')
|
||||
@RequirePermissions("episode:create")
|
||||
createEpisode(@Body() dto: CreateEpisodeDto) {
|
||||
return this.dramasService.createEpisode(dto);
|
||||
}
|
||||
|
||||
@Patch('/api/admin/v1/episodes/:id')
|
||||
@Patch("/api/admin/v1/episodes/:id")
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
||||
@RequirePermissions('episode:update')
|
||||
updateEpisode(@Param('id') id: string, @Body() dto: UpdateEpisodeDto) {
|
||||
@RequirePermissions("episode:update")
|
||||
updateEpisode(@Param("id") id: string, @Body() dto: UpdateEpisodeDto) {
|
||||
return this.dramasService.updateEpisode(Number(id), dto);
|
||||
}
|
||||
|
||||
@Patch('/api/admin/v1/dramas/:dramaId/episodes/:episodeId')
|
||||
@Patch("/api/admin/v1/dramas/:dramaId/episodes/:episodeId")
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
||||
@RequirePermissions('episode:update')
|
||||
updateDramaEpisode(
|
||||
@Param('dramaId') dramaId: string,
|
||||
@Param('episodeId') episodeId: string,
|
||||
@Body() dto: UpdateEpisodeDto,
|
||||
) {
|
||||
return this.dramasService.updateDramaEpisode(
|
||||
Number(dramaId),
|
||||
Number(episodeId),
|
||||
dto,
|
||||
);
|
||||
@RequirePermissions("episode:update")
|
||||
updateDramaEpisode(@Param("dramaId") dramaId: string, @Param("episodeId") episodeId: string, @Body() dto: UpdateEpisodeDto) {
|
||||
return this.dramasService.updateDramaEpisode(Number(dramaId), Number(episodeId), dto);
|
||||
}
|
||||
|
||||
@Post('/api/admin/v1/dramas/:id/submit-review')
|
||||
@Post("/api/admin/v1/dramas/:id/submit-review")
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
||||
@RequirePermissions('drama:update')
|
||||
submitReview(@Param('id') id: string) {
|
||||
@RequirePermissions("drama:update")
|
||||
submitReview(@Param("id") id: string) {
|
||||
return this.dramasService.submitReview(Number(id));
|
||||
}
|
||||
|
||||
@Patch('/api/admin/v1/episodes/:id/publish-status')
|
||||
@Patch("/api/admin/v1/episodes/:id/publish-status")
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(AdminJwtAuthGuard, PermissionsGuard)
|
||||
@RequirePermissions('episode:update')
|
||||
updatePublishStatus(
|
||||
@Param('id') id: string,
|
||||
@Body() dto: { publishStatus: PublishStatus },
|
||||
) {
|
||||
return this.dramasService.updateEpisodePublishStatus(
|
||||
Number(id),
|
||||
dto.publishStatus,
|
||||
);
|
||||
@RequirePermissions("episode:update")
|
||||
updatePublishStatus(@Param("id") id: string, @Body() dto: { publishStatus: PublishStatus }) {
|
||||
return this.dramasService.updateEpisodePublishStatus(Number(id), dto.publishStatus);
|
||||
}
|
||||
|
||||
@Get('/api/app/v1/dramas')
|
||||
listPublishedDramas(
|
||||
@Query('page') page?: string,
|
||||
@Query('pageSize') pageSize?: string,
|
||||
@Query('title') title?: string,
|
||||
) {
|
||||
@Get("/api/app/v1/dramas")
|
||||
listPublishedDramas(@Query("page") page?: string, @Query("pageSize") pageSize?: string, @Query("title") title?: string) {
|
||||
return this.dramasService.listPublishedDramas({
|
||||
page: Number(page) || 1,
|
||||
pageSize: Number(pageSize) || 10,
|
||||
@@ -153,20 +119,16 @@ export class DramasController {
|
||||
});
|
||||
}
|
||||
|
||||
@Get('/api/app/v1/dramas/:id/episodes')
|
||||
listPublishedEpisodes(
|
||||
@Param('id') id: string,
|
||||
@Query('page') page?: string,
|
||||
@Query('pageSize') pageSize?: string,
|
||||
) {
|
||||
@Get("/api/app/v1/dramas/:id/episodes")
|
||||
listPublishedEpisodes(@Param("id") id: string, @Query("page") page?: string, @Query("pageSize") pageSize?: string) {
|
||||
return this.dramasService.listPublishedEpisodes(Number(id), {
|
||||
page: Number(page) || 1,
|
||||
pageSize: Number(pageSize) || 20,
|
||||
});
|
||||
}
|
||||
|
||||
@Get('/api/app/v1/dramas/:id')
|
||||
getPublishedDrama(@Param('id') id: string) {
|
||||
@Get("/api/app/v1/dramas/:id")
|
||||
getPublishedDrama(@Param("id") id: string) {
|
||||
return this.dramasService.getPublishedDramaOrThrow(Number(id));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,4 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
Param,
|
||||
Patch,
|
||||
Post,
|
||||
Query,
|
||||
UseGuards,
|
||||
} from "@nestjs/common";
|
||||
import { Body, Controller, Get, Param, Patch, Post, Query, UseGuards } from "@nestjs/common";
|
||||
import { ApiBearerAuth, ApiTags } from "@nestjs/swagger";
|
||||
import { RequirePermissions } from "../admin/decorators/require-permissions.decorator";
|
||||
import { AdminJwtAuthGuard } from "../admin/guards/admin-jwt-auth.guard";
|
||||
@@ -68,10 +59,7 @@ export class MediaController {
|
||||
|
||||
@Get("/upload-jobs")
|
||||
@RequirePermissions("media:read")
|
||||
listUploadJobs(
|
||||
@Query("page") page?: string,
|
||||
@Query("pageSize") pageSize?: string,
|
||||
) {
|
||||
listUploadJobs(@Query("page") page?: string, @Query("pageSize") pageSize?: string) {
|
||||
return this.mediaService.listUploadJobs({
|
||||
page: Number(page) || 1,
|
||||
pageSize: Number(pageSize) || 20,
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { Injectable, NotFoundException, Optional } from '@nestjs/common';
|
||||
import { InjectDataSource } from '@nestjs/typeorm';
|
||||
import { DataSource } from 'typeorm';
|
||||
import { PaginatedResponse } from '../../common/dto/paginated-response.dto';
|
||||
import { User } from './entities/user.entity';
|
||||
import { UserRecord } from './interfaces/user-record.interface';
|
||||
import { Injectable, NotFoundException, Optional } from "@nestjs/common";
|
||||
import { InjectDataSource } from "@nestjs/typeorm";
|
||||
import { DataSource } from "typeorm";
|
||||
import { PaginatedResponse } from "../../common/dto/paginated-response.dto";
|
||||
import { User } from "./entities/user.entity";
|
||||
import { UserRecord } from "./interfaces/user-record.interface";
|
||||
|
||||
interface UpsertTikTokUserInput {
|
||||
tiktokOpenId: string;
|
||||
@@ -48,15 +48,13 @@ export class UsersService {
|
||||
tiktokUnionId: input.tiktokUnionId,
|
||||
nickname: input.nickname,
|
||||
avatarUrl: input.avatarUrl,
|
||||
status: 'active',
|
||||
status: "active",
|
||||
});
|
||||
const saved = await repository.save(user);
|
||||
return { user: this.toRecord(saved), isNewUser: true };
|
||||
}
|
||||
|
||||
const existing = this.users.find(
|
||||
(user) => user.tiktokOpenId === input.tiktokOpenId,
|
||||
);
|
||||
const existing = this.users.find((user) => user.tiktokOpenId === input.tiktokOpenId);
|
||||
const now = new Date().toISOString();
|
||||
|
||||
if (existing) {
|
||||
@@ -73,7 +71,7 @@ export class UsersService {
|
||||
tiktokUnionId: input.tiktokUnionId,
|
||||
nickname: input.nickname,
|
||||
avatarUrl: input.avatarUrl,
|
||||
status: 'active',
|
||||
status: "active",
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
};
|
||||
@@ -99,7 +97,7 @@ export class UsersService {
|
||||
const pageSize = Math.min(Math.max(pagination.pageSize, 1), 100);
|
||||
const repository = this.dataSource.getRepository(User);
|
||||
const [records, total] = await repository.findAndCount({
|
||||
order: { id: 'DESC' },
|
||||
order: { id: "DESC" },
|
||||
skip: (page - 1) * pageSize,
|
||||
take: pageSize,
|
||||
});
|
||||
@@ -116,18 +114,18 @@ export class UsersService {
|
||||
async getUserOrThrow(id: number) {
|
||||
const user = await this.findById(id);
|
||||
if (!user) {
|
||||
throw new NotFoundException('User not found');
|
||||
throw new NotFoundException("User not found");
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
async updateStatus(id: number, status: 'active' | 'disabled') {
|
||||
async updateStatus(id: number, status: "active" | "disabled") {
|
||||
if (this.hasDatabase()) {
|
||||
const repository = this.dataSource.getRepository(User);
|
||||
const user = await repository.findOne({ where: { id } });
|
||||
if (!user) {
|
||||
throw new NotFoundException('User not found');
|
||||
throw new NotFoundException("User not found");
|
||||
}
|
||||
|
||||
user.status = status;
|
||||
@@ -151,16 +149,13 @@ export class UsersService {
|
||||
tiktokUnionId: user.tiktokUnionId,
|
||||
nickname: user.nickname,
|
||||
avatarUrl: user.avatarUrl,
|
||||
status: user.status as 'active' | 'disabled',
|
||||
status: user.status as "active" | "disabled",
|
||||
createdAt: user.createdAt?.toISOString?.() ?? new Date().toISOString(),
|
||||
updatedAt: user.updatedAt?.toISOString?.() ?? new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
private paginate<T>(
|
||||
records: T[],
|
||||
input: PaginationInput,
|
||||
): PaginatedResponse<T> {
|
||||
private paginate<T>(records: T[], input: PaginationInput): PaginatedResponse<T> {
|
||||
const page = Math.max(input.page, 1);
|
||||
const pageSize = Math.min(Math.max(input.pageSize, 1), 100);
|
||||
const start = (page - 1) * pageSize;
|
||||
|
||||
Reference in New Issue
Block a user