- MarketPackage 模型/迁移:市场商品作为可购买实体(payable),复用 Payment 订单 - MarketPurchaseService:购买创建订单、已购判断、免费/付费门槛校验 - payment.paid 监听(核心注册,仅支付插件启用时)标记商品已购 - 市场契约:条目含 name/type/price(分)/download_url;插件市场 Tab 只显示 plugin、主题市场 Tab 只显示 theme;付费商品显示「购买」按钮与「已购买」徽章,未购买无法安装 - 新增主题市场 Tab(原仅插件页有市场) - 测试:购买/沙箱支付标记已购/重复购买/安装门槛/免费条目/支付插件禁用
149 lines
4.4 KiB
PHP
149 lines
4.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
namespace App\Filament\Pages;
|
|
|
|
use App\Blog\Services\MarketplaceClient;
|
|
use App\Blog\Services\MarketPurchaseService;
|
|
use App\Blog\Services\PackageInstaller;
|
|
use App\Blog\Support\ThemeManager;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Pages\Page;
|
|
use Livewire\WithFileUploads;
|
|
|
|
class Themes extends Page
|
|
{
|
|
use WithFileUploads;
|
|
|
|
protected static \UnitEnum|string|null $navigationGroup = '管理';
|
|
|
|
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-swatch';
|
|
|
|
protected static ?string $navigationLabel = '主题管理';
|
|
|
|
protected static ?string $title = '主题管理';
|
|
|
|
protected string $view = 'filament.pages.themes';
|
|
|
|
public $zip;
|
|
|
|
public array $marketItems = [];
|
|
|
|
public function getThemesProperty(): array
|
|
{
|
|
return app(ThemeManager::class)->all();
|
|
}
|
|
|
|
public function activateTheme(string $name): void
|
|
{
|
|
try {
|
|
app(ThemeManager::class)->activate($name);
|
|
Notification::make()->title('已激活主题:'.$name)->success()->send();
|
|
} catch (\Throwable $e) {
|
|
Notification::make()->title('激活失败')->body($e->getMessage())->danger()->send();
|
|
}
|
|
}
|
|
|
|
public function uploadThemeZip(): void
|
|
{
|
|
$this->validate([
|
|
'zip' => ['required', 'file', 'mimes:zip'],
|
|
]);
|
|
|
|
try {
|
|
$result = app(PackageInstaller::class)->installZip($this->zip->getRealPath());
|
|
Notification::make()->title('主题安装成功:'.$result['key'])->success()->send();
|
|
} catch (\Throwable $e) {
|
|
Notification::make()->title('安装失败')->body($e->getMessage())->danger()->send();
|
|
}
|
|
|
|
$this->zip = null;
|
|
}
|
|
|
|
public function uninstallTheme(string $name): void
|
|
{
|
|
try {
|
|
app(PackageInstaller::class)->uninstallTheme($name);
|
|
Notification::make()->title('已删除主题:'.$name)->success()->send();
|
|
} catch (\Throwable $e) {
|
|
Notification::make()->title('删除失败')->body($e->getMessage())->danger()->send();
|
|
}
|
|
}
|
|
|
|
public function loadThemeMarket(): void
|
|
{
|
|
try {
|
|
$items = app(MarketplaceClient::class)->items();
|
|
$this->marketItems = array_values(array_filter(
|
|
$items,
|
|
fn (array $item) => ($item['type'] ?? 'plugin') === 'theme'
|
|
));
|
|
Notification::make()->title('市场已刷新:'.count($this->marketItems).' 个主题')->success()->send();
|
|
} catch (\Throwable $e) {
|
|
Notification::make()->title('市场加载失败')->body($e->getMessage())->danger()->send();
|
|
}
|
|
}
|
|
|
|
public function installFromThemeMarket(string $index): void
|
|
{
|
|
$item = $this->marketItems[(int) $index] ?? null;
|
|
|
|
if (! $item) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
app(MarketPurchaseService::class)->assertCanInstall($item);
|
|
|
|
$result = app(PackageInstaller::class)->installFromMarket($item);
|
|
Notification::make()->title('主题安装成功:'.$result['key'])->success()->send();
|
|
} catch (\Throwable $e) {
|
|
Notification::make()->title('安装失败')->body($e->getMessage())->danger()->send();
|
|
}
|
|
}
|
|
|
|
public function purchaseThemeMarketItem(string $index)
|
|
{
|
|
$item = $this->marketItems[(int) $index] ?? null;
|
|
|
|
if (! $item) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$payment = app(MarketPurchaseService::class)->purchase($item);
|
|
|
|
if (! $payment) {
|
|
Notification::make()->title('该商品已购买')->info()->send();
|
|
|
|
return;
|
|
}
|
|
|
|
return redirect()->route('pay.checkout', $payment);
|
|
} catch (\Throwable $e) {
|
|
Notification::make()->title('购买失败')->body($e->getMessage())->danger()->send();
|
|
}
|
|
}
|
|
|
|
public function isThemeMarketPurchased(string $index): bool
|
|
{
|
|
$item = $this->marketItems[(int) $index] ?? null;
|
|
|
|
if (! $item) {
|
|
return false;
|
|
}
|
|
|
|
return app(MarketPurchaseService::class)->isPurchased(
|
|
app(MarketPurchaseService::class)->itemKey($item),
|
|
app(MarketPurchaseService::class)->itemType($item)
|
|
);
|
|
}
|
|
|
|
public function getMarketConfiguredProperty(): bool
|
|
{
|
|
return (bool) config('market.url');
|
|
}
|
|
}
|