Просмотр исходного кода

feat: 改进标签颜色处理并添加CORS支持

- 将标签默认颜色从黑色(#000000)改为白色(#ffffff)
- 添加生成鲜艳随机颜色的工具函数
- 在创建/更新文章时为标签使用随机颜色
- 为NestJS应用启用CORS支持,允许所有来源访问
- 优化文章查询,按创建时间降序排列
Sakulin 2 месяцев назад
Родитель
Сommit
f09ecdd5d0
4 измененных файлов с 44 добавлено и 24 удалено
  1. 1 1
      prisma/schema.prisma
  2. 8 0
      src/main.ts
  3. 25 23
      src/post/post.controller.ts
  4. 10 0
      src/utils/index.ts

+ 1 - 1
prisma/schema.prisma

@@ -10,7 +10,7 @@ generator client {
 model Tag {
     id        Int      @id @default(autoincrement())
     name      String   @unique
-    color     String   @default("#000000")
+    color     String   @default("#ffffff")
     createdAt DateTime @default(now())
     posts     Post[]
 }

+ 8 - 0
src/main.ts

@@ -3,6 +3,14 @@ import { AppModule } from './app.module';
 
 async function bootstrap() {
   const app = await NestFactory.create(AppModule);
+
+  app.enableCors({
+    origin: '*',
+    methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
+    preflightContinue: false,
+    optionsSuccessStatus: 204,
+  });
+
   await app.listen(process.env.PORT ?? 3000);
 }
 bootstrap();

+ 25 - 23
src/post/post.controller.ts

@@ -10,6 +10,7 @@ import {
 } from '@nestjs/common';
 import { AuthService } from 'src/auth/auth.service';
 import { DatasourceService } from 'src/datasource/datasource.service';
+import { generateVibrantRandomColor } from 'src/utils';
 
 interface PostBody {
   title: string;
@@ -40,30 +41,31 @@ export class PostController {
       };
     }
 
-    const query = {
-      skip,
-      take,
-      select: {
-        id: true,
-        title: true,
-        createdAt: true,
-        updatedAt: true,
-        cate: true,
-        descirption: true,
-        tags: {
-          select: {
-            name: true,
-            id: true,
-            color: true,
-          },
-        },
-      },
-    };
-
     const prisma = this.datasource.getPrisma();
     return {
       code: 200,
-      data: await prisma.post.findMany(query),
+      data: await prisma.post.findMany({
+        skip,
+        take,
+        orderBy: {
+          createdAt: 'desc',
+        },
+        select: {
+          id: true,
+          title: true,
+          createdAt: true,
+          updatedAt: true,
+          cate: true,
+          descirption: true,
+          tags: {
+            select: {
+              name: true,
+              id: true,
+              color: true,
+            },
+          },
+        },
+      }),
     };
   }
 
@@ -122,7 +124,7 @@ export class PostController {
           tags: {
             connectOrCreate: body.tags.map((tag) => ({
               where: { name: tag },
-              create: { name: tag, color: '#000000' },
+              create: { name: tag, color: generateVibrantRandomColor() },
             })),
           },
         },
@@ -165,7 +167,7 @@ export class PostController {
             set: [],
             connectOrCreate: body.tags.map((tag) => ({
               where: { name: tag },
-              create: { name: tag, color: '#000000' },
+              create: { name: tag, color: generateVibrantRandomColor() },
             })),
           },
         },

+ 10 - 0
src/utils/index.ts

@@ -0,0 +1,10 @@
+/**
+ * 生成鲜艳的随机颜色
+ * @returns 以十六进制字符串表示的颜色值,例如 "#FF0000"
+ */
+export function generateVibrantRandomColor() {
+  const r = Math.floor(Math.random() * 256);
+  const g = Math.floor(Math.random() * 256);
+  const b = Math.floor(Math.random() * 256);
+  return `#${r.toString(16).padStart(2, '0')}${g.toString(16).padStart(2, '0')}${b.toString(16).padStart(2, '0')}`;
+}