diff --git a/package-lock.json b/package-lock.json index e5aa84d..ec5b33c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,16 +1,16 @@ { - "name": "modularkit-demo", + "name": "wbrick-demo", "version": "1.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "modularkit-demo", + "name": "wbrick-demo", "version": "1.0.0", "license": "ISC", "dependencies": { "koa": "^2.14.2", - "wbrick": "^0.0.14" + "wbrick": "^0.0.17" }, "devDependencies": { "cross-env": "^7.0.3" @@ -1177,9 +1177,9 @@ } }, "node_modules/wbrick": { - "version": "0.0.14", - "resolved": "https://registry.npmjs.org/wbrick/-/wbrick-0.0.14.tgz", - "integrity": "sha512-VZkka/FYH3nQ40Ax1puzsno7D7In3uc2gfC7qesotDW4CvLeX5yx51z7OLfD8Hn0xHFHqW/Q1tnDAePLOX94GQ==", + "version": "0.0.17", + "resolved": "https://registry.npmjs.org/wbrick/-/wbrick-0.0.17.tgz", + "integrity": "sha512-XZDfA+wheq5R0XsKVleUM0Xet7Vj1j2FxF/BlCdHgkcwlcF6dajIyEyQVYGGhki5pZ8I0uIxO2gzdmnLGDjIzw==", "dependencies": { "config": "^3.3.9", "ioredis": "^5.3.2", @@ -2060,9 +2060,9 @@ "integrity": "sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==" }, "wbrick": { - "version": "0.0.14", - "resolved": "https://registry.npmjs.org/wbrick/-/wbrick-0.0.14.tgz", - "integrity": "sha512-VZkka/FYH3nQ40Ax1puzsno7D7In3uc2gfC7qesotDW4CvLeX5yx51z7OLfD8Hn0xHFHqW/Q1tnDAePLOX94GQ==", + "version": "0.0.17", + "resolved": "https://registry.npmjs.org/wbrick/-/wbrick-0.0.17.tgz", + "integrity": "sha512-XZDfA+wheq5R0XsKVleUM0Xet7Vj1j2FxF/BlCdHgkcwlcF6dajIyEyQVYGGhki5pZ8I0uIxO2gzdmnLGDjIzw==", "requires": { "config": "^3.3.9", "ioredis": "^5.3.2", diff --git a/package.json b/package.json index 44faa82..20ccb0f 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/plugins/manager/controller/user.js b/plugins/manager/controller/user.js new file mode 100644 index 0000000..a418b5d --- /dev/null +++ b/plugins/manager/controller/user.js @@ -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; diff --git a/plugins/manager/index.js b/plugins/manager/index.js index 42f30e8..cc33c37 100644 --- a/plugins/manager/index.js +++ b/plugins/manager/index.js @@ -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' - }); - } - }); - } }; \ No newline at end of file diff --git a/plugins/manager/routes/index.js b/plugins/manager/routes/index.js index e69de29..4870b0f 100644 --- a/plugins/manager/routes/index.js +++ b/plugins/manager/routes/index.js @@ -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; +} \ No newline at end of file diff --git a/plugins/manager/service/user.js b/plugins/manager/service/user.js new file mode 100644 index 0000000..cf2c165 --- /dev/null +++ b/plugins/manager/service/user.js @@ -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;