post(['username','password']); // 输入验证 if(empty($input['username']) || empty($input['password'])){ return $this->resultFail('用户名或密码不能为空!'); } // 登录频率限制 $loginKey = 'mlbb_login_attempt_' . md5($input['username'] . request()->ip()); $attempts = Cache::store('redis')->get($loginKey, 0); if($attempts >= 5){ return $this->resultFail('登录尝试次数过多,请稍后再试!'); } try { // 加密后密码 $password = auth_code($input['password'], "ENCODE", Env::get($this->authKey)); // 用户信息 $userInfo = (new Members())->where(['username' => $input['username'], 'password' => $password])->field('id,username,channel_id,flag')->find(); if (empty($userInfo)) { // 增加失败次数 Cache::store('redis')->set($loginKey, $attempts + 1, 300); return $this->resultFail('用户名或密码错误!'); } // 账号是否被冻结 if (1 == $userInfo['flag']) { $this->resultFail('您的账号已被冻结,请联系客服QQ:' . CUSTOMER_SERVICE_QQ); } // 子账号是否被冻结 // $subaccountInfo = model('Subaccount')->where(['member_id' => $userInfo['id'], 'game_id' => ['in', $this->game_id]])->find(); // if (1 == $subaccountInfo['flag']) { // $this->resultFail('您的子账号已被冻结,请联系客服QQ:' . CUSTOMER_SERVICE_QQ); // } // 获取用户的渠道ID(必须在游戏内注册账号和登录过的) $channel_ids = Db::table('nw_subaccount')->where(['member_id' => $userInfo['id'], 'game_id' => ['in', [344, 345, 10]]])->column('channel_id'); if(!$channel_ids){ $this->resultFail('请在游戏内注册账号和登录!'); } $channel_id = $channel_ids[0]; // 登录成功,清除失败记录 Cache::store('redis')->rm($loginKey); $nowTime = NOW_TIMESTAMP; (new Members())->save(['update_time' => $nowTime, 'login_time' => $nowTime], ['id' => $userInfo['id']]); // 判断当前账户的 旅人活动信息是否存在,没有则初始化 if(Db::table('lr_user_info')->where('user_id', $userInfo['id'])->count() <= 0){ Db::table('lr_user_info')->insert(['user_id' => $userInfo['id'], 'username' => $userInfo['username'], 'channel_id' => $channel_id, 'create_time' => date('Y-m-d H:i:s', $nowTime)]); } //token的随机码 $token_random = random(8); $token = auth_code($userInfo['id'] . '|' . $userInfo['username'] . '|' . $token_random . '|sdk|mlbb', "ENCODE", Env::get($this->authKey)); // redis中玩家登录安全控制的随机码 Cache::store('redis')->set('token|sdk|mlbb|' . $userInfo['id'], $token_random, 60 * 60 * 24 * 7); $returnData['token'] = $token; $returnData['time'] = $nowTime; return $this->resultSuccess($returnData); } catch (\Exception $e) { \think\Log::error('Mlbb@Login error: ' . $e->getMessage()); return $this->resultFail('系统异常:'.$e->getMessage()); } } }