|
@@ -25,14 +25,11 @@ use think\file\UploadedFile;
|
|
|
use app\common\service\FileService;
|
|
use app\common\service\FileService;
|
|
|
use app\model\FileManager as FileManagerModel;
|
|
use app\model\FileManager as FileManagerModel;
|
|
|
use app\facade\FileFacade;
|
|
use app\facade\FileFacade;
|
|
|
|
|
+use app\utils\ReUtils;
|
|
|
|
|
|
|
|
class FileManager extends Base
|
|
class FileManager extends Base
|
|
|
{
|
|
{
|
|
|
protected $modelName = 'FileManager';
|
|
protected $modelName = 'FileManager';
|
|
|
- protected $t_suffix = '_thumb';
|
|
|
|
|
- protected $width = 400; // 缩略图高度
|
|
|
|
|
- protected $height = 300;
|
|
|
|
|
- protected $storage_path = 'public/storage';
|
|
|
|
|
|
|
|
|
|
public function index()
|
|
public function index()
|
|
|
{
|
|
{
|
|
@@ -65,35 +62,78 @@ class FileManager extends Base
|
|
|
)->check(['file' => $file]);
|
|
)->check(['file' => $file]);
|
|
|
|
|
|
|
|
$savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
|
|
$savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
|
|
|
-
|
|
|
|
|
- $savename = str_replace('\\', '/', $savename);
|
|
|
|
|
- $infoid = (int) $this->request->param('infoid');
|
|
|
|
|
- $cjid = (int) $this->request->param('cjid');
|
|
|
|
|
- $username = $this->getSysUser()->username;
|
|
|
|
|
|
|
|
|
|
- FileManagerModel::saveFileInfo($file, $savename, $file->getOriginalName(), $infoid, $cjid, $username);
|
|
|
|
|
|
|
+ $urlpath = Config::get('filesystem.disks.public.url');
|
|
|
|
|
+
|
|
|
|
|
+ $pictureurl = str_replace("\\", "/", $urlpath . '/' . $savename);
|
|
|
|
|
|
|
|
return json([
|
|
return json([
|
|
|
'uploaded' => 1,
|
|
'uploaded' => 1,
|
|
|
'fileName' => basename($savename),
|
|
'fileName' => basename($savename),
|
|
|
- 'url' => Config::get('filesystem.disks.public.url') . '/' . $savename
|
|
|
|
|
|
|
+ 'url' => $pictureurl
|
|
|
]);
|
|
]);
|
|
|
} catch (\think\exception\ValidateException $e) {
|
|
} catch (\think\exception\ValidateException $e) {
|
|
|
$this->error($e->getMessage());
|
|
$this->error($e->getMessage());
|
|
|
return json([
|
|
return json([
|
|
|
- 'uploaded' => 1,
|
|
|
|
|
|
|
+ 'uploaded' => 0,
|
|
|
'error' => ['message' => $e->getMessage()]
|
|
'error' => ['message' => $e->getMessage()]
|
|
|
]);
|
|
]);
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
return json([
|
|
return json([
|
|
|
- 'uploaded' => 1,
|
|
|
|
|
|
|
+ 'uploaded' => 0,
|
|
|
'error' => ['message' => '图片不能为空']
|
|
'error' => ['message' => '图片不能为空']
|
|
|
]);
|
|
]);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 本地图片上传
|
|
|
|
|
+ * @param file upload_file 上传的文件
|
|
|
|
|
+ */
|
|
|
|
|
+ public function uploadImage()
|
|
|
|
|
+ {
|
|
|
|
|
+ if ($this->request->isPost()) {
|
|
|
|
|
+ $file = $this->request->file('upload_file');
|
|
|
|
|
+
|
|
|
|
|
+ if ($file) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ validate(
|
|
|
|
|
+ [
|
|
|
|
|
+ 'file' => [
|
|
|
|
|
+ // 限制文件大小(单位b),这里限制为4M
|
|
|
|
|
+ 'fileSize' => 4 * 1024 * 1024,
|
|
|
|
|
+ // 限制文件后缀,多个后缀以英文逗号分割
|
|
|
|
|
+ 'fileExt' => 'jpg,png,gif,jpeg,webp,jfif'
|
|
|
|
|
+ ]
|
|
|
|
|
+ ],
|
|
|
|
|
+ [
|
|
|
|
|
+ 'file.fileSize' => '文件太大',
|
|
|
|
|
+ 'file.fileExt' => '不支持的文件后缀',
|
|
|
|
|
+ ]
|
|
|
|
|
+ )->check(['file' => $file]);
|
|
|
|
|
+
|
|
|
|
|
+ $savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
|
|
|
|
|
+
|
|
|
|
|
+ $urlpath = Config::get('filesystem.disks.public.url');
|
|
|
|
|
+
|
|
|
|
|
+ $pictureurl = str_replace("\\", "/", $urlpath . '/' . $savename);
|
|
|
|
|
+
|
|
|
|
|
+ return ReUtils::result([
|
|
|
|
|
+ 'filename' => $pictureurl
|
|
|
|
|
+ ]);
|
|
|
|
|
+ } catch (ValidateException $e) {
|
|
|
|
|
+ return ReUtils::error($e->getMessage());
|
|
|
|
|
+ } catch (Exception $e) {
|
|
|
|
|
+ return ReUtils::error($e->getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return ReUtils::error('图片不能为空');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 文件上传
|
|
* 文件上传
|
|
|
*/
|
|
*/
|
|
@@ -131,22 +171,9 @@ class FileManager extends Base
|
|
|
$url = Config::get('filesystem.disks.public.url');
|
|
$url = Config::get('filesystem.disks.public.url');
|
|
|
|
|
|
|
|
foreach ($uploadFiles as $file) {
|
|
foreach ($uploadFiles as $file) {
|
|
|
- $fileinfo = new FileManagerModel();
|
|
|
|
|
-
|
|
|
|
|
- $fileinfo->title = $file->getOriginalName();
|
|
|
|
|
- $fileinfo->filesize = $file->getSize();
|
|
|
|
|
- $fileinfo->filetime = $file->getCTime();
|
|
|
|
|
- $fileinfo->fileextension = $file->extension();
|
|
|
|
|
- $fileinfo->username = $this->getSysUser()->username;
|
|
|
|
|
- $fileinfo->hash_md5 = $file->md5();
|
|
|
|
|
-
|
|
|
|
|
$savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
|
|
$savename = \think\facade\Filesystem::disk('public')->putFile('/', $file);
|
|
|
-
|
|
|
|
|
- $fileinfo->filepath = $url . '/' . $savename;
|
|
|
|
|
-
|
|
|
|
|
- $fileinfo->save();
|
|
|
|
|
|
|
|
|
|
- $savenames[] = $fileinfo->filepath;
|
|
|
|
|
|
|
+ $savenames[] = $url . '/' . $savename;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return json(['code' => 0, 'msg'=>'上传成功', 'filename'=>$savenames]);
|
|
return json(['code' => 0, 'msg'=>'上传成功', 'filename'=>$savenames]);
|
|
@@ -158,59 +185,6 @@ class FileManager extends Base
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 本地图片上传
|
|
|
|
|
- * @file upload_file 上传的文件
|
|
|
|
|
- * @param string $img_id 图片ipnut text id 默认值 picture
|
|
|
|
|
- * @param boolean $thumb 是否制作缩略图
|
|
|
|
|
- * @param int $width 缩略图最大宽
|
|
|
|
|
- * @param int $height 缩略图最大高
|
|
|
|
|
- * @param int $id 信息ID
|
|
|
|
|
- * @param int $cjid 信息临时ID
|
|
|
|
|
- * @param bool $saveoriginal 生成缩略图后是否保存原图 默认保存 saveoriginal
|
|
|
|
|
- */
|
|
|
|
|
- public function uploadLocalImg(string $img_id = 'picture', $thumb = false, $width = 400, $height = 300,
|
|
|
|
|
- $original = false, $id = 0, $cjid = 0)
|
|
|
|
|
- {
|
|
|
|
|
- if ($this->request->isPost()) {
|
|
|
|
|
- $file = $this->request->file('upload_file');
|
|
|
|
|
-
|
|
|
|
|
- if ($file) {
|
|
|
|
|
- try {
|
|
|
|
|
- validate(
|
|
|
|
|
- [
|
|
|
|
|
- 'file' => [
|
|
|
|
|
- // 限制文件大小(单位b),这里限制为4M
|
|
|
|
|
- 'fileSize' => 4 * 1024 * 1024,
|
|
|
|
|
- // 限制文件后缀,多个后缀以英文逗号分割
|
|
|
|
|
- 'fileExt' => 'jpg,png,gif,jpeg,webp,jfif'
|
|
|
|
|
- ]
|
|
|
|
|
- ],
|
|
|
|
|
- [
|
|
|
|
|
- 'file.fileSize' => '文件太大',
|
|
|
|
|
- 'file.fileExt' => '不支持的文件后缀',
|
|
|
|
|
- ]
|
|
|
|
|
- )->check(['file' => $file]);
|
|
|
|
|
-
|
|
|
|
|
- $result = $this->dealUploadImg($file, $thumb, $width, $height, $original, $id, $cjid);
|
|
|
|
|
-
|
|
|
|
|
- return json([
|
|
|
|
|
- 'code' => 0,
|
|
|
|
|
- 'img_id' => $img_id,
|
|
|
|
|
- 'picname' => $result['picname'],
|
|
|
|
|
- 'thumb' => $result['thumbname']
|
|
|
|
|
- ]);
|
|
|
|
|
- } catch (ValidateException $e) {
|
|
|
|
|
- $this->error($e->getMessage());
|
|
|
|
|
- } catch (Exception $e) {
|
|
|
|
|
- $this->error($e->getMessage());
|
|
|
|
|
- }
|
|
|
|
|
- } else {
|
|
|
|
|
- $this->error('图片不能为空');
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* 网络图片上传
|
|
* 网络图片上传
|
|
|
* @paran string url_file 网络图片地址
|
|
* @paran string url_file 网络图片地址
|
|
@@ -221,7 +195,7 @@ class FileManager extends Base
|
|
|
* @param int $height 缩略图最大高
|
|
* @param int $height 缩略图最大高
|
|
|
* @param bool $overwrite 生成缩略图后是否保存原图
|
|
* @param bool $overwrite 生成缩略图后是否保存原图
|
|
|
*/
|
|
*/
|
|
|
- public function uploadUrlImg(string $img_id = 'picture', $thumb = false, $width = 400, $height = 300,
|
|
|
|
|
|
|
+ public function uploadUrlImage(string $img_id = 'picture', $thumb = false, $width = 400, $height = 300,
|
|
|
$original = false, $id = 0, $cjid = 0)
|
|
$original = false, $id = 0, $cjid = 0)
|
|
|
{
|
|
{
|
|
|
if ($this->request->isPost()) {
|
|
if ($this->request->isPost()) {
|
|
@@ -230,8 +204,23 @@ class FileManager extends Base
|
|
|
if ($urlImg) {
|
|
if ($urlImg) {
|
|
|
try {
|
|
try {
|
|
|
$file = FileFacade::downloadUrlImg($urlImg);
|
|
$file = FileFacade::downloadUrlImg($urlImg);
|
|
|
-
|
|
|
|
|
- $result = $this->dealUploadImg($file, $thumb, $width, $height, $original, $id, $cjid);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ $urlpath = Config::get('filesystem.disks.public.url');
|
|
|
|
|
+
|
|
|
|
|
+ $username = $this->getSysUser()->username;
|
|
|
|
|
+
|
|
|
|
|
+ $savename = str_replace('\\', '/', $file->hashName());
|
|
|
|
|
+
|
|
|
|
|
+ $originalName = $file->getOriginalName();
|
|
|
|
|
+
|
|
|
|
|
+ $thumbname = "";
|
|
|
|
|
+
|
|
|
|
|
+ \think\facade\Filesystem::disk('public')->putFileAs('/', $file, $savename);
|
|
|
|
|
+
|
|
|
|
|
+ $result = [
|
|
|
|
|
+ 'picname' => $savename,
|
|
|
|
|
+ 'thumbname' => $thumbname,
|
|
|
|
|
+ ];
|
|
|
|
|
|
|
|
// 删除临时文件
|
|
// 删除临时文件
|
|
|
@unlink($file->getRealPath());
|
|
@unlink($file->getRealPath());
|
|
@@ -250,55 +239,4 @@ class FileManager extends Base
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 处理上传的图片
|
|
|
|
|
- */
|
|
|
|
|
- protected function dealUploadImg(UploadedFile $file, $thumb, $width, $height, $original, $id, $cjid): array
|
|
|
|
|
- {
|
|
|
|
|
- $urlpath = Config::get('filesystem.disks.public.url');
|
|
|
|
|
-
|
|
|
|
|
- $username = $this->getSysUser()->username;
|
|
|
|
|
-
|
|
|
|
|
- $savename = str_replace('\\', '/', $file->hashName());
|
|
|
|
|
-
|
|
|
|
|
- $originalName = $file->getOriginalName();
|
|
|
|
|
-
|
|
|
|
|
- $thumbname = "";
|
|
|
|
|
-
|
|
|
|
|
- if ($thumb == true) {
|
|
|
|
|
- $image = Image::open($file);
|
|
|
|
|
-
|
|
|
|
|
- $image->thumb($width, $height, 1);
|
|
|
|
|
-
|
|
|
|
|
- $ext = $file->extension();
|
|
|
|
|
-
|
|
|
|
|
- if ($original == true) {
|
|
|
|
|
- \think\facade\Filesystem::disk('public')->putFileAs('/', $file, $savename);
|
|
|
|
|
-
|
|
|
|
|
- $thumbname = str_replace('.' . $ext, '', $savename) . $this->t_suffix . '.' . $ext;
|
|
|
|
|
-
|
|
|
|
|
- $savepath = str_replace('\\', '/', Config::get('filesystem.disks.public.root') . '/' . $thumbname);
|
|
|
|
|
-
|
|
|
|
|
- $image->save($savepath);
|
|
|
|
|
-
|
|
|
|
|
- FileManagerModel::saveFileInfo($savepath, $thumbname, $originalName, $id, $cjid, $username);
|
|
|
|
|
-
|
|
|
|
|
- $thumbname = $urlpath . '/' . $thumbname;
|
|
|
|
|
- } else {
|
|
|
|
|
- $image->save(Config::get('filesystem.disks.public.root') . '/' . $savename);
|
|
|
|
|
- }
|
|
|
|
|
- } else {
|
|
|
|
|
- \think\facade\Filesystem::disk('public')->putFileAs('/', $file, $savename);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- $fileinfo = FileManagerModel::saveFileInfo($file, $savename, $originalName, $id, $cjid, $username);
|
|
|
|
|
-
|
|
|
|
|
- return [
|
|
|
|
|
- 'picname' => $fileinfo->filepath,
|
|
|
|
|
- 'thumbname' => $thumbname,
|
|
|
|
|
- ];
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|