添加登录通知

main
gzydong 2021-09-12 16:26:28 +08:00
parent a776454e6d
commit 5f669d156b
2 changed files with 75 additions and 0 deletions

View File

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace App\Cache;
use App\Cache\Repository\HashRedis;
use App\Helpers\JsonHelper;
use App\Support\IpAddress;
class IpAddressCache extends HashRedis
{
protected $name = 'ip-address';
public function getAddressInfo(string $ip)
{
$result = $this->get($ip);
if (!empty($result)) {
return JsonHelper::decode($result);
}
$result = di()->get(IpAddress::class)->read($ip);
if (!empty($result)) {
$this->add($ip, JsonHelper::encode($result));
}
return $result;
}
}

46
app/Support/IpAddress.php Normal file
View File

@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace App\Support;
use Hyperf\Guzzle\ClientFactory;
class IpAddress
{
public function read(string $ip)
{
return $this->request($ip);
}
private function request(string $ip)
{
$api = config('juhe_api.ip');
$client = di()->get(ClientFactory::class)->create([]);
$params = [
'ip' => $ip,
'key' => $api['key'],
];
$response = $client->get($api['api'] . '?' . http_build_query($params));
if ($response->getStatusCode() == 200) {
$result = json_decode($response->getBody()->getContents(), true);
if ($result['resultcode'] != '200') {
return [];
}
$result = $result['result'];
return [
'country' => $result['Country'] ?? '',
'province' => $result['Province'] ?? '',
'city' => $result['City'] ?? '',
'isp' => $result['Isp'] ?? '',
'ip' => $ip,
];
}
return [];
}
}