toArray()); HookService::afterListen('system_admin_login', $adminInfo, null, false, SystemBehavior::class); return true; } /** * 保存当前登陆用户信息 */ public static function setLoginInfo($adminInfo) { //补充角色信息 $roleSign = SystemRole::where(['id' => $adminInfo['roles']])->value('sign'); $adminInfo['role_sign'] = $roleSign ? $roleSign : ""; Session::set('adminId', $adminInfo['id'], 'admin'); Session::set('adminInfo', $adminInfo, 'admin'); } /** * 清空当前登陆用户信息 */ public static function clearLoginInfo() { Session::delete('adminInfo', 'admin'); Session::delete('adminId', 'admin'); Session::clear('admin'); } /** * 检查用户登陆状态 * @return bool */ public static function hasActiveAdmin() { return Session::has('adminId', 'admin') && Session::has('adminInfo', 'admin'); } /** * 获得登陆用户信息 * @return mixed */ public static function activeAdminInfoOrFail() { $adminInfo = Session::get('adminInfo', 'admin'); if (!$adminInfo) exception('请登陆'); if (!$adminInfo['status']) exception('该账号已被关闭!'); return $adminInfo; } /** * 获得登陆用户Id 如果没有直接抛出错误 * @return mixed */ public static function activeAdminIdOrFail() { $adminId = Session::get('adminId', 'admin'); if (!$adminId) exception('访问用户为登陆登陆!'); return $adminId; } /** * @return array */ public static function activeAdminAuthOrFail() { $adminInfo = self::activeAdminInfoOrFail(); return $adminInfo['level'] === 0 ? SystemRole::getAllAuth() : SystemRole::rolesByAuth($adminInfo['roles']); } /** * 获得有效管理员信息 * @param $id * @return static */ public static function getValidAdminInfoOrFail($id) { $adminInfo = self::get($id); if (!$adminInfo) exception('用户不能存在!'); if (!$adminInfo['status']) exception('该账号已被关闭!'); return $adminInfo; } /** * @param $field * @return false|\PDOStatement|string|\think\Collection */ 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 (isset($where['name']) && $where['name'] != '') { $model = $model->where('account|real_name', 'LIKE', "%$where[name]%"); } if (isset($where['roles']) && $where['roles'] != '') { $model = $model->where("CONCAT(',',roles,',') LIKE '%,$where[roles],%'"); } if (isset($where['level'])) { $model = $model->where('level', $where['level']); } $model = $model->where('is_del', 0); return self::page($model, function ($admin, $key) { $admin->roles = SystemRole::where('id', 'IN', $admin->roles)->column('role_name'); }, $where); } /**根据身份获取后台账号列表 * @param $sign 角色身份标识 * @return false|\PDOStatement|string|\think\Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function getRoleAdmin($sign = false) { $where['a.status'] = 1; if ((is_array($sign) && !$sign) || !$sign) { $role_sign = SystemRole::where('status', 1) ->field('sign') ->select() ->toArray(); $sign = array_column($role_sign, 'sign'); } if (!is_array($sign) && $sign) { $where['r.sign'] = $sign; } return self::where($where) ->whereIn('sign', $sign) ->alias('a') ->join('__SYSTEM_ROLE__ r', 'a.roles = r.id', 'LEFT') ->field('a.id as admin_id, a.account as admin_name, a.roles as role_id, a.level as admin_level, a.status as admin_status') ->select() ->toArray(); } /** * 检测用户权限身份 */ public static function testUserLevel($user) { $sign = self::sign; $role = SystemRole::where('sign', $sign)->where('status', 1)->find(); if (!$role) return false; $admin = self::where('phone', $user['phone'])->find(); if (!$admin) return false; if (in_array($role['id'], explode(',', $admin['roles']))) { return true; } else { return false; } } }