-
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 8, 2024
1 parent
9c0d625
commit a51dfb2
Showing
6 changed files
with
74 additions
and
96 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,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; |
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 |
---|---|---|
@@ -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' | ||
}); | ||
} | ||
}); | ||
} | ||
}; |
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,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; | ||
} |
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,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; |