const router = require ( 'koa-router' ) ( )
// 全局路由
router . prefix ( '/container' )
router . get ( '/' , async ( ctx , next ) => {
const arg = ctx . request . query
if ( ! Object . keys ( arg ) . includes ( 'menuId' ) ) {
ctx . body = {
data : { } ,
state : false ,
message : '缺少MenuId'
}
return
}
const [ rows , fields ] = await global . SQL . execute ( 'SELECT * FROM `container` WHERE `isdelete` = 0 AND `menuId` = ?' , [ arg . menuId ] ) ;
ctx . body = {
data : rows ,
state : true ,
message : '获取内容成功'
}
} )
router . get ( '/getPage' , async ( ctx , next ) => {
const arg = ctx . request . query
if ( ! Object . keys ( arg ) . includes ( 'route' ) ) {
ctx . body = {
data : { } ,
state : false ,
message : '缺少Route'
}
return
}
const [ oldRows , oldFields ] = await global . SQL . execute ( 'SELECT * FROM `menu` WHERE `isdelete` = 0 AND `route` = ?' , [ arg . route ] ) ;
console . log ( oldRows )
if ( oldRows . length == 0 ) {
ctx . body = {
data : { } ,
state : false ,
message : '未找到页面'
}
return
}
const [ rows , fields ] = await global . SQL . execute ( 'SELECT * FROM `container` WHERE `isdelete` = 0 AND `menuId` = ?' , [ oldRows [ 0 ] . id ] ) ;
ctx . body = {
data : rows ,
state : true ,
message : '获取内容成功'
}
} )
router . delete ( '/' , async ( ctx , next ) => {
const arg = ctx . request . query
console . log ( arg )
if ( ! Object . keys ( arg ) . includes ( 'id' ) ) {
ctx . body = {
data : { } ,
state : false ,
message : '缺少id'
}
return
}
const [ rows , fields ] = await global . SQL . execute ( 'UPDATE container SET isdelete = 1 WHERE `id` = ?' , [ arg . id ] ) ;
ctx . body = {
data : rows ,
state : true ,
message : '删除内容成功'
}
} )
router . put ( '/' , async ( ctx , next ) => {
const arg = ctx . request . body
const field = [ 'id' , 'name' , 'rank' , 'config' , 'body' ]
const noField = [ ]
field . map ( item => {
if ( ! Object . keys ( arg ) . includes ( item ) ) noField . push ( item )
} )
if ( noField . length != 0 ) {
ctx . body = {
data : { } ,
state : false ,
message : '缺少' + noField
}
return
}
const [ rows , fields ] = await global . SQL . execute ( 'UPDATE container SET name = ? , rank = ? , config = ? , body = ? WHERE id = ?' , [ arg . name , arg . rank , arg . config , arg . body , Number ( arg . id ) ] ) ;
ctx . body = ctx . body = {
data : rows ,
state : true ,
message : '更新成功'
}
} )
router . post ( '/createContainer' , async ( ctx , next ) => {
const arg = ctx . request . body
const field = [ 'menuId' , 'name' , 'rank' , 'config' , 'body' ]
const noField = [ ]
field . map ( item => {
if ( ! Object . keys ( arg ) . includes ( item ) ) noField . push ( item )
} )
if ( noField . length != 0 ) {
ctx . body = {
data : { } ,
state : false ,
message : '缺少' + noField
}
return
}
const createTime = new Date ( ) . getTime ( )
const [ rows , fields ] = await global . SQL . execute ( 'INSERT INTO container (menuid, name, rank, config, body, createTime) VALUES (?, ?, ?, ?, ?, ?)' , [ arg . menuId , arg . name , arg . rank , arg . config , arg . body , createTime ] ) ;
ctx . body = {
data : rows ,
state : true ,
message : '创建成功'
}
} )
module . exports = router