2026-03-10 18:45:36 +08:00
< ? php
namespace app\api\controller ;
use DateTime ;
use think\Controller ;
use think\Db ;
use think\db\Query ;
use think\Request ;
use think\Cache ;
use think\Log ;
header ( " Access-Control-Allow-Origin:* " );
header ( " Access-Control-Allow-Methods:GET, POST, OPTIONS, DELETE " );
header ( " Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding " );
class Role extends Common
{
//获取路由下对应的权限角色
public function getRouterList ()
{
$param = request () -> param ();
$currPage = isset ( $param [ 'currPage' ]) ? $param [ 'currPage' ] : 1 ;
$pageSize = isset ( $param [ 'pageSize' ]) ? $param [ 'pageSize' ] : 50 ;
$roleList = db ( 'system_role' ) -> page ( $currPage , $pageSize ) -> select ();
$total = db ( 'system_role' ) -> count ();
$res = [];
foreach ( $roleList as & $v )
{
$routerIds = explode ( " , " , $v [ 'router_id' ]);
$routers = db ( 'system_router' )
-> whereIn ( 'id' , $routerIds )
-> select ();
$v [ 'routers' ] = $routers ;
}
unset ( $v ); // 解除引用
$res [ 'list' ] = $roleList ;
$res [ 'total' ] = $total ;
$this -> sendSuccess ( $res );
}
//获取路由列表
public function getRouterListAll ()
{
$param = request () -> param ();
$routerList = db ( 'system_router' ) -> select ();
$this -> sendSuccess ( $routerList );
}
//获取router下对应的role
public function getRouterRoleMap ()
{
// $param = request()->param();
$results = db ( 'system_router' ) -> alias ( 'router' )
-> join ( 'system_role' , 'system_role.router_id = router.id OR system_role.router_id LIKE concat(router.id, \',%\') OR system_role.router_id LIKE concat(\'%,\', router.id) OR system_role.router_id LIKE concat(\'%,\', router.id, \',%\') OR system_role.router_id LIKE concat(\'%,\', router.id, \',%\')' , 'LEFT' )
-> field ( 'router.*, GROUP_CONCAT(system_role.role_name) as roles' )
2026-05-19 13:30:47 +08:00
-> group ( 'router.id' )
2026-03-10 18:45:36 +08:00
-> select ();
$this -> sendSuccess ( $results );
}
//更新role对应的router
public function updateRoleRouter (){
$param = request () -> param ();
$dbRoleName = 'system_role' ;
$update = [];
if ( isset ( $param [ 'role_name' ])) $update [ 'role_name' ] = $param [ 'role_name' ];
if ( isset ( $param [ 'role_des' ])) $update [ 'role_des' ] = $param [ 'role_des' ];
if ( isset ( $param [ 'router_id' ])) $update [ 'router_id' ] = $param [ 'router_id' ];
$int = db ( $dbRoleName ) -> where ( 'id' , $param [ 'id' ])
-> update ( $update );
if ( $int > 0 ){
return $this -> sendSuccess ( " succeed " );
} else {
return $this -> sendError ( " 修改失败 " );
}
}
//添加role以及对应的router
public function addRoleRouter (){
$param = request () -> param ();
$dbRoleName = 'system_role' ;
$roleInfo = [];
if ( isset ( $param [ 'role_name' ])) $roleInfo [ 'role_name' ] = $param [ 'role_name' ];
if ( isset ( $param [ 'role_des' ])) $roleInfo [ 'role_des' ] = $param [ 'role_des' ];
if ( isset ( $param [ 'router_id' ])) $roleInfo [ 'router_id' ] = $param [ 'router_id' ];
$int = db ( $dbRoleName ) -> insertGetId ( $roleInfo );
if ( $int > 0 ){
return $this -> sendSuccess ( " succeed " );
} else {
return $this -> sendError ( " 插入失败 " );
}
}
//删除
public function deleteRole ()
{
$param = request () -> param ();
$dbRoleName = 'system_role' ;
$int = db ( $dbRoleName ) -> delete ( $param [ 'id' ]);
if ( $int > 0 ){
return $this -> sendSuccess ( " succeed " );
} else {
return $this -> sendError ( " 删除失败 " );
}
}
}