Skip to content

Commit

Permalink
更新:优化后台文件管理页面
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuchunshu committed Jan 18, 2023
1 parent 6a07f4d commit b723fb4
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 48 deletions.
18 changes: 15 additions & 3 deletions app/Plugins/User/menu.php
Expand Up @@ -38,7 +38,7 @@

//文件管理
menu()->add(105,[
'url' => '/admin/users/files',
'url' => '/admin/files',
'name' => '文件管理',
'icon' => '<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-file-info" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
Expand All @@ -51,7 +51,19 @@

//文件管理
menu()->add(1051,[
'url' => '/admin/users/files',
'url' => '/admin/files/user',
'name' => '用户文件',
'icon' => '<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-file-info" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
<path d="M14 3v4a1 1 0 0 0 1 1h4"></path>
<path d="M17 21h-10a2 2 0 0 1 -2 -2v-14a2 2 0 0 1 2 -2h7l5 5v11a2 2 0 0 1 -2 2z"></path>
<path d="M11 14h1v4h1"></path>
<path d="M12 11h.01"></path>
</svg>',
'parent_id' => 105
]);
menu()->add(1050,[
'url' => '/admin/files',
'name' => '文件列表',
'icon' => '<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-file-info" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
Expand All @@ -65,7 +77,7 @@

//文件管理
menu()->add(1052,[
'url' => '/admin/users/files/upload',
'url' => '/admin/files/upload',
'name' => '上传文件',
'icon' => '<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-file-info" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
Expand Down
86 changes: 49 additions & 37 deletions app/Plugins/User/resources/views/Admin/Files/index.blade.php
Expand Up @@ -7,54 +7,66 @@
<div class="col-md-12">
<div class="border-0 card">
<div class="card-body">
<h3 class="card-title">文件管理</h3>
<h3 class="card-title">用户文件管理</h3>
<div class="row">
<div class="col"></div>

</div>
@if($page->count())
<div class="table-responsive" id="vue-users-files">
<table
class="table table-vcenter table-nowrap">
<thead>
<tr>
<th>#</th>
<th>类型</th>
<th>文件名</th>
<th>创建者</th>
<th>路径</th>
<th>url</th>
<th>创建时间</th>
<th class="w-1"></th>
<th class="w-1"></th>
</tr>
</thead>
<tbody>
@foreach($page as $data)

<div class="table-responsive" id="vue-users-files">
<table
class="table table-vcenter table-nowrap">
<thead>
<tr>
<th>#</th>
<th>类型</th>
<th>路径</th>
<th>文件名</th>
<th>url</th>
<th>创建时间</th>
<th class="w-1">文件大小</th>
<th class="w-1"></th>
</tr>
</thead>
<tbody>
@if($page->count())
@foreach($page as $file)
<tr>
<td>{{$data->id}}</td>
<td>
<span class="avatar bg-blue-lt">{{file_suffix($data->path)}}</span>
</td>
<td @@click="alert('{{path_file_name($data->path)}}')" class="text-truncate" style="max-width: 100px">
{{path_file_name($data->path)}}
{{$file['id']}}
</td>
<td>
<a href="/users/{{$data->user->id}}.html"><span class="avatar" style="background-image: url({{super_avatar($data->user)}})"></span></a>
<span class="avatar bg-blue-lt">{{$file['extension']}}</span>
</td>
<td @@click="alert('{{$data->path}}')" data-bs-toggle="tooltip" data-bs-placement="top" title="{{$data->path}}" class="text-truncate" style="max-width: 100px">{{$data->path}}</td>
<td class="text-truncate" style="max-width: 100px"><a href="{{$data->url}}">{{$data->url}}</a></td>
<td>{{$data->created_at}}</td>
<td><a @@click="download('{{$data->url}}')" href="#">下载</a></td>
<td><a @@click="remove({{$data->id}})" href="#">删除</a></td>
<td @@click="alert('{{$file['path']}}')" class="text-truncate"
style="max-width: 100px">
{{($file['path'])}}
</td>
<td @@click="alert('{{$file['filename']}}')" data-bs-toggle="tooltip"
data-bs-placement="top" title="{{$file['filename']}}" class="text-truncate"
style="max-width: 100px">{{$file['filename']}}</td>
<td class="text-truncate" style="max-width: 100px"><a
href="{{$file['url']}}">{{$file['url']}}</a></td>
<td>{{$file['date']}}</td>
<td>{{round($file['size']/1024,2)}}KB</td>
<td><a @@click="download('{{$file['url']}}')" href="#">下载</a></td>
</tr>
@endforeach
</tbody>
</table>
</div>
@else
无用户
@endif
@else
<tr>
<td>无更多结果</td>
<td>无更多结果</td>
<td>无更多结果</td>
<td>无更多结果</td>
<td>无更多结果</td>
<td>无更多结果</td>
<td>无更多结果</td>
<td>无更多结果</td>
</tr>
@endif
</tbody>
</table>
</div>
</div>
{!! make_page($page) !!}
</div>
Expand Down
Expand Up @@ -8,7 +8,7 @@
<div class="border-0 card">
<div class="card-body">
<h3 class="card-title">文件上传</h3>
<form action="?Redirect=/admin/users/files/upload" method="post" enctype="multipart/form-data">
<form action="?Redirect=/admin/files/upload" method="post" enctype="multipart/form-data">
<x-csrf/>
<div class="mb-3">
<input type="file" class="form-control" id="file" name="file">
Expand Down
66 changes: 66 additions & 0 deletions app/Plugins/User/resources/views/Admin/Files/user.blade.php
@@ -0,0 +1,66 @@
@extends("app")

@section('title',"文件管理")


@section('content')
<div class="col-md-12">
<div class="border-0 card">
<div class="card-body">
<h3 class="card-title">用户文件管理</h3>
<div class="row">
<div class="col"></div>

</div>
@if($page->count())
<div class="table-responsive" id="vue-users-files">
<table
class="table table-vcenter table-nowrap">
<thead>
<tr>
<th>#</th>
<th>类型</th>
<th>文件名</th>
<th>创建者</th>
<th>路径</th>
<th>url</th>
<th>创建时间</th>
<th class="w-1"></th>
<th class="w-1"></th>
</tr>
</thead>
<tbody>
@foreach($page as $data)
<tr>
<td>{{$data->id}}</td>
<td>
<span class="avatar bg-blue-lt">{{file_suffix($data->path)}}</span>
</td>
<td @@click="alert('{{path_file_name($data->path)}}')" class="text-truncate" style="max-width: 100px">
{{path_file_name($data->path)}}
</td>
<td>
<a href="/users/{{$data->user->id}}.html"><span class="avatar" style="background-image: url({{super_avatar($data->user)}})"></span></a>
</td>
<td @@click="alert('{{$data->path}}')" data-bs-toggle="tooltip" data-bs-placement="top" title="{{$data->path}}" class="text-truncate" style="max-width: 100px">{{$data->path}}</td>
<td class="text-truncate" style="max-width: 100px"><a href="{{$data->url}}">{{$data->url}}</a></td>
<td>{{$data->created_at}}</td>
<td><a @@click="download('{{$data->url}}')" href="#">下载</a></td>
<td><a @@click="remove({{$data->id}})" href="#">删除</a></td>
</tr>
@endforeach
</tbody>
</table>
</div>
@else
无用户
@endif
</div>
{!! make_page($page) !!}
</div>
</div>
@endsection

@section('scripts')
<script src="{{ mix("plugins/User/js/user.js") }}"></script>
@endsection
53 changes: 48 additions & 5 deletions app/Plugins/User/src/Controller/Admin/FilesController.php
Expand Up @@ -18,16 +18,27 @@
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\Paginator\LengthAwarePaginator;
use Hyperf\Utils\Collection;
use Symfony\Component\Finder\Finder;

#[Controller(prefix: '/admin/users/files')]
#[Controller(prefix: '/admin/files')]
#[Middleware(AdminMiddleware::class)]
class FilesController
{
#[GetMapping(path: 'user')]
public function user_file()
{
$page = UserUpload::query()->orderBy('id', 'desc')->with('user')->paginate(30);

return view('User::Admin.Files.user', ['page' => $page]);
}

#[GetMapping(path: '')]
public function index()
{
$page = UserUpload::query()->orderBy('id', 'desc')->with('user')->paginate(30);
return view('User::Admin.Files.index', ['page' => $page]);

return view('User::Admin.Files.index', ['page' => $this->page()]);
}

#[GetMapping(path: 'upload')]
Expand All @@ -42,9 +53,41 @@ public function upload_submit(UploadFile $request, UploadHandler $uploader)
$file = $request->file('file');
$data = $uploader->save($file, 'admin', admin_auth()->id());
if ($data['success'] === true) {
return redirect()->url('/admin/users/files/upload?url=' . $data['path'])->with('success', '上传成功!')->go();
return redirect()->url('/admin/files/upload?url=' . $data['path'])->with('success', '上传成功!')->go();
}

return redirect()->url('/admin/files/upload')->with('danger', '上传失败!')->go();
}

private function get_all_files()
{
$path = public_path('/upload');
$files = [];
$result = Finder::create()->in($path)->files();
$id=1;
foreach ($result as $item) {
$files[] = [
'id' => $id++,
'extension' => $item->getExtension(),
'size' => $item->getSize(),
'date' => date('Y-m-d H:i:s', $item->getCTime()),
'filename' => $item->getFilename(),
'url' => url('/upload/' . $item->getRelativePath() . '/' . $item->getFilename()),
'path' => $item->getRealPath(),
];
}
return $files;
}

private function page()
{
$currentPage = (int) request()->input('page', 1);
$perPage = (int) request()->input('per_page', 15);

// 这里根据 $currentPage 和 $perPage 进行数据查询,以下使用 Collection 代替
$collection = new Collection($this->get_all_files());

return redirect()->url('/admin/users/files/upload')->with('danger', '上传失败!')->go();
$data = array_values($collection->forPage($currentPage, $perPage)->toArray());
return new LengthAwarePaginator($data, count($this->get_all_files()), $perPage, $currentPage);
}
}
2 changes: 1 addition & 1 deletion app/helpers.php
Expand Up @@ -39,7 +39,7 @@
function public_path($path = ''): string
{
if ($path !== '') {
return config('server.settings.document_root') . '/' . $path;
return config('server.settings.document_root') . '/' . ltrim($path,'/');
}
return config('server.settings.document_root');
}
Expand Down
2 changes: 1 addition & 1 deletion resources/views/setting/theme/common.blade.php
Expand Up @@ -16,7 +16,7 @@
<div class="col-lg-4">
<div class="form-label">网站icon</div>
<input type="text" class="form-control" v-model="data.theme_common_icon">
<small>填写链接,<a href="/admin/users/files/upload" target="_blank">点我</a> 上传文件</small>
<small>填写链接,<a href="/admin/files/upload" target="_blank">点我</a> 上传文件</small>
</div>
<div class="col-lg-4">
<div class="form-label">友情链接</div>
Expand Down

0 comments on commit b723fb4

Please sign in to comment.