debug
This commit is contained in:
215
code/extend/template/page/CusteomPage.php
Normal file
215
code/extend/template/page/CusteomPage.php
Normal file
@@ -0,0 +1,215 @@
|
||||
<?php
|
||||
|
||||
namespace template\page;
|
||||
|
||||
use think\Paginator;
|
||||
|
||||
class CusteomPage extends Paginator
|
||||
{
|
||||
/**
|
||||
* 上一页按钮
|
||||
* @param string $text
|
||||
* @return string
|
||||
*/
|
||||
protected function getPreviousButton(string $text = "上一页"): string
|
||||
{
|
||||
|
||||
if ($this->currentPage() <= 1) {
|
||||
return $this->getDisabledTextWrapper($text);
|
||||
}
|
||||
|
||||
$url = $this->url(
|
||||
$this->currentPage() - 1
|
||||
);
|
||||
|
||||
return $this->getPageLinkWrapper($url, $text);
|
||||
}
|
||||
|
||||
/**
|
||||
* 下一页按钮
|
||||
* @param string $text
|
||||
* @return string
|
||||
*/
|
||||
protected function getNextButton(string $text = '下一页'): string
|
||||
{
|
||||
if (!$this->hasMore) {
|
||||
return $this->getDisabledTextWrapper($text);
|
||||
}
|
||||
|
||||
$url = $this->url($this->currentPage() + 1);
|
||||
|
||||
return $this->getPageLinkWrapper($url, $text);
|
||||
}
|
||||
|
||||
/**
|
||||
* 页码按钮
|
||||
* @return string
|
||||
*/
|
||||
/**
|
||||
* 页码按钮
|
||||
* @return string
|
||||
*/
|
||||
protected function getLinks(): string
|
||||
{
|
||||
if ($this->simple) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$block = [
|
||||
'first' => null,
|
||||
'slider' => null,
|
||||
'last' => null,
|
||||
];
|
||||
|
||||
if ($this->lastPage <= 3) {
|
||||
// 如果总页数少于或等于3,显示所有页码
|
||||
$block['first'] = $this->getUrlRange(1, $this->lastPage);
|
||||
} else {
|
||||
if ($this->currentPage == 1) {
|
||||
// 如果当前页码是第一页,显示前两页和第三页
|
||||
$block['first'] = $this->getUrlRange(1, 3);
|
||||
} elseif ($this->currentPage == $this->lastPage) {
|
||||
// 如果当前页码是最后一页,显示最后三页
|
||||
$block['first'] = $this->getUrlRange($this->lastPage - 2, $this->lastPage);
|
||||
} else {
|
||||
// 否则,显示当前页码及其前后各一页
|
||||
$block['first'] = $this->getUrlRange($this->currentPage - 1, $this->currentPage + 1);
|
||||
}
|
||||
}
|
||||
|
||||
$html = '';
|
||||
|
||||
if (is_array($block['first'])) {
|
||||
$html .= $this->getUrlLinks($block['first']);
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染分页html
|
||||
* @return mixed
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
if ($this->hasPages()) {
|
||||
if ($this->simple) {
|
||||
return sprintf(
|
||||
'<ul class="pager">%s %s</ul>',
|
||||
$this->getPreviousButton(),
|
||||
$this->getNextButton()
|
||||
);
|
||||
} else {
|
||||
return sprintf(
|
||||
'<ul class="pagination justify-content-center">%s %s %s %s</ul>%s',
|
||||
$this->getPreviousButton(),
|
||||
$this->getLinks(),
|
||||
$this->getNextButton(),
|
||||
$this->getTotalInfo(),
|
||||
$this->getTargePage(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 跳转
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getTargePage(): string
|
||||
{
|
||||
return '<div class="jump-box">
|
||||
<input type="number" name="page" class="page-link pagination-num" placeholder="跳转页">
|
||||
<button type="button" data-url="" data-max="' . $this->lastPage() . '"
|
||||
class="btn btn-primary btn-sm pagination-click">跳转</button>
|
||||
</div>';
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getTotalInfo(): string
|
||||
{
|
||||
return '<li class="page-item"><span class="page-link">' . $this->currentPage() . '/' . $this->lastPage() . '</span></li>';
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成一个可点击的按钮
|
||||
*
|
||||
* @param string $url
|
||||
* @param string $page
|
||||
* @return string
|
||||
*/
|
||||
protected function getAvailablePageWrapper(string $url, string $page): string
|
||||
{
|
||||
return '<li class="page-item"><a class="page-link" href="' . htmlentities($url) . '">' . $page . '</a></li>';
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成一个禁用的按钮
|
||||
*
|
||||
* @param string $text
|
||||
* @return string
|
||||
*/
|
||||
protected function getDisabledTextWrapper(string $text): string
|
||||
{
|
||||
return '<li class="page-item"><span class="page-link">' . $text . '</span></li>';
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成一个激活的按钮
|
||||
*
|
||||
* @param string $text
|
||||
* @return string
|
||||
*/
|
||||
protected function getActivePageWrapper(string $text): string
|
||||
{
|
||||
return '<li class="page-item active"><span class="page-link">' . $text . '</span></li>';
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成省略号按钮
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getDots(): string
|
||||
{
|
||||
return $this->getDisabledTextWrapper('...');
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量生成页码按钮.
|
||||
*
|
||||
* @param array $urls
|
||||
* @return string
|
||||
*/
|
||||
protected function getUrlLinks(array $urls): string
|
||||
{
|
||||
$html = '';
|
||||
|
||||
foreach ($urls as $page => $url) {
|
||||
$html .= $this->getPageLinkWrapper($url, $page);
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成普通页码按钮
|
||||
*
|
||||
* @param string $url
|
||||
* @param string $page
|
||||
* @return string
|
||||
*/
|
||||
protected function getPageLinkWrapper(string $url, string $page): string
|
||||
{
|
||||
if ($this->currentPage() == $page) {
|
||||
return $this->getActivePageWrapper($page);
|
||||
}
|
||||
|
||||
return $this->getAvailablePageWrapper($url, $page);
|
||||
}
|
||||
}
|
||||
152
code/extend/template/page/CusteomPage02.php
Normal file
152
code/extend/template/page/CusteomPage02.php
Normal file
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
namespace template\page;
|
||||
|
||||
use think\Paginator;
|
||||
|
||||
class CusteomPage02
|
||||
{
|
||||
/**
|
||||
* 生成分页按钮数据
|
||||
* @param int $currentPage 当前页码
|
||||
* @param int $totalItems 总数据量
|
||||
* @param int $itemsPerPage 每页条数
|
||||
* @param string $baseUrl 基础 URL 模板(如 /male/xuanhuan-xiuzhen/finished/page{page})
|
||||
* @param int $totalButtons 总按钮数量(必须大于3)
|
||||
* @param array $buttonLabels 按钮文本配置
|
||||
* @return array 分页按钮数据
|
||||
*/
|
||||
public static function generate(
|
||||
$currentPage,
|
||||
$totalItems,
|
||||
$itemsPerPage,
|
||||
$baseUrl,
|
||||
$totalButtons = 5,
|
||||
$buttonLabels = []
|
||||
) {
|
||||
// 默认按钮文本
|
||||
$defaultLabels = [
|
||||
'prev' => '上一页',
|
||||
'next' => '下一页',
|
||||
'ellipsis' => '...'
|
||||
];
|
||||
$labels = array_merge($defaultLabels, $buttonLabels);
|
||||
|
||||
// 验证 totalButtons 必须大于 3
|
||||
if ($totalButtons < 3) {
|
||||
throw new \InvalidArgumentException('Total buttons must be greater than 3');
|
||||
}
|
||||
|
||||
// 计算总页数
|
||||
$totalPages = ceil($totalItems / $itemsPerPage);
|
||||
if ($totalPages <= 1) return []; // 只有一页时返回空数组
|
||||
|
||||
// 确保当前页码在有效范围内
|
||||
$currentPage = max(1, min($currentPage, $totalPages));
|
||||
|
||||
// 计算除去“上一页”和“下一页”后可显示的页码按钮数
|
||||
$pageButtons = $totalButtons - 2; // 减去“上一页”和“下一页”
|
||||
|
||||
// 初始化按钮数组
|
||||
$buttons = [];
|
||||
|
||||
// 上一页按钮
|
||||
$buttons[] = [
|
||||
'type' => 'prev',
|
||||
'label' => $labels['prev'],
|
||||
'url' => $currentPage > 1 ? str_replace('{page}', $currentPage - 1, $baseUrl) : null,
|
||||
'rel' => $currentPage > 1 ? 'prev' : null,
|
||||
'disabled' => $currentPage <= 1
|
||||
];
|
||||
|
||||
// 计算页码范围
|
||||
$half = floor($pageButtons / 2);
|
||||
$start = max(1, $currentPage - $half);
|
||||
$end = $start + $pageButtons - 1;
|
||||
|
||||
// 调整范围,确保不超过总页数
|
||||
if ($end > $totalPages) {
|
||||
$end = $totalPages;
|
||||
$start = max(1, $end - $pageButtons + 1);
|
||||
}
|
||||
|
||||
// 如果总页数大于可显示的页码数,添加省略号逻辑
|
||||
if ($totalPages > $pageButtons) {
|
||||
if ($start > 1) {
|
||||
$buttons[] = [
|
||||
'type' => 'page',
|
||||
'label' => '1',
|
||||
'url' => str_replace('{page}', 1, $baseUrl),
|
||||
'current' => false
|
||||
];
|
||||
if ($start > 2) {
|
||||
$buttons[] = [
|
||||
'type' => 'ellipsis',
|
||||
'label' => $labels['ellipsis'],
|
||||
'url' => null
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// 添加页码按钮
|
||||
for ($i = $start; $i <= $end; $i++) {
|
||||
$buttons[] = [
|
||||
'type' => 'page',
|
||||
'label' => (string)$i,
|
||||
'url' => str_replace('{page}', $i, $baseUrl),
|
||||
'current' => $i === $currentPage
|
||||
];
|
||||
}
|
||||
|
||||
if ($end < $totalPages) {
|
||||
if ($end < $totalPages - 1) {
|
||||
$buttons[] = [
|
||||
'type' => 'ellipsis',
|
||||
'label' => $labels['ellipsis'],
|
||||
'url' => null
|
||||
];
|
||||
}
|
||||
$buttons[] = [
|
||||
'type' => 'page',
|
||||
'label' => (string)$totalPages,
|
||||
'url' => str_replace('{page}', $totalPages, $baseUrl),
|
||||
'current' => false
|
||||
];
|
||||
}
|
||||
} else {
|
||||
// 总页数少于等于可显示页码数,直接全部显示
|
||||
for ($i = 1; $i <= $totalPages; $i++) {
|
||||
$buttons[] = [
|
||||
'type' => 'page',
|
||||
'label' => (string)$i,
|
||||
'url' => str_replace('{page}', $i, $baseUrl),
|
||||
'current' => $i === $currentPage
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// 下一页按钮
|
||||
$buttons[] = [
|
||||
'type' => 'next',
|
||||
'label' => $labels['next'],
|
||||
'url' => $currentPage < $totalPages ? str_replace('{page}', $currentPage + 1, $baseUrl) : null,
|
||||
'rel' => $currentPage < $totalPages ? 'next' : null,
|
||||
'disabled' => $currentPage >= $totalPages
|
||||
];
|
||||
|
||||
return $buttons;
|
||||
}
|
||||
}
|
||||
|
||||
// // 测试示例 1:显示 5 个按钮
|
||||
// $currentPage = 3;
|
||||
// $totalItems = 100;
|
||||
// $itemsPerPage = 10;
|
||||
// $baseUrl = '/male/xuanhuan-xiuzhen/finished/page{page}';
|
||||
// $buttons = Pagination::generate($currentPage, $totalItems, $itemsPerPage, $baseUrl, 5);
|
||||
// print_r($buttons);
|
||||
|
||||
// // 测试示例 2:显示 7 个按钮
|
||||
// $buttons = Pagination::generate($currentPage, $totalItems, $itemsPerPage, $baseUrl, 7);
|
||||
// print_r($buttons);
|
||||
//
|
||||
Reference in New Issue
Block a user