80 lines
2.4 KiB
PHP
80 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\PrintPlugin;
|
|
use App\Models\PrintPluginInstallation;
|
|
use App\Services\PrintPluginService;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Notification;
|
|
|
|
class CheckPrintPluginUpdates extends Command
|
|
{
|
|
protected $signature = 'print-plugin:check-updates {--plugin= : 指定插件代码}';
|
|
protected $description = '检测打印插件更新';
|
|
|
|
protected PrintPluginService $pluginService;
|
|
|
|
public function __construct(PrintPluginService $pluginService)
|
|
{
|
|
parent::__construct();
|
|
$this->pluginService = $pluginService;
|
|
}
|
|
|
|
public function handle(): int
|
|
{
|
|
$pluginCode = $this->option('plugin');
|
|
|
|
if ($pluginCode) {
|
|
$this->checkPlugin($pluginCode);
|
|
} else {
|
|
$this->info('检测所有插件更新...');
|
|
PrintPlugin::where('status', '!=', 'inactive')->each(function ($plugin) {
|
|
$this->checkPlugin($plugin->code);
|
|
});
|
|
}
|
|
|
|
$this->info('检测完成');
|
|
return 0;
|
|
}
|
|
|
|
protected function checkPlugin(string $code): void
|
|
{
|
|
$this->info("检测插件: {$code}");
|
|
|
|
$result = $this->pluginService->checkUpdates($code);
|
|
|
|
if ($result) {
|
|
$this->warn("发现新版本: {$result['old_version']} -> {$result['new_version']}");
|
|
|
|
// 通知需要更新的用户
|
|
$usersToNotify = $this->pluginService->getUsersNeedingUpdate($code);
|
|
|
|
foreach ($usersToNotify as $userInfo) {
|
|
$this->notifyUser($userInfo);
|
|
}
|
|
|
|
$this->info("已通知 {$usersToNotify->count() ?? count($usersToNotify)} 个用户");
|
|
} else {
|
|
$this->info("{$code}: 无更新");
|
|
}
|
|
}
|
|
|
|
protected function notifyUser(array $userInfo): void
|
|
{
|
|
// 记录通知日志
|
|
Log::channel('print_plugin')->info('插件更新通知', [
|
|
'user_id' => $userInfo['user_id'],
|
|
'device_id' => $userInfo['device_id'],
|
|
'current_version' => $userInfo['current_version'],
|
|
'latest_version' => $userInfo['latest_version'],
|
|
'time' => now()->toIso8601String(),
|
|
]);
|
|
|
|
// TODO: 发送邮件/短信通知
|
|
// 可以通过 Notification 发送
|
|
}
|
|
}
|