MacroPageMiddleware.php 725 B

123456789101112131415161718192021222324
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\System\Middleware;
  4. use Closure;
  5. use SixShop\Core\Request;
  6. use think\Exception;
  7. use think\Response;
  8. class MacroPageMiddleware
  9. {
  10. public function handle(Request $request, Closure $next): Response
  11. {
  12. $request->macro('pageAndLimit', function (): array {
  13. $page = $this->get('page/d', 1);
  14. $limit = $this->get('limit/d', $this->get('per_page/d', 10));
  15. throw_if($page < 1, Exception::class, '页码不能小于1');
  16. throw_if($limit < 1 || $limit > 200, Exception::class, '每页数量必须在1-200之间');
  17. return ['page' => $page, 'list_rows' => $limit];
  18. });
  19. return $next($request);
  20. }
  21. }