更新政府端和银行端
This commit is contained in:
72
bank-backend/config/database.js
Normal file
72
bank-backend/config/database.js
Normal file
@@ -0,0 +1,72 @@
|
||||
const { Sequelize } = require('sequelize');
|
||||
|
||||
// 从环境变量获取数据库配置
|
||||
const dialect = process.env.DB_DIALECT || 'mysql';
|
||||
const config = {
|
||||
logging: false,
|
||||
define: {
|
||||
timestamps: true,
|
||||
underscored: true,
|
||||
freezeTableName: true
|
||||
}
|
||||
};
|
||||
|
||||
// 根据数据库类型配置不同的选项
|
||||
if (dialect === 'sqlite') {
|
||||
config.storage = process.env.DB_STORAGE || './bank_database.sqlite';
|
||||
config.dialect = 'sqlite';
|
||||
} else {
|
||||
config.host = process.env.DB_HOST || '129.211.213.226';
|
||||
config.port = process.env.DB_PORT || 9527;
|
||||
config.dialect = 'mysql';
|
||||
config.timezone = '+08:00';
|
||||
config.define.charset = 'utf8mb4';
|
||||
config.define.collate = 'utf8mb4_unicode_ci';
|
||||
config.pool = {
|
||||
max: 10,
|
||||
min: 0,
|
||||
acquire: 30000,
|
||||
idle: 10000
|
||||
};
|
||||
}
|
||||
|
||||
let sequelize;
|
||||
if (dialect === 'sqlite') {
|
||||
sequelize = new Sequelize(config);
|
||||
} else {
|
||||
sequelize = new Sequelize(
|
||||
process.env.DB_NAME || 'ningxia_bank',
|
||||
process.env.DB_USER || 'root',
|
||||
process.env.DB_PASSWORD || 'aiotAiot123!',
|
||||
config
|
||||
);
|
||||
}
|
||||
|
||||
// 测试数据库连接(最多重试3次)
|
||||
const MAX_RETRIES = 3;
|
||||
let retryCount = 0;
|
||||
|
||||
const testConnection = async () => {
|
||||
try {
|
||||
await sequelize.authenticate();
|
||||
console.log('✅ 银行系统数据库连接成功');
|
||||
} catch (err) {
|
||||
console.error('❌ 银行系统数据库连接失败:', err.message);
|
||||
if (retryCount < MAX_RETRIES) {
|
||||
retryCount++;
|
||||
console.log(`🔄 正在重试连接 (${retryCount}/${MAX_RETRIES})...`);
|
||||
setTimeout(testConnection, 5000); // 5秒后重试
|
||||
} else {
|
||||
console.error('❌ 数据库连接失败,应用将使用模拟数据运行');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 异步测试连接,不阻塞应用启动
|
||||
testConnection().catch(() => {
|
||||
console.log('📊 数据库连接测试完成,应用继续启动');
|
||||
});
|
||||
|
||||
// 兼容导出:同时支持 require('.../database') 和 const { sequelize } = require('.../database')
|
||||
module.exports = sequelize;
|
||||
module.exports.sequelize = sequelize;
|
||||
216
bank-backend/config/swagger.js
Normal file
216
bank-backend/config/swagger.js
Normal file
@@ -0,0 +1,216 @@
|
||||
/**
|
||||
* Swagger API文档配置
|
||||
* @file swagger.js
|
||||
* @description API文档配置和定义
|
||||
*/
|
||||
const swaggerJSDoc = require('swagger-jsdoc');
|
||||
|
||||
const options = {
|
||||
definition: {
|
||||
openapi: '3.0.0',
|
||||
info: {
|
||||
title: '银行管理后台API',
|
||||
version: '1.0.0',
|
||||
description: '银行管理后台系统API文档',
|
||||
contact: {
|
||||
name: '银行开发团队',
|
||||
email: 'dev@bank.com'
|
||||
},
|
||||
license: {
|
||||
name: 'MIT',
|
||||
url: 'https://opensource.org/licenses/MIT'
|
||||
}
|
||||
},
|
||||
servers: [
|
||||
{
|
||||
url: `http://localhost:${process.env.PORT || 5351}`,
|
||||
description: '开发服务器'
|
||||
},
|
||||
{
|
||||
url: 'https://api.bank.com',
|
||||
description: '生产服务器'
|
||||
}
|
||||
],
|
||||
components: {
|
||||
securitySchemes: {
|
||||
bearerAuth: {
|
||||
type: 'http',
|
||||
scheme: 'bearer',
|
||||
bearerFormat: 'JWT',
|
||||
description: 'JWT访问令牌'
|
||||
}
|
||||
},
|
||||
schemas: {
|
||||
Error: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
success: {
|
||||
type: 'boolean',
|
||||
example: false
|
||||
},
|
||||
message: {
|
||||
type: 'string',
|
||||
example: '错误信息'
|
||||
},
|
||||
errors: {
|
||||
type: 'array',
|
||||
items: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
field: {
|
||||
type: 'string'
|
||||
},
|
||||
message: {
|
||||
type: 'string'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
Success: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
success: {
|
||||
type: 'boolean',
|
||||
example: true
|
||||
},
|
||||
message: {
|
||||
type: 'string',
|
||||
example: '操作成功'
|
||||
},
|
||||
data: {
|
||||
type: 'object'
|
||||
}
|
||||
}
|
||||
},
|
||||
Pagination: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
page: {
|
||||
type: 'integer',
|
||||
example: 1
|
||||
},
|
||||
limit: {
|
||||
type: 'integer',
|
||||
example: 10
|
||||
},
|
||||
total: {
|
||||
type: 'integer',
|
||||
example: 100
|
||||
},
|
||||
pages: {
|
||||
type: 'integer',
|
||||
example: 10
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
responses: {
|
||||
UnauthorizedError: {
|
||||
description: '未授权访问',
|
||||
content: {
|
||||
'application/json': {
|
||||
schema: {
|
||||
$ref: '#/components/schemas/Error'
|
||||
},
|
||||
example: {
|
||||
success: false,
|
||||
message: '访问被拒绝,未提供令牌'
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
ForbiddenError: {
|
||||
description: '权限不足',
|
||||
content: {
|
||||
'application/json': {
|
||||
schema: {
|
||||
$ref: '#/components/schemas/Error'
|
||||
},
|
||||
example: {
|
||||
success: false,
|
||||
message: '权限不足'
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
NotFoundError: {
|
||||
description: '资源不存在',
|
||||
content: {
|
||||
'application/json': {
|
||||
schema: {
|
||||
$ref: '#/components/schemas/Error'
|
||||
},
|
||||
example: {
|
||||
success: false,
|
||||
message: '请求的资源不存在'
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
ValidationError: {
|
||||
description: '输入数据验证失败',
|
||||
content: {
|
||||
'application/json': {
|
||||
schema: {
|
||||
$ref: '#/components/schemas/Error'
|
||||
},
|
||||
example: {
|
||||
success: false,
|
||||
message: '输入数据验证失败',
|
||||
errors: [
|
||||
{
|
||||
field: 'email',
|
||||
message: '邮箱格式不正确'
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
InternalServerError: {
|
||||
description: '服务器内部错误',
|
||||
content: {
|
||||
'application/json': {
|
||||
schema: {
|
||||
$ref: '#/components/schemas/Error'
|
||||
},
|
||||
example: {
|
||||
success: false,
|
||||
message: '服务器内部错误'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
security: [
|
||||
{
|
||||
bearerAuth: []
|
||||
}
|
||||
],
|
||||
tags: [
|
||||
{
|
||||
name: 'Users',
|
||||
description: '用户管理相关接口'
|
||||
},
|
||||
{
|
||||
name: 'Accounts',
|
||||
description: '账户管理相关接口'
|
||||
},
|
||||
{
|
||||
name: 'Transactions',
|
||||
description: '交易管理相关接口'
|
||||
}
|
||||
]
|
||||
},
|
||||
apis: [
|
||||
'./routes/*.js',
|
||||
'./controllers/*.js'
|
||||
]
|
||||
};
|
||||
|
||||
const specs = swaggerJSDoc(options);
|
||||
|
||||
module.exports = specs;
|
||||
Reference in New Issue
Block a user