create-dp-koa 1.0.0
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.
- package/README.md +50 -0
- package/index.mjs +97 -0
- package/package.json +33 -0
- package/template/.env.development +9 -0
- package/template/.env.production +12 -0
- package/template/.github/workflows/ci-cd.yml +182 -0
- package/template/.trae/documents/controller_development_plan.md +386 -0
- package/template/.trae/skills/00-backend-core.skill.md +50 -0
- package/template/.trae/skills/01-backend-skill-router.skill.md +55 -0
- package/template/.trae/skills/10-backend-api.skill.md +54 -0
- package/template/.trae/skills/11-backend-controller-recipes.skill.md +107 -0
- package/template/.trae/skills/20-backend-repository.skill.md +25 -0
- package/template/.trae/skills/21-backend-service.skill.md +135 -0
- package/template/.trae/skills/25-backend-comments-and-doc.skill.md +97 -0
- package/template/.trae/skills/30-backend-validation.skill.md +320 -0
- package/template/.trae/skills/40-backend-error-logging.skill.md +21 -0
- package/template/.trae/skills/50-backend-bootstrap-lifecycle.skill.md +90 -0
- package/template/.trae/skills/60-backend-router-registration.skill.md +71 -0
- package/template/.trae/skills/70-backend-middleware.skill.md +98 -0
- package/template/.trae/skills/80-backend-utils-and-libs.skill.md +90 -0
- package/template/.trae/skills/85-backend-plugins.rule.md +64 -0
- package/template/.trae/skills/90-backend-testing.skill.md +29 -0
- package/template/.trae/skills/README.md +49 -0
- package/template/.vscode/launch.json +38 -0
- package/template/.vscode/settings.json +1 -0
- package/template/Dockerfile +36 -0
- package/template/README.md +229 -0
- package/template/docker-compose.yml +135 -0
- package/template/docs/API_DOCUMENTATION.md +837 -0
- package/template/docs/ARCHITECTURE_REFACTOR.md +109 -0
- package/template/docs/CACHE_MIGRATION_GUIDE.md +142 -0
- package/template/docs/DEPLOYMENT_GUIDE.md +1062 -0
- package/template/docs/DEVELOPMENT_GUIDE.md +1097 -0
- package/template/docs/DOCUMENTATION_CLEANUP_REPORT.md +166 -0
- package/template/docs/DOCUMENTATION_COMPLETION_REPORT.md +223 -0
- package/template/docs/DOCUMENTATION_INDEX.md +294 -0
- package/template/docs/DOCUMENTATION_STRUCTURE.md +221 -0
- package/template/docs/ENTERPRISE_ANNOTATION_SYSTEM_GUIDE.md +2069 -0
- package/template/docs/ENTERPRISE_DATABASE_ARCHITECTURE.md +318 -0
- package/template/docs/ENTERPRISE_DEPLOYMENT_GUIDE.md +547 -0
- package/template/docs/ENTERPRISE_ERROR_HANDLING_GUIDE.md +357 -0
- package/template/docs/ENTERPRISE_LOGGING_SYSTEM_GUIDE.md +494 -0
- package/template/docs/ENVIRONMENT_CONFIG_EXAMPLE.md +69 -0
- package/template/docs/FINAL_IMPLEMENTATION_SUMMARY.md +206 -0
- package/template/docs/HEALTH_CHECK_ROUTE_FIX.md +134 -0
- package/template/docs/IMPLEMENTATION_CHECKLIST.md +204 -0
- package/template/docs/INSTALLATION_GUIDE.md +611 -0
- package/template/docs/INTERCEPTOR_TESTING_REPORT.md +226 -0
- package/template/docs/INTERCEPTOR_TESTING_SCRIPTS.md +143 -0
- package/template/docs/LOGGING_OPTIMIZATION_GUIDE.md +126 -0
- package/template/docs/MEMORY_DATABASE_GUIDE.md +212 -0
- package/template/docs/NEW_ROUTER_INTEGRATION_GUIDE.md +345 -0
- package/template/docs/NEW_ROUTER_INTEGRATION_SUMMARY.md +259 -0
- package/template/docs/NEW_ROUTER_USAGE_GUIDE.md +364 -0
- package/template/docs/QUICK_START.md +268 -0
- package/template/docs/ROUTE_SLASH_COMPATIBILITY_FIX.md +191 -0
- package/template/docs/SERVICE_INTERCEPTOR_GUIDE.md +243 -0
- package/template/docs/SERVICE_LAYER_INDEX.md +205 -0
- package/template/docs/SERVICE_PATTERN_GUIDE.md +270 -0
- package/template/docs/SERVICE_RETURN_VALUE_SPECIFICATION.md +466 -0
- package/template/docs/SWAGGER_DEBUG_MODE_GUIDE.md +80 -0
- package/template/docs/SWAGGER_INTEGRATION_GUIDE.md +416 -0
- package/template/docs/TRANSACTION_MANAGER_USAGE.md +360 -0
- package/template/docs/TROUBLESHOOTING.md +869 -0
- package/template/env.production.example +62 -0
- package/template/jest.config.js +34 -0
- package/template/package-lock.json +13240 -0
- package/template/package.json +119 -0
- package/template/patches/typeorm+0.3.25.patch +22 -0
- package/template/scripts/sync-template.mjs +84 -0
- package/template/scripts/test-annotation-system.sh +48 -0
- package/template/scripts/test-core-functionality.sh +28 -0
- package/template/src/annotations/decorators/ConfigManagement.ts +9 -0
- package/template/src/annotations/decorators/DistributedTracing.ts +9 -0
- package/template/src/annotations/decorators/EnterprisePerformance.ts +9 -0
- package/template/src/annotations/decorators/PerformanceMonitor.ts +32 -0
- package/template/src/annotations/decorators/SecurityAudit.ts +9 -0
- package/template/src/annotations/index.ts +50 -0
- package/template/src/annotations/processors/ConfigManagementProcessor.ts +369 -0
- package/template/src/annotations/processors/DistributedTracingProcessor.ts +288 -0
- package/template/src/annotations/processors/EnterprisePerformanceProcessor.ts +189 -0
- package/template/src/annotations/processors/PerformanceMonitorProcessor.ts +101 -0
- package/template/src/annotations/processors/SecurityAuditProcessor.ts +345 -0
- package/template/src/annotations/processors/SwaggerProcessor.ts +612 -0
- package/template/src/annotations/processors/index.ts +10 -0
- package/template/src/app.ts +123 -0
- package/template/src/controllers/base.controller.ts +41 -0
- package/template/src/controllers/cacheManagement.controller.ts +131 -0
- package/template/src/controllers/captcha.controller.ts +57 -0
- package/template/src/controllers/demo/AnnotationDemoController.ts +118 -0
- package/template/src/controllers/example/EnterpriseExampleController.ts +297 -0
- package/template/src/controllers/example/ExampleController.ts +110 -0
- package/template/src/controllers/example/NewAnnotationExampleController.ts +159 -0
- package/template/src/controllers/example/SwaggerExampleController.ts +205 -0
- package/template/src/controllers/example/TransactionExample.controller.ts +336 -0
- package/template/src/controllers/health.controller.ts +235 -0
- package/template/src/controllers/home/register.controller.ts +58 -0
- package/template/src/controllers/home/ytGoods.controller.ts +92 -0
- package/template/src/controllers/home/ytShop.controller.ts +135 -0
- package/template/src/controllers/home/ytUser.controller.ts +89 -0
- package/template/src/controllers/logManagement.controller.ts +396 -0
- package/template/src/controllers/public/emailSend.controller.ts +65 -0
- package/template/src/controllers/public/ytUserAuth.controller.ts +174 -0
- package/template/src/controllers/testData.controller.ts +253 -0
- package/template/src/dto/controller/example/NewAnnotationExampleController.dto.ts +73 -0
- package/template/src/dto/controller/home/emailSend.controller.dto.ts +40 -0
- package/template/src/dto/controller/home/register.controller.dto.ts +45 -0
- package/template/src/dto/controller/home/ytGoods.controller.dto.ts +55 -0
- package/template/src/dto/controller/home/ytShop.controller.dto.ts +69 -0
- package/template/src/dto/controller/home/ytUser.controller.dto.ts +44 -0
- package/template/src/dto/controller/public/ytUserAuth.controller.dto.ts +63 -0
- package/template/src/dto/goods.dto.ts +212 -0
- package/template/src/dto/service/ytService.dto.ts +13 -0
- package/template/src/dto/user.dto.ts +177 -0
- package/template/src/entity/base.entity.ts +13 -0
- package/template/src/entity/columnTypes.ts +13 -0
- package/template/src/entity/goodsImagesUnlockKey.entity.ts +33 -0
- package/template/src/entity/goodsUnlocker.entity.ts +34 -0
- package/template/src/entity/index.ts +15 -0
- package/template/src/entity/shop.entity.ts +52 -0
- package/template/src/entity/shopUser.entity.ts +41 -0
- package/template/src/entity/ytGoods.entity.ts +94 -0
- package/template/src/entity/ytUser.entity.ts +96 -0
- package/template/src/examples/InterceptorExampleRunner.ts +284 -0
- package/template/src/examples/ServiceInterceptorExample.ts +214 -0
- package/template/src/examples/SwaggerProcessorExample.ts +169 -0
- package/template/src/examples/TransactionManagerDemo.ts +377 -0
- package/template/src/examples/cacheExamples.ts +155 -0
- package/template/src/framework/decorator/controller.ts +311 -0
- package/template/src/framework/decorator/processor/AnnotationDecorators.ts +100 -0
- package/template/src/framework/decorator/processor/AnnotationProcessor.ts +156 -0
- package/template/src/framework/decorator/processor/AnnotationProcessorConfig.ts +45 -0
- package/template/src/framework/decorator/processor/AnnotationRegistry.ts +117 -0
- package/template/src/framework/decorator/processor/AnnotationSystemInitializer.ts +95 -0
- package/template/src/framework/decorator/processor/ProcessorManager.ts +76 -0
- package/template/src/framework/decorator/processor/processors/CustomProcessors.ts +126 -0
- package/template/src/framework/decorator/processor/processors/DefaultProcessors.ts +207 -0
- package/template/src/framework/decorator/refactored/DecoratorFactory.ts +99 -0
- package/template/src/framework/decorator/refactored/DecoratorMetadataManager.ts +125 -0
- package/template/src/framework/decorator/refactored/DecoratorValidator.ts +128 -0
- package/template/src/framework/decorator/refactored/TypeSafeDecorators.ts +139 -0
- package/template/src/framework/decorator/refactored/index.ts +98 -0
- package/template/src/framework/decorator/swagger.ts +150 -0
- package/template/src/framework/interceptors/AdvancedServiceCallInterceptor.ts +375 -0
- package/template/src/framework/interceptors/ServiceCallInterceptor.ts +348 -0
- package/template/src/framework/interceptors/index.ts +19 -0
- package/template/src/framework/plugins/registry.ts +63 -0
- package/template/src/framework/plugins/types.ts +15 -0
- package/template/src/framework/types/ServiceResult.ts +151 -0
- package/template/src/framework/types/index.ts +16 -0
- package/template/src/framework/utils/CacheManager.ts +430 -0
- package/template/src/framework/utils/CacheService.ts +248 -0
- package/template/src/framework/utils/DtoValidator.ts +164 -0
- package/template/src/framework/utils/MigrationHelper.ts +179 -0
- package/template/src/framework/utils/MigrationManager.ts +256 -0
- package/template/src/framework/utils/NewRouter.ts +207 -0
- package/template/src/framework/utils/TransactionManager.ts +172 -0
- package/template/src/framework/utils/bootstrap.ts +445 -0
- package/template/src/framework/utils/cache.ts +269 -0
- package/template/src/framework/utils/databaseConfig.ts +148 -0
- package/template/src/framework/utils/db.ts +39 -0
- package/template/src/framework/utils/dbMonitor.ts +106 -0
- package/template/src/framework/utils/dynamicSwagger.ts +410 -0
- package/template/src/framework/utils/function.ts +61 -0
- package/template/src/framework/utils/gracefulShutdown.ts +131 -0
- package/template/src/framework/utils/logger.ts +388 -0
- package/template/src/framework/utils/metrics.ts +182 -0
- package/template/src/framework/utils/router.ts +417 -0
- package/template/src/framework/utils/swagger.ts +184 -0
- package/template/src/framework/utils/testDb.ts +19 -0
- package/template/src/framework/utils/token.ts +23 -0
- package/template/src/framework/utils/transform.ts +17 -0
- package/template/src/libs/aokEmailSender.ts +42 -0
- package/template/src/libs/captcha.ts +37 -0
- package/template/src/libs/cos.ts +45 -0
- package/template/src/libs/mCache.ts +7 -0
- package/template/src/libs/serviceValidate.ts +3 -0
- package/template/src/libs/tecentSms.ts +51 -0
- package/template/src/middlewares/a.middleware.ts +6 -0
- package/template/src/middlewares/error.middleware.ts +14 -0
- package/template/src/middlewares/logging.middleware.ts +187 -0
- package/template/src/middlewares/static.middleware.ts +79 -0
- package/template/src/middlewares/swagger.middleware.ts +70 -0
- package/template/src/middlewares/token.middleware.ts +32 -0
- package/template/src/migrations/1700000000000-InitialDatabaseStructure.ts +172 -0
- package/template/src/migrations/index.ts +6 -0
- package/template/src/plugins/weboffice/core/context.ts +47 -0
- package/template/src/plugins/weboffice/core/errors.ts +51 -0
- package/template/src/plugins/weboffice/core/types.ts +63 -0
- package/template/src/plugins/weboffice/core/utils.ts +7 -0
- package/template/src/plugins/weboffice/entities/index.ts +3 -0
- package/template/src/plugins/weboffice/entities/webofficeFile.entity.ts +28 -0
- package/template/src/plugins/weboffice/entities/webofficeFileVersion.entity.ts +29 -0
- package/template/src/plugins/weboffice/http/routes.ts +179 -0
- package/template/src/plugins/weboffice/index.ts +23 -0
- package/template/src/plugins/weboffice/services/webofficeCallback.service.ts +274 -0
- package/template/src/repository/UserRepository.ts +122 -0
- package/template/src/repository/base/BaseRepository.ts +124 -0
- package/template/src/repository/interfaces/IBaseRepository.ts +67 -0
- package/template/src/routers/index.ts +49 -0
- package/template/src/service/base.service.ts +116 -0
- package/template/src/service/paramValidateTest.service.ts +139 -0
- package/template/src/service/ytGoods.service.ts +42 -0
- package/template/src/service/ytShop.service.ts +90 -0
- package/template/src/service/ytUser.service.ts +451 -0
- package/template/src/test/swaggerParameterTest.ts +90 -0
- package/template/src/utils/testDataInitializer.ts +296 -0
- package/template/static/output.json +15203 -0
- package/template/test/controllers/controllers.test.ts +173 -0
- package/template/test/controllers/example/ExampleController.test.ts +222 -0
- package/template/test/controllers/example/NewAnnotationExampleController.test.ts +200 -0
- package/template/test/framework/TransactionManagerDemo.test.ts +363 -0
- package/template/test/framework/annotation/AnnotationDecorators.test.ts +222 -0
- package/template/test/framework/annotation/AnnotationExecutor.test.ts +246 -0
- package/template/test/framework/annotation/AnnotationProcessor.test.ts +179 -0
- package/template/test/framework/annotation/CustomProcessors.test.ts +313 -0
- package/template/test/framework/annotation/DefaultProcessors.test.ts +371 -0
- package/template/test/framework/annotation/NewRouter.test.ts +272 -0
- package/template/test/framework/annotation/ProcessorManager.test.ts +248 -0
- package/template/test/framework/annotation/setup.ts +26 -0
- package/template/test/framework/cache.test.ts +101 -0
- package/template/test/framework/databaseConfig.test.ts +142 -0
- package/template/test/integration/integration.test.ts +153 -0
- package/template/test/plugins/weboffice/http.routes.int.test.ts +61 -0
- package/template/test/service/business.test.ts +87 -0
- package/template/test/service/paramValidateTest.service.test.ts +184 -0
- package/template/test/service/ytUser.service.test.ts +566 -0
- package/template/test/setup.ts +20 -0
- package/template/test/setupAfterEnv.ts +14 -0
- package/template/test/utils/testHelpers.ts +220 -0
- package/template/test_output.txt +0 -0
- package/template/tsconfig.build.json +17 -0
- package/template/tsconfig.json +31 -0
- package/template/webpack.config.js +71 -0
- package/template/yarn.lock +7354 -0
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
import { Context } from 'koa'
|
|
2
|
+
import { logger } from '@src/framework/utils/logger'
|
|
3
|
+
import { BaseController } from '@src/controllers/base.controller'
|
|
4
|
+
import fs from 'fs'
|
|
5
|
+
import path from 'path'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 日志管理控制器
|
|
9
|
+
* 提供日志查询、下载、清理等功能
|
|
10
|
+
*/
|
|
11
|
+
export class LogManagementController extends BaseController {
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 获取日志统计信息
|
|
15
|
+
*/
|
|
16
|
+
async getLogStats(ctx: Context) {
|
|
17
|
+
try {
|
|
18
|
+
const logsDir = path.join(process.cwd(), 'logs')
|
|
19
|
+
const stats = {
|
|
20
|
+
totalFiles: 0,
|
|
21
|
+
totalSize: 0,
|
|
22
|
+
fileTypes: {} as Record<string, { count: number, size: number }>,
|
|
23
|
+
lastModified: null as Date | null
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (fs.existsSync(logsDir)) {
|
|
27
|
+
const files = fs.readdirSync(logsDir)
|
|
28
|
+
|
|
29
|
+
for (const file of files) {
|
|
30
|
+
const filePath = path.join(logsDir, file)
|
|
31
|
+
const fileStat = fs.statSync(filePath)
|
|
32
|
+
|
|
33
|
+
if (fileStat.isFile()) {
|
|
34
|
+
stats.totalFiles++
|
|
35
|
+
stats.totalSize += fileStat.size
|
|
36
|
+
|
|
37
|
+
const ext = path.extname(file) || 'log'
|
|
38
|
+
if (!stats.fileTypes[ext]) {
|
|
39
|
+
stats.fileTypes[ext] = { count: 0, size: 0 }
|
|
40
|
+
}
|
|
41
|
+
stats.fileTypes[ext].count++
|
|
42
|
+
stats.fileTypes[ext].size += fileStat.size
|
|
43
|
+
|
|
44
|
+
if (!stats.lastModified || fileStat.mtime > stats.lastModified) {
|
|
45
|
+
stats.lastModified = fileStat.mtime
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
logger.info('Log stats retrieved', { stats })
|
|
52
|
+
|
|
53
|
+
ctx.body = {
|
|
54
|
+
code: 0,
|
|
55
|
+
data: stats,
|
|
56
|
+
message: '日志统计信息获取成功'
|
|
57
|
+
}
|
|
58
|
+
} catch (error) {
|
|
59
|
+
logger.error('Failed to get log stats', error as Error)
|
|
60
|
+
ctx.status = 500
|
|
61
|
+
ctx.body = {
|
|
62
|
+
code: 500,
|
|
63
|
+
message: '获取日志统计信息失败'
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* 获取日志文件列表
|
|
70
|
+
*/
|
|
71
|
+
async getLogFiles(ctx: Context) {
|
|
72
|
+
try {
|
|
73
|
+
const logsDir = path.join(process.cwd(), 'logs')
|
|
74
|
+
const files: any[] = []
|
|
75
|
+
|
|
76
|
+
if (fs.existsSync(logsDir)) {
|
|
77
|
+
const fileList = fs.readdirSync(logsDir)
|
|
78
|
+
|
|
79
|
+
for (const file of fileList) {
|
|
80
|
+
const filePath = path.join(logsDir, file)
|
|
81
|
+
const fileStat = fs.statSync(filePath)
|
|
82
|
+
|
|
83
|
+
if (fileStat.isFile()) {
|
|
84
|
+
files.push({
|
|
85
|
+
name: file,
|
|
86
|
+
size: fileStat.size,
|
|
87
|
+
modified: fileStat.mtime,
|
|
88
|
+
created: fileStat.birthtime,
|
|
89
|
+
type: path.extname(file) || 'log'
|
|
90
|
+
})
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// 按修改时间排序
|
|
96
|
+
files.sort((a, b) => b.modified.getTime() - a.modified.getTime())
|
|
97
|
+
|
|
98
|
+
logger.info('Log files list retrieved', { fileCount: files.length })
|
|
99
|
+
|
|
100
|
+
ctx.body = {
|
|
101
|
+
code: 0,
|
|
102
|
+
data: files,
|
|
103
|
+
message: '日志文件列表获取成功'
|
|
104
|
+
}
|
|
105
|
+
} catch (error) {
|
|
106
|
+
logger.error('Failed to get log files', error as Error)
|
|
107
|
+
ctx.status = 500
|
|
108
|
+
ctx.body = {
|
|
109
|
+
code: 500,
|
|
110
|
+
message: '获取日志文件列表失败'
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 下载日志文件
|
|
117
|
+
*/
|
|
118
|
+
async downloadLogFile(ctx: Context) {
|
|
119
|
+
try {
|
|
120
|
+
const { filename } = ctx.params
|
|
121
|
+
|
|
122
|
+
if (!filename || !filename.match(/^[a-zA-Z0-9._-]+$/)) {
|
|
123
|
+
ctx.status = 400
|
|
124
|
+
ctx.body = {
|
|
125
|
+
code: 400,
|
|
126
|
+
message: '无效的文件名'
|
|
127
|
+
}
|
|
128
|
+
return
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const filePath = path.join(process.cwd(), 'logs', filename)
|
|
132
|
+
|
|
133
|
+
if (!fs.existsSync(filePath)) {
|
|
134
|
+
ctx.status = 404
|
|
135
|
+
ctx.body = {
|
|
136
|
+
code: 404,
|
|
137
|
+
message: '日志文件不存在'
|
|
138
|
+
}
|
|
139
|
+
return
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// 审计日志
|
|
143
|
+
logger.audit('LOG_DOWNLOAD', filename, {
|
|
144
|
+
userId: ctx.state.user?.id,
|
|
145
|
+
ip: ctx.ip,
|
|
146
|
+
userAgent: ctx.get('User-Agent')
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
ctx.set('Content-Type', 'application/octet-stream')
|
|
150
|
+
ctx.set('Content-Disposition', `attachment; filename="${filename}"`)
|
|
151
|
+
ctx.body = fs.createReadStream(filePath)
|
|
152
|
+
|
|
153
|
+
} catch (error) {
|
|
154
|
+
logger.error('Failed to download log file', error as Error, {
|
|
155
|
+
filename: ctx.params.filename
|
|
156
|
+
})
|
|
157
|
+
ctx.status = 500
|
|
158
|
+
ctx.body = {
|
|
159
|
+
code: 500,
|
|
160
|
+
message: '下载日志文件失败'
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* 查看日志内容
|
|
167
|
+
*/
|
|
168
|
+
async viewLogContent(ctx: Context) {
|
|
169
|
+
try {
|
|
170
|
+
const { filename } = ctx.params
|
|
171
|
+
const { lines = 100, offset = 0 } = ctx.query
|
|
172
|
+
|
|
173
|
+
if (!filename || !filename.match(/^[a-zA-Z0-9._-]+$/)) {
|
|
174
|
+
ctx.status = 400
|
|
175
|
+
ctx.body = {
|
|
176
|
+
code: 400,
|
|
177
|
+
message: '无效的文件名'
|
|
178
|
+
}
|
|
179
|
+
return
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const filePath = path.join(process.cwd(), 'logs', filename)
|
|
183
|
+
|
|
184
|
+
if (!fs.existsSync(filePath)) {
|
|
185
|
+
ctx.status = 404
|
|
186
|
+
ctx.body = {
|
|
187
|
+
code: 404,
|
|
188
|
+
message: '日志文件不存在'
|
|
189
|
+
}
|
|
190
|
+
return
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const content = fs.readFileSync(filePath, 'utf8')
|
|
194
|
+
const linesArray = content.split('\n')
|
|
195
|
+
const startLine = Math.max(0, Number(offset))
|
|
196
|
+
const endLine = Math.min(linesArray.length, startLine + Number(lines))
|
|
197
|
+
|
|
198
|
+
const selectedLines = linesArray.slice(startLine, endLine)
|
|
199
|
+
|
|
200
|
+
// 审计日志
|
|
201
|
+
logger.audit('LOG_VIEW', filename, {
|
|
202
|
+
userId: ctx.state.user?.id,
|
|
203
|
+
lines: Number(lines),
|
|
204
|
+
offset: Number(offset),
|
|
205
|
+
ip: ctx.ip
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
ctx.body = {
|
|
209
|
+
code: 0,
|
|
210
|
+
data: {
|
|
211
|
+
filename,
|
|
212
|
+
totalLines: linesArray.length,
|
|
213
|
+
selectedLines: selectedLines,
|
|
214
|
+
startLine,
|
|
215
|
+
endLine,
|
|
216
|
+
hasMore: endLine < linesArray.length
|
|
217
|
+
},
|
|
218
|
+
message: '日志内容获取成功'
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
} catch (error) {
|
|
222
|
+
logger.error('Failed to view log content', error as Error, {
|
|
223
|
+
filename: ctx.params.filename
|
|
224
|
+
})
|
|
225
|
+
ctx.status = 500
|
|
226
|
+
ctx.body = {
|
|
227
|
+
code: 500,
|
|
228
|
+
message: '查看日志内容失败'
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* 清理过期日志
|
|
235
|
+
*/
|
|
236
|
+
async cleanExpiredLogs(ctx: Context) {
|
|
237
|
+
try {
|
|
238
|
+
const { days = 30 } = ctx.request.body
|
|
239
|
+
const logsDir = path.join(process.cwd(), 'logs')
|
|
240
|
+
const cutoffDate = new Date()
|
|
241
|
+
cutoffDate.setDate(cutoffDate.getDate() - Number(days))
|
|
242
|
+
|
|
243
|
+
let deletedCount = 0
|
|
244
|
+
let deletedSize = 0
|
|
245
|
+
|
|
246
|
+
if (fs.existsSync(logsDir)) {
|
|
247
|
+
const files = fs.readdirSync(logsDir)
|
|
248
|
+
|
|
249
|
+
for (const file of files) {
|
|
250
|
+
const filePath = path.join(logsDir, file)
|
|
251
|
+
const fileStat = fs.statSync(filePath)
|
|
252
|
+
|
|
253
|
+
if (fileStat.isFile() && fileStat.mtime < cutoffDate) {
|
|
254
|
+
deletedSize += fileStat.size
|
|
255
|
+
fs.unlinkSync(filePath)
|
|
256
|
+
deletedCount++
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// 审计日志
|
|
262
|
+
logger.audit('LOG_CLEANUP', 'expired_logs', {
|
|
263
|
+
userId: ctx.state.user?.id,
|
|
264
|
+
days: Number(days),
|
|
265
|
+
deletedCount,
|
|
266
|
+
deletedSize,
|
|
267
|
+
ip: ctx.ip
|
|
268
|
+
})
|
|
269
|
+
|
|
270
|
+
ctx.body = {
|
|
271
|
+
code: 0,
|
|
272
|
+
data: {
|
|
273
|
+
deletedCount,
|
|
274
|
+
deletedSize,
|
|
275
|
+
cutoffDate: cutoffDate.toISOString()
|
|
276
|
+
},
|
|
277
|
+
message: `成功清理 ${deletedCount} 个过期日志文件`
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
} catch (error) {
|
|
281
|
+
logger.error('Failed to clean expired logs', error as Error)
|
|
282
|
+
ctx.status = 500
|
|
283
|
+
ctx.body = {
|
|
284
|
+
code: 500,
|
|
285
|
+
message: '清理过期日志失败'
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* 设置日志级别
|
|
292
|
+
*/
|
|
293
|
+
async setLogLevel(ctx: Context) {
|
|
294
|
+
try {
|
|
295
|
+
const { level, category = 'default' } = ctx.request.body
|
|
296
|
+
|
|
297
|
+
const validLevels = ['trace', 'debug', 'info', 'warn', 'error', 'fatal']
|
|
298
|
+
if (!validLevels.includes(level)) {
|
|
299
|
+
ctx.status = 400
|
|
300
|
+
ctx.body = {
|
|
301
|
+
code: 400,
|
|
302
|
+
message: '无效的日志级别'
|
|
303
|
+
}
|
|
304
|
+
return
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// 这里需要实现动态设置日志级别的逻辑
|
|
308
|
+
// 由于log4js的限制,可能需要重启应用才能生效
|
|
309
|
+
|
|
310
|
+
// 审计日志
|
|
311
|
+
logger.audit('LOG_LEVEL_CHANGE', category, {
|
|
312
|
+
userId: ctx.state.user?.id,
|
|
313
|
+
oldLevel: 'unknown',
|
|
314
|
+
newLevel: level,
|
|
315
|
+
ip: ctx.ip
|
|
316
|
+
})
|
|
317
|
+
|
|
318
|
+
ctx.body = {
|
|
319
|
+
code: 0,
|
|
320
|
+
data: {
|
|
321
|
+
category,
|
|
322
|
+
level,
|
|
323
|
+
message: '日志级别设置成功,重启应用后生效'
|
|
324
|
+
},
|
|
325
|
+
message: '日志级别设置成功'
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
} catch (error) {
|
|
329
|
+
logger.error('Failed to set log level', error as Error)
|
|
330
|
+
ctx.status = 500
|
|
331
|
+
ctx.body = {
|
|
332
|
+
code: 500,
|
|
333
|
+
message: '设置日志级别失败'
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* 获取实时日志流
|
|
340
|
+
*/
|
|
341
|
+
async getLogStream(ctx: Context) {
|
|
342
|
+
try {
|
|
343
|
+
const { filename } = ctx.params
|
|
344
|
+
|
|
345
|
+
if (!filename || !filename.match(/^[a-zA-Z0-9._-]+$/)) {
|
|
346
|
+
ctx.status = 400
|
|
347
|
+
ctx.body = {
|
|
348
|
+
code: 400,
|
|
349
|
+
message: '无效的文件名'
|
|
350
|
+
}
|
|
351
|
+
return
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const filePath = path.join(process.cwd(), 'logs', filename)
|
|
355
|
+
|
|
356
|
+
if (!fs.existsSync(filePath)) {
|
|
357
|
+
ctx.status = 404
|
|
358
|
+
ctx.body = {
|
|
359
|
+
code: 404,
|
|
360
|
+
message: '日志文件不存在'
|
|
361
|
+
}
|
|
362
|
+
return
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// 设置SSE响应头
|
|
366
|
+
ctx.set({
|
|
367
|
+
'Content-Type': 'text/event-stream',
|
|
368
|
+
'Cache-Control': 'no-cache',
|
|
369
|
+
'Connection': 'keep-alive',
|
|
370
|
+
'Access-Control-Allow-Origin': '*',
|
|
371
|
+
'Access-Control-Allow-Headers': 'Cache-Control'
|
|
372
|
+
})
|
|
373
|
+
|
|
374
|
+
// 审计日志
|
|
375
|
+
logger.audit('LOG_STREAM_START', filename, {
|
|
376
|
+
userId: ctx.state.user?.id,
|
|
377
|
+
ip: ctx.ip
|
|
378
|
+
})
|
|
379
|
+
|
|
380
|
+
// 创建可读流
|
|
381
|
+
const stream = fs.createReadStream(filePath, { encoding: 'utf8' })
|
|
382
|
+
|
|
383
|
+
ctx.body = stream
|
|
384
|
+
|
|
385
|
+
} catch (error) {
|
|
386
|
+
logger.error('Failed to get log stream', error as Error, {
|
|
387
|
+
filename: ctx.params.filename
|
|
388
|
+
})
|
|
389
|
+
ctx.status = 500
|
|
390
|
+
ctx.body = {
|
|
391
|
+
code: 500,
|
|
392
|
+
message: '获取日志流失败'
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { Body,Post, ResponseValidateIf, ResponseValidator } from "@src/framework/decorator/controller";
|
|
2
|
+
import { BaseController, ControllerResponse } from "@src/controllers/base.controller";
|
|
3
|
+
import { SendEmailDto, SendEmailResponseDto } from "@src/dto/controller/home/emailSend.controller.dto";
|
|
4
|
+
import { Inject } from "dp-ioc2";
|
|
5
|
+
import * as jwt from "jsonwebtoken";
|
|
6
|
+
import { YtUserService } from "@src/service/ytUser.service";
|
|
7
|
+
import { logger } from "@src/framework/utils/logger";
|
|
8
|
+
import { AokEmailSender } from "@src/libs/aokEmailSender";
|
|
9
|
+
|
|
10
|
+
const emailSender = new AokEmailSender(process.env.aok_email_api_key ?? "330b853af20ef6762f259545ef7b4c17")
|
|
11
|
+
|
|
12
|
+
export class EmailSendController extends BaseController {
|
|
13
|
+
|
|
14
|
+
@Inject(YtUserService)
|
|
15
|
+
ytUserService: YtUserService;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 短信验证码邮件
|
|
19
|
+
* @param body
|
|
20
|
+
* @returns
|
|
21
|
+
*/
|
|
22
|
+
@ResponseValidateIf(SendEmailResponseDto, (data) => data && data.data)
|
|
23
|
+
@Post()
|
|
24
|
+
async sendRegisterEmail(
|
|
25
|
+
@Body(SendEmailDto) body: SendEmailDto
|
|
26
|
+
): Promise<ControllerResponse<SendEmailResponseDto>> {
|
|
27
|
+
|
|
28
|
+
// 1. 检查邮箱是否已注册
|
|
29
|
+
// const exist = await this.ytUserService.findByEmail(body.email);
|
|
30
|
+
// if (exist) {
|
|
31
|
+
// return this.fail(-1, "该邮箱已注册");
|
|
32
|
+
// }
|
|
33
|
+
|
|
34
|
+
// 2. 生成随机验证码(4位数字)
|
|
35
|
+
const vcode = Math.floor(1000 + Math.random() * 9000).toString();
|
|
36
|
+
|
|
37
|
+
// 3. 设置验证码过期时间(5分钟后)
|
|
38
|
+
const expire = Date.now() + 5 * 60 * 1000;
|
|
39
|
+
|
|
40
|
+
// 4. 生成JWT token包含验证码和过期时间
|
|
41
|
+
const vcodeToken = jwt.sign(
|
|
42
|
+
{ vcode, expire, email: body.email },
|
|
43
|
+
process.env.jwt_secret ?? "123456",
|
|
44
|
+
{ expiresIn: "5m" }
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
// 5. 发送邮件验证码(模拟)
|
|
48
|
+
const result = await emailSender.sendEmail({
|
|
49
|
+
templateId: "E_106565864351",
|
|
50
|
+
templateData: {
|
|
51
|
+
vcode: String(vcode),
|
|
52
|
+
login_link: `
|
|
53
|
+
<a href="https://ytfx.yndaopu.com/login/${vcode}">快捷登录链接</a>
|
|
54
|
+
`
|
|
55
|
+
},
|
|
56
|
+
to: body.email,
|
|
57
|
+
})
|
|
58
|
+
// console.log(result)
|
|
59
|
+
// logger.info(`发送邮件验证码到${body.email}: ${vcode}`);
|
|
60
|
+
return this.success({
|
|
61
|
+
expire,
|
|
62
|
+
vcodeToken
|
|
63
|
+
}, "验证码发送成功");
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { Body, ControllerCache, Get, Post, Query, ResponseValidateIf } from "@src/framework/decorator/controller";
|
|
2
|
+
import { BaseController, ControllerResponse } from "@src/controllers/base.controller";
|
|
3
|
+
import { Inject } from "dp-ioc2";
|
|
4
|
+
import { YtUserService } from "@src/service/ytUser.service";
|
|
5
|
+
import jwt from 'jsonwebtoken';
|
|
6
|
+
import md5 from 'md5';
|
|
7
|
+
import svgCaptcha from 'svg-captcha';
|
|
8
|
+
import { GetYtUserBasicInfoDto, GetYtUserBasicInfoResponseDto, YtUserLoginAutoDto, YtUserLoginDto } from "@src/dto/controller/public/ytUserAuth.controller.dto";
|
|
9
|
+
import { YtUserEntity, YtUserStatusEnum, YtUserTypeEnum } from "@src/entity/ytUser.entity";
|
|
10
|
+
import { CommonServiceResultCode } from "@src/framework/types/ServiceResult";
|
|
11
|
+
import { logger } from "@src/framework/utils/logger";
|
|
12
|
+
import { isDebug } from "@src/framework/utils/function";
|
|
13
|
+
|
|
14
|
+
interface IvcodeInfo {
|
|
15
|
+
vcode: string
|
|
16
|
+
expire: number
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class YtUserAuthController extends BaseController {
|
|
20
|
+
@Inject(YtUserService)
|
|
21
|
+
ytUserService: YtUserService;
|
|
22
|
+
|
|
23
|
+
@Post()
|
|
24
|
+
async login(
|
|
25
|
+
@Body(YtUserLoginDto) body: YtUserLoginDto
|
|
26
|
+
): Promise<ControllerResponse<string>> {
|
|
27
|
+
|
|
28
|
+
// 验证注册码
|
|
29
|
+
let vcodeInfo: IvcodeInfo | null = null;
|
|
30
|
+
try {
|
|
31
|
+
vcodeInfo = jwt.verify(body.vcodeToken, process.env.jwt_secret ?? "123456") as any;
|
|
32
|
+
} catch (err) {
|
|
33
|
+
return this.fail(-1, "验证码可能失效");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!vcodeInfo) {
|
|
37
|
+
return this.fail(-1, "验证码解析失败");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (vcodeInfo?.expire < new Date().getTime()) {
|
|
41
|
+
return this.fail(-1, "验证码过期");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (vcodeInfo.vcode !== body.vcode) {
|
|
45
|
+
return this.fail(-1, "验证码不正确");
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 1. 验证用户是否存在
|
|
49
|
+
let user = await this.ytUserService.findByEmail(body.email);
|
|
50
|
+
if (!user) {
|
|
51
|
+
const ytUser = new YtUserEntity();
|
|
52
|
+
ytUser.nickName = '用户' + body.email.split("@")[0].slice(0, 5);
|
|
53
|
+
ytUser.email = body.email;
|
|
54
|
+
ytUser.avatar = "https://ytfx.yndaopu.com/static/images/Avatar-1.png";
|
|
55
|
+
// ytUser.password = md5(body.password);
|
|
56
|
+
let result = await this.ytUserService.add(ytUser);
|
|
57
|
+
// if (result.code != CommonServiceResultCode.SUCCESS) {
|
|
58
|
+
// logger.warn(`注册注册失败` + JSON.stringify(body), result.message);
|
|
59
|
+
// return this.fail(-1, "注册失败");
|
|
60
|
+
// }
|
|
61
|
+
user = result
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (user.status == YtUserStatusEnum.FORBIDDEN) {
|
|
65
|
+
return this.fail(-1, "用户已被禁用");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
// 3. 生成JWT token
|
|
70
|
+
const token = jwt.sign(
|
|
71
|
+
{ userId: user.id, type: user.userType },
|
|
72
|
+
process.env.jwt_secret ?? "123456",
|
|
73
|
+
{ expiresIn: "7d" }
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
return this.success(token, "登录成功");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* 自动注册和登录
|
|
82
|
+
* @param body
|
|
83
|
+
* @returns
|
|
84
|
+
*/
|
|
85
|
+
@Post()
|
|
86
|
+
async loginAuto(
|
|
87
|
+
@Body(YtUserLoginAutoDto) body: YtUserLoginAutoDto
|
|
88
|
+
): Promise<ControllerResponse<string>> {
|
|
89
|
+
|
|
90
|
+
// 验证注册码
|
|
91
|
+
let vcodeInfo: IvcodeInfo | null = null;
|
|
92
|
+
try {
|
|
93
|
+
vcodeInfo = jwt.verify(body.vcodeToken, process.env.jwt_secret ?? "123456") as any;
|
|
94
|
+
} catch (err) {
|
|
95
|
+
return this.fail(-1, "验证码可能失效");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (!vcodeInfo) {
|
|
99
|
+
return this.fail(-1, "验证码解析失败");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (vcodeInfo?.expire < new Date().getTime()) {
|
|
103
|
+
return this.fail(-1, "验证码过期");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (String(vcodeInfo.vcode).toLowerCase() !== String(body.vcode).toLowerCase()) {
|
|
107
|
+
return this.fail(-1, "验证码不正确");
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
let user = new YtUserEntity();
|
|
111
|
+
user.nickName = `新用户-${vcodeInfo.vcode}`;
|
|
112
|
+
user.email = `${vcodeInfo.vcode}@example.com`
|
|
113
|
+
user.avatar = "https://ytfx.yndaopu.com/static/images/Avatar-1.png";
|
|
114
|
+
user = await this.ytUserService.add(user)
|
|
115
|
+
// 3. 生成JWT token
|
|
116
|
+
const token = jwt.sign(
|
|
117
|
+
{ userId: user.id, type: user.userType },
|
|
118
|
+
process.env.jwt_secret ?? "123456",
|
|
119
|
+
{ expiresIn: "360d" }
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
return this.success(token, "登录成功");
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
@ControllerCache((query: GetYtUserBasicInfoDto) => `YtUserAuthController-getYtUserBasicInfo-${query.id}`, {
|
|
126
|
+
ttl: {
|
|
127
|
+
max: 60 * 5,
|
|
128
|
+
min: 30,
|
|
129
|
+
},
|
|
130
|
+
enable: !isDebug()
|
|
131
|
+
})
|
|
132
|
+
@ResponseValidateIf(GetYtUserBasicInfoResponseDto, (data) => data && data.data)
|
|
133
|
+
@Get()
|
|
134
|
+
async getYtUserBasicInfo(
|
|
135
|
+
@Query(GetYtUserBasicInfoDto) query: GetYtUserBasicInfoDto,
|
|
136
|
+
): Promise<ControllerResponse<GetYtUserBasicInfoResponseDto | null>> {
|
|
137
|
+
let userResult = await this.ytUserService.getById(YtUserEntity, query.id);
|
|
138
|
+
|
|
139
|
+
if (!userResult) {
|
|
140
|
+
return this.fail(-1, "用户不存在");
|
|
141
|
+
}
|
|
142
|
+
return this.success({
|
|
143
|
+
id: userResult.id,
|
|
144
|
+
name: userResult.nickName,
|
|
145
|
+
avatar: userResult.avatar,
|
|
146
|
+
}, "获取成功");
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* 创建验证码
|
|
153
|
+
* @returns
|
|
154
|
+
*/
|
|
155
|
+
@Get()
|
|
156
|
+
async getCaptcha() {
|
|
157
|
+
const captcha = svgCaptcha.create({
|
|
158
|
+
size: 4,
|
|
159
|
+
ignoreChars: '0o1i',
|
|
160
|
+
noise: 2,
|
|
161
|
+
color: true
|
|
162
|
+
});
|
|
163
|
+
const token = jwt.sign(
|
|
164
|
+
{ vcode: captcha.text, expire: new Date().getTime() + 1000 * 60 * 5 },
|
|
165
|
+
process.env.jwt_secret ?? "123456"
|
|
166
|
+
);
|
|
167
|
+
return {
|
|
168
|
+
svg: captcha.data,
|
|
169
|
+
token
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
}
|