Ejemplo n.º 1
0
 public static function getTreeByAdmin($admin_id = -1)
 {
     if ($admin_id == -1) {
         $admin = new SysUser();
         $admin->account = 'root';
     } else {
         $admin = SysUser::find($admin_id);
         if (empty($admin)) {
             throw new Exception("没有查找到admin数据", 10003);
         }
     }
     if ($admin->account == 'root') {
         $list = SysMenu::get();
     } else {
         $list = SysMenu::select('sys_menus.*', 'sys_role_menus.r_id AS checked')->join('sys_role_menus', function ($q) {
             $q->on('sys_menus.id', '=', 'sys_role_menus.m_id');
         })->join('sys_user_roles', function ($q) use($admin_id) {
             $q->on('sys_user_roles.r_id', '=', 'sys_role_menus.r_id')->where('sys_user_roles.admin_id', '=', $admin_id);
         })->groupBy('sys_menus.id')->get();
     }
     $re = SysMenu::makeTree($list);
     return $re['tree'];
 }
 public function listUserRole($id)
 {
     $per_page = Input::get('per_page', null);
     try {
         $admin = SysUser::find($id);
         if (empty($admin)) {
             throw new Exception("没有找到用户", 10001);
         }
         $query = SysRole::select('sys_roles.*')->join('sys_user_roles', function ($q) use($id) {
             $q->on('sys_roles.id', '=', 'sys_user_roles.r_id')->where('sys_user_roles.admin_id', '=', $id);
         });
         $list = $query->paginate($per_page);
         $data['rows'] = [];
         foreach ($list as $key => $role) {
             $data['rows'][] = $role->showInList();
         }
         $data['total'] = $list->getTotal();
         $re = Tools::reTrue('获取角色成功', $data, $list);
     } catch (Exception $e) {
         $re = Tools::reFalse($e->getCode(), '获取角色失败:' . $e->getMessage());
     }
     return Response::json($re);
 }