232 lines
6.1 KiB
PHP
Executable File
232 lines
6.1 KiB
PHP
Executable File
<?php
|
||
|
||
namespace App\Services\Tg\Hook\Mod;
|
||
|
||
//use App\Models\BladeJctj as ModelBladeJctj;
|
||
use App\Services\Tg\Hook\Mod\DDT;
|
||
use App\Models\option as ModelOption;
|
||
|
||
class Upload extends Base {
|
||
|
||
private $oDDT;
|
||
|
||
public function __construct($aUpdate)
|
||
{
|
||
|
||
$this->oDDT = new DDT();
|
||
$this->oDDT->_setPrimaryKey("id"); // 可选,默认id
|
||
// $this->oDDT->_setStrField(["content"]); // 可选,指定长文本字段
|
||
$this->oDDT->_setThisName("upload"); // 必须,me用,正常就是类名,只不过后期没准改名,所以就这里写死
|
||
|
||
}
|
||
|
||
public function __call($sName, $aArg)
|
||
{
|
||
|
||
return $this->oDDT->$sName($aArg[0]);
|
||
|
||
}
|
||
|
||
public function limit($aParam)
|
||
{
|
||
$iSkip = $aParam["aArg"][1] ?? 0;
|
||
$iTake = $aParam["aArg"][2] ?? 0;
|
||
$sLike = $aParam["like"] ?? '';
|
||
$isDel = $aParam["is_del"] ?? false;
|
||
|
||
if ($iSkip == "?") {
|
||
return "#limit {skip} {take}";
|
||
}
|
||
|
||
if (!$iTake) {
|
||
$iTake = ModelOption::_hit("upload", "limit");
|
||
}
|
||
|
||
// if (!$iSkip) {
|
||
// return "需要skip";
|
||
// }
|
||
|
||
if (!$iTake) {
|
||
return "需要take";
|
||
}
|
||
|
||
$t = $this->getFileList($iSkip, $iTake, $sLike, $isDel);
|
||
|
||
return $t;
|
||
|
||
}
|
||
|
||
public function page($aParam)
|
||
{
|
||
$iPage = $aParam["aArg"][1] ?? 1;
|
||
$sLike = $aParam["like"] ?? '';
|
||
|
||
if ($iPage == "?") {
|
||
return "#page {page:defaut=1}";
|
||
}
|
||
|
||
$iLimit = ModelOption::_hit("upload", "limit") ?? 0;
|
||
|
||
if (!$iLimit) {
|
||
return "需要option里的limit值";
|
||
}
|
||
|
||
$iPageTrue = $iPage - 1;
|
||
|
||
$iSkip = $iPageTrue * $iLimit;
|
||
|
||
$aParam["aArg"][1] = $iSkip;
|
||
$aParam["aArg"][2] = $iLimit;
|
||
|
||
return $this->limit($aParam);
|
||
}
|
||
|
||
public function like($aParam)
|
||
{
|
||
$iPage = $aParam["aOption"][1] ?? 1;
|
||
$sLike = $aParam["aArg"][1] ?? false;
|
||
// $is_del = $aParam
|
||
|
||
if ($sLike == "?") {
|
||
return "#like {like}";
|
||
}
|
||
|
||
if (!$sLike) {
|
||
return "需要like";
|
||
}
|
||
|
||
$aParam["like"] = $sLike;
|
||
$aParam["aArg"][1] = $iPage;
|
||
$aParam["aOption"] = [];
|
||
|
||
return $this->page($aParam);
|
||
}
|
||
|
||
public function list($aParam)
|
||
{
|
||
return $this->page($aParam);
|
||
}
|
||
|
||
public function del($aParam)
|
||
{
|
||
$sName = $aParam["aArg"][1] ?? false;
|
||
|
||
if ($sName == "?") {
|
||
return "#del {name}";
|
||
}
|
||
|
||
if (!$sName) {
|
||
return "需要name";
|
||
}
|
||
|
||
$filePath = 'upload/'.$sName; // 替换为你要删除的文件路径
|
||
|
||
if (file_exists($filePath)) {
|
||
if (unlink($filePath)) {
|
||
return "文件已成功删除。";
|
||
} else {
|
||
return "删除文件时出错。";
|
||
}
|
||
} else {
|
||
return "文件不存在。";
|
||
}
|
||
}
|
||
|
||
public function show($aParam)
|
||
{
|
||
$sName = $aParam["aArg"][1] ?? false;
|
||
|
||
if ($sName == "?") {
|
||
return "#show {name}";
|
||
}
|
||
|
||
if (!$sName) {
|
||
return "需要name";
|
||
}
|
||
|
||
$str = "";
|
||
$str .= $sName."\n\n";
|
||
$str .= "<img src='/upload/".$sName."'>";
|
||
|
||
return $str;
|
||
}
|
||
|
||
public function likeDel($aParam)
|
||
{
|
||
$aParam["is_del"] = true;
|
||
|
||
return $this->like($aParam);
|
||
}
|
||
|
||
private function getFileList($skip = 0, $take = 10, $searchTerm = '', $is_del = false) // 添加 is_del 参数
|
||
{
|
||
$directory = 'upload';
|
||
|
||
if (is_dir($directory)) {
|
||
$str = "";
|
||
$str .= "<table>";
|
||
$str .= "<tr><th>NO</th><th>文件名</th><th>预览</th></tr>"; // 添加表头
|
||
|
||
$files = scandir($directory);
|
||
$count = 0; // 计数器
|
||
$id = 1; // ID 从1开始
|
||
$countDel = 0;
|
||
|
||
foreach ($files as $file) {
|
||
// 排除当前目录(.)和上级目录(..)
|
||
if ($file !== '.' && $file !== '..') {
|
||
// 如果提供了搜索关键字,检查文件名是否包含该关键字
|
||
if ($searchTerm && stripos($file, $searchTerm) === false) {
|
||
continue; // 不包含关键字则跳过
|
||
}
|
||
|
||
// 如果 is_del 为 true,删除匹配的文件
|
||
if ($is_del) {
|
||
$filePath = $directory . '/' . $file;
|
||
if (file_exists($filePath)) {
|
||
unlink($filePath); // 删除文件
|
||
$countDel++;
|
||
}
|
||
continue; // 删除后跳过当前循环
|
||
}
|
||
|
||
// 跳过指定数量的文件
|
||
if ($count < $skip) {
|
||
$count++;
|
||
continue; // 跳过当前循环
|
||
}
|
||
|
||
// 检查是否超过取的限制
|
||
if ($count >= $skip + $take) {
|
||
break; // 超过限制则退出循环
|
||
}
|
||
|
||
$str .= "<tr>";
|
||
$str .= "<td>".$id."</td>"; // 输出 ID
|
||
$str .= "<td>".$file."</td>";
|
||
$str .= "<td><img height='50px' src='./upload/".$file."'></td>";
|
||
$str .= "</tr>";
|
||
|
||
$count++; // 增加计数
|
||
$id++; // 增加 ID
|
||
}
|
||
}
|
||
|
||
$str .= "</table>";
|
||
|
||
if ($is_del === true) {
|
||
return $countDel;
|
||
} else {
|
||
return $str;
|
||
}
|
||
|
||
} else {
|
||
return "目录不存在。";
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
}
|