From c306393a194311d1f56bf3ca60de20f1d7a1ace2 Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Thu, 19 Jan 2023 23:09:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=20=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E8=AE=BE=E5=A4=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Admin/DeviceController.php | 21 +++-- app/Jobs/Support/EMQXKickClientJob.php | 76 +++++++++++++++++++ app/Support/EmqxSupport.php | 24 ++---- resources/views/admin/device/index.blade.php | 15 +++- routes/admin.php | 4 +- 5 files changed, 111 insertions(+), 29 deletions(-) create mode 100644 app/Jobs/Support/EMQXKickClientJob.php diff --git a/app/Http/Controllers/Admin/DeviceController.php b/app/Http/Controllers/Admin/DeviceController.php index 09c68d7..710130c 100644 --- a/app/Http/Controllers/Admin/DeviceController.php +++ b/app/Http/Controllers/Admin/DeviceController.php @@ -4,6 +4,7 @@ use App\Exceptions\EmqxSupportException; use App\Http\Controllers\Controller; +use App\Jobs\Support\EMQXKickClientJob; use App\Support\EmqxSupport; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; @@ -38,16 +39,22 @@ public function index(Request $request): RedirectResponse|View // return view('admin.device.show', compact('client')); // } - - /** - * @throws EmqxSupportException - */ - public function destroy($client_id): RedirectResponse + public function destroy(Request $request): RedirectResponse { $emqx = new EmqxSupport(); - $emqx->kickClient($client_id); + if ($request->filled('client_id')) { + $emqx->kickClient($request->input('client_id')); + } - return back()->with('success', '此客户端已下线。'); + if ($request->filled('username')) { + $username = $request->input('username'); + $module_name = explode('.', $username)[0]; + + $this->dispatch(new EMQXKickClientJob(null, $module_name, false)); + $this->dispatch(new EMQXKickClientJob(null, $module_name . '.', true)); + } + + return back()->with('success', '正在让它们下线。'); } } diff --git a/app/Jobs/Support/EMQXKickClientJob.php b/app/Jobs/Support/EMQXKickClientJob.php new file mode 100644 index 0000000..ac5240a --- /dev/null +++ b/app/Jobs/Support/EMQXKickClientJob.php @@ -0,0 +1,76 @@ +client_id = $client_id; + $this->username = $username; + $this->like_username = $like_username; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle(): void + { + $emqx = new EmqxSupport(); + + if ($this->client_id) { + $emqx->api()->delete('/clients/' . $this->client_id); + } + + if ($this->username) { + $query = 'username'; + if ($this->like_username) { + $query = 'like_username'; + } + + try { + $clients = $emqx->clients([$query => $this->username]); + } catch (EmqxSupportException $e) { + Log::error('emqx connect failed.', [$e]); + return; + } + + if ($clients) { + // 循环翻页 + for ($i = 1; $i <= $clients['meta']['count']; $i++) { + try { + $clients = $emqx->clients([$query => $this->username, 'page' => $i]); + } catch (EmqxSupportException $e) { + Log::error('emqx connect failed.', [$e]); + continue; + } + + foreach ($clients['data'] as $client) { + dispatch(new self($client['clientid'], null)); + } + } + } + } + } +} diff --git a/app/Support/EmqxSupport.php b/app/Support/EmqxSupport.php index f376233..d4e7e8a 100644 --- a/app/Support/EmqxSupport.php +++ b/app/Support/EmqxSupport.php @@ -3,6 +3,7 @@ namespace App\Support; use App\Exceptions\EmqxSupportException; +use App\Jobs\Support\EMQXKickClientJob; use Illuminate\Http\Client\ConnectionException; use Illuminate\Http\Client\PendingRequest; use Illuminate\Support\Facades\Http; @@ -11,9 +12,9 @@ class EmqxSupport { /** - * @throws EmqxSupportException + * 移除客户端 */ - public function kickClient($client_id = null, $username = null): void + public function kickClient($client_id = null, $username = null, bool $like_username = false): void { // 如果都为空,直接返回 if (empty($client_id) && empty($username)) { @@ -25,26 +26,11 @@ public function kickClient($client_id = null, $username = null): void } if ($username) { - try { - $clients = $this->clients(['username' => $username]); - } catch (EmqxSupportException $e) { - throw new EmqxSupportException($e->getMessage()); - } - - if ($clients) { - // 循环翻页 - for ($i = 1; $i <= $clients['meta']['count']; $i++) { - $clients = $this->clients(['username' => $username, 'page' => $i]); - - foreach ($clients['data'] as $client) { - $this->api()->delete('/clients/' . $client['clientid']); - } - } - } + dispatch(new EMQXKickClientJob(null, $username, $like_username)); } } - private function api(): PendingRequest + public function api(): PendingRequest { return Http::baseUrl(config('emqx.api_url'))->withBasicAuth(config('emqx.api_key'), config('emqx.api_secret')); } diff --git a/resources/views/admin/device/index.blade.php b/resources/views/admin/device/index.blade.php index d37183f..41c34a9 100644 --- a/resources/views/admin/device/index.blade.php +++ b/resources/views/admin/device/index.blade.php @@ -70,15 +70,26 @@ @if ($c['subscriptions_cnt'] > 0) {{ $c['subscriptions_cnt'] }} 个 @else - 没有 + 没有订阅 @endif -
+ @csrf @method('DELETE') +
+ +
+ @csrf + @method('DELETE') + + + +
+ @endforeach diff --git a/routes/admin.php b/routes/admin.php index 30afdc3..e82ef88 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -47,7 +47,9 @@ Route::resource('user-groups', UserGroupController::class); - Route::resource('devices', DeviceController::class)->only(['index', 'destroy']); + Route::get('devices', [DeviceController::class, 'index'])->name('devices.index'); + Route::delete('devices', [DeviceController::class, 'destroy'])->name('devices.destroy'); + Route::resource('notifications', NotificationController::class)->only(['create', 'store']);