You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.7 KiB

2 years ago
'use strict';
const fs = require('fs');
const path = require('path');
const utils = require('./utils')
const routes = require('./routes');
//const redisConnect = require('./service/redis')
const socketConect = require('./service/socket')
//const paasRequest = require('./service/paasRequest');
const authenticator = require('./middlewares/authenticator');
//const clickHouseClient = require('./service/clickHouseClient')
const schedule = require('./schedule')
// const apiLog = require('./middlewares/api-log');
module.exports.entry = function (app, router, opts) {
app.fs.logger.log('info', '[FS-AUTH]', 'Inject auth and api mv into router.');
2 years ago
app.fs.api = app.fs.api || {};
app.fs.opts = opts || {};
app.fs.utils = app.fs.utils || {};
app.fs.api.authAttr = app.fs.api.authAttr || {};
app.fs.api.logAttr = app.fs.api.logAttr || {};
2 years ago
// 顺序固定 ↓
//redisConnect(app, opts)
socketConect(app, opts)
2 years ago
// 实例其他平台请求方法
//paasRequest(app, opts)
2 years ago
// clickHouse 数据库 client
//clickHouseClient(app, opts)
2 years ago
// 工具类函数
utils(app, opts)
2 years ago
// 定时任务
schedule(app, opts)
2 years ago
//鉴权中间件
router.use(authenticator(app, opts));
2 years ago
// 日志记录
// router.use(apiLog(app, opts));
2 years ago
router = routes(app, router, opts);
2 years ago
};
module.exports.models = function (dc) { // dc = { orm: Sequelize对象, ORM: Sequelize, models: {} }
// 模型关系摘出来 初始化之后再定义关系才行
fs.readdirSync(path.join(__dirname, '/models')).forEach((filename) => {
require(`./models/${filename}`)(dc)
});
2 years ago
2 years ago
const { Department, User, UserResource, Resource, Project, Point, PatrolPlan
} = dc.models;
2 years ago
UserResource.belongsTo(User, { foreignKey: 'userId', targetKey: 'id' });
User.hasMany(UserResource, { foreignKey: 'userId', sourceKey: 'id' });
2 years ago
UserResource.belongsTo(Resource, { foreignKey: 'resourceId', targetKey: 'code' });
Resource.hasMany(UserResource, { foreignKey: 'resourceId', sourceKey: 'code' });
Resource.hasMany(Resource, { foreignKey: 'parentResource', sourceKey: 'code' });
2 years ago
User.belongsTo(Department, { foreignKey: 'departmentId', targetKey: 'id' });
Department.hasMany(User, { foreignKey: 'departmentId', sourceKey: 'id' });
Point.belongsTo(Project, { foreignKey: 'projectId', targetKey: 'id' });
Project.hasMany(Point, { foreignKey: 'projectId', sourceKey: 'id' });
2 years ago
PatrolPlan.belongsTo(Project, { foreignKey: 'structureId', targetKey: 'id' });
Project.hasMany(PatrolPlan, { foreignKey: 'structureId', sourceKey: 'id' });
PatrolPlan.belongsTo(User, { foreignKey: 'userId', targetKey: 'id' });
User.hasMany(PatrolPlan, { foreignKey: 'userId', sourceKey: 'id' });
2 years ago
};