init
This commit is contained in:
39
middleware/baseModel.js
Normal file
39
middleware/baseModel.js
Normal file
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* 模型导出模块(代理模式)
|
||||
* 从 Framework 动态获取已初始化的模型
|
||||
* 使用 Proxy 延迟获取,确保 Framework 已初始化
|
||||
*/
|
||||
|
||||
const Sequelize = require("sequelize");
|
||||
const Framework = require("../framework/node-core-framework.js");
|
||||
const db = require("./db_proxy");
|
||||
|
||||
module.exports = new Proxy({}, {
|
||||
get(_, prop) {
|
||||
// 获取 Framework 初始化的模型
|
||||
const models = Framework.getModels();
|
||||
|
||||
if (!models) {
|
||||
throw new Error('Framework not initialized. Please call Framework.init() first.');
|
||||
}
|
||||
|
||||
// 特殊属性处理
|
||||
if (prop === 'Sequelize') {
|
||||
return Sequelize;
|
||||
}
|
||||
|
||||
if (prop === 'op') {
|
||||
return Sequelize.Op;
|
||||
}
|
||||
|
||||
if (prop === 'querySql') {
|
||||
return async (sql) => {
|
||||
return await db.sequelize.query(sql, { type: Sequelize.QueryTypes.SELECT });
|
||||
};
|
||||
}
|
||||
|
||||
// 返回对应的模型
|
||||
return models[prop];
|
||||
}
|
||||
});
|
||||
|
||||
7
middleware/base_controller.js
Normal file
7
middleware/base_controller.js
Normal file
@@ -0,0 +1,7 @@
|
||||
/**
|
||||
* 基础控制器中间件(兼容层)
|
||||
* Framework 已自动处理路由注册,此文件仅用于向后兼容
|
||||
*/
|
||||
|
||||
module.exports = () => async (ctx, next) => await next();
|
||||
|
||||
24
middleware/db_proxy.js
Normal file
24
middleware/db_proxy.js
Normal file
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* 数据库连接代理
|
||||
* 从 Framework 获取 Sequelize 实例
|
||||
*/
|
||||
|
||||
const Framework = require("../framework/node-core-framework.js");
|
||||
|
||||
module.exports = new Proxy({}, {
|
||||
get(_, prop) {
|
||||
const models = Framework.getModels();
|
||||
|
||||
if (!models) {
|
||||
throw new Error('Framework not initialized. Please call Framework.init() first.');
|
||||
}
|
||||
|
||||
const db = {
|
||||
sequelize: models.sequelize || global.sequelize,
|
||||
Sequelize: models.Sequelize
|
||||
};
|
||||
|
||||
return db[prop];
|
||||
}
|
||||
});
|
||||
|
||||
22
middleware/redis_proxy.js
Normal file
22
middleware/redis_proxy.js
Normal file
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Redis 服务代理
|
||||
* 从 Framework 获取 redisService
|
||||
*/
|
||||
|
||||
const Framework = require("../framework/node-core-framework.js");
|
||||
|
||||
module.exports = new Proxy({}, {
|
||||
get(_, prop) {
|
||||
const services = Framework.getServices();
|
||||
const redisService = services?.redisService;
|
||||
|
||||
if (!redisService) {
|
||||
throw new Error('Redis service not available. Framework may not be initialized.');
|
||||
}
|
||||
|
||||
return typeof redisService[prop] === 'function'
|
||||
? redisService[prop].bind(redisService)
|
||||
: redisService[prop];
|
||||
}
|
||||
});
|
||||
|
||||
51
middleware/schedule.js
Normal file
51
middleware/schedule.js
Normal file
@@ -0,0 +1,51 @@
|
||||
const node_schedule = require("node-schedule");
|
||||
const logs = require('../tool/logs_proxy');
|
||||
|
||||
class Schedule {
|
||||
// 执行标记,防止并发执行
|
||||
constructor() {
|
||||
this.running_flags = {
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行带锁的任务
|
||||
* @param {string} task_name - 任务名称
|
||||
* @param {Function} task_fn - 任务函数
|
||||
*/
|
||||
async execute_with_lock(task_name, task_fn) {
|
||||
// 如果正在执行,跳过本次执行
|
||||
if (this.running_flags[task_name]) {
|
||||
logs.log(`[定时任务] ${task_name} 正在执行中,跳过本次执行`);
|
||||
return;
|
||||
}
|
||||
|
||||
// 设置执行标记
|
||||
this.running_flags[task_name] = true;
|
||||
|
||||
try {
|
||||
await task_fn();
|
||||
} catch (error) {
|
||||
logs.error(`[定时任务] ${task_name} 执行失败:`, error);
|
||||
} finally {
|
||||
// 清除执行标记
|
||||
this.running_flags[task_name] = false;
|
||||
}
|
||||
}
|
||||
|
||||
async init() {
|
||||
const bizSubscriptionLogic = require("../api/service/biz_subscription_logic");
|
||||
|
||||
node_schedule.scheduleJob("10 0 * * *", async () => {
|
||||
await this.execute_with_lock("biz_subscription_expire", async () => {
|
||||
const n = await bizSubscriptionLogic.expireDueSubscriptions();
|
||||
logs.log(`[定时任务] 订阅到期扫描完成,更新行数: ${n}`);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const schedule = new Schedule();
|
||||
module.exports = schedule;
|
||||
|
||||
15
middleware/sqlUpdate.js
Normal file
15
middleware/sqlUpdate.js
Normal file
@@ -0,0 +1,15 @@
|
||||
const { querySql, op } = require("./baseModel");
|
||||
class SqlUpdate {
|
||||
async init() {
|
||||
// await this.initDb()
|
||||
}
|
||||
|
||||
|
||||
// 测试数据库专用用于 初始化db
|
||||
async initDb() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
const sqlUpdate = new SqlUpdate();
|
||||
module.exports = sqlUpdate;
|
||||
Reference in New Issue
Block a user