diff --git a/app/adminapi/controller/UploadController.php b/app/adminapi/controller/UploadController.php index 1bee7dd30..f8e0a4cd1 100755 --- a/app/adminapi/controller/UploadController.php +++ b/app/adminapi/controller/UploadController.php @@ -60,4 +60,18 @@ class UploadController extends BaseAdminController } } -} \ No newline at end of file + /** + * @notes 上传文件 + * @return Json + */ + 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()); + } + } +} diff --git a/app/common/service/UploadService.php b/app/common/service/UploadService.php index 32e2bf40b..ad4a32a3c 100755 --- a/app/common/service/UploadService.php +++ b/app/common/service/UploadService.php @@ -39,7 +39,7 @@ class UploadService try { $config = [ 'default' => ConfigService::get('storage', 'default', 'local'), - 'engine' => ConfigService::get('storage') ?? ['local'=>[]], + 'engine' => ConfigService::get('storage') ?? ['local' => []], ]; // 2、执行文件上传 @@ -50,7 +50,7 @@ class UploadService // 校验上传文件后缀 if (!in_array(strtolower($fileInfo['ext']), config('project.file_image'))) { - throw new Exception("上传图片不允许上传". $fileInfo['ext'] . "文件"); + throw new Exception("上传图片不允许上传" . $fileInfo['ext'] . "文件"); } // 上传文件 @@ -62,7 +62,7 @@ class UploadService // 3、处理文件名称 if (strlen($fileInfo['name']) > 128) { $name = substr($fileInfo['name'], 0, 123); - $nameEnd = substr($fileInfo['name'], strlen($fileInfo['name'])-5, strlen($fileInfo['name'])); + $nameEnd = substr($fileInfo['name'], strlen($fileInfo['name']) - 5, strlen($fileInfo['name'])); $fileInfo['name'] = $name . $nameEnd; } @@ -71,7 +71,7 @@ class UploadService 'cid' => $cid, 'type' => FileEnum::IMAGE_TYPE, 'name' => $fileInfo['name'], - 'uri' => $saveDir . '/' . str_replace("\\","/", $fileName), + 'uri' => $saveDir . '/' . str_replace("\\", "/", $fileName), 'source' => $source, 'source_id' => $sourceId, 'create_time' => time(), @@ -86,7 +86,6 @@ class UploadService 'uri' => FileService::getFileUrl($file['uri']), 'url' => $file['uri'] ]; - } catch (Exception $e) { throw new Exception($e->getMessage()); } @@ -108,7 +107,7 @@ class UploadService try { $config = [ 'default' => ConfigService::get('storage', 'default', 'local'), - 'engine' => ConfigService::get('storage') ?? ['local'=>[]], + 'engine' => ConfigService::get('storage') ?? ['local' => []], ]; // 2、执行文件上传 @@ -119,7 +118,7 @@ class UploadService // 校验上传文件后缀 if (!in_array(strtolower($fileInfo['ext']), config('project.file_video'))) { - throw new Exception("上传视频不允许上传". $fileInfo['ext'] . "文件"); + throw new Exception("上传视频不允许上传" . $fileInfo['ext'] . "文件"); } // 上传文件 @@ -131,7 +130,7 @@ class UploadService // 3、处理文件名称 if (strlen($fileInfo['name']) > 128) { $name = substr($fileInfo['name'], 0, 123); - $nameEnd = substr($fileInfo['name'], strlen($fileInfo['name'])-5, strlen($fileInfo['name'])); + $nameEnd = substr($fileInfo['name'], strlen($fileInfo['name']) - 5, strlen($fileInfo['name'])); $fileInfo['name'] = $name . $nameEnd; } @@ -140,7 +139,7 @@ class UploadService 'cid' => $cid, 'type' => FileEnum::VIDEO_TYPE, 'name' => $fileInfo['name'], - 'uri' => $saveDir . '/' . str_replace("\\","/", $fileName), + 'uri' => $saveDir . '/' . str_replace("\\", "/", $fileName), 'source' => $source, 'source_id' => $sourceId, 'create_time' => time(), @@ -155,10 +154,74 @@ class UploadService 'uri' => FileService::getFileUrl($file['uri']), 'url' => $file['uri'] ]; - } catch (Exception $e) { throw new Exception($e->getMessage()); } } -} \ No newline at end of file + /** + * @notes 上传文件 + * @param $cid + * @param int $user_id + * @param string $saveDir + * @return array + * @throws Exception + * @author 段誉 + * @date 2021/12/29 16:30 + */ + public static function file($cid, int $sourceId = 0, int $source = FileEnum::SOURCE_ADMIN, string $saveDir = 'uploads/files') + { + 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::FILE_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()); + } + } +} diff --git a/config/project.php b/config/project.php index 3ba585da1..38e960821 100755 --- a/config/project.php +++ b/config/project.php @@ -32,19 +32,19 @@ return [ // 后台管理员token(登录令牌)配置 'admin_token' => [ - 'expire_duration' => 3600 * 8,//管理后台token过期时长(单位秒) - 'be_expire_duration' => 3600,//管理后台token临时过期前时长,自动续期 + 'expire_duration' => 3600 * 8, //管理后台token过期时长(单位秒) + 'be_expire_duration' => 3600, //管理后台token临时过期前时长,自动续期 ], // 商城用户token(登录令牌)配置 'user_token' => [ - 'expire_duration' => 3600 * 8,//用户token过期时长(单位秒) - 'be_expire_duration' => 3600,//用户token临时过期前时长,自动续期 + 'expire_duration' => 3600 * 8, //用户token过期时长(单位秒) + 'be_expire_duration' => 3600, //用户token临时过期前时长,自动续期 ], // 列表页 'lists' => [ - 'page_size_max' => 25000,//列表页查询数量限制(列表页每页数量、导出每页数量) + 'page_size_max' => 25000, //列表页查询数量限制(列表页每页数量、导出每页数量) 'page_size' => 25, //默认每页数量 ], @@ -54,14 +54,14 @@ return [ 'user_avatar' => 'resource/image/adminapi/default/default_avatar.png', 'qq_group' => 'resource/image/adminapi/default/qq_group.png', // qq群 'customer_service' => 'resource/image/adminapi/default/customer_service.jpg', // 客服 - 'menu_admin' => 'resource/image/adminapi/default/menu_admin.png',// 首页快捷菜单-管理员 + 'menu_admin' => 'resource/image/adminapi/default/menu_admin.png', // 首页快捷菜单-管理员 'menu_role' => 'resource/image/adminapi/default/menu_role.png', // 首页快捷菜单-角色 - 'menu_dept' => 'resource/image/adminapi/default/menu_dept.png',// 首页快捷菜单-部门 - 'menu_dict' => 'resource/image/adminapi/default/menu_dict.png',// 首页快捷菜单-字典 - 'menu_generator' => 'resource/image/adminapi/default/menu_generator.png',// 首页快捷菜单-代码生成器 - 'menu_auth' => 'resource/image/adminapi/default/menu_auth.png',// 首页快捷菜单-菜单权限 - 'menu_web' => 'resource/image/adminapi/default/menu_web.png',// 首页快捷菜单-网站信息 - 'menu_file' => 'resource/image/adminapi/default/menu_file.png',// 首页快捷菜单-素材中心 + 'menu_dept' => 'resource/image/adminapi/default/menu_dept.png', // 首页快捷菜单-部门 + 'menu_dict' => 'resource/image/adminapi/default/menu_dict.png', // 首页快捷菜单-字典 + 'menu_generator' => 'resource/image/adminapi/default/menu_generator.png', // 首页快捷菜单-代码生成器 + 'menu_auth' => 'resource/image/adminapi/default/menu_auth.png', // 首页快捷菜单-菜单权限 + 'menu_web' => 'resource/image/adminapi/default/menu_web.png', // 首页快捷菜单-网站信息 + 'menu_file' => 'resource/image/adminapi/default/menu_file.png', // 首页快捷菜单-素材中心 ], // 文件上传限制 (图片) @@ -73,7 +73,10 @@ return [ 'file_video' => [ 'wmv', 'avi', 'mpg', 'mpeg', '3gp', 'mov', 'mp4', 'flv', 'f4v', 'rmvb', 'mkv' ], - + // 文件上传限制 (文件) + 'file_file' => [ + 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf', 'txt', 'zip','rar','tar' + ], // 登录设置 'login' => [ // 登录方式:1-账号密码登录;2-手机短信验证码登录