<?php


namespace app\services\connect;


use app\models\manager\Manager;
use freedom\utils\SocketSession;
use think\Request;

/**
 * TODO 经理登录服务
 * Class ManagerConnectService
 * @package app\services\connect
 */
class ManagerConnectService
{
    /**
     * TODO 控制台登录处理
     * @param Request $event
     * @return void
     */
    public static function doManagerConnect(Request $event){
        $ws = app('\think\swoole\WebSocket');
        $userId = intval($event->get('userid'));
        $loginToken = trim($event->get('login_token'));
        $username = trim($event->get('account'));
        if(isset($username) && isset($loginToken) && isset($userId) && $userId > 0){
            $userInfo = Manager::get(['id' => $userId, 'status' => 1]);
            if($userInfo && $userInfo['login_token'] == $loginToken && $username == $userInfo['username']){
                SocketSession::saveSocketSession(['user_id' => $userId, 'username' => $username],'manager');
                $tableManager = app('swoole.table.manager');
                $managerSession = $tableManager->get((string) $userId);
                $ws->setSender(0)->to($managerSession['fd'])->emit('onlineLogin',['status' => true]);
            }else{
                $ws->emit('onlineLogin',['status' => false,'msg' => 'link_server_fail']);
                $ws->close();
            }
        }else{
            $ws->emit('onlineLogin',['status' => false,'msg' => 'link_server_fail']);
            $ws->close();
        }
    }
}