const router = require ( 'koa-router' ) ( )
// 全局路由
router . prefix ( '/menu' )
router . get ( '/getMenu' , async ( ctx , next ) => {
const [ rows , fields ] = await global . SQL . execute ( 'SELECT * FROM `menu` WHERE `isdelete` = 0' ) ;
ctx . body = {
data : rows ,
state : true ,
message : '获取内容成功'
}
} )
router . delete ( '/deleteMenu' , async ( ctx , next ) => {
const arg = ctx . request . query ;
if ( ! arg . id ) {
ctx . body = {
data : { } ,
state : false ,
message : '缺少id'
}
return
}
const [ rows , fields ] = await global . SQL . execute ( 'UPDATE menu SET isdelete = 1 WHERE `id` = ?' , [ arg . id ] ) ;
ctx . body = {
data : rows ,
state : true ,
message : '删除菜单成功'
}
} )
router . put ( '/updateMenu' , async ( ctx , next ) => {
const arg = ctx . request . body ;
const field = [ 'name' , 'route' , 'rank' , 'remarks' , 'father' , 'icon' ]
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 menu SET name = ? , rank = ? , remarks = ? , route = ?, icon = ? WHERE id = ?' , [ arg . name , arg . rank , arg . remarks , arg . route , arg . icon , Number ( arg . id ) ] ) ;
ctx . body = ctx . body = {
data : rows ,
state : true ,
message : '更新成功'
}
} )
router . post ( '/createMneuItem' , async ( ctx , next ) => {
const arg = ctx . request . body ;
const field = [ 'name' , 'route' , 'rank' , 'remarks' , 'father' , 'icon' ]
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 [ checket , checketFields ] = await global . SQL . execute ( 'SELECT * FROM `menu` WHERE `isdelete` = 0 AND `route` = ?' , [ arg . route ] ) ;
if ( checket . length != 0 ) {
ctx . body = {
data : { } ,
state : false ,
message : '路由标志已存在:' + arg . route
}
return
}
const createTime = new Date ( ) . getTime ( )
const [ rows , fields ] = await global . SQL . execute ( 'INSERT INTO menu (name, rank, route, father, remarks, createTime) VALUES (?, ?, ?, ?, ?, ?)' , [ arg . name , arg . rank , arg . route , arg . father , arg . remarks , createTime ] )
ctx . body = {
data : rows ,
state : true ,
message : '创建成功'
}
} )
module . exports = router