Skip to content

Commit

Permalink
暂存
Browse files Browse the repository at this point in the history
  • Loading branch information
shitiandmw committed Jan 5, 2024
1 parent 86809a5 commit 9c0d625
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 88 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "modularkit-demo",
"name": "wbrick-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
Expand All @@ -12,7 +12,7 @@
"license": "ISC",
"dependencies": {
"koa": "^2.14.2",
"wbrick": "^0.0.12"
"wbrick": "^0.0.14"
},
"devDependencies": {
"cross-env": "^7.0.3"
Expand Down
97 changes: 97 additions & 0 deletions plugins/manager/index.js
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'
});
}
});
}
};
13 changes: 13 additions & 0 deletions plugins/manager/model/role.js
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;
};
21 changes: 21 additions & 0 deletions plugins/manager/model/user.js
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.
66 changes: 0 additions & 66 deletions plugins/user/index.js

This file was deleted.

13 changes: 0 additions & 13 deletions plugins/user/model/user.js

This file was deleted.

0 comments on commit 9c0d625

Please sign in to comment.