@svton/cli 1.2.0 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/dist/index.js +20 -7
  2. package/dist/index.mjs +20 -7
  3. package/features.json +339 -0
  4. package/package.json +4 -1
  5. package/templates/apps/admin/next-env.d.ts +2 -0
  6. package/templates/apps/admin/next.config.js +15 -0
  7. package/templates/apps/admin/package.json.tpl +54 -0
  8. package/templates/apps/admin/postcss.config.js +6 -0
  9. package/templates/apps/admin/src/app/globals.css +37 -0
  10. package/templates/apps/admin/src/app/layout.tsx +19 -0
  11. package/templates/apps/admin/src/app/login/page.tsx +96 -0
  12. package/templates/apps/admin/src/app/page.tsx +8 -0
  13. package/templates/apps/admin/src/app/users/page.tsx +165 -0
  14. package/templates/apps/admin/src/components/ui/switch.tsx +29 -0
  15. package/templates/apps/admin/src/hooks/useAPI.ts +130 -0
  16. package/templates/apps/admin/src/lib/api-client.ts +112 -0
  17. package/templates/apps/admin/src/lib/api-server.ts +95 -0
  18. package/templates/apps/admin/tailwind.config.js +54 -0
  19. package/templates/apps/admin/tsconfig.json +22 -0
  20. package/templates/apps/backend/.env.example +29 -0
  21. package/templates/apps/backend/nest-cli.json +8 -0
  22. package/templates/apps/backend/package.json.tpl +57 -0
  23. package/templates/apps/backend/prisma/schema.prisma +72 -0
  24. package/templates/apps/backend/prisma/seed.ts +32 -0
  25. package/templates/apps/backend/src/app.controller.ts +15 -0
  26. package/templates/apps/backend/src/app.module.ts +85 -0
  27. package/templates/apps/backend/src/app.service.ts +12 -0
  28. package/templates/apps/backend/src/auth/auth.controller.ts +31 -0
  29. package/templates/apps/backend/src/auth/auth.module.ts +27 -0
  30. package/templates/apps/backend/src/auth/auth.service.ts +89 -0
  31. package/templates/apps/backend/src/auth/jwt-auth.guard.ts +5 -0
  32. package/templates/apps/backend/src/auth/jwt.strategy.ts +27 -0
  33. package/templates/apps/backend/src/config/env.schema.ts +35 -0
  34. package/templates/apps/backend/src/main.ts +51 -0
  35. package/templates/apps/backend/src/object-storage/object-storage.controller.ts +114 -0
  36. package/templates/apps/backend/src/object-storage/object-storage.module.ts +7 -0
  37. package/templates/apps/backend/src/prisma/prisma.module.ts +9 -0
  38. package/templates/apps/backend/src/prisma/prisma.service.ts +13 -0
  39. package/templates/apps/backend/src/user/user.controller.ts +50 -0
  40. package/templates/apps/backend/src/user/user.module.ts +12 -0
  41. package/templates/apps/backend/src/user/user.service.ts +117 -0
  42. package/templates/apps/backend/tsconfig.json +23 -0
  43. package/templates/apps/mobile/babel.config.js +8 -0
  44. package/templates/apps/mobile/config/index.ts +65 -0
  45. package/templates/apps/mobile/package.json.tpl +48 -0
  46. package/templates/apps/mobile/project.config.json.tpl +17 -0
  47. package/templates/apps/mobile/src/app.config.ts +9 -0
  48. package/templates/apps/mobile/src/app.scss +4 -0
  49. package/templates/apps/mobile/src/app.ts +8 -0
  50. package/templates/apps/mobile/src/hooks/useAPI.ts +285 -0
  51. package/templates/apps/mobile/src/pages/index/index.scss +7 -0
  52. package/templates/apps/mobile/src/pages/index/index.tsx +49 -0
  53. package/templates/apps/mobile/src/services/api.ts +155 -0
  54. package/templates/apps/mobile/src/services/upload.service.ts +41 -0
  55. package/templates/apps/mobile/tsconfig.json +21 -0
  56. package/templates/configs/authz.config.ts +10 -0
  57. package/templates/configs/cache.config.ts +14 -0
  58. package/templates/configs/oauth.config.ts +20 -0
  59. package/templates/configs/payment.config.ts +44 -0
  60. package/templates/configs/queue.config.ts +21 -0
  61. package/templates/configs/rate-limit.config.ts +16 -0
  62. package/templates/configs/sms.config.ts +11 -0
  63. package/templates/configs/storage.config.ts +14 -0
  64. package/templates/examples/README.md +258 -0
  65. package/templates/examples/authz/README.md +273 -0
  66. package/templates/examples/authz/roles.guard.ts +37 -0
  67. package/templates/examples/authz/user.controller.ts +116 -0
  68. package/templates/examples/cache/README.md +82 -0
  69. package/templates/examples/cache/user.controller.ts +42 -0
  70. package/templates/examples/cache/user.service.ts +78 -0
  71. package/templates/examples/oauth/README.md +192 -0
  72. package/templates/examples/oauth/auth.controller.ts +99 -0
  73. package/templates/examples/oauth/auth.service.ts +97 -0
  74. package/templates/examples/payment/README.md +151 -0
  75. package/templates/examples/payment/order.controller.ts +56 -0
  76. package/templates/examples/payment/order.service.ts +132 -0
  77. package/templates/examples/payment/webhook.controller.ts +73 -0
  78. package/templates/examples/queue/README.md +134 -0
  79. package/templates/examples/queue/email.controller.ts +34 -0
  80. package/templates/examples/queue/email.processor.ts +68 -0
  81. package/templates/examples/queue/email.service.ts +64 -0
  82. package/templates/examples/rate-limit/README.md +249 -0
  83. package/templates/examples/rate-limit/api.controller.ts +113 -0
  84. package/templates/examples/sms/README.md +121 -0
  85. package/templates/examples/sms/sms.service.ts +69 -0
  86. package/templates/examples/sms/verification.controller.ts +100 -0
  87. package/templates/examples/storage/README.md +224 -0
  88. package/templates/examples/storage/upload.controller.ts +117 -0
  89. package/templates/examples/storage/upload.service.ts +123 -0
  90. package/templates/packages/types/package.json.tpl +16 -0
  91. package/templates/packages/types/src/api.ts +88 -0
  92. package/templates/packages/types/src/common.ts +89 -0
  93. package/templates/packages/types/src/index.ts +3 -0
  94. package/templates/packages/types/tsconfig.json +16 -0
  95. package/templates/skills/authz.skill.md +42 -0
  96. package/templates/skills/base.skill.md +57 -0
  97. package/templates/skills/cache.skill.md +88 -0
  98. package/templates/skills/oauth.skill.md +41 -0
  99. package/templates/skills/payment.skill.md +129 -0
  100. package/templates/skills/queue.skill.md +140 -0
  101. package/templates/skills/rate-limit.skill.md +38 -0
  102. package/templates/skills/sms.skill.md +39 -0
  103. package/templates/skills/storage.skill.md +42 -0
