| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- <?php
- namespace App\Module\AppGame\Middleware;
- use App\Module\AppGame\SessionApp;
- use App\Module\System\Services\ConfigService;
- use App\Module\Ulogic\Models\CodeError;
- use App\Module\Ulogic\Models\UserBan;
- use App\Module\User\Enums\STATUS2;
- use App\Module\User\Logic\User;
- use Closure;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- use UCore\Helper\Logger;
- use Uraus\Kku\Response;
- /**
- * 明文检测
- */
- class Code
- {
- private $handlerMap = [
- // 登录
- 'App\Module\AppGame\Handler\Public\Login4ursHandler' => 'login_code',
- // 种菜
- 'App\Module\AppGame\Handler\Land\SowHandler' => 'sow_code',
- // 施肥
- 'App\Module\AppGame\Handler\Land\FertilizerHandler' => 'fertilizer_code',
- // 收菜
- 'App\Module\AppGame\Handler\Land\HarvestHandler' => 'harvest_code',
- // 市场买卖
- 'App\Module\AppGame\Handler\Matchexchange\AddHandler' => 'add_code'
- ];
- /**
- * Handle an incoming request.
- *
- * @param $name
- * @param \Illuminate\Http\Request $request
- * @param \Closure $next
- * @return mixed
- */
- public function handle(Request $request, Closure $next)
- {
- // dd($request->attributes->get('_handler'));
- Logger::info('明文中间件执行');
- // 是否校验明文
- $isCheck = ConfigService::getValueDefault('is_check_code');
- if (!$isCheck) {
- return $next($request);
- }
- // 获取当前请求的Handler类
- $handler = $request->attributes->get('_handler');
- $class = get_class($handler);
- // 当前请求是否需要校验明文
- if (isset($this->handlerMap[$class])) {
- // 获取动作
- $action = $this->handlerMap[$class];
- // 获取用户明文
- $userCode = $request->header('sysCode');
- // 系统明文
- $systemCode = ConfigService::getValueDefault($this->handlerMap[$class]);
- Logger::info('动作:'.$action.' 用户输入明文:'.$userCode.' 系统明文:'.$systemCode);
- // 判断明文是否一致
- $check = false;
- if ($userCode === $systemCode) {
- $check = true;
- }
- // 明文不一致
- if (!$check) {
- // 登录单独处理
- if ($action == 'login_code') {
- return $this->loginError();
- }
- $userId = SessionApp::getUserId();
- // 记录错误次数
- $this->error($userId, $this->handlerMap[$class]);
- // 查询是否封禁
- $this->isNeedProhibit($userId);
- }
- }
- return $next($request);
- }
- /**
- * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Foundation\Application|\Illuminate\Http\JsonResponse|\Illuminate\Http\Response|object
- * 登录明文错误,不允许登录
- */
- private function loginError()
- {
- $response = new Response();
- $response->setCode(\Uraus\Kku\Common\RESPONSE_CODE::REQUEST_ERROR);
- $response->setMsg('非法登录');
- return \App\Module\AppGame\Tools\Protobuf::response($response);
- }
- /**
- * @param $userId
- * @param $type
- * @return void
- * 记录明文错误
- */
- private function error($userId, $type)
- {
- if (!$userId) {
- $userId = 0;
- }
- DB::transaction(function () use ($userId, $type) {
- $record = CodeError::firstOrNew([
- 'user_id' => $userId,
- 'type' => $type
- ]);
- if ($record->exists) {
- $record->increment('num');
- } else {
- $record->num = 1;
- $record->save();
- }
- });
- }
- /**
- * @param $userId
- * @return void
- * 封禁账号
- */
- private function isNeedProhibit($userId)
- {
- $sysTemCount = ConfigService::getValueDefault('sys_code_error_prohibit_count', 20);
- $userCount = CodeError::query()->where('user_id', $userId)->sum('num');
- if ($userCount >= $sysTemCount) {
- $insert = [
- 'user_id' => $userId,
- 'type' => 3,
- 'admin_id' => 0,
- 'end_time' => 0,
- 'remark' => '明文错误达到'.$sysTemCount.'次封禁',
- 'created_at' => date('Y-m-d H:i:s'),
- 'updated_at' => date('Y-m-d H:i:s'),
- ];
- try {
- DB::beginTransaction();
- // 修改用户封禁状态
- User::changeStatus2($userId, STATUS2::Ban->value());
- // 记录用户封禁信息
- UserBan::query()->insert($insert);
- SessionApp::removeUKeys($userId);
- DB::commit();
- } catch (\Exception $e) {
- Logger::error('明文封禁错误:'.$e->getMessage());
- DB::rollBack();
- }
- }
- }
- }
|