mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-23 19:37:23 +08:00
Tracker URl
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\System;
|
||||
|
||||
use App\Filament\OptionsTrait;
|
||||
use App\Filament\Resources\System\TrackerUrlResource\Pages;
|
||||
use App\Filament\Resources\System\TrackerUrlResource\RelationManagers;
|
||||
use App\Models\TrackerUrl;
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
||||
|
||||
class TrackerUrlResource extends Resource
|
||||
{
|
||||
use OptionsTrait;
|
||||
|
||||
protected static ?string $model = TrackerUrl::class;
|
||||
|
||||
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
|
||||
|
||||
protected static ?string $navigationGroup = 'System';
|
||||
|
||||
protected static ?int $navigationSort = 10;
|
||||
|
||||
public static function getNavigationLabel(): string
|
||||
{
|
||||
return __('admin.sidebar.tracker_url');
|
||||
}
|
||||
|
||||
public static function getBreadcrumb(): string
|
||||
{
|
||||
return self::getNavigationLabel();
|
||||
}
|
||||
|
||||
public static function form(Form $form): Form
|
||||
{
|
||||
return $form
|
||||
->schema([
|
||||
Forms\Components\TextInput::make('url')->required(),
|
||||
Forms\Components\Radio::make('is_default')
|
||||
->label(__('label.is_default'))
|
||||
->options(self::getYesNoOptions())
|
||||
->required(true)
|
||||
->inline()
|
||||
,
|
||||
Forms\Components\Radio::make('enabled')
|
||||
->label(__('label.enabled'))
|
||||
->options(self::getEnableDisableOptions(1, 0))
|
||||
->required(true)
|
||||
->inline()
|
||||
,
|
||||
Forms\Components\TextInput::make('priority')
|
||||
->label(__('label.priority'))->numeric()
|
||||
->default(0)
|
||||
->helperText(__('label.priority_help'))
|
||||
,
|
||||
]);
|
||||
}
|
||||
|
||||
public static function getEloquentQuery(): Builder
|
||||
{
|
||||
return TrackerUrl::query()
|
||||
->orderBy('is_default', 'desc')
|
||||
->orderBy('priority', 'desc')
|
||||
->orderBy('id', 'desc')
|
||||
;
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
Tables\Columns\TextColumn::make('id')
|
||||
,
|
||||
Tables\Columns\TextColumn::make('url')
|
||||
,
|
||||
Tables\Columns\IconColumn::make('is_default')
|
||||
->label(__('label.is_default'))
|
||||
->boolean()
|
||||
,
|
||||
Tables\Columns\IconColumn::make('enabled')
|
||||
->label(__('label.enabled'))
|
||||
->boolean()
|
||||
,
|
||||
Tables\Columns\TextColumn::make('priority')
|
||||
->label(__('label.priority'))
|
||||
,
|
||||
Tables\Columns\TextColumn::make('updated_at')
|
||||
->label(__('label.updated_at'))
|
||||
,
|
||||
])
|
||||
->filters([
|
||||
//
|
||||
])
|
||||
->actions([
|
||||
Tables\Actions\EditAction::make(),
|
||||
Tables\Actions\DeleteAction::make(),
|
||||
])
|
||||
->bulkActions([
|
||||
Tables\Actions\BulkActionGroup::make([
|
||||
Tables\Actions\DeleteBulkAction::make(),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => Pages\ManageTrackerUrls::route('/'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\System\TrackerUrlResource\Pages;
|
||||
|
||||
use App\Filament\PageListSingle;
|
||||
use App\Filament\Resources\System\TrackerUrlResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\ManageRecords;
|
||||
|
||||
class ManageTrackerUrls extends PageListSingle
|
||||
{
|
||||
protected static string $resource = TrackerUrlResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -87,7 +87,7 @@ class PluginStore extends Model
|
||||
{
|
||||
$log = "listAll, withoutCache: $withoutCache";
|
||||
$cacheKey = "nexus_plugin_store_all";
|
||||
$cacheTime = 86400*100;
|
||||
$cacheTime = 86400;
|
||||
if (is_null(self::$rows)) {
|
||||
$log .= ", is_null";
|
||||
if ($withoutCache) {
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Nexus\Database\NexusDB;
|
||||
|
||||
class TrackerUrl extends NexusModel
|
||||
{
|
||||
protected $fillable = ['url', 'enabled', 'is_default', 'priority'];
|
||||
|
||||
public $timestamps = true;
|
||||
|
||||
const TRACKER_URL_CACHE_KEY = "TRACKER_URL";
|
||||
const TRACKER_URL_DEFAULT_CACHE_KEY = "TRACKER_URL_DEFAULT";
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::saved(function (TrackerUrl $model) {
|
||||
if ($model->is_default == 1) {
|
||||
self::query()->where("id", "!=", $model->id)->update(["is_default" => 0]);
|
||||
}
|
||||
//添加 id 与 URL 映射
|
||||
$redis = NexusDB::redis();
|
||||
$redis->del(self::TRACKER_URL_CACHE_KEY);
|
||||
$list = self::listAll();
|
||||
$first = $list->first();
|
||||
$hasDefault = false;
|
||||
foreach ($list as $item) {
|
||||
$redis->hset(self::TRACKER_URL_CACHE_KEY, $item->id, $item->url);
|
||||
if ($item->is_default == 1) {
|
||||
$hasDefault = true;
|
||||
$redis->set(self::TRACKER_URL_DEFAULT_CACHE_KEY, $item->url);
|
||||
}
|
||||
}
|
||||
if (!$hasDefault && $first) {
|
||||
$redis->set(self::TRACKER_URL_DEFAULT_CACHE_KEY, $first->url);
|
||||
}
|
||||
});
|
||||
static::saving(function (TrackerUrl $model) {
|
||||
if ($model->is_default == 1) {
|
||||
$model->enabled = 1;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static function listAll()
|
||||
{
|
||||
return self::query()
|
||||
->where("enabled", 1)
|
||||
->orderBy("is_default", "desc")
|
||||
->orderBy("priority", "desc")
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function getById(int $trackerUrlId)
|
||||
{
|
||||
$redis = NexusDB::redis();
|
||||
if ($trackerUrlId == 0) {
|
||||
return $redis->get(self::TRACKER_URL_DEFAULT_CACHE_KEY);
|
||||
}
|
||||
$result = $redis->hget(self::TRACKER_URL_CACHE_KEY, $trackerUrlId);
|
||||
if (!$result) {
|
||||
$result = $redis->get(self::TRACKER_URL_DEFAULT_CACHE_KEY);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user