toArray()); Session::save(); } /** * 清空当前登陆用户信息 */ public static function clearLoginInfo() { Session::delete('adminInfo'); Session::delete('adminId'); Session::save(); } /** * 检查用户登陆状态 * @return bool */ public static function hasActiveAdmin() { return Session::has('adminId') && Session::has('adminInfo'); } /** * 获得登陆用户信息 * @return mixed * @throws \Exception */ public static function activeAdminInfoOrFail() { $adminInfo = Session::get('adminInfo'); if (!$adminInfo) exception('请登陆'); if (!$adminInfo['status']) exception('该账号已被关闭!'); return $adminInfo; } /** * 获得登陆用户Id 如果没有直接抛出错误 * @return mixed * @throws \Exception */ public static function activeAdminIdOrFail() { $adminId = Session::get('adminId'); if (!$adminId) exception('访问用户为登陆登陆!'); return $adminId; } /** * @return array|null * @throws \Exception */ public static function activeAdminAuthOrFail() { $adminInfo = self::activeAdminInfoOrFail(); if (is_object($adminInfo)) $adminInfo = $adminInfo->toArray(); return $adminInfo['level'] === 0 ? SystemRole::getAllAuth() : SystemRole::rolesByAuth($adminInfo['roles']); } /** * 获得有效管理员信息 * @param $id * @return mixed * @throws \Exception */ public static function getValidAdminInfoOrFail($id) { $adminInfo = self::get($id); if (!$adminInfo) exception('用户不能存在!'); if (!$adminInfo['status']) exception('该账号已被关闭!'); return $adminInfo; } /** * @param string $field * @param int $level * @return \think\Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function getOrdAdmin($field = 'real_name,id', $level = 0) { return self::where('level', '>=', $level)->field($field)->select(); } public static function getTopAdmin($field = 'real_name,id') { return self::where('level', 0)->field($field)->select(); } /** * @param $where * @return array */ public static function systemPage($where) { $model = new self; if ($where['name'] != '') $model = $model->where('account|real_name', 'LIKE', "%$where[name]%"); if ($where['roles'] != '') $model = $model->where("CONCAT(',',roles,',') LIKE '%,$where[roles],%'"); $model = $model->where('level', $where['level'])->where('is_del', 0); return self::page($model, function ($admin) { $admin->roles = SystemRole::where('id', 'IN', $admin->roles)->column('role_name', 'id'); }, $where); } }