初始化仓库

This commit is contained in:
wangxiaowei
2025-04-22 14:09:52 +08:00
commit 8b100110bb
5155 changed files with 664201 additions and 0 deletions

View File

@ -0,0 +1,218 @@
<?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;
public function __construct()
{
//参数验证
(new ListsValidate())->get()->goCheck();
//请求参数设置
$this->request = request();
$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());
}
}

View File

@ -0,0 +1,39 @@
<?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;
interface ListsExcelInterface
{
/**
* @notes 设置导出字段
* @return array
* @author 令狐冲
* @date 2021/7/21 16:04
*/
public function setExcelFields(): array;
/**
* @notes 设置导出文件名
* @return string
* @author 令狐冲
* @date 2021/7/26 17:47
*/
public function setFileName():string;
}

View File

@ -0,0 +1,136 @@
<?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\cache\ExportCache;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
trait ListsExcelTrait
{
public int $pageStart = 1; //导出开始页码
public int $pageEnd = 200; //导出介绍页码
public string $fileName = ''; //文件名称
/**
* @notes 创建excel
* @param $excelFields
* @param $lists
* @return string
* @throws \PhpOffice\PhpSpreadsheet\Exception
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
* @author 令狐冲
* @date 2021/7/21 16:04
*/
public function createExcel($excelFields, $lists)
{
$title = array_values($excelFields);
$data = [];
foreach ($lists as $row) {
$temp = [];
foreach ($excelFields as $key => $excelField) {
$fieldData = $row[$key];
if (is_numeric($fieldData) && strlen($fieldData) >= 12) {
$fieldData .= "\t";
}
$temp[$key] = $fieldData;
}
$data[] = $temp;
}
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
//设置单元格内容
foreach ($title as $key => $value) {
// 单元格内容写入
$sheet->setCellValueByColumnAndRow($key + 1, 1, $value);
}
$row = 2; //从第二行开始
foreach ($data as $item) {
$column = 1;
foreach ($item as $value) {
//单元格内容写入
$sheet->setCellValueByColumnAndRow($column, $row, $value);
$column++;
}
$row++;
}
$getHighestRowAndColumn = $sheet->getHighestRowAndColumn();
$HighestRow = $getHighestRowAndColumn['row'];
$column = $getHighestRowAndColumn['column'];
$titleScope = 'A1:' . $column . '1';//第一标题范围A1:D1)
$sheet->getStyle($titleScope)
->getFill()
->setFillType(Fill::FILL_SOLID) // 设置填充样式
->getStartColor()
->setARGB('00B0F0');
// 设置文字颜色为白色
$sheet->getStyle($titleScope)->getFont()->getColor()
->setARGB('FFFFFF');
// $sheet->getStyle('B2')->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_DATE_YYYYMMDD);
$spreadsheet->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
$allCope = 'A1:' . $column . $HighestRow;//整个表格范围A1:D5
$sheet->getStyle($allCope)->getBorders()->getAllBorders()->setBorderStyle(Border::BORDER_THIN);
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
//创建excel文件
$exportCache = new ExportCache();
$src = $exportCache->getSrc();
if (!file_exists($src)) {
mkdir($src, 0775, true);
}
$writer->save($src . $this->fileName);
//设置本地excel缓存并返回下载地址
$vars = ['file' => $exportCache->setFile($this->fileName)];
return (string)(url('adminapi/download/export', $vars, true, true));
}
/**
* @notes 获取导出信息
* @return array
* @author 令狐冲
* @date 2021/7/29 16:08
*/
public function excelInfo()
{
$count = $this->count();
$sum_page = max(ceil($count / $this->pageSize), 1);
return [
'count' => $count, //所有数据记录数
'page_size' => $this->pageSize,//每页记录数
'sum_page' => $sum_page,//一共多少页
'max_page' => floor($this->pageSizeMax / $this->pageSize),//最多导出多少页
'all_max_size' => $this->pageSizeMax,//最多导出记录数
'page_start' => $this->pageStart,//导出范围页码开始值
'page_end' => min($sum_page, $this->pageEnd),//导出范围页码结束值
'file_name' => $this->fileName,//默认文件名
];
}
}

View File

@ -0,0 +1,30 @@
<?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;
interface ListsExtendInterface
{
/**
* @notes 扩展字段
* @return mixed
* @author 令狐冲
* @date 2021/7/21 17:45
*/
public function extend();
}

