改进 分页

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,8 +35,7 @@
</thead> </thead>
<tbody> <tbody>
@if(isset($clients)) @foreach($clients as $c)
@foreach($clients['data'] as $c)
<tr> <tr>
<td> <td>
{{ $c['clientid'] }} {{ $c['clientid'] }}
@ -93,45 +92,9 @@
</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