2025-12-24 16:59:05 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace plugin\admin\app\controller;
|
|
|
|
|
|
2025-12-25 06:02:38 +08:00
|
|
|
use Exception;
|
2025-12-24 16:59:05 +08:00
|
|
|
use support\Request;
|
|
|
|
|
use support\Response;
|
|
|
|
|
use support\think\Db;
|
|
|
|
|
use taoser\facade\Validate;
|
|
|
|
|
use Shopwwi\WebmanFilesystem\Facade\Storage;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 附件管理
|
|
|
|
|
*
|
|
|
|
|
* @icon fa fa-circle-o
|
|
|
|
|
*/
|
|
|
|
|
class FilesController extends Crud
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var \app\model\Files
|
|
|
|
|
*/
|
|
|
|
|
protected $model = null;
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->model = new \app\model\Files();
|
|
|
|
|
}
|
|
|
|
|
function list(Request $request)
|
|
|
|
|
{
|
2025-12-25 06:02:38 +08:00
|
|
|
$list = $this->model->paginate();
|
|
|
|
|
return $this->success(__('successful'),$list);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
function felist(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$list = $this->model->paginate();
|
|
|
|
|
$result = [];
|
|
|
|
|
$list->each(function($item)use(&$result){
|
|
|
|
|
array_push($result,[
|
|
|
|
|
'tag' => "test",
|
|
|
|
|
'name' => $item["origin_name"],
|
|
|
|
|
//'thumb'=> $item["file_url"],
|
|
|
|
|
'url' => $item["file_url"],
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
return json($result);
|
2025-12-24 16:59:05 +08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
function feupload(Request $request): Response
|
|
|
|
|
{
|
|
|
|
|
$savePath = $request->post('savePath','files');
|
2025-12-25 06:02:38 +08:00
|
|
|
$data = $this->base($request, $savePath);
|
|
|
|
|
return json([
|
|
|
|
|
'link' => $data['file_url'],
|
|
|
|
|
]);
|
2025-12-24 16:59:05 +08:00
|
|
|
}
|
|
|
|
|
function upload(Request $request): Response
|
|
|
|
|
{
|
|
|
|
|
$savePath = $request->post('savePath','files');
|
|
|
|
|
$data = $this->base($request, $savePath);
|
2025-12-25 06:02:38 +08:00
|
|
|
return $this->success( '上传成功', $data);
|
2025-12-24 16:59:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取上传数据
|
|
|
|
|
* @param Request $request
|
|
|
|
|
* @param $savePath 保存位置
|
|
|
|
|
* @return array
|
|
|
|
|
* @throws \Exception
|
|
|
|
|
*/
|
2025-12-25 06:02:38 +08:00
|
|
|
protected function base(Request $request, $savePath): mixed
|
2025-12-24 16:59:05 +08:00
|
|
|
{
|
|
|
|
|
// 适配器 local默认是存储在runtime目录下 public默认是存储在public目录下
|
|
|
|
|
// 可访问的静态文件建议public
|
|
|
|
|
// 默认适配器是local
|
|
|
|
|
//Storage::adapter('public');
|
2025-12-25 06:02:38 +08:00
|
|
|
$validate = Validate::rule('savePath', 'alphaNum');
|
|
|
|
|
$data = ['savePath' => $savePath];
|
|
|
|
|
if (!$validate->check($data)) {
|
|
|
|
|
throw new Exception( '参数错误:'.$validate->getError());
|
|
|
|
|
}
|
2025-12-24 16:59:05 +08:00
|
|
|
$savePath = trim($savePath,'/');
|
2025-12-25 06:02:38 +08:00
|
|
|
//$savePath = 'upload/'.$savePath;
|
|
|
|
|
//$savePath = $savePath;
|
2025-12-24 16:59:05 +08:00
|
|
|
$file = current($request->file());
|
2025-12-25 06:02:38 +08:00
|
|
|
if (!$file || !$file->isValid()) {
|
|
|
|
|
throw new Exception( '未找到文件');
|
|
|
|
|
}
|
2025-12-24 16:59:05 +08:00
|
|
|
$mimetype = explode(',',Config('site.upload_mimetype'));
|
|
|
|
|
$maxsize = Config('site.upload_maxsize')*1024*1024;
|
2025-12-25 06:02:38 +08:00
|
|
|
$result = Storage::adapter('minio')
|
2025-12-24 16:59:05 +08:00
|
|
|
->path($savePath)
|
|
|
|
|
->size($maxsize)
|
|
|
|
|
->extYes($mimetype)
|
|
|
|
|
->upload($file);
|
|
|
|
|
$save_datas = [
|
2025-12-25 06:02:38 +08:00
|
|
|
'admin_id' => admin_id(),
|
|
|
|
|
'category' => 'default',
|
|
|
|
|
'adapter' => $result->adapter,
|
|
|
|
|
'origin_name' => $result->origin_name,
|
|
|
|
|
'file_name' => $result->file_name,
|
|
|
|
|
'size' => $result->size,
|
|
|
|
|
'mime_type' => $result->mime_type,
|
|
|
|
|
'extension' => $result->extension,
|
|
|
|
|
'file_height' => $result->file_height,
|
|
|
|
|
'file_width' => $result->file_width,
|
|
|
|
|
'file_url' => $result->file_url,
|
|
|
|
|
'sha1' => $result->storage_key ?:sha1_file(public_path($result->file_name)),
|
|
|
|
|
'use_count' => 0,
|
2025-12-24 16:59:05 +08:00
|
|
|
];
|
2025-12-25 06:02:38 +08:00
|
|
|
|
|
|
|
|
return \app\model\Files::create($save_datas);
|
2025-12-24 16:59:05 +08:00
|
|
|
}
|
|
|
|
|
}
|