mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-14 12:30:49 +08:00
Refactor IP History
This commit is contained in:
40
app/Models/IpLog.php
Normal file
40
app/Models/IpLog.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
|
||||
class IpLog extends NexusModel
|
||||
{
|
||||
protected $table = 'iplog';
|
||||
|
||||
protected $fillable = ['ip', 'userid', 'access', 'uri', 'count'];
|
||||
|
||||
protected function ipLocation(): Attribute
|
||||
{
|
||||
return new Attribute(
|
||||
get: fn (mixed $value, array $attributes) => $this->getIpLocation($attributes['ip'])
|
||||
);
|
||||
}
|
||||
|
||||
private function getIpLocation(string $ip)
|
||||
{
|
||||
$result = get_ip_location_from_geoip($ip);
|
||||
$out = $result['name'];
|
||||
$suffix = [];
|
||||
if (!empty($result['city_en'])) {
|
||||
$suffix[] = $result['city_en'];
|
||||
}
|
||||
if (!empty($result['country_en'])) {
|
||||
$suffix[] = $result['country_en'];
|
||||
}
|
||||
if (!empty($result['continent_en'])) {
|
||||
$suffix[] = $result['continent_en'];
|
||||
}
|
||||
if (!empty($suffix)) {
|
||||
$out .= " " . implode(', ', $suffix);
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Nexus\Database\NexusDB;
|
||||
@@ -16,6 +17,13 @@ class NexusModel extends Model
|
||||
|
||||
protected $connection = NexusDB::ELOQUENT_CONNECTION_NAME;
|
||||
|
||||
protected function usernameForAdmin(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn (mixed $value, array $attributes) => username_for_admin($attributes['uid'] ?? $attributes['userid'] ?? $attributes['user_id'])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param \DateTimeInterface $date
|
||||
|
||||
@@ -108,7 +108,7 @@ class Setting extends NexusModel
|
||||
{
|
||||
$redis = NexusDB::redis();
|
||||
$key = self::USER_TOKEN_PERMISSION_ALLOWED_CACHE_KRY;
|
||||
$redis->del($key);
|
||||
$redis->unlink($key);
|
||||
//must not use cache
|
||||
if (empty($allowed)) {
|
||||
$allowed = self::getFromDb("permission.user_token_allowed");
|
||||
|
||||
@@ -35,7 +35,7 @@ class TrackerUrl extends NexusModel
|
||||
{
|
||||
//添加 id 与 URL 映射
|
||||
$redis = NexusDB::redis();
|
||||
$redis->del(self::TRACKER_URL_CACHE_KEY);
|
||||
$redis->unlink(self::TRACKER_URL_CACHE_KEY);
|
||||
$list = self::listAll();
|
||||
$first = $list->first();
|
||||
$hasDefault = false;
|
||||
|
||||
Reference in New Issue
Block a user