This commit is contained in:
Default
2025-03-15 20:12:21 +08:00
parent 105ffe11c7
commit eb9035a7ae
134 changed files with 15450 additions and 119 deletions

View 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);
}
}