-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
shitiandmw
committed
Jan 5, 2024
1 parent
86809a5
commit 9c0d625
Showing
10 changed files
with
140 additions
and
88 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
const Router = require('koa-router'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const { log } = require('console'); | ||
module.exports = class Plugin { | ||
constructor(dependencies) { | ||
this.app = {}; | ||
this.router = new Router(); | ||
// 接收注入的关键模块 | ||
this.routerInterface = dependencies.routerInterface; | ||
this.mongoInterface = dependencies.mongoInterface; | ||
|
||
this.app.logger = dependencies.loggerInterface; | ||
this.app.redis = dependencies.redisInterface; | ||
this.app.pluginApi = dependencies.apiInterface; | ||
this.app.pluginEvent = dependencies.eventInterface; | ||
this.app.hook = dependencies.hookInterface; | ||
this.app.model = {}; | ||
|
||
} | ||
|
||
initialize() { | ||
try { | ||
this.setupRoutes(); | ||
this.loadRouter(); | ||
this.loadModel(); | ||
this.initData(); | ||
} catch (error) { | ||
this.app.logger.error(error); | ||
} | ||
} | ||
/** | ||
* 加载路由 | ||
*/ | ||
loadRouter() { | ||
this.routerInterface && this.routerInterface.registerRoutes(this.router); | ||
} | ||
|
||
/** | ||
* 加载模型 | ||
* @returns | ||
*/ | ||
loadModel() { | ||
if (!this.mongoInterface) { | ||
this.app.logger.info("mongoInterface is null"); | ||
return; | ||
} | ||
|
||
const modelDefinitions = {}; | ||
const modelInstances = {}; // 用于存储实例化的模型 | ||
// 遍历当前目录下的所有模型文件 | ||
fs.readdirSync(__dirname + '/model').forEach(file => { | ||
if (path.extname(file) === '.js') { | ||
const modelName = path.basename(file, '.js'); | ||
modelDefinitions[modelName] = __dirname + '/model/' + file; | ||
} | ||
}); | ||
let property = 'user'; | ||
// 使用 Proxy 实现懒加载 | ||
this.app.model = new Proxy(modelDefinitions, { | ||
get: (target, property) => { | ||
if (!target[property]) { | ||
return undefined; // 模型定义不存在 | ||
} | ||
if (!modelInstances[property]) { | ||
// 如果模型尚未创建,则加载并创建模型 | ||
const model = require(target[property])(this.mongoInterface); | ||
modelInstances[property] = model; // 存储已创建的模型 | ||
} | ||
return modelInstances[property]; | ||
} | ||
}); | ||
} | ||
|
||
setupRoutes() { | ||
this.router.get('/', async ctx => { | ||
this.app.logger.info('Plugin A is handling the request , path: ' + ctx.path); | ||
ctx.body = 'Response from Plugin A'; | ||
}); | ||
// 其他路由... | ||
} | ||
|
||
// 初始化数据 | ||
initData() { | ||
// 如果user表中没有数据,则初始化一个管理员 | ||
this.app.model.user.countDocuments().then(count => { | ||
this.app.logger.info(`user count: ${count}`); | ||
if (count === 0) { | ||
this.app.model.user.create({ | ||
name: '管理员', | ||
account: 'admin', | ||
password: '123456' | ||
}); | ||
} | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const { Schema } = require("mongoose") | ||
module.exports = factory => { | ||
const TaskSchema = new Schema({ | ||
name: { type: String, required: true }, // 角色名称 | ||
code: { type: String, required: true }, // 角色编码 | ||
description: { type: String }, // 角色描述 | ||
permissions: [{ type: [ Object ] }], // 角色权限 | ||
createdAt: { type: Date, default: Date.now },// 创建时间 | ||
updatedAt: { type: Date, default: Date.now },// 更新时间 | ||
}); | ||
let schema = factory.createModel('role', TaskSchema); | ||
return schema; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const { Schema } = require("mongoose") | ||
module.exports = factory => { | ||
const TaskSchema = new Schema({ | ||
name: { type: String, required: true }, // 管理员姓名 | ||
email: { type: String }, // 管理员邮箱 | ||
account: { type: String, required: true }, // 管理员账号 | ||
password: {type: String, required: true}, // 密码 | ||
createdAt: { type: Date, default: Date.now }, // 创建时间 | ||
updatedAt: { type: Date, default: Date.now }, // 更新时间 | ||
lastLoginDate: { type: Date }, // 最后登录时间 | ||
lastLoginIp: { type: String }, // 最后登陆ip | ||
isEnabled: { type: Boolean, default: true }, // 是否启用 | ||
roles: { | ||
type: [{ type: Schema.Types.ObjectId, ref: 'role' }], | ||
default: [] | ||
} // 用户角色 | ||
}); | ||
|
||
let schema = factory.createModel('user', TaskSchema); | ||
return schema; | ||
}; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.