Login4ursHandler.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Module\AppGame\Handler\Public;
  3. use App\Module\AppGame\Handler\BaseHandler;
  4. use App\Module\User\Enums\STATUS2;
  5. use Dotenv\Exception\ValidationException;
  6. use Google\Protobuf\Internal\Message;
  7. use Illuminate\Support\Facades\Log;
  8. use UCore\Exception\LogicException;
  9. use UCore\Exception\ValidateException;
  10. use UCore\Helper\Logger;
  11. use Uraus\Kku\Request\RequestPublicLogin4urs;
  12. use Uraus\Kku\Response\ResponsePublicLogin4urs;
  13. use Uraus\Kku\Response\LastLoginInfo;
  14. /**
  15. * 处理URS login4urs登录请求
  16. *
  17. * 使用URS的手机号+密码进行登录,自动创建用户并同步推荐关系
  18. */
  19. class Login4ursHandler extends BaseHandler
  20. {
  21. /**
  22. * 是否需要登录
  23. *
  24. * @var bool
  25. */
  26. protected bool $need_login = false;
  27. /**
  28. * 处理login4urs登录请求
  29. *
  30. * @param RequestPublicLogin4urs $data 登录请求数据
  31. * @return ResponsePublicLogin4urs 登录响应
  32. */
  33. public function handle(Message $data): Message
  34. {
  35. // throw new LogicException('报错给前端');
  36. $mobile = '';
  37. $password = '';
  38. try {
  39. Log::info('Login4urs登录请求开始处理');
  40. // 1. 验证登录参数
  41. $mobile = $data->getMobile();
  42. $password = $data->getPassword();
  43. Log::info('获取登录参数', [
  44. 'mobile' => $mobile,
  45. 'has_password' => !empty($password)
  46. ]);
  47. if (Logger($mobile)) {
  48. Log::error('手机号验证失败', ['error' => '手机号不能为空']);
  49. throw new \Exception('手机号不能为空');
  50. }
  51. if (empty($password)) {
  52. Logger::error('密码验证失败', ['error' => '密码不能为空']);
  53. throw new \Exception('密码不能为空');
  54. }
  55. // 2. 使用Login4uHandler的公共静态方法处理URS登录逻辑
  56. $loginResult = Login4uHandler::processUrsLoginByMobilePassword($mobile, $password);
  57. // 3. 创建响应对象
  58. $response = new ResponsePublicLogin4urs();
  59. // 设置用户状态(默认不封禁)
  60. $response->setIsProhibit(false);
  61. if ($loginResult['user']->status2->value == STATUS2::Ban->value) {
  62. $response->setIsProhibit(true);
  63. }else{
  64. $response->setToken($loginResult['sessionId']);
  65. }
  66. // 设置最后登录信息
  67. $lastLoginInfo = new LastLoginInfo();
  68. $lastLoginInfo->setLastLoginTimes(time());
  69. $response->setLastLoginInfo($lastLoginInfo);
  70. Log::info('URS login4urs登录成功', [
  71. 'mobile' => $mobile,
  72. 'urs_user_id' => $loginResult['ursUserId'],
  73. 'farm_user_id' => $loginResult['farmUserId'],
  74. 'session_id' => $loginResult['sessionId']
  75. ]);
  76. return $response;
  77. } catch (\Exception $e) {
  78. Log::error('URS login4urs登录失败', [
  79. 'mobile' => $mobile ?? '',
  80. 'has_password' => !empty($password),
  81. 'error' => $e->getMessage(),
  82. 'trace' => $e->getTraceAsString()
  83. ]);
  84. throw $e;
  85. }
  86. }
  87. }