Login.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace app\api\controller\mlbb;
  3. use app\common\model\Members;
  4. use think\Cache;
  5. use think\Db;
  6. use think\Env;
  7. use think\Request;
  8. class Login extends Mlbb
  9. {
  10. public function _initialize(){}
  11. public function index(){
  12. $input = Request::instance()->post(['username','password']);
  13. // 输入验证
  14. if(empty($input['username']) || empty($input['password'])){
  15. return $this->resultFail('用户名或密码不能为空!');
  16. }
  17. // 登录频率限制
  18. $loginKey = 'mlbb_login_attempt_' . md5($input['username'] . request()->ip());
  19. $attempts = Cache::store('redis')->get($loginKey, 0);
  20. if($attempts >= 5){
  21. return $this->resultFail('登录尝试次数过多,请稍后再试!');
  22. }
  23. try {
  24. // 加密后密码
  25. $password = auth_code($input['password'], "ENCODE", Env::get($this->authKey));
  26. // 用户信息
  27. $userInfo = (new Members())->where(['username' => $input['username'], 'password' => $password])->field('id,username,channel_id,flag')->find();
  28. if (empty($userInfo)) {
  29. // 增加失败次数
  30. Cache::store('redis')->set($loginKey, $attempts + 1, 300);
  31. return $this->resultFail('用户名或密码错误!');
  32. }
  33. // 账号是否被冻结
  34. if (1 == $userInfo['flag']) {
  35. $this->resultFail('您的账号已被冻结,请联系客服QQ:' . CUSTOMER_SERVICE_QQ);
  36. }
  37. // 子账号是否被冻结
  38. // $subaccountInfo = model('Subaccount')->where(['member_id' => $userInfo['id'], 'game_id' => ['in', $this->game_id]])->find();
  39. // if (1 == $subaccountInfo['flag']) {
  40. // $this->resultFail('您的子账号已被冻结,请联系客服QQ:' . CUSTOMER_SERVICE_QQ);
  41. // }
  42. // 获取用户的渠道ID(必须在游戏内注册账号和登录过的)
  43. $channel_ids = Db::table('nw_subaccount')->where(['member_id' => $userInfo['id'], 'game_id' => ['in', [344, 345, 10]]])->column('channel_id');
  44. if(!$channel_ids){
  45. $this->resultFail('请在游戏内注册账号和登录!');
  46. }
  47. $channel_id = $channel_ids[0];
  48. // 登录成功,清除失败记录
  49. Cache::store('redis')->rm($loginKey);
  50. $nowTime = NOW_TIMESTAMP;
  51. (new Members())->save(['update_time' => $nowTime, 'login_time' => $nowTime], ['id' => $userInfo['id']]);
  52. // 判断当前账户的 旅人活动信息是否存在,没有则初始化
  53. if(Db::table('lr_user_info')->where('user_id', $userInfo['id'])->count() <= 0){
  54. 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)]);
  55. }
  56. //token的随机码
  57. $token_random = random(8);
  58. $token = auth_code($userInfo['id'] . '|' . $userInfo['username'] . '|' . $token_random . '|sdk|mlbb', "ENCODE", Env::get($this->authKey));
  59. // redis中玩家登录安全控制的随机码
  60. Cache::store('redis')->set('token|sdk|mlbb|' . $userInfo['id'], $token_random, 60 * 60 * 24 * 7);
  61. $returnData['token'] = $token;
  62. $returnData['time'] = $nowTime;
  63. return $this->resultSuccess($returnData);
  64. } catch (\Exception $e) {
  65. \think\Log::error('Mlbb@Login error: ' . $e->getMessage());
  66. return $this->resultFail('系统异常:'.$e->getMessage());
  67. }
  68. }
  69. }