This commit is contained in:
Your Name
2026-08-11 17:39:41 +08:00
parent cfe4c82c90
commit 25467b9d91
350 changed files with 201115 additions and 132208 deletions
+225 -225
View File
@@ -1,226 +1,226 @@
<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台(PHP版)
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用,可去除界面版权logo
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
// | github下载:https://github.com/likeshop-github/likeadmin
// | 访问官网:https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\common\lists;
use app\common\enum\ExportEnum;
use app\common\service\JsonService;
use app\common\validate\ListsValidate;
use app\Request;
use think\facade\Config;
/**
* 数据列表基类
* Class BaseDataLists
* @package app\common\lists
*/
abstract class BaseDataLists implements ListsInterface
{
use ListsSearchTrait;
use ListsSortTrait;
use ListsExcelTrait;
public Request $request; //请求对象
public int $pageNo; //页码
public int $pageSize; //每页数量
public int $limitOffset; //limit查询offset值
public int $limitLength; //limit查询数量
public int $pageSizeMax;
public int $pageType = 0; //默认类型:0-一般分页;1-不分页,获取最大所有数据
protected string $orderBy;
protected string $field;
protected $startTime;
protected $endTime;
protected $start;
protected $end;
protected array $params;
protected $sortOrder = [];
public string $export;
/**
* 管理端列表:在 request 就绪后写入 adminId/adminInfo(见 BaseAdminDataLists
*/
protected function initAdminIdentity(): void
{
}
public function __construct()
{
//参数验证
(new ListsValidate())->get()->goCheck();
//请求参数设置
$this->request = request();
// admin 列表子类在 initExport 中可能触发 count/lists,须先于 initPage/initExport 写入身份
$this->initAdminIdentity();
$this->params = $this->request->param();
//分页初始化
$this->initPage();
//搜索初始化
$this->initSearch();
//排序初始化
$this->initSort();
//导出初始化
$this->initExport();
}
/**
* @notes 分页参数初始化
* @author 令狐冲
* @date 2021/7/30 23:55
*/
private function initPage()
{
$this->pageSizeMax = Config::get('project.lists.page_size_max');
$this->pageSize = Config::get('project.lists.page_size');
$this->pageType = $this->request->get('page_type', 1);
if ($this->pageType == 1) {
//分页
$this->pageNo = $this->request->get('page_no', 1) ?: 1;
$this->pageSize = $this->request->get('page_size', $this->pageSize) ?: $this->pageSize;
} else {
//不分页
$this->pageNo = 1;//强制到第一页
$this->pageSize = $this->pageSizeMax;// 直接取最大记录数
}
//limit查询参数设置
$this->limitOffset = ($this->pageNo - 1) * $this->pageSize;
$this->limitLength = $this->pageSize;
}
/**
* @notes 初始化搜索
* @return array
* @author 令狐冲
* @date 2021/7/31 00:00
*/
private function initSearch()
{
if (!($this instanceof ListsSearchInterface)) {
return [];
}
$startTime = $this->request->get('start_time');
if ($startTime) {
$this->startTime = strtotime($startTime);
}
$endTime = $this->request->get('end_time');
if ($endTime) {
$this->endTime = strtotime($endTime);
}
$this->start = $this->request->get('start');
$this->end = $this->request->get('end');
return $this->searchWhere = $this->createWhere($this->setSearch());
}
/**
* @notes 初始化排序
* @return array|string[]
* @author 令狐冲
* @date 2021/7/31 00:03
*/
private function initSort()
{
if (!($this instanceof ListsSortInterface)) {
return [];
}
$this->field = $this->request->get('field', '');
$this->orderBy = $this->request->get('order_by', '');
return $this->sortOrder = $this->createOrder($this->setSortFields(), $this->setDefaultOrder());
}
/**
* @notes 导出初始化
* @return false|\think\response\Json
* @author 令狐冲
* @date 2021/7/31 01:15
*/
private function initExport()
{
$this->export = $this->request->get('export', '');
//不做导出操作
if ($this->export != ExportEnum::INFO && $this->export != ExportEnum::EXPORT) {
return false;
}
//导出操作,但是没有实现导出接口
if (!($this instanceof ListsExcelInterface)) {
return JsonService::throw('该列表不支持导出');
}
$this->fileName = $this->request->get('file_name', '') ?: $this->setFileName();
//不导出文件,不初始化一下参数
if ($this->export != ExportEnum::EXPORT) {
return false;
}
//导出文件名设置
$this->fileName .= '-' . date('Y-m-d-His') . '.xlsx';
//导出文件准备
//指定导出范围(例:第2页到,第5页的数据)
if ($this->pageType == 1) {
$this->pageStart = $this->request->get('page_start', $this->pageStart);
$this->pageEnd = $this->request->get('page_end', $this->pageEnd);
//改变查询数量参数(例:第2页到,第5页的数据,查询->page(2,(5-2+1)*25)
$this->limitOffset = ($this->pageStart - 1) * $this->pageSize;
$this->limitLength = ($this->pageEnd - $this->pageStart + 1) * $this->pageSize;
}
$count = $this->count();
//判断导出范围是否有数据
if ($count == 0 || ceil($count / $this->pageSize) < $this->pageStart) {
$msg = $this->pageType ? '第' . $this->pageStart . '页到第' . $this->pageEnd . '页没有数据,无法导出' : '没有数据,无法导出';
return JsonService::throw($msg);
}
}
/**
* @notes 不需要分页,可以调用此方法,无需查询第二次
* @return int
* @author 令狐冲
* @date 2021/7/6 00:34
*/
public function defaultCount(): int
{
return count($this->lists());
}
<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台(PHP版)
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用,可去除界面版权logo
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
// | github下载:https://github.com/likeshop-github/likeadmin
// | 访问官网:https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\common\lists;
use app\common\enum\ExportEnum;
use app\common\service\JsonService;
use app\common\validate\ListsValidate;
use app\Request;
use think\facade\Config;
/**
* 数据列表基类
* Class BaseDataLists
* @package app\common\lists
*/
abstract class BaseDataLists implements ListsInterface
{
use ListsSearchTrait;
use ListsSortTrait;
use ListsExcelTrait;
public Request $request; //请求对象
public int $pageNo; //页码
public int $pageSize; //每页数量
public int $limitOffset; //limit查询offset值
public int $limitLength; //limit查询数量
public int $pageSizeMax;
public int $pageType = 0; //默认类型:0-一般分页;1-不分页,获取最大所有数据
protected string $orderBy;
protected string $field;
protected $startTime;
protected $endTime;
protected $start;
protected $end;
protected array $params;
protected $sortOrder = [];
public string $export;
/**
* 管理端列表:在 request 就绪后写入 adminId/adminInfo(见 BaseAdminDataLists
*/
protected function initAdminIdentity(): void
{
}
public function __construct()
{
//参数验证
(new ListsValidate())->get()->goCheck();
//请求参数设置
$this->request = request();
// admin 列表子类在 initExport 中可能触发 count/lists,须先于 initPage/initExport 写入身份
$this->initAdminIdentity();
$this->params = $this->request->param();
//分页初始化
$this->initPage();
//搜索初始化
$this->initSearch();
//排序初始化
$this->initSort();
//导出初始化
$this->initExport();
}
/**
* @notes 分页参数初始化
* @author 令狐冲
* @date 2021/7/30 23:55
*/
private function initPage()
{
$this->pageSizeMax = Config::get('project.lists.page_size_max');
$this->pageSize = Config::get('project.lists.page_size');
$this->pageType = $this->request->get('page_type', 1);
if ($this->pageType == 1) {
//分页
$this->pageNo = $this->request->get('page_no', 1) ?: 1;
$this->pageSize = $this->request->get('page_size', $this->pageSize) ?: $this->pageSize;
} else {
//不分页
$this->pageNo = 1;//强制到第一页
$this->pageSize = $this->pageSizeMax;// 直接取最大记录数
}
//limit查询参数设置
$this->limitOffset = ($this->pageNo - 1) * $this->pageSize;
$this->limitLength = $this->pageSize;
}
/**
* @notes 初始化搜索
* @return array
* @author 令狐冲
* @date 2021/7/31 00:00
*/
private function initSearch()
{
if (!($this instanceof ListsSearchInterface)) {
return [];
}
$startTime = $this->request->get('start_time');
if ($startTime) {
$this->startTime = strtotime($startTime);
}
$endTime = $this->request->get('end_time');
if ($endTime) {
$this->endTime = strtotime($endTime);
}
$this->start = $this->request->get('start');
$this->end = $this->request->get('end');
return $this->searchWhere = $this->createWhere($this->setSearch());
}
/**
* @notes 初始化排序
* @return array|string[]
* @author 令狐冲
* @date 2021/7/31 00:03
*/
private function initSort()
{
if (!($this instanceof ListsSortInterface)) {
return [];
}
$this->field = $this->request->get('field', '');
$this->orderBy = $this->request->get('order_by', '');
return $this->sortOrder = $this->createOrder($this->setSortFields(), $this->setDefaultOrder());
}
/**
* @notes 导出初始化
* @return false|\think\response\Json
* @author 令狐冲
* @date 2021/7/31 01:15
*/
private function initExport()
{
$this->export = $this->request->get('export', '');
//不做导出操作
if ($this->export != ExportEnum::INFO && $this->export != ExportEnum::EXPORT) {
return false;
}
//导出操作,但是没有实现导出接口
if (!($this instanceof ListsExcelInterface)) {
return JsonService::throw('该列表不支持导出');
}
$this->fileName = $this->request->get('file_name', '') ?: $this->setFileName();
//不导出文件,不初始化一下参数
if ($this->export != ExportEnum::EXPORT) {
return false;
}
//导出文件名设置
$this->fileName .= '-' . date('Y-m-d-His') . '.xlsx';
//导出文件准备
//指定导出范围(例:第2页到,第5页的数据)
if ($this->pageType == 1) {
$this->pageStart = $this->request->get('page_start', $this->pageStart);
$this->pageEnd = $this->request->get('page_end', $this->pageEnd);
//改变查询数量参数(例:第2页到,第5页的数据,查询->page(2,(5-2+1)*25)
$this->limitOffset = ($this->pageStart - 1) * $this->pageSize;
$this->limitLength = ($this->pageEnd - $this->pageStart + 1) * $this->pageSize;
}
$count = $this->count();
//判断导出范围是否有数据
if ($count == 0 || ceil($count / $this->pageSize) < $this->pageStart) {
$msg = $this->pageType ? '第' . $this->pageStart . '页到第' . $this->pageEnd . '页没有数据,无法导出' : '没有数据,无法导出';
return JsonService::throw($msg);
}
}
/**
* @notes 不需要分页,可以调用此方法,无需查询第二次
* @return int
* @author 令狐冲
* @date 2021/7/6 00:34
*/
public function defaultCount(): int
{
return count($this->lists());
}
}
@@ -1,122 +1,122 @@
<?php
declare(strict_types=1);
namespace app\common\lists\Traits;
use app\common\service\DataScope\DataScopeService;
use think\db\Query;
/**
* 列表按「数据范围」过滤。
*
* 使用前置条件:宿主类须通过 BaseAdminDataLists 获得 $this->adminId 与 $this->adminInfo。
*
* 三种用法:
* - applyDataScopeByOwner($q, 'creator_id') 直接按某列 IN
* - applyDataScopeByOwnerColumns($q, ['creator_id', 'doctor_id']) 多列 OR
* - applyDataScopeByExists($q, $sqlTemplate, 'owner_expr') 通过 exists 子查询(跨表)
*/
trait HasDataScopeFilter
{
/**
* 单列过滤。null 表示豁免(ALL)。
*/
protected function applyDataScopeByOwner($query, string $ownerField): bool
{
if (!$this->dataScopeShouldApply()) {
return false;
}
$ids = $this->getDataScopeVisibleAdminIds();
if ($ids === null) {
return false;
}
if ($ids === []) {
$query->whereRaw('0 = 1');
return true;
}
$query->whereIn($ownerField, $ids);
return true;
}
/**
* 多列 OR 过滤(任意属主列命中即可)。
*
* @param string[] $ownerFields
*/
protected function applyDataScopeByOwnerColumns($query, array $ownerFields): bool
{
if (!$this->dataScopeShouldApply()) {
return false;
}
$ids = $this->getDataScopeVisibleAdminIds();
if ($ids === null) {
return false;
}
if ($ids === []) {
$query->whereRaw('0 = 1');
return true;
}
$query->where(function ($q) use ($ownerFields, $ids) {
$first = true;
foreach ($ownerFields as $f) {
if ($first) {
$q->whereIn($f, $ids);
$first = false;
} else {
$q->whereOr(function ($qq) use ($f, $ids) {
$qq->whereIn($f, $ids);
});
}
}
});
return true;
}
/**
* exists 子查询方式。
* $subSqlTemplate 内部可使用占位符 `__OWNER_IDS__`,将被替换成逗号分隔的整数列表。
*/
protected function applyDataScopeByExistsSql($query, string $subSqlTemplate): bool
{
if (!$this->dataScopeShouldApply()) {
return false;
}
$ids = $this->getDataScopeVisibleAdminIds();
if ($ids === null) {
return false;
}
if ($ids === []) {
$query->whereRaw('0 = 1');
return true;
}
$inList = implode(',', $ids);
$sql = str_replace('__OWNER_IDS__', $inList, $subSqlTemplate);
$query->whereExists($sql);
return true;
}
/**
* 可见 admin idnull = 全部。
*
* @return array<int>|null
*/
protected function getDataScopeVisibleAdminIds(): ?array
{
$adminId = property_exists($this, 'adminId') ? (int) $this->adminId : 0;
$adminInfo = property_exists($this, 'adminInfo') && is_array($this->adminInfo) ? $this->adminInfo : [];
return DataScopeService::getVisibleAdminIds($adminId, $adminInfo);
}
protected function dataScopeShouldApply(): bool
{
return DataScopeService::isEnabled();
}
}
<?php
declare(strict_types=1);
namespace app\common\lists\Traits;
use app\common\service\DataScope\DataScopeService;
use think\db\Query;
/**
* 列表按「数据范围」过滤。
*
* 使用前置条件:宿主类须通过 BaseAdminDataLists 获得 $this->adminId 与 $this->adminInfo。
*
* 三种用法:
* - applyDataScopeByOwner($q, 'creator_id') 直接按某列 IN
* - applyDataScopeByOwnerColumns($q, ['creator_id', 'doctor_id']) 多列 OR
* - applyDataScopeByExists($q, $sqlTemplate, 'owner_expr') 通过 exists 子查询(跨表)
*/
trait HasDataScopeFilter
{
/**
* 单列过滤。null 表示豁免(ALL)。
*/
protected function applyDataScopeByOwner($query, string $ownerField): bool
{
if (!$this->dataScopeShouldApply()) {
return false;
}
$ids = $this->getDataScopeVisibleAdminIds();
if ($ids === null) {
return false;
}
if ($ids === []) {
$query->whereRaw('0 = 1');
return true;
}
$query->whereIn($ownerField, $ids);
return true;
}
/**
* 多列 OR 过滤(任意属主列命中即可)。
*
* @param string[] $ownerFields
*/
protected function applyDataScopeByOwnerColumns($query, array $ownerFields): bool
{
if (!$this->dataScopeShouldApply()) {
return false;
}
$ids = $this->getDataScopeVisibleAdminIds();
if ($ids === null) {
return false;
}
if ($ids === []) {
$query->whereRaw('0 = 1');
return true;
}
$query->where(function ($q) use ($ownerFields, $ids) {
$first = true;
foreach ($ownerFields as $f) {
if ($first) {
$q->whereIn($f, $ids);
$first = false;
} else {
$q->whereOr(function ($qq) use ($f, $ids) {
$qq->whereIn($f, $ids);
});
}
}
});
return true;
}
/**
* exists 子查询方式。
* $subSqlTemplate 内部可使用占位符 `__OWNER_IDS__`,将被替换成逗号分隔的整数列表。
*/
protected function applyDataScopeByExistsSql($query, string $subSqlTemplate): bool
{
if (!$this->dataScopeShouldApply()) {
return false;
}
$ids = $this->getDataScopeVisibleAdminIds();
if ($ids === null) {
return false;
}
if ($ids === []) {
$query->whereRaw('0 = 1');
return true;
}
$inList = implode(',', $ids);
$sql = str_replace('__OWNER_IDS__', $inList, $subSqlTemplate);
$query->whereExists($sql);
return true;
}
/**
* 可见 admin idnull = 全部。
*
* @return array<int>|null
*/
protected function getDataScopeVisibleAdminIds(): ?array
{
$adminId = property_exists($this, 'adminId') ? (int) $this->adminId : 0;
$adminInfo = property_exists($this, 'adminInfo') && is_array($this->adminInfo) ? $this->adminInfo : [];
return DataScopeService::getVisibleAdminIds($adminId, $adminInfo);
}
protected function dataScopeShouldApply(): bool
{
return DataScopeService::isEnabled();
}
}