diff --git a/application/api/controller/Image.php b/application/api/controller/Image.php index 73efeb65..953f49e8 100644 --- a/application/api/controller/Image.php +++ b/application/api/controller/Image.php @@ -10,6 +10,7 @@ namespace app\api\controller; use app\common\model\Images; +use app\index\controller\User; class Image extends Base { @@ -44,6 +45,19 @@ class Image extends Base $this->response('success', $images); } + public function delete() + { + $user = new User(); + $data = str_replace(',', ',', $this->param('id')); + if (strpos($data, ',') !== false) { + $data = explode(',', $data); + } + if ($user->deleteImages($data)) { + return $this->response('删除成功!'); + } + return $this->response('删除失败!', [], 500); + } + private function parseData($data) { $data['upload_time'] = $data->getData('create_time'); diff --git a/application/index/controller/User.php b/application/index/controller/User.php index d0752e07..b7ca225a 100644 --- a/application/index/controller/User.php +++ b/application/index/controller/User.php @@ -47,55 +47,53 @@ class User extends Base public function deleteImages($deleteId = null) { - if ($this->request->isPost()) { - Db::startTrans(); - try { - $id = $deleteId ? $deleteId : $this->request->post('id'); - $deletes = []; // 需要删除的文件 - if (is_array($id)) { - $images = Images::all($id); - foreach ($images as &$value) { - $deletes[$value->strategy][] = $value->pathname; - $value->delete(); - unset($value); - } - } else { - $image = Images::get($id); - if (!$image) { - throw new Exception('没有找到该图片数据'); - } - $deletes[$image->strategy][] = $image->pathname; - $image->delete(); + Db::startTrans(); + try { + $id = $deleteId ? $deleteId : $this->request->post('id'); + $deletes = []; // 需要删除的文件 + if (is_array($id)) { + $images = Images::all($id); + foreach ($images as &$value) { + $deletes[$value->strategy][] = $value->pathname; + $value->delete(); + unset($value); + } + } else { + $image = Images::get($id); + if (!$image) { + throw new Exception('没有找到该图片数据'); + } + $deletes[$image->strategy][] = $image->pathname; + $image->delete(); + } + // 是否开启软删除(开启了只删除记录,不删除文件) + if (!$this->config['soft_delete']) { + $strategy = []; + // 实例化所有储存策略驱动 + $strategyAll = array_keys(Config::pull('strategy')); + foreach ($strategyAll as $value) { + // 获取储存策略驱动 + $strategy[$value] = $this->getStrategyInstance($value); } - // 是否开启软删除(开启了只删除记录,不删除文件) - if (!$this->config['soft_delete']) { - $strategy = []; - // 实例化所有储存策略驱动 - $strategyAll = array_keys(Config::pull('strategy')); - foreach ($strategyAll as $value) { - // 获取储存策略驱动 - $strategy[$value] = $this->getStrategyInstance($value); - } - foreach ($deletes as $key => $val) { - if (1 === count($val)) { - if (!$strategy[$key]->delete(isset($val[0]) ? $val[0] : null)) { - throw new Exception('删除失败'); - } - } else { - if (!$strategy[$key]->deletes($val)) { - throw new Exception('批量删除失败'); - } + foreach ($deletes as $key => $val) { + if (1 === count($val)) { + if (!$strategy[$key]->delete(isset($val[0]) ? $val[0] : null)) { + throw new Exception('删除失败'); + } + } else { + if (!$strategy[$key]->deletes($val)) { + throw new Exception('批量删除失败'); } } } - Db::commit(); - } catch (Exception $e) { - Db::rollback(); - return $deleteId ? false : $this->error($e->getMessage()); } - return $deleteId ? true : $this->success('删除成功'); + Db::commit(); + } catch (Exception $e) { + Db::rollback(); + return $deleteId ? false : $this->error($e->getMessage()); } + return $deleteId ? true : $this->success('删除成功'); } public function createFolder() diff --git a/route/route.php b/route/route.php index 8a94a2c0..35672629 100644 --- a/route/route.php +++ b/route/route.php @@ -19,6 +19,7 @@ Route::group('api', function () { Route::any('upload', 'api/Upload/index'); Route::any('image', 'api/Image/find'); Route::any('images', 'api/Image/items'); + Route::any('delete', 'api/Image/delete'); }) ->header('Access-Control-Allow-Headers', 'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With, Token') ->allowCrossDomain();