增加上传接口

This commit is contained in:
wisp-x 2019-10-31 14:06:33 +08:00
parent b082e1bc7b
commit 119f7851de
3 changed files with 55 additions and 42 deletions

View File

@ -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');

View File

@ -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()

View File

@ -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();