params = $params; } public function handle() { $isEnableTelegram = true; $this->insertLog(); if ($isEnableTelegram) { $cacheKey = $this->getCacheKey($this->params['device_no']); $cacheData = make(CacheInterface::class)->get($cacheKey); $diffHours = 99999999; if (!empty($cacheData['time'])) { $diffHours = abs(Carbon::parse($cacheData['time'])->diffInHours(Carbon::now())); } if ($diffHours > 24) { $this->reportSameDeviceNo(); } } } private function insertLog() { $arrIpAddr = Ip::find($this->params['ip']); array_pop($arrIpAddr); //剔除最后一个下标, 国标 $arrIpAddr = array_filter($arrIpAddr); $ipAddr = !empty($arrIpAddr) ? implode('|', $arrIpAddr) : ''; $nowDate = toMongoIsoDate(Carbon::now()); $setarr = [ 'user_uid' => $this->params['user_id'], 'login_at' => $nowDate, 'version' => $this->params['version'], 'device_type' => $this->params['device_type'], 'device_no' => $this->params['device_no'], 'finger_hash' => $this->params['finger_hash'], 'ip' => $this->params['ip'], 'ip_addr' => $ipAddr, ]; make(UserLoginLogModel::class)->db()->insertOne($setarr); } private function reportSameDeviceNo() { $arrDiff = []; // $tmpIp = '180.142.72.108'; $tmpIp = $this->params['ip']; $pipeline = [ [ '$match' => [ 'device_no' => $this->params['device_no'], 'ip' => [ '$ne' => $tmpIp, ], ], ], [ '$group' => [ '_id' => '$ip', 'ip_addr' => [ '$first' => '$ip_addr', ], 'mid' => [ '$first' => '$_id', ], 'user_uid' => [ '$first' => '$user_uid', ], 'list_login_at' => [ '$push' => '$login_at', ], 'last_login_at' => [ '$first' => '$login_at', ], 'device_type' => [ '$first' => '$device_type', ], 'version' => [ '$first' => '$version', ], ], ], [ '$sort' => [ 'last_login_at' => -1 ], ], [ '$limit' => 6, ], ]; $options = []; /** * @var \MongoDB\Driver\Cursor $cursor */ $cursor = make(UserLoginLogModel::class)->db()->aggregate($pipeline, $options); if (!$cursor->isDead()) { $tempArrIpAddr = Ip::find($this->params['ip']); array_pop($tempArrIpAddr); //剔除最后一个下标, 国标 $tempArrIpAddr = array_filter($tempArrIpAddr); // 入库的这个IP和以前的对比 $i = 0; foreach ($cursor as $itemObj) { $id = $itemObj->mid->__toString(); $isDiff = false; $ipAddr = $itemObj->ip_addr; $arrIpAddr = !empty($ipAddr) ? explode('|', $ipAddr) : []; /** * @var \ArrayObject $listLoginAt */ $listLoginAt = $itemObj->list_login_at; $listLoginAt->count(); $lastLoginAt = $itemObj->last_login_at; $ip = $itemObj->_id; $userUid = $itemObj->user_uid; $version = $itemObj->version; $deviceType = $itemObj->device_type; $tmpData = [ 'user_uid' => $userUid, 'device_no' => $this->params['device_no'], 'device_type' => $deviceType, 'version' => $version, 'login_at' => null, 'ip' => $ip, 'ip_addr' => $ipAddr, ]; if (!empty($tempArrIpAddr) && !empty($arrIpAddr) && count($tempArrIpAddr) >= 2 && count($arrIpAddr) >= 2) { if ($tempArrIpAddr[0] != $arrIpAddr[0]) { $isDiff = true; $tmpData['login_at'] = $lastLoginAt; } elseif ($tempArrIpAddr[1] != $arrIpAddr[1]) { $isDiff = true; $tmpData['login_at'] = $lastLoginAt; } } if ($isDiff) { $arrDiff[] = $tmpData; } $i++; } } if (!empty($arrDiff)) { $nowDate = Carbon::now()->toDateTimeString(); $arrIpAddr = Ip::find($this->params['ip']); array_pop($arrIpAddr); //剔除最后一个下标, 国标 $arrIpAddr = array_filter($arrIpAddr); $nowIpAddr = !empty($arrIpAddr) ? implode('|', $arrIpAddr) : ''; $arrCurrentLogin = [ 'user_uid' => $this->params['user_id'], 'device_no' => $this->params['device_no'], 'device_type' => $this->params['device_type'], 'version' => $this->params['version'], 'login_at' => toMongoIsoDate($nowDate), 'ip' => $this->params['ip'], 'ip_addr' => $nowIpAddr, ]; $cacheKey = $this->getCacheKey($this->params['device_no']); make(CacheInterface::class)->set($cacheKey, ['time' => $nowDate], 24 * 60 * 60); $appFlag = env('APP_NAME', ''); $msgContent = <<formatCurrentLoginMsg($arrCurrentLogin). "\n"; foreach ($arrDiff as $k => $v) { $liNo = $k + 1; $msgContent .= $this->formatAddonMsg($liNo, $v); } $msgContent = "\r\n" .rtrim($msgContent) . "\n"; $msgContent .= "\n---------------\n"; make(TelegramRepository::class)->sendCommonText($msgContent); } } private function formatAddonMsg($liNo, $v) { $loginDate = !empty($v['login_at']) ? mongoIsoDateToCarbonTime($v['login_at']) : '未知'; $ipInfo = $v['ip_addr'] . '('. $v['ip'] .')'; $deviceTypeName = '未知'; if ($v['device_type'] == 'I') { $deviceTypeName = 'iOS'; } elseif ($v['device_type'] == 'A') { $deviceTypeName = 'Android'; } elseif ($v['device_type'] == 'H') { $deviceTypeName = 'WebH5'; } $userUid = $v['user_uid']; $deviceNo = $v['device_no']; $version = $v['version']; $strTpl = <<