catchAdmin/catch/system/controller/Attachments.php

57 lines
1.7 KiB
PHP
Raw Normal View History

2020-04-29 17:37:45 +08:00
<?php
namespace catchAdmin\system\controller;
use catcher\base\CatchController;
use catcher\CatchResponse;
use catchAdmin\system\model\Attachments as AttachmentsModel;
use catcher\Utils;
2020-09-03 18:55:25 +08:00
use catcher\facade\FileSystem;
2020-04-29 17:37:45 +08:00
class Attachments extends CatchController
{
2020-07-25 10:04:13 +08:00
/**
* 列表
*
* @time 2020年07月25日
* @param AttachmentsModel $model
* @return \think\response\Json
*/
2020-04-29 17:37:45 +08:00
public function index(AttachmentsModel $model)
{
return CatchResponse::paginate($model->getList());
}
2020-07-25 10:04:13 +08:00
/**
* 删除
*
* @time 2020年07月25日
* @param $id
* @param AttachmentsModel $model
* @throws \League\Flysystem\FileNotFoundException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return \think\response\Json
*/
2020-04-29 17:37:45 +08:00
public function delete($id, AttachmentsModel $model)
{
2020-07-25 10:04:13 +08:00
$attachments = $model->whereIn('id', Utils::stringToArrayBy($id))->select();
2020-04-29 17:37:45 +08:00
2020-07-25 10:04:13 +08:00
if ($model->deleteBy($id)) {
foreach ($attachments as $attachment) {
2020-09-03 18:55:25 +08:00
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);
}
2020-04-29 17:37:45 +08:00
}
}
return CatchResponse::success();
}
}