fixed:修复附件无法删除

This commit is contained in:
JaguarJack
2021-03-01 21:01:37 +08:00
parent 17491ca7da
commit c3b36013d7
5 changed files with 99 additions and 88 deletions

View File

@@ -4,8 +4,6 @@ namespace catchAdmin\system\controller;
use catcher\base\CatchController;
use catcher\CatchResponse;
use catchAdmin\system\model\Attachments as AttachmentsModel;
use catcher\Utils;
use catcher\facade\FileSystem;
class Attachments extends CatchController
{
@@ -35,22 +33,6 @@ class Attachments extends CatchController
*/
public function delete($id, AttachmentsModel $model)
{
$attachments = $model->whereIn('id', Utils::stringToArrayBy($id))->select();
if ($model->deleteBy($id)) {
foreach ($attachments as $attachment) {
if ($attachment->driver == 'local') {
$localPath = config('filesystem.disks.local.root') . DIRECTORY_SEPARATOR;
$path = $localPath . str_replace('\\','\/', $attachment->path);
if (FileSystem::exists($path)) {
Filesystem::delete($path);
}
} else {
Filesystem::delete($attachment->path);
}
}
}
return CatchResponse::success();
return CatchResponse::success($model->deletes($id));
}
}

View File

@@ -3,8 +3,10 @@ namespace catchAdmin\system\model;
use catchAdmin\system\model\search\AttachmentsSearch;
use catcher\base\CatchModel;
use catcher\Utils;
use think\file\UploadedFile;
use think\Model;
use think\facade\Filesystem;
class Attachments extends CatchModel
{
@@ -54,4 +56,37 @@ class Attachments extends CatchModel
'path' => $data['path']
]);
}
/**
* 批量删除
*
* @time 2021年03月01日
* @param $id
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return bool
*/
public function deletes($id): bool
{
Utils::setFilesystemConfig();
$this->whereIn('id', Utils::stringToArrayBy($id))
->select()
->each(function ($attachment){
if ($attachment->delete()) {
if ($attachment->driver == 'local') {
$localPath = config('filesystem.disks.local.root') . DIRECTORY_SEPARATOR;
$path = $localPath . str_replace('\\','\/', $attachment->path);
if (file_exists($path)) {
Filesystem::delete($path);
}
} else {
Filesystem::delete($attachment->path);
}
}
});
return true;
}
}

View File

@@ -24,6 +24,6 @@ trait DeveloperSearch
public function searchStatusAttr($query, $value, $data)
{
return $query->where('driver', $value);
return $query->where('status', $value);
}
}