Replies: 1 comment
-
|
以下代码完整复制 `<?php namespace App\Helpers; trait FileManagerTrait { } |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
amis没有文件选择器,我在tp里面使用了大佬封装的包,因为不会使用pr所以使用这种方式提交一下自己使用Picker 列表选择器做的文件选择器,有点瑕疵,需要两个接口实现文件获取和路径获取;
`<?php
namespace TpAmis\Traits;
trait FileManagerTrait
{
protected function getFileList($path = '')
{
return $this->getFilesAndDirectories($path);
}
protected function getDirNode($path = '')
{
$pathItems = $this->pathToNestedArray($path);
$GroupButtions = [];
foreach ($pathItems as $pathNod => $path) {
$path = preg_replace('#[\\/]+#', '/', $path);
}
protected function fileSelect($name, $lable = '', $NodepathUrl = '', $FileListUrl = '')$NodepathUrl . '?' . getUrlQuery(['path' => '$ {path}']))
{
return amis()->PickerControl($name, $lable)->source(
amis()->BaseApi()->methon('post')->url($FileListUrl)
)
->valueField('id')
->labelField('name')
->pickerSchema(
amis()->CRUD2Table()
->name('thelist')
->id('fileManagerList')
->headerToolbar([
amis()->Service()->id('fileManager')->name('fileManagerApi')->schemaApi(
amis()->BaseApi()
->url(
->method('post')
)
])
->columns([
amis()->TableColumn('type', '类型'),
amis()->TableColumn('icon', '图标')->type('image'),
amis()->TableColumn('name', '名称'),
amis()->TableColumn('site', '大小'),
amis()->TableColumn('ext', '后缀'),
// amis()->TableColumn('path', '路径'),
])
->rowClassNameExpr('${type === "dir" ? "" : "no-click"}', )
->onEvent(
[
'rowClick' => [
'actions' => [
[
"actionType" => 'rebuild',
"componentId" => "fileManager",
'args' => [
"path" => '${event.data.item.path}'
]
],
[
"actionType" => "reload",
"data" => [
"path" => '${event.data.item.path}'
]
]
}
private $root_path;
private $public_path;
private $upload_path;
private $directory;
/**
*/
public function base()
{
}
/**
获取指定目录下的所有文件和文件夹名称,并区分文件和文件夹
@return array 文件和文件夹信息列表
*/
public function getFilesAndDirectories($directory = '')
{
$this->base();
$items = [];
foreach (new \DirectoryIterator($this->upload_path . $directory) as $fileInfo) {
if ($fileInfo->isDot()) {
continue; // 跳过 . 和 ..
}
}
return $items;
}
/**
计算文件大小
@param int $byte
@return string
*/
private function trans_byte(int $byte)
{
if ($byte == 0) {
return '';
}
$KB = 1024;
$MB = 1024 * $KB;
$GB = 1024 * $MB;
$TB = 1024 * $GB;
if ($byte < $KB) {
return $byte . " B";
} elseif ($byte < $MB) {
return round($byte / $KB, 2) . " KB";
} elseif ($byte < $GB) {
return round($byte / $MB, 2) . " MB";
} elseif ($byte < $TB) {
return round($byte / $GB, 2) . " GB";
} else {
return round($byte / $TB, 2) . " TB";
}
}
/**
将文件路径转换为数组,数组从高到底每一个值都比上一个值多一个路径节点名称
@return array 路径数组
*/
public function pathToNestedArray($directory = '')
{
$this->base();
$directory = str_replace($this->upload_path, '', $directory);
// 分割路径为节点
$parts = explode(DIRECTORY_SEPARATOR, $directory);
// 初始化结果数组
$result = [];
$currentPath = '';
// 遍历每个路径节点
foreach ($parts as $part) {
// 拼接当前路径
$currentPath .= ($currentPath === '' ? '/' : DIRECTORY_SEPARATOR) . $part;
}
return $result;
}
Beta Was this translation helpful? Give feedback.
All reactions