@@ -0,0 +1,22 @@
1
+ {
2
+ "compilerOptions": {
3
+ "lib": ["dom", "dom.iterable", "esnext"],
4
+ "allowJs": true,
5
+ "skipLibCheck": true,
6
+ "strict": true,
7
+ "noEmit": true,
8
+ "esModuleInterop": true,
9
+ "module": "esnext",
10
+ "moduleResolution": "bundler",
11
+ "resolveJsonModule": true,
12
+ "isolatedModules": true,
13
+ "jsx": "preserve",
14
+ "incremental": true,
15
+ "plugins": [{ "name": "next" }],
16
+ "paths": {
17
+ "@/*": ["./src/*"]
18
+ }
19
+ },
20
+ "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
21
+ "exclude": ["node_modules"]
22
+ }
@@ -0,0 +1,29 @@
1
+ # 应用配置
2
+ NODE_ENV=development
3
+ PORT=3000
4
+
5
+ # 数据库
6
+ DATABASE_URL="mysql://root:password@localhost:3306/mydb"
7
+
8
+ # Redis
9
+ REDIS_URL=redis://localhost:6379
10
+ # 或者分开配置
11
+ # REDIS_HOST=localhost
12
+ # REDIS_PORT=6379
13
+ # REDIS_PASSWORD=
14
+
15
+ # JWT
16
+ JWT_SECRET=your-jwt-secret-key-must-be-at-least-32-chars
17
+ JWT_EXPIRES_IN=7d
18
+
19
+ # 七牛云对象存储(可选)
20
+ # QINIU_ACCESS_KEY=your-access-key
21
+ # QINIU_SECRET_KEY=your-secret-key
22
+ # QINIU_BUCKET=your-bucket
23
+ # QINIU_REGION=z0
24
+ # QINIU_CDN_URL=https://cdn.example.com
25
+
26
+ # 阿里云短信(可选)
27
+ # ALIYUN_ACCESS_KEY_ID=your-access-key-id
28
+ # ALIYUN_ACCESS_KEY_SECRET=your-access-key-secret
29
+ # SMS_SIGN_NAME=your-sign-name
@@ -0,0 +1,8 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/nest-cli",
3
+ "collection": "@nestjs/schematics",
4
+ "sourceRoot": "src",
5
+ "compilerOptions": {
6
+ "deleteOutDir": true
7
+ }
8
+ }
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "{{ORG_NAME}}/backend",
3
+ "version": "1.0.0",
4
+ "description": "{{PROJECT_NAME}} 后端 API",
5
+ "prisma": {
6
+ "seed": "ts-node prisma/seed.ts"
7
+ },
8
+ "scripts": {
9
+ "build": "nest build",
10
+ "dev": "NODE_ENV=development nest start --watch",
11
+ "start": "NODE_ENV=production node dist/main",
12
+ "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
13
+ "test": "jest",
14
+ "type-check": "tsc --noEmit",
15
+ "prisma:generate": "prisma generate",
16
+ "prisma:migrate": "prisma migrate dev",
17
+ "prisma:migrate:deploy": "prisma migrate deploy",
18
+ "prisma:seed": "ts-node prisma/seed.ts",
19
+ "prisma:studio": "prisma studio",
20
+ "db:init": "pnpm prisma:generate && pnpm prisma:migrate && pnpm prisma:seed",
21
+ "clean": "rm -rf dist"
22
+ },
23
+ "dependencies": {
24
+ "@nestjs/common": "^10.3.0",
25
+ "@nestjs/config": "^3.1.1",
26
+ "@nestjs/core": "^10.3.0",
27
+ "@nestjs/jwt": "^10.2.0",
28
+ "@nestjs/passport": "^10.0.3",
29
+ "@nestjs/platform-express": "^10.3.0",
30
+ "@nestjs/swagger": "^7.1.17",
31
+ "@prisma/client": "^5.7.1",
32
+ "{{ORG_NAME}}/types": "workspace:*",
33
+ "bcrypt": "^5.1.1",
34
+ "class-transformer": "^0.5.1",
35
+ "class-validator": "^0.14.0",
36
+ "ioredis": "^5.3.2",
37
+ "passport": "^0.7.0",
38
+ "passport-jwt": "^4.0.1",
39
+ "reflect-metadata": "^0.2.1",
40
+ "rxjs": "^7.8.1"
41
+ },
42
+ "devDependencies": {
43
+ "@nestjs/cli": "^10.2.1",
44
+ "@nestjs/schematics": "^10.0.3",
45
+ "@nestjs/testing": "^10.2.10",
46
+ "@types/bcrypt": "^5.0.2",
47
+ "@types/express": "^4.17.21",
48
+ "@types/jest": "^29.5.11",
49
+ "@types/node": "^20.10.0",
50
+ "@types/passport-jwt": "^3.0.13",
51
+ "jest": "^29.7.0",
52
+ "prisma": "^5.7.0",
53
+ "ts-jest": "^29.1.1",
54
+ "ts-node": "^10.9.2",
55
+ "typescript": "^5.3.0"
56
+ }
57
+ }
@@ -0,0 +1,72 @@
1
+ generator client {
2
+ provider = "prisma-client-js"
3
+ }
4
+
5
+ datasource db {
6
+ provider = "mysql"
7
+ url = env("DATABASE_URL")
8
+ }
9
+
10
+ // 用户模型
11
+ model User {
12
+ id Int @id @default(autoincrement())
13
+ phone String @unique
14
+ password String
15
+ nickname String
16
+ avatar String?
17
+ email String?
18
+ bio String? @db.Text
19
+ role String @default("user") // user, admin, super_admin
20
+ status Int @default(1) // 0: 禁用, 1: 启用
21
+ lastLoginAt DateTime?
22
+ createdAt DateTime @default(now())
23
+ updatedAt DateTime @updatedAt
24
+
25
+ contents Content[]
26
+ comments Comment[]
27
+
28
+ @@index([phone])
29
+ @@index([role])
30
+ @@index([status])
31
+ @@map("users")
32
+ }
33
+
34
+ // 内容模型
35
+ model Content {
36
+ id Int @id @default(autoincrement())
37
+ title String
38
+ content String @db.Text
39
+ images String? @db.Text // JSON array
40
+ status String @default("draft") // draft, pending, published, rejected
41
+ viewCount Int @default(0)
42
+ likeCount Int @default(0)
43
+ commentCount Int @default(0)
44
+ authorId Int
45
+ author User @relation(fields: [authorId], references: [id], onDelete: Cascade)
46
+ createdAt DateTime @default(now())
47
+ updatedAt DateTime @updatedAt
48
+
49
+ comments Comment[]
50
+
51
+ @@index([authorId])
52
+ @@index([status])
53
+ @@index([createdAt])
54
+ @@map("contents")
55
+ }
56
+
57
+ // 评论模型
58
+ model Comment {
59
+ id Int @id @default(autoincrement())
60
+ content String @db.Text
61
+ contentId Int
62
+ content_ Content @relation(fields: [contentId], references: [id], onDelete: Cascade)
63
+ authorId Int
64
+ author User @relation(fields: [authorId], references: [id], onDelete: Cascade)
65
+ likeCount Int @default(0)
66
+ createdAt DateTime @default(now())
67
+ updatedAt DateTime @updatedAt
68
+
69
+ @@index([contentId])
70
+ @@index([authorId])
71
+ @@map("comments")
72
+ }
@@ -0,0 +1,32 @@
1
+ import { PrismaClient } from '@prisma/client';
2
+
3
+ const prisma = new PrismaClient();
4
+
5
+ async function main() {
6
+ console.log('🌱 开始初始化数据...');
7
+
8
+ // 创建管理员用户
9
+ const admin = await prisma.user.upsert({
10
+ where: { phone: '13800000000' },
11
+ update: {},
12
+ create: {
13
+ phone: '13800000000',
14
+ password: '$2b$10$example', // 需要用 bcrypt 生成
15
+ nickname: '管理员',
16
+ role: 'admin',
17
+ status: 1,
18
+ },
19
+ });
20
+
21
+ console.log('✅ 管理员用户:', admin);
22
+ console.log('🎉 数据初始化完成!');
23
+ }
24
+
25
+ main()
26
+ .catch((e) => {
27
+ console.error(e);
28
+ process.exit(1);
29
+ })
30
+ .finally(async () => {
31
+ await prisma.$disconnect();
32
+ });
@@ -0,0 +1,15 @@
1
+ import { Controller, Get } from '@nestjs/common';
2
+ import { ApiTags, ApiOperation } from '@nestjs/swagger';
3
+ import { AppService } from './app.service';
4
+
5
+ @ApiTags('健康检查')
6
+ @Controller()
7
+ export class AppController {
8
+ constructor(private readonly appService: AppService) {}
9
+
10
+ @Get()
11
+ @ApiOperation({ summary: '健康检查' })
12
+ getHealth() {
13
+ return this.appService.getHealth();
14
+ }
15
+ }
@@ -0,0 +1,85 @@
1
+ import { Module } from '@nestjs/common';
2
+ import { ConfigModule, ConfigService } from '@nestjs/config';
3
+ import { AppController } from './app.controller';
4
+ import { AppService } from './app.service';
5
+ import { PrismaModule } from './prisma/prisma.module';
6
+ import { AuthModule } from './auth/auth.module';
7
+ import { UserModule } from './user/user.module';
8
+
9
+ // Svton 基础设施包(按需启用)
10
+ // import { createZodValidate } from '@svton/nestjs-config-schema';
11
+ // import { HttpModule } from '@svton/nestjs-http';
12
+ // import { LoggerModule } from '@svton/nestjs-logger';
13
+ // import { RedisModule } from '@svton/nestjs-redis';
14
+ // import { AuthzModule } from '@svton/nestjs-authz';
15
+ // import { ObjectStorageModule } from '@svton/nestjs-object-storage';
16
+ // import { createQiniuAdapter } from '@svton/nestjs-object-storage-qiniu-kodo';
17
+ // import { envSchema } from './config/env.schema';
18
+
19
+ @Module({
20
+ imports: [
21
+ // 配置模块(可启用 schema 验证)
22
+ ConfigModule.forRoot({
23
+ isGlobal: true,
24
+ // validate: createZodValidate(envSchema),
25
+ }),
26
+
27
+ // HTTP 规范化(统一响应/异常格式)
28
+ // HttpModule.forRoot({
29
+ // successCode: 0,
30
+ // successMessage: 'success',
31
+ // excludePaths: ['/health', '/api-docs'],
32
+ // }),
33
+
34
+ // 日志模块
35
+ // LoggerModule.forRootAsync({
36
+ // imports: [ConfigModule],
37
+ // inject: [ConfigService],
38
+ // useFactory: (config: ConfigService) => ({
39
+ // appName: 'backend',
40
+ // env: config.get('NODE_ENV'),
41
+ // prettyPrint: config.get('NODE_ENV') !== 'production',
42
+ // }),
43
+ // }),
44
+
45
+ // Redis 模块
46
+ // RedisModule.forRootAsync({
47
+ // imports: [ConfigModule],
48
+ // inject: [ConfigService],
49
+ // useFactory: (config: ConfigService) => ({
50
+ // url: config.get('REDIS_URL'),
51
+ // keyPrefix: 'app:',
52
+ // }),
53
+ // }),
54
+
55
+ // RBAC 权限模块
56
+ // AuthzModule.forRoot({
57
+ // userRoleField: 'role',
58
+ // enableGlobalGuard: false,
59
+ // }),
60
+
61
+ // 对象存储模块(七牛云示例)
62
+ // ObjectStorageModule.forRootAsync({
63
+ // imports: [ConfigModule],
64
+ // inject: [ConfigService],
65
+ // useFactory: (config: ConfigService) => ({
66
+ // defaultBucket: config.get('QINIU_BUCKET'),
67
+ // publicBaseUrl: config.get('QINIU_CDN_URL'),
68
+ // adapter: createQiniuAdapter({
69
+ // accessKey: config.get('QINIU_ACCESS_KEY'),
70
+ // secretKey: config.get('QINIU_SECRET_KEY'),
71
+ // bucket: config.get('QINIU_BUCKET'),
72
+ // region: config.get('QINIU_REGION'),
73
+ // publicDomain: config.get('QINIU_CDN_URL'),
74
+ // }),
75
+ // }),
76
+ // }),
77
+
78
+ PrismaModule,
79
+ AuthModule,
80
+ UserModule,
81
+ ],
82
+ controllers: [AppController],
83
+ providers: [AppService],
84
+ })
85
+ export class AppModule {}
@@ -0,0 +1,12 @@
1
+ import { Injectable } from '@nestjs/common';
2
+
3
+ @Injectable()
4
+ export class AppService {
5
+ getHealth() {
6
+ return {
7
+ status: 'ok',
8
+ timestamp: new Date().toISOString(),
9
+ environment: process.env.NODE_ENV || 'development',
10
+ };
11
+ }
12
+ }
@@ -0,0 +1,31 @@
1
+ import { Controller, Post, Body, Get, UseGuards, Request } from '@nestjs/common';
2
+ import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
3
+ import { AuthService } from './auth.service';
4
+ import { JwtAuthGuard } from './jwt-auth.guard';
5
+ import type { LoginDto, RegisterDto } from '{{ORG_NAME}}/types';
6
+
7
+ @ApiTags('认证')
8
+ @Controller('auth')
9
+ export class AuthController {
10
+ constructor(private authService: AuthService) {}
11
+
12
+ @Post('login')
13
+ @ApiOperation({ summary: '登录' })
14
+ async login(@Body() dto: LoginDto) {
15
+ return this.authService.login(dto);
16
+ }
17
+
18
+ @Post('register')
19
+ @ApiOperation({ summary: '注册' })
20
+ async register(@Body() dto: RegisterDto) {
21
+ return this.authService.register(dto);
22
+ }
23
+
24
+ @Get('me')
25
+ @UseGuards(JwtAuthGuard)
26
+ @ApiBearerAuth()
27
+ @ApiOperation({ summary: '获取当前用户信息' })
28
+ async getProfile(@Request() req: any) {
29
+ return this.authService.validateUser(req.user.sub);
30
+ }
31
+ }
@@ -0,0 +1,27 @@
1
+ import { Module } from '@nestjs/common';
2
+ import { JwtModule } from '@nestjs/jwt';
3
+ import { PassportModule } from '@nestjs/passport';
4
+ import { ConfigModule, ConfigService } from '@nestjs/config';
5
+ import { AuthController } from './auth.controller';
6
+ import { AuthService } from './auth.service';
7
+ import { JwtStrategy } from './jwt.strategy';
8
+ import { PrismaModule } from '../prisma/prisma.module';
9
+
10
+ @Module({
11
+ imports: [
12
+ PrismaModule,
13
+ PassportModule.register({ defaultStrategy: 'jwt' }),
14
+ JwtModule.registerAsync({
15
+ imports: [ConfigModule],
16
+ useFactory: async (configService: ConfigService) => ({
17
+ secret: configService.get<string>('JWT_SECRET'),
18
+ signOptions: { expiresIn: '7d' },
19
+ }),
20
+ inject: [ConfigService],
21
+ }),
22
+ ],
23
+ controllers: [AuthController],
24
+ providers: [AuthService, JwtStrategy],
25
+ exports: [AuthService],
26
+ })
27
+ export class AuthModule {}
@@ -0,0 +1,89 @@
1
+ import { Injectable, UnauthorizedException, ConflictException } from '@nestjs/common';
2
+ import { JwtService } from '@nestjs/jwt';
3
+ import * as bcrypt from 'bcrypt';
4
+ import { PrismaService } from '../prisma/prisma.service';
5
+ import type { LoginDto, RegisterDto, LoginVo } from '{{ORG_NAME}}/types';
6
+
7
+ @Injectable()
8
+ export class AuthService {
9
+ constructor(
10
+ private prisma: PrismaService,
11
+ private jwtService: JwtService,
12
+ ) {}
13
+
14
+ async login(dto: LoginDto): Promise<LoginVo> {
15
+ const user = await this.prisma.user.findUnique({
16
+ where: { phone: dto.phone },
17
+ });
18
+
19
+ if (!user) {
20
+ throw new UnauthorizedException('用户不存在');
21
+ }
22
+
23
+ const isPasswordValid = await bcrypt.compare(dto.password, user.password);
24
+ if (!isPasswordValid) {
25
+ throw new UnauthorizedException('密码错误');
26
+ }
27
+
28
+ if (user.status === 0) {
29
+ throw new UnauthorizedException('账号已被禁用');
30
+ }
31
+
32
+ const payload = { sub: user.id, phone: user.phone, role: user.role };
33
+ const accessToken = this.jwtService.sign(payload);
34
+ const refreshToken = this.jwtService.sign(payload, { expiresIn: '30d' });
35
+
36
+ return {
37
+ accessToken,
38
+ refreshToken,
39
+ user: {
40
+ id: user.id,
41
+ phone: user.phone,
42
+ nickname: user.nickname,
43
+ avatar: user.avatar,
44
+ role: user.role as any,
45
+ status: user.status as any,
46
+ createdAt: user.createdAt.toISOString(),
47
+ updatedAt: user.updatedAt.toISOString(),
48
+ },
49
+ };
50
+ }
51
+
52
+ async register(dto: RegisterDto): Promise<LoginVo> {
53
+ const existingUser = await this.prisma.user.findUnique({
54
+ where: { phone: dto.phone },
55
+ });
56
+
57
+ if (existingUser) {
58
+ throw new ConflictException('手机号已被注册');
59
+ }
60
+
61
+ const hashedPassword = await bcrypt.hash(dto.password, 10);
62
+
63
+ const user = await this.prisma.user.create({
64
+ data: {
65
+ phone: dto.phone,
66
+ password: hashedPassword,
67
+ nickname: dto.nickname,
68
+ role: 'user',
69
+ status: 1,
70
+ },
71
+ });
72
+
73
+ return this.login({ phone: dto.phone, password: dto.password });
74
+ }
75
+
76
+ async validateUser(userId: number) {
77
+ return this.prisma.user.findUnique({
78
+ where: { id: userId },
79
+ select: {
80
+ id: true,
81
+ phone: true,
82
+ nickname: true,
83
+ avatar: true,
84
+ role: true,
85
+ status: true,
86
+ },
87
+ });
88
+ }
89
+ }
@@ -0,0 +1,5 @@
1
+ import { Injectable } from '@nestjs/common';
2
+ import { AuthGuard } from '@nestjs/passport';
3
+
4
+ @Injectable()
5
+ export class JwtAuthGuard extends AuthGuard('jwt') {}
@@ -0,0 +1,27 @@
1
+ import { Injectable, UnauthorizedException } from '@nestjs/common';
2
+ import { PassportStrategy } from '@nestjs/passport';
3
+ import { ExtractJwt, Strategy } from 'passport-jwt';
4
+ import { ConfigService } from '@nestjs/config';
5
+ import { AuthService } from './auth.service';
6
+
7
+ @Injectable()
8
+ export class JwtStrategy extends PassportStrategy(Strategy) {
9
+ constructor(
10
+ private configService: ConfigService,
11
+ private authService: AuthService,
12
+ ) {
13
+ super({
14
+ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
15
+ ignoreExpiration: false,
16
+ secretOrKey: configService.get<string>('JWT_SECRET'),
17
+ });
18
+ }
19
+
20
+ async validate(payload: any) {
21
+ const user = await this.authService.validateUser(payload.sub);
22
+ if (!user) {
23
+ throw new UnauthorizedException();
24
+ }
25
+ return { sub: payload.sub, phone: payload.phone, role: payload.role };
26
+ }
27
+ }
@@ -0,0 +1,35 @@
1
+ import { z } from 'zod';
2
+
3
+ /**
4
+ * 环境变量 Schema
5
+ * 使用 @svton/nestjs-config-schema 进行验证
6
+ */
7
+ export const envSchema = z.object({
8
+ // 应用配置
9
+ NODE_ENV: z.enum(['development', 'production', 'test']).default('development'),
10
+ PORT: z.coerce.number().default(3000),
11
+
12
+ // 数据库
13
+ DATABASE_URL: z.string().url(),
14
+
15
+ // Redis(可选)
16
+ REDIS_URL: z.string().url().optional(),
17
+
18
+ // JWT
19
+ JWT_SECRET: z.string().min(32),
20
+ JWT_EXPIRES_IN: z.string().default('7d'),
21
+
22
+ // 七牛云对象存储(可选)
23
+ QINIU_ACCESS_KEY: z.string().optional(),
24
+ QINIU_SECRET_KEY: z.string().optional(),
25
+ QINIU_BUCKET: z.string().optional(),
26
+ QINIU_REGION: z.string().optional(),
27
+ QINIU_CDN_URL: z.string().url().optional(),
28
+
29
+ // 阿里云短信(可选)
30
+ ALIYUN_ACCESS_KEY_ID: z.string().optional(),
31
+ ALIYUN_ACCESS_KEY_SECRET: z.string().optional(),
32
+ SMS_SIGN_NAME: z.string().optional(),
33
+ });
34
+
35
+ export type EnvConfig = z.infer<typeof envSchema>;
@@ -0,0 +1,51 @@
1
+ import { NestFactory } from '@nestjs/core';
2
+ import { ValidationPipe } from '@nestjs/common';
3
+ import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
4
+ import * as express from 'express';
5
+ import { AppModule } from './app.module';
6
+
7
+ async function bootstrap() {
8
+ const app = await NestFactory.create(AppModule, {
9
+ // 如果使用 @svton/nestjs-logger,启用 bufferLogs
10
+ // bufferLogs: true,
11
+ });
12
+
13
+ // 如果使用 @svton/nestjs-logger,设置 logger
14
+ // app.useLogger(app.get(Logger));
15
+
16
+ // Raw body 中间件(用于对象存储回调验签)
17
+ // 仅对特定路径启用,避免影响全局 JSON 解析
18
+ app.use('/object-storage/callback', express.raw({ type: '*/*' }));
19
+
20
+ // 全局验证管道
21
+ app.useGlobalPipes(
22
+ new ValidationPipe({
23
+ whitelist: true,
24
+ transform: true,
25
+ forbidNonWhitelisted: true,
26
+ }),
27
+ );
28
+
29
+ // CORS
30
+ app.enableCors({
31
+ origin: true,
32
+ credentials: true,
33
+ });
34
+
35
+ // Swagger 文档
36
+ const config = new DocumentBuilder()
37
+ .setTitle('API 文档')
38
+ .setDescription('项目 API 接口文档')
39
+ .setVersion('1.0')
40
+ .addBearerAuth()
41
+ .build();
42
+ const document = SwaggerModule.createDocument(app, config);
43
+ SwaggerModule.setup('api-docs', app, document);
44
+
45
+ const port = process.env.PORT || 3000;
46
+ await app.listen(port);
47
+ console.log(`🚀 Server running on http://localhost:${port}`);
48
+ console.log(`📚 API Docs: http://localhost:${port}/api-docs`);
49
+ }
50
+
51
+ bootstrap();