'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.'); 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 || {}; // 顺序固定 ↓ //redisConnect(app, opts) socketConect(app, opts) // 实例其他平台请求方法 //paasRequest(app, opts) // clickHouse 数据库 client //clickHouseClient(app, opts) // 工具类函数 utils(app, opts) // 定时任务 schedule(app, opts) //鉴权中间件 router.use(authenticator(app, opts)); // 日志记录 // router.use(apiLog(app, opts)); router = routes(app, router, opts); }; module.exports.models = function (dc) { // dc = { orm: Sequelize对象, ORM: Sequelize, models: {} } // 模型关系摘出来 初始化之后再定义关系才行 fs.readdirSync(path.join(__dirname, '/models')).forEach((filename) => { require(`./models/${filename}`)(dc) }); const { Camera,Company, Department, Post, RoleGroup, Role, RoleResource, User, UserDepartment, UserPost, Site, ProjectDisclosure, ProjectDisclosureFiles, Coordinate, ProblemReport, ProblemReportFile, Worker, WorkerAttendance, RiskReport, Metting, HideDangerRectify, HideDangerRectifySites, HideDangerDispose } = dc.models; Metting.belongsTo(User, { foreignKey: 'submitUser', targetKey: 'id' }); User.hasMany(Metting, { foreignKey: 'submitUser', sourceKey: 'id' }); Department.belongsTo(Company, { foreignKey: 'companyId', targetKey: 'id' }); Company.hasMany(Department, { foreignKey: 'companyId', sourceKey: 'id' }); Post.belongsTo(Company, { foreignKey: 'companyId', targetKey: 'id' }); Company.hasMany(Post, { foreignKey: 'companyId', sourceKey: 'id' }); Post.belongsTo(Department, { foreignKey: 'departmentId', targetKey: 'id' }); Department.hasMany(Post, { foreignKey: 'departmentId', sourceKey: 'id' }); Role.belongsTo(RoleGroup, { foreignKey: 'roleGroupId', targetKey: 'id' }); RoleGroup.hasMany(Role, { foreignKey: 'roleGroupId', sourceKey: 'id' }); RoleResource.belongsTo(Role, { foreignKey: 'roleId', targetKey: 'id' }); Role.hasMany(RoleResource, { foreignKey: 'roleId', sourceKey: 'id' }); UserDepartment.belongsTo(User, { foreignKey: 'userId', targetKey: 'id' }); User.hasMany(UserDepartment, { foreignKey: 'userId', sourceKey: 'id' }); UserDepartment.belongsTo(Department, { foreignKey: 'departmentId', targetKey: 'id' }); Department.hasMany(UserDepartment, { foreignKey: 'departmentId', sourceKey: 'id' }); UserPost.belongsTo(User, { foreignKey: 'userId', targetKey: 'id' }); User.hasMany(UserPost, { foreignKey: 'userId', sourceKey: 'id' }); UserPost.belongsTo(Post, { foreignKey: 'postId', targetKey: 'id' }); Post.hasMany(UserPost, { foreignKey: 'postId', sourceKey: 'id' }); User.belongsTo(Role, { foreignKey: 'roleId', targetKey: 'id' }); Role.hasMany(User, { foreignKey: 'roleId', sourceKey: 'id' }); ProjectDisclosure.belongsTo(Site, { foreignKey: 'siteId', targetKey: 'id' }); Site.hasMany(ProjectDisclosure, { foreignKey: 'siteId', sourceKey: 'id' }); ProjectDisclosure.belongsTo(User, { foreignKey: 'submiter', targetKey: 'id' }); User.hasMany(ProjectDisclosure, { foreignKey: 'submiter', sourceKey: 'id' }); ProjectDisclosureFiles.belongsTo(ProjectDisclosure, { foreignKey: 'projectDisclosureId', targetKey: 'id' }); ProjectDisclosure.hasMany(ProjectDisclosureFiles, { foreignKey: 'projectDisclosureId', sourceKey: 'id' }); ProblemReport.belongsTo(User, { foreignKey: 'reporter', sourceKey: 'id' }); ProblemReport.hasMany(ProblemReportFile, { foreignKey: 'reportId', sourceKey: 'id' }); ProblemReport.belongsTo(Site, { foreignKey: 'siteId', sourceKey: 'id' }); Site.hasMany(ProblemReport, { foreignKey: 'siteId', sourceKey: 'id' }); Site.belongsTo(Company, { foreignKey: 'companyId', targetKey: 'id' }); Company.hasMany(Site, { foreignKey: 'companyId', sourceKey: 'id' }); Coordinate.belongsTo(Site, { foreignKey: 'siteId', targetKey: 'id' }); Site.hasMany(Coordinate, { foreignKey: 'siteId', sourceKey: 'id' }); WorkerAttendance.belongsTo(Worker, { foreignKey: 'workerId', targetKey: 'id' }); Worker.hasMany(WorkerAttendance, { foreignKey: 'workerId', sourceKey: 'id' }); RiskReport.belongsTo(Site, { foreignKey: 'siteId', targetKey: 'id' }); Site.hasMany(RiskReport, { foreignKey: 'siteId', sourceKey: 'id' }); HideDangerRectifySites.belongsTo(HideDangerRectify, { foreignKey: 'rectifyId', targetKey: 'id' }); HideDangerRectify.hasMany(HideDangerRectifySites, { foreignKey: 'rectifyId', sourceKey: 'id' }); HideDangerRectifySites.belongsTo(Site, { foreignKey: 'siteId', targetKey: 'id' }); Site.hasMany(HideDangerRectifySites, { foreignKey: 'siteId', sourceKey: 'id' }); HideDangerDispose.belongsTo(HideDangerRectifySites, { foreignKey: 'rectifySiteId', targetKey: 'id' }); HideDangerRectifySites.hasMany(HideDangerDispose, { foreignKey: 'rectifySiteId', sourceKey: 'id' }); HideDangerDispose.belongsTo(User, { foreignKey: 'disposeUser', targetKey: 'id' }); User.hasMany(HideDangerDispose, { foreignKey: 'disposeUser', sourceKey: 'id' }); Camera.belongsTo(Site, { foreignKey: 'siteId', targetKey: 'id' }); Site.hasMany(Camera, { foreignKey: 'siteId', sourceKey: 'id' }); };