Code.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace App\Module\AppGame\Middleware;
  3. use App\Module\AppGame\SessionApp;
  4. use App\Module\System\Services\ConfigService;
  5. use App\Module\Ulogic\Models\CodeError;
  6. use App\Module\Ulogic\Models\UserBan;
  7. use App\Module\User\Enums\STATUS2;
  8. use App\Module\User\Logic\User;
  9. use Closure;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\DB;
  12. use UCore\Helper\Logger;
  13. use Uraus\Kku\Response;
  14. /**
  15. * 明文检测
  16. */
  17. class Code
  18. {
  19. private $handlerMap = [
  20. // 登录
  21. 'App\Module\AppGame\Handler\Public\Login4ursHandler' => 'login_code',
  22. // 种菜
  23. 'App\Module\AppGame\Handler\Land\SowHandler' => 'sow_code',
  24. // 施肥
  25. 'App\Module\AppGame\Handler\Land\FertilizerHandler' => 'fertilizer_code',
  26. // 收菜
  27. 'App\Module\AppGame\Handler\Land\HarvestHandler' => 'harvest_code',
  28. // 市场买卖
  29. 'App\Module\AppGame\Handler\Matchexchange\AddHandler' => 'add_code'
  30. ];
  31. /**
  32. * Handle an incoming request.
  33. *
  34. * @param $name
  35. * @param \Illuminate\Http\Request $request
  36. * @param \Closure $next
  37. * @return mixed
  38. */
  39. public function handle(Request $request, Closure $next)
  40. {
  41. // dd($request->attributes->get('_handler'));
  42. Logger::info('明文中间件执行');
  43. // 是否校验明文
  44. $isCheck = ConfigService::getValueDefault('is_check_code');
  45. if (!$isCheck) {
  46. return $next($request);
  47. }
  48. // 获取当前请求的Handler类
  49. $handler = $request->attributes->get('_handler');
  50. $class = get_class($handler);
  51. // 当前请求是否需要校验明文
  52. if (isset($this->handlerMap[$class])) {
  53. // 获取动作
  54. $action = $this->handlerMap[$class];
  55. // 获取用户明文
  56. $userCode = $request->header('sysCode');
  57. // 系统明文
  58. $systemCode = ConfigService::getValueDefault($this->handlerMap[$class]);
  59. Logger::info('动作:'.$action.' 用户输入明文:'.$userCode.' 系统明文:'.$systemCode);
  60. // 判断明文是否一致
  61. $check = false;
  62. if ($userCode === $systemCode) {
  63. $check = true;
  64. }
  65. // 明文不一致
  66. if (!$check) {
  67. // 登录单独处理
  68. if ($action == 'login_code') {
  69. return $this->loginError();
  70. }
  71. $userId = SessionApp::getUserId();
  72. // 记录错误次数
  73. $this->error($userId, $this->handlerMap[$class]);
  74. // 查询是否封禁
  75. $this->isNeedProhibit($userId);
  76. }
  77. }
  78. return $next($request);
  79. }
  80. /**
  81. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Foundation\Application|\Illuminate\Http\JsonResponse|\Illuminate\Http\Response|object
  82. * 登录明文错误,不允许登录
  83. */
  84. private function loginError()
  85. {
  86. $response = new Response();
  87. $response->setCode(\Uraus\Kku\Common\RESPONSE_CODE::REQUEST_ERROR);
  88. $response->setMsg('非法登录');
  89. return \App\Module\AppGame\Tools\Protobuf::response($response);
  90. }
  91. /**
  92. * @param $userId
  93. * @param $type
  94. * @return void
  95. * 记录明文错误
  96. */
  97. private function error($userId, $type)
  98. {
  99. if (!$userId) {
  100. $userId = 0;
  101. }
  102. DB::transaction(function () use ($userId, $type) {
  103. $record = CodeError::firstOrNew([
  104. 'user_id' => $userId,
  105. 'type' => $type
  106. ]);
  107. if ($record->exists) {
  108. $record->increment('num');
  109. } else {
  110. $record->num = 1;
  111. $record->save();
  112. }
  113. });
  114. }
  115. /**
  116. * @param $userId
  117. * @return void
  118. * 封禁账号
  119. */
  120. private function isNeedProhibit($userId)
  121. {
  122. $sysTemCount = ConfigService::getValueDefault('sys_code_error_prohibit_count', 20);
  123. $userCount = CodeError::query()->where('user_id', $userId)->sum('num');
  124. if ($userCount >= $sysTemCount) {
  125. $insert = [
  126. 'user_id' => $userId,
  127. 'type' => 3,
  128. 'admin_id' => 0,
  129. 'end_time' => 0,
  130. 'remark' => '明文错误达到'.$sysTemCount.'次封禁',
  131. 'created_at' => date('Y-m-d H:i:s'),
  132. 'updated_at' => date('Y-m-d H:i:s'),
  133. ];
  134. try {
  135. DB::beginTransaction();
  136. // 修改用户封禁状态
  137. User::changeStatus2($userId, STATUS2::Ban->value());
  138. // 记录用户封禁信息
  139. UserBan::query()->insert($insert);
  140. SessionApp::removeUKeys($userId);
  141. DB::commit();
  142. } catch (\Exception $e) {
  143. Logger::error('明文封禁错误:'.$e->getMessage());
  144. DB::rollBack();
  145. }
  146. }
  147. }
  148. }