// +---------------------------------------------------------------------- namespace app\services\workerman; use app\services\system\admin\AdminAuthServices; use crmeb\exceptions\AuthException; use crmeb\services\workerman\Response; use Workerman\Connection\TcpConnection; /** * 管理后台长连接业务处理 * 处理 admin 端 workerman 连接的登录鉴权等业务逻辑 */ class WorkermanHandle { /** * @var WorkermanService */ protected $service; /** * @param WorkermanService $service */ public function __construct(WorkermanService &$service) { $this->service = &$service; } /** * 管理员登录鉴权 * @param TcpConnection $connection * @param array $res * @param Response $response * @return bool|null */ public function login(TcpConnection &$connection, array $res, Response $response) { if (!isset($res['data']) || !$token = $res['data']) { return $response->close([ 'msg' => '授权失败!' ]); } try { /** @var AdminAuthServices $adminAuthService */ $adminAuthService = app()->make(AdminAuthServices::class); $authInfo = $adminAuthService->parseToken($token); } catch (AuthException $e) { return $response->close([ 'msg' => $e->getMessage(), 'code' => $e->getCode() ]); } if (!$authInfo || !isset($authInfo['id'])) { return $response->close([ 'msg' => '授权失败!' ]); } $connection->adminInfo = $authInfo; $connection->adminId = $authInfo['id']; $this->service->setUser($connection); return $response->success(); } }