Преглед изворни кода

新增:后台上传视频文件

Yahoo пре 4 година
родитељ
комит
7c6d052693

+ 1 - 2
app/api/controller/Upload.php

@@ -61,8 +61,7 @@ class Upload extends Controller
         // 设置上传文件的信息
         $storage->setUploadFile('file')
             ->setRootDirName((string)$this->getStoreId())
-            ->setValidationScene('image')
-            ->upload();
+            ->setValidationScene('image');
         // 执行文件上传
         if (!$storage->upload()) {
             return $this->renderError('图片上传失败:' . $storage->getError());

+ 16 - 5
app/common/library/storage/FileValidate.php

@@ -8,28 +8,39 @@
 // +----------------------------------------------------------------------
 // | Author: 萤火科技 <admin@yiovo.com>
 // +----------------------------------------------------------------------
-declare (strict_types = 1);
+declare (strict_types=1);
 
 namespace app\common\library\storage;
 
+/**
+ * 文件上传验证类
+ * Class FileValidate
+ * @package app\common\library\storage
+ */
 class FileValidate extends \think\Validate
 {
     // 验证规则
     protected $rule = [
+        // 图片文件: jpg,jpeg,png,bmp,gif
         // 文件大小: 2MB = (1024 * 1024 * 2) = 2097152 字节
-        // 文件扩展名: jpg,jpeg,png,bmp,gif
         'image' => 'filesize:2097152|fileExt:jpg,jpeg,png,bmp,gif',
+
+        // 视频文件: mp4
+        // 文件大小: 10MB = (1024 * 1024 * 10) = 10485760 字节
+        'video' => 'filesize:10485760|fileExt:mp4',
     ];
 
     // 错误提示信息
     protected $message = [
-        'image.filesize' => '文件大小不能超出2MB',
-        'image.fileExt' => '文件扩展名有误',
+        'image.filesize' => '图片文件大小不能超出2MB',
+        'image.fileExt' => '图片文件扩展名有误',
+        'video.filesize' => '视频文件大小不能超出10MB',
+        'video.fileExt' => '视频文件扩展名有误',
     ];
 
     // 验证场景
     protected $scene = [
         'image' => ['image'],
+        'video' => ['video'],
     ];
-
 }

+ 1 - 1
app/common/library/storage/engine/Basics.php

@@ -84,7 +84,7 @@ abstract class Basics
     private function throwFileError(Exception $e)
     {
         $maxSize = ini_get('upload_max_filesize');
-        $myMsg = $e->getCode() === 1 ? "上传的文件超出了服务器最大限制: {$maxSize}" : false;
+        $myMsg = $e->getCode() === 1 ? "上传的文件超出了服务器最大限制: {$maxSize};可修改php.ini文件中upload_max_filesize项调整" : false;
         throwError($myMsg ?: $e->getMessage());
     }
 

+ 10 - 10
app/store/controller/Files.php

@@ -8,10 +8,11 @@
 // +----------------------------------------------------------------------
 // | Author: 萤火科技 <admin@yiovo.com>
 // +----------------------------------------------------------------------
-declare (strict_types = 1);
+declare (strict_types=1);
 
 namespace app\store\controller;
 
+use think\response\Json;
 use app\store\model\UploadFile as UploadFileModel;
 
 /**
@@ -23,10 +24,10 @@ class Files extends Controller
 {
     /**
      * 文件列表
-     * @return array
+     * @return Json
      * @throws \think\db\exception\DbException
      */
-    public function list()
+    public function list(): Json
     {
         $model = new UploadFileModel;
         $list = $model->getList($this->request->param());
@@ -36,9 +37,9 @@ class Files extends Controller
     /**
      * 编辑文件
      * @param int $fileId
-     * @return array
+     * @return Json
      */
-    public function edit(int $fileId)
+    public function edit(int $fileId): Json
     {
         // 文件详情
         $model = UploadFileModel::detail($fileId);
@@ -52,13 +53,13 @@ class Files extends Controller
     /**
      * 删除文件(批量)
      * @param array $fileIds 文件id集
-     * @return array
+     * @return Json
      * @throws \think\Exception
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    public function delete(array $fileIds)
+    public function delete(array $fileIds): Json
     {
         $model = new UploadFileModel;
         if (!$model->setDelete($fileIds)) {
@@ -71,9 +72,9 @@ class Files extends Controller
      * 移动文件到指定分组(批量)
      * @param int $groupId
      * @param array $fileIds
-     * @return array
+     * @return Json
      */
-    public function moveGroup(int $groupId, array $fileIds)
+    public function moveGroup(int $groupId, array $fileIds): Json
     {
         $model = new UploadFileModel;
         if (!$model->moveGroup($groupId, $fileIds)) {
@@ -81,5 +82,4 @@ class Files extends Controller
         }
         return $this->renderSuccess('操作成功');
     }
-
 }

+ 31 - 5
app/store/controller/Upload.php

@@ -17,6 +17,7 @@ use app\store\model\UploadFile as UploadFileModel;
 use app\common\enum\Setting as SettingEnum;
 use app\common\enum\file\FileType as FileTypeEnum;
 use app\common\library\storage\Driver as StorageDriver;
+use think\response\Json;
 
 /**
  * 文件库管理
@@ -45,18 +46,17 @@ class Upload extends Controller
     /**
      * 图片上传接口
      * @param int $groupId 分组ID
-     * @return array
+     * @return Json
      * @throws \think\Exception
      */
-    public function image(int $groupId = 0)
+    public function image(int $groupId = 0): Json
     {
         // 实例化存储驱动
         $storage = new StorageDriver($this->config);
         // 设置上传文件的信息
         $storage->setUploadFile('iFile')
-            ->setRootDirName((string)$this->getStoreId())
-            ->setValidationScene('image')
-            ->upload();
+            ->setRootDirName((string)$this->storeId)
+            ->setValidationScene('image');
         // 执行文件上传
         if (!$storage->upload()) {
             return $this->renderError('图片上传失败:' . $storage->getError());
@@ -70,4 +70,30 @@ class Upload extends Controller
         return $this->renderSuccess(['fileInfo' => $model->toArray()], '图片上传成功');
     }
 
+    /**
+     * 视频上传接口
+     * @param int $groupId 分组ID
+     * @return Json
+     * @throws \think\Exception
+     */
+    public function video(int $groupId = 0): Json
+    {
+        // 实例化存储驱动
+        $storage = new StorageDriver($this->config);
+        // 设置上传文件的信息
+        $storage->setUploadFile('iFile')
+            ->setRootDirName((string)$this->storeId)
+            ->setValidationScene('video');
+        // 执行文件上传
+        if (!$storage->upload()) {
+            return $this->renderError('视频上传失败:' . $storage->getError());
+        }
+        // 文件信息
+        $fileInfo = $storage->getSaveFileInfo();
+        // 添加文件库记录
+        $model = new UploadFileModel;
+        $model->add($fileInfo, FileTypeEnum::VIDEO, $groupId);
+        // 图片上传成功
+        return $this->renderSuccess(['fileInfo' => $model->toArray()], '视频上传成功');
+    }
 }

+ 8 - 19
app/store/model/UploadFile.php

@@ -31,7 +31,7 @@ class UploadFile extends UploadFileModel
      * @return \think\Paginator
      * @throws \think\db\exception\DbException
      */
-    public function getList(array $param = [])
+    public function getList(array $param = []): \think\Paginator
     {
         // 商品列表获取条件
         $params = $this->setQueryDefaultValue($param, [
@@ -65,9 +65,9 @@ class UploadFile extends UploadFileModel
     /**
      * 移入|移出回收站
      * @param bool $isRecycle
-     * @return false|int
+     * @return bool|false
      */
-    public function setRecycle(bool $isRecycle = true)
+    public function setRecycle(bool $isRecycle = true): bool
     {
         return $this->save(['is_recycle' => (int)$isRecycle]);
     }
@@ -81,7 +81,7 @@ class UploadFile extends UploadFileModel
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    public function setDelete(array $fileIds)
+    public function setDelete(array $fileIds): bool
     {
         // 验证文件数量
         if (count($fileIds) > 15) {
@@ -106,23 +106,13 @@ class UploadFile extends UploadFileModel
         return true;
     }
 
-//    /**
-//     * 批量软删除
-//     * @param $fileIds
-//     * @return $this
-//     */
-//    public function softDelete($fileIds)
-//    {
-//        return $this->where('file_id', 'in', $fileIds)->update(['is_recycle' => 1]);
-//    }
-
     /**
      * 批量移动文件分组
      * @param int $groupId
      * @param array $fileIds
      * @return $this
      */
-    public function moveGroup(int $groupId, array $fileIds)
+    public function moveGroup(int $groupId, array $fileIds): UploadFile
     {
         return $this->where('file_id', 'in', $fileIds)->update(['group_id' => $groupId]);
     }
@@ -132,9 +122,9 @@ class UploadFile extends UploadFileModel
      * @param array $data
      * @param int $fileType
      * @param int $groupId
-     * @return false|int|mixed
+     * @return bool|false
      */
-    public function add(array $data, int $fileType, int $groupId = 0)
+    public function add(array $data, int $fileType, int $groupId = 0): bool
     {
         return $this->save([
             'group_id' => $groupId > 0 ? (int)$groupId : 0,
@@ -155,9 +145,8 @@ class UploadFile extends UploadFileModel
      * @param array $data
      * @return bool
      */
-    public function edit(array $data)
+    public function edit(array $data): bool
     {
         return $this->allowField(['file_name', 'group_id'])->save($data) !== false;
     }
-
 }