View File

@ -0,0 +1,37 @@
<?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;
interface ListsInterface
{
/**
* @notes 实现数据列表
* @return array
* @author 令狐冲
* @date 2021/7/6 00:33
*/
public function lists(): array;
/**
* @notes 实现数据列表记录数
* @return int
* @author 令狐冲
* @date 2021/7/6 00:34
*/
public function count(): int;
}

View File

@ -0,0 +1,29 @@
<?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;
interface ListsSearchInterface
{
/**
* @notes 设置搜索条件
* @return array
* @author 令狐冲
* @date 2021/7/7 19:44
*/
public function setSearch(): array;
}

View File

@ -0,0 +1,108 @@
<?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;
trait ListsSearchTrait
{
protected array $params;
protected $searchWhere = [];
/**
* @notes 搜索条件生成
* @param $search
* @return array
* @author 令狐冲
* @date 2021/7/7 19:36
*/
private function createWhere($search)
{
if (empty($search)) {
return [];
}
$where = [];
foreach ($search as $whereType => $whereFields) {
switch ($whereType) {
case '=':
case '<>':
case '>':
case '>=':
case '<':
case '<=':
case 'in':
foreach ($whereFields as $whereField) {
$paramsName = substr_symbol_behind($whereField);
if (!isset($this->params[$paramsName]) || $this->params[$paramsName] == '') {
continue;
}
$where[] = [$whereField, $whereType, $this->params[$paramsName]];
}
break;
case '%like%':
foreach ($whereFields as $whereField) {
$paramsName = substr_symbol_behind($whereField);
if (!isset($this->params[$paramsName]) || empty($this->params[$paramsName])) {
continue;
}
$where[] = [$whereField, 'like', '%' . $this->params[$paramsName] . '%'];
}
break;
case '%like':
foreach ($whereFields as $whereField) {
$paramsName = substr_symbol_behind($whereField);
if (!isset($this->params[$paramsName]) || empty($this->params[$paramsName])) {
continue;
}
$where[] = [$whereField, 'like', '%' . $this->params[$paramsName]];
}
break;
case 'like%':
foreach ($whereFields as $whereField) {
$paramsName = substr_symbol_behind($whereField);
if (!isset($this->params[$paramsName]) || empty($this->params[$paramsName])) {
continue;
}
$where[] = [$whereField, 'like', $this->params[$paramsName] . '%'];
}
break;
case 'between_time':
if (!is_numeric($this->startTime) || !is_numeric($this->endTime)) {
break;
}
$where[] = [$whereFields, 'between', [$this->startTime, $this->endTime]];
break;
case 'between':
if (empty($this->start) || empty($this->end)) {
break;
}
$where[] = [$whereFields, 'between', [$this->start, $this->end]];
break;
case 'find_in_set': // find_in_set查询
foreach ($whereFields as $whereField) {
$paramsName = substr_symbol_behind($whereField);
if (!isset($this->params[$paramsName]) || $this->params[$paramsName] == '') {
continue;
}
$where[] = [$whereField, 'find in set', $this->params[$paramsName]];
}
break;
}
}
return $where;
}
}

View File

@ -0,0 +1,38 @@
<?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;
interface ListsSortInterface
{
/**
* @notes 设置支持排序字段
* @return array
* @author 令狐冲
* @date 2021/7/7 19:44
*/
public function setSortFields(): array;
/**
* @notes 设置默认排序条件
* @return array
* @author 令狐冲
* @date 2021/7/16 00:01
*/
public function setDefaultOrder():array;
}

View File

@ -0,0 +1,53 @@
<?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;
trait ListsSortTrait
{
protected string $orderBy;
protected string $field;
/**
* @notes 生成排序条件
* @param $sortField
* @param $defaultOrder
* @return array|string[]
* @author 令狐冲
* @date 2021/7/16 00:06
*/
private function createOrder($sortField, $defaultOrder)
{
if (empty($sortField) || empty($this->orderBy) || empty($this->field) || !in_array($this->field, array_keys($sortField))) {
return $defaultOrder;
}
if (isset($sortField[$this->field])) {
$field = $sortField[$this->field];
} else {
return $defaultOrder;
}
if ($this->orderBy == 'desc') {
return [$field => 'desc'];
}
if ($this->orderBy == 'asc') {
return [$field => 'asc'];
}
return $defaultOrder;
}
}