From 0e1663a16676dbb3091741ffafc49cf06e81e2d1 Mon Sep 17 00:00:00 2001 From: weiz Date: Mon, 4 Dec 2023 10:44:39 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=9C=80=E6=96=B0=E7=89=88=E6=9C=AC=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/AppUpdateController.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 app/api/controller/AppUpdateController.php diff --git a/app/api/controller/AppUpdateController.php b/app/api/controller/AppUpdateController.php new file mode 100644 index 0000000..e04a884 --- /dev/null +++ b/app/api/controller/AppUpdateController.php @@ -0,0 +1,13 @@ +where('')->order('id desc')->findOrEmpty(); + return $this->success('请求成功',$data->toArray()); + } + } \ No newline at end of file -- 2.40.1 From 1205579a17aa8a01f645d4089adf796dc24295f2 Mon Sep 17 00:00:00 2001 From: weiz Date: Mon, 4 Dec 2023 13:43:53 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/adminapi/controller/UploadController.php | 11 ++++ app/common/service/UploadService.php | 57 ++++++++++++++++++++ app/common/service/storage/engine/Server.php | 2 +- config/project.php | 5 ++ 4 files changed, 74 insertions(+), 1 deletion(-) diff --git a/app/adminapi/controller/UploadController.php b/app/adminapi/controller/UploadController.php index 1bee7dd..f45d518 100644 --- a/app/adminapi/controller/UploadController.php +++ b/app/adminapi/controller/UploadController.php @@ -59,5 +59,16 @@ class UploadController extends BaseAdminController return $this->fail($e->getMessage()); } } + + public function file() + { + try { + $cid = $this->request->post('cid', 0); + $result = UploadService::file($cid); + return $this->success('上传成功', $result); + } catch (Exception $e) { + return $this->fail($e->getMessage()); + } + } } \ No newline at end of file diff --git a/app/common/service/UploadService.php b/app/common/service/UploadService.php index 32e2bf4..e403a75 100644 --- a/app/common/service/UploadService.php +++ b/app/common/service/UploadService.php @@ -160,5 +160,62 @@ class UploadService throw new Exception($e->getMessage()); } } + + public static function file($cid, int $sourceId = 0, int $source = FileEnum::SOURCE_ADMIN, string $saveDir = 'uploads/file') + { + try { + $config = [ + 'default' => ConfigService::get('storage', 'default', 'local'), + 'engine' => ConfigService::get('storage') ?? ['local'=>[]], + ]; + + // 2、执行文件上传 + $StorageDriver = new StorageDriver($config); + $StorageDriver->setUploadFile('file'); + $fileName = $StorageDriver->getFileName(); + $fileInfo = $StorageDriver->getFileInfo(); + // 校验上传文件后缀 + if (!in_array(strtolower($fileInfo['ext']), config('project.file_file'))) { + throw new Exception("上传文件不允许上传". $fileInfo['ext'] . "文件"); + } + + // 上传文件 + $saveDir = $saveDir . '/' . date('Ymd'); + if (!$StorageDriver->upload($saveDir)) { + throw new Exception($StorageDriver->getError()); + } + + // 3、处理文件名称 + if (strlen($fileInfo['name']) > 128) { + $name = substr($fileInfo['name'], 0, 123); + $nameEnd = substr($fileInfo['name'], strlen($fileInfo['name'])-5, strlen($fileInfo['name'])); + $fileInfo['name'] = $name . $nameEnd; + } + + // 4、写入数据库中 + $file = File::create([ + 'cid' => $cid, + 'type' => FileEnum::VIDEO_TYPE, + 'name' => $fileInfo['name'], + 'uri' => $saveDir . '/' . str_replace("\\","/", $fileName), + 'source' => $source, + 'source_id' => $sourceId, + 'create_time' => time(), + ]); + + // 5、返回结果 + return [ + 'id' => $file['id'], + 'cid' => $file['cid'], + 'type' => $file['type'], + 'name' => $file['name'], + 'uri' => FileService::getFileUrl($file['uri']), + 'url' => $file['uri'] + ]; + + } catch (Exception $e) { + throw new Exception($e->getMessage()); + } + } } \ No newline at end of file diff --git a/app/common/service/storage/engine/Server.php b/app/common/service/storage/engine/Server.php index 07bf0d5..77f6e72 100644 --- a/app/common/service/storage/engine/Server.php +++ b/app/common/service/storage/engine/Server.php @@ -42,7 +42,7 @@ abstract class Server } // 校验上传文件后缀 - $limit = array_merge(config('project.file_image'), config('project.file_video')); + $limit = array_merge(config('project.file_image'), config('project.file_video'),config('project.file_file')); if (!in_array(strtolower($this->file->extension()), $limit)) { throw new Exception('不允许上传' . $this->file->extension() . '后缀文件'); } diff --git a/config/project.php b/config/project.php index 3ba585d..b34e9c1 100644 --- a/config/project.php +++ b/config/project.php @@ -73,6 +73,11 @@ return [ 'file_video' => [ 'wmv', 'avi', 'mpg', 'mpeg', '3gp', 'mov', 'mp4', 'flv', 'f4v', 'rmvb', 'mkv' ], + + // 文件上传限制 (文件) + 'file_file' => [ + 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf', 'txt','apk','ipa','wgt' + ], // 登录设置 'login' => [ -- 2.40.1