Skip to content

Commit

Permalink
优化插件的目录结构
Browse files Browse the repository at this point in the history
  • Loading branch information
shitiandmw committed Jan 8, 2024
1 parent 9c0d625 commit a51dfb2
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 96 deletions.
18 changes: 9 additions & 9 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"license": "ISC",
"dependencies": {
"koa": "^2.14.2",
"wbrick": "^0.0.14"
"wbrick": "^0.0.17"
},
"devDependencies": {
"cross-env": "^7.0.3"
Expand Down
19 changes: 19 additions & 0 deletions plugins/manager/controller/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { Controller } = require('wbrick');

class UserController extends Controller {
async handle() {
let count = await this.app.model.user.countDocuments();
// 获得请求参数
const { name, email } = this.ctx.query;
// 现在可以使用 this.ctx 访问上下文
this.ctx.body = `Response from UserController,Test app.model:${count},name:${name},email:${email}`;
}
async testService()
{
this.ctx.user = "shitian";
let user = await this.ctx.service.user.test();
this.ctx.body = 'Response from UserController,Test service.ctx:'+user;
}
}

module.exports = UserController;
101 changes: 15 additions & 86 deletions plugins/manager/index.js
Original file line number Diff line number Diff line change
@@ -1,97 +1,26 @@
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;
const { Plugin } = require('wbrick');
const routes = require('./routes');

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 = {};
module.exports = class ManagerPlugin extends Plugin {

}

initialize() {
async initialize() {
try {
this.setupRoutes();
this.loadRouter();
this.modelPath = path.join(__dirname, 'model');
this.controllerPath = path.join(__dirname, 'controller');
this.servicePath = path.join(__dirname, 'service');

this.loadModel();
this.initData();
this.LoadController();
this.loadService();
this.loadRouter(routes(this.app));

// 初始化超级管理员
await this.app.service.user.initUser();

} 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'
});
}
});
}
};
10 changes: 10 additions & 0 deletions plugins/manager/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const Router = require('koa-router');

module.exports = (app) => {
const router = new Router();
const { controller } = app;
router.get('/', controller.user.handle);
router.get('/test', controller.user.testService);
// 其他路由...
return router;
}
20 changes: 20 additions & 0 deletions plugins/manager/service/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { Service } = require('wbrick');
class UserService extends Service {
async test() {
return this.ctx.user;
}

async initUser() {
let userCount = await this.app.model.user.countDocuments();
this.app.logger.info('initUser userCount:' + userCount);
if (userCount === 0) {
await this.app.model.user.create({
name: '管理员',
account: 'admin',
password: '123456'
});
}
}
}

module.exports = UserService;

0 comments on commit a51dfb2

Please sign in to comment.