改进 分页

This commit is contained in:
iVampireSP.com 2023-01-19 23:24:19 +08:00
parent c306393a19
commit 3342cf8d8d
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
3 changed files with 83 additions and 96 deletions

View File

@ -17,10 +17,9 @@ public function index(Request $request): RedirectResponse|View
$emqx = new EmqxSupport(); $emqx = new EmqxSupport();
try { try {
$clients = $emqx->clients([ $clients = $emqx->pagination([
'clientid' => $request->input('client_id'), 'clientid' => $request->input('client_id'),
'username' => $request->input('username'), 'username' => $request->input('username'),
'page' => $request->input('page'),
]); ]);
} catch (EmqxSupportException $e) { } catch (EmqxSupportException $e) {
return back()->with('error', $e->getMessage()); return back()->with('error', $e->getMessage());

View File

@ -6,11 +6,15 @@
use App\Jobs\Support\EMQXKickClientJob; use App\Jobs\Support\EMQXKickClientJob;
use Illuminate\Http\Client\ConnectionException; use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\PendingRequest; use Illuminate\Http\Client\PendingRequest;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Request;
class EmqxSupport class EmqxSupport
{ {
private int $limit_per_page = 50;
/** /**
* 移除客户端 * 移除客户端
*/ */
@ -42,7 +46,7 @@ public function clients($params = [])
{ {
// merge params // merge params
$params = array_merge([ $params = array_merge([
'limit' => 100, 'limit' => $this->limit_per_page,
'isTrusted' => true, 'isTrusted' => true,
], $params); ], $params);
@ -59,4 +63,25 @@ public function clients($params = [])
throw new EmqxSupportException('无法获取客户端列表。'); throw new EmqxSupportException('无法获取客户端列表。');
} }
} }
/**
* @throws EmqxSupportException
*/
public function pagination($params = []): LengthAwarePaginator
{
$page = Request::input('page', 1);
$params = array_merge([
'page' => $page,
], $params);
$clients = $this->clients($params);
$data = $clients['data'];
$total = $clients['meta']['count'];
$limit = $clients['meta']['limit'];
return new LengthAwarePaginator($data, $total, $limit, $page, [
'path' => route('admin.devices.index'),
]);
}
} }

View File

@ -35,103 +35,66 @@
</thead> </thead>
<tbody> <tbody>
@if(isset($clients)) @foreach($clients as $c)
@foreach($clients['data'] as $c) <tr>
<tr> <td>
<td> {{ $c['clientid'] }}
{{ $c['clientid'] }} </td>
</td> <td>
<td> <a href="?username={{ $c['username'] }}">{{ $c['username'] }}</a>
<a href="?username={{ $c['username'] }}">{{ $c['username'] }}</a> </td>
</td> <td>
<td> {{ $c['node'] }}
{{ $c['node'] }} </td>
</td> <td>
<td> {{ $c['proto_name'] . ' v' . $c['proto_ver'] }}
{{ $c['proto_name'] . ' v' . $c['proto_ver'] }} </td>
</td> <td>
<td> {{ $c['ip_address'] }}
{{ $c['ip_address'] }} </td>
</td> <td>
<td> @if ($c['clean_start'])
@if ($c['clean_start']) <span class="badge text-success">干净启动</span>
<span class="badge text-success">干净启动</span> @endif
@endif @if ($c['recv_oct'])
@if ($c['recv_oct']) <br/>
<br/> <span class="badge text-success">接收字节: {{ $c['recv_oct'] }}</span>
<span class="badge text-success">接收字节: {{ $c['recv_oct'] }}</span> @endif
@endif @if ($c['send_oct'])
@if ($c['send_oct']) <br/>
<br/> <span class="badge text-success">发送字节: {{ $c['send_oct'] }}</span>
<span class="badge text-success">发送字节: {{ $c['send_oct'] }}</span> @endif
@endif </td>
</td> <td>
<td> @if ($c['subscriptions_cnt'] > 0)
@if ($c['subscriptions_cnt'] > 0) <span class="text-success">{{ $c['subscriptions_cnt'] }} </span>
<span class="text-success">{{ $c['subscriptions_cnt'] }} </span> @else
@else <span class="text-danger">没有订阅</span>
<span class="text-danger">没有订阅</span> @endif
@endif </td>
</td> <td>
<td> <form action="{{ route('admin.devices.destroy') }}" method="post">
<form action="{{ route('admin.devices.destroy') }}" method="post"> @csrf
@csrf @method('DELETE')
@method('DELETE') <input type="hidden" name="client_id" value="{{ $c['clientid'] }}">
<input type="hidden" name="client_id" value="{{ $c['clientid'] }}"> <button type="submit" class="btn btn-danger btn-sm">踢出</button>
<button type="submit" class="btn btn-danger btn-sm">踢出</button> </form>
</form>
<form class="mt-2" action="{{ route('admin.devices.destroy') }}" method="post" <form class="mt-2" action="{{ route('admin.devices.destroy') }}" method="post"
onsubmit="return confirm('将踢出此模块以及它的所有客户端。')"> onsubmit="return confirm('将踢出此模块以及它的所有客户端。')">
@csrf @csrf
@method('DELETE') @method('DELETE')
<input type="hidden" name="username" value="{{ $c['username'] }}"> <input type="hidden" name="username" value="{{ $c['username'] }}">
<input type="hidden" name="like_username" value="1"/> <input type="hidden" name="like_username" value="1"/>
<button type="submit" class="btn btn-danger btn-sm">踢出所有</button> <button type="submit" class="btn btn-danger btn-sm">踢出所有</button>
</form> </form>
</td> </td>
</tr> </tr>
@endforeach @endforeach
@endif
</tbody> </tbody>
</table> </table>
@if(isset($clients)) {{ $clients->links() }}
<div class="d-none flex-sm-fill d-sm-flex align-items-sm-center justify-content-sm-between">
<div>
<p class="small text-muted">
{{ $clients['meta']['count'] }} 个设备,当前在第 {{ Request::input('page', 1) }} 页。
</p>
</div>
<div>
<ul class="pagination">
<li class="page-item @if (!($clients['meta']['page'] > 1)) disabled @endif">
<a class="page-link" href="?page={{ $clients['meta']['page'] - 1 }}"
aria-label="上一页 &raquo;">&lsaquo;</a>
</li>
@for($i = 1; $i <= ceil($clients['meta']['count'] / $clients['meta']['limit']); $i++)
@if ($i == Request::input('page', 1))
<li class="page-item @if ($i == Request::input('page', 1)) active @endif">
<span class="page-link" href="?page={{ $i }}">{{ $i }}</span>
</li>
@else
<li class="page-item @if ($i == Request::input('page', 1)) active @endif">
<a class="page-link" href="?page={{ $i }}">{{ $i }}</a>
</li>
@endif
@endfor
<li class="page-item @if ($clients['meta']['count'] == Request::input('page', 1) * count($clients['data'])) disabled @endif">
<a class="page-link" href="?page={{ $clients['meta']['page'] + 1}}" rel="next"
aria-label="下一页 &raquo;">&rsaquo;</a>
</li>
</ul>
</div>
</div>
@endif
@endsection @endsection