简化路由代码

master
yutent 2024-07-01 10:34:58 +08:00
parent e436429872
commit b9c752f8c8
1 changed files with 6 additions and 4 deletions

View File

@ -10,10 +10,12 @@ export function createRouter() {
return function (req, res, next) { return function (req, res, next) {
let debug = this.get('debug') let debug = this.get('debug')
let dynamic = this.get('dynamic') let dynamic = this.get('dynamic')
let _ctrl = dynamic ? 'index' : req.controller
let _Module = this.$load(_ctrl)
// 1. 先判断控制器是否存在 // 1. 先判断控制器是否存在
if (!this.$load(dynamic ? 'index' : req.controller)) { if (!_Module) {
return res.error(`Controller [${req.controller}] not found`, 404) return res.error(`Controller [${_ctrl}] not found`, 404)
} }
// 2. 默认二级路由为index // 2. 默认二级路由为index
@ -22,7 +24,7 @@ export function createRouter() {
} }
// 3. 实例化控制器 // 3. 实例化控制器
this.$load(dynamic ? 'index' : req.controller) _Module
.then(async ModuleController => { .then(async ModuleController => {
let ctrol, route, act let ctrol, route, act
let err = '' let err = ''
@ -61,7 +63,7 @@ export function createRouter() {
} }
} }
} else { } else {
err = new Error(`Controller [${req.controller}] load error`) err = new Error(`Controller [${_ctrl}] load error`)
err.status = 500 err.status = 500
} }