<?php


namespace app\services\connect;


use app\models\user\User;
use freedom\utils\SocketSession;
use think\Request;

/**
 * TODO 会员登录服务
 * Class UserConnectService
 * @package app\services\connect
 */
class UserConnectService
{
    /**
     * TODO 控制台登录处理
     * @param Request $event
     * @return void
     */
    public static function doUserConnect(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 = User::get(['id' => $userId, 'status' => 1, 'is_delete' => 0]);
            if($userInfo && $userInfo['login_token'] == $loginToken && $username == $userInfo['username']){
                SocketSession::saveSocketSession(['user_id' => $userId, 'username' => $username],'user');
                $ws->emit('onlineLogin',['status' => true, 'money' => $userInfo['money']]);
            }else{
                $ws->emit('onlineLogin',['status' => false, 'money' => 0, 'msg' => 'link_server_fail']);
                $ws->close();
            }
        }else{
            $ws->emit('onlineLogin',['status' => false, 'money' => 0, 'msg' => 'link_server_fail']);
            $ws->close();
        }
    }
}