From f1c87201e6e2012784187d2105ffdb8ddee86a3c Mon Sep 17 00:00:00 2001 From: linyaostalker <602604991@qq.com> Date: Fri, 22 Nov 2019 20:30:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=9B=BE=E7=89=87=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E5=92=8C=E5=88=A0=E9=99=A4=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/logic/file/FileManager.php | 13 +++++++---- .../goods/src/controllers/GoodsController.php | 23 +++++++++++++++---- .../goods/src/logic/goods/GoodsManager.php | 13 ++++++----- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/backend/logic/file/FileManager.php b/backend/logic/file/FileManager.php index d164216..579096a 100644 --- a/backend/logic/file/FileManager.php +++ b/backend/logic/file/FileManager.php @@ -34,11 +34,11 @@ class FileManager foreach($array as $key => $value){ foreach ($value as $k => $v) { if($v == $keyword){ - return $key; + return ['status' => true, 'info' => '操作成功', 'type' => $key]; } } } - return false; + return ['status' => false, 'info' => '操作失败']; } /** @@ -94,9 +94,9 @@ class FileManager $new_file->alias = $temFile->alias; $new_file->path = $temFile->path; if($new_file->save()) { - return ['status' => true, 'file_id' => $new_file->id]; + return ['status' => true, 'info' => '操作成功', 'file_id' => $new_file->id]; } else { - return ['status' => false]; + return ['status' => false, 'info' => '操作失败']; } } @@ -111,9 +111,12 @@ class FileManager $file_model = File::findOne($value); if($file_model){ $file_model->is_delete = File::IS_DELETE_YES; - $file_model->save(); + if($file_model->save()){ + return ['status' => false, 'info' => '操作失败']; + } } } } + return ['status' => true, 'info' => '操作成功']; } } \ No newline at end of file diff --git a/vendor/antgoods/goods/src/controllers/GoodsController.php b/vendor/antgoods/goods/src/controllers/GoodsController.php index 9dbc2a7..e82edc5 100644 --- a/vendor/antgoods/goods/src/controllers/GoodsController.php +++ b/vendor/antgoods/goods/src/controllers/GoodsController.php @@ -85,7 +85,11 @@ class GoodsController extends Controller $file_id_str = $model->imageId; $model->save(); $goods_manager = new GoodsManager(); - $goods_manager->saveFile($file_id_str, $model); + $save_file_res = $goods_manager->saveFile(explode($file_id_str), $model); + if($save_file_res['status']){ + $model->image = $save_file_res['first_file_id']; + $model->save(); + } return $this->redirect('index'); } @@ -105,8 +109,17 @@ class GoodsController extends Controller { $model = $this->findModel($id); $model->imageId = $model->image; + $old_id_arr = $model->image; - if ($model->load(Yii::$app->request->post()) && $model->save()) { + if ($model->load(Yii::$app->request->post())) { + $file_id_str = $model->imageId; + $model->save(); + $goods_manager = new GoodsManager(); + $save_file_res = $goods_manager->saveFile(explode($file_id_str), $model, $old_id_arr); + if($save_file_res['status']){ + $model->image = $save_file_res['first_file_id']; + $model->save(); + } return $this->redirect('index'); } @@ -183,9 +196,9 @@ class GoodsController extends Controller $model->user_id = Yii::$app->user->identity->id; $model->name = $file_name; $file_manager = new \backend\logic\file\FileManager(); - $type = $file_manager->searchType(\backend\logic\file\FileManager::$extension, pathinfo($data['path'])['extension']); - if ($type) { - $model->type = $type; + $type_res = $file_manager->searchType(\backend\logic\file\FileManager::$extension, pathinfo($data['path'])['extension']); + if ($type_res['status']) { + $model->type = $type_res['type']; } $model->alias = $data['alias']; $model->path = $data['path']; diff --git a/vendor/antgoods/goods/src/logic/goods/GoodsManager.php b/vendor/antgoods/goods/src/logic/goods/GoodsManager.php index 4f69054..c52e8d3 100644 --- a/vendor/antgoods/goods/src/logic/goods/GoodsManager.php +++ b/vendor/antgoods/goods/src/logic/goods/GoodsManager.php @@ -14,11 +14,11 @@ class GoodsManager * @return bool * 保存新文件,删除不需要的文件操作 */ - public function saveFile($newFileIdArr, $oldFileIdArr = [], $goodsModel, $filetype = 1) + public function saveFile($newFileIdArr, $goodsModel, $oldFileIdArr = [], $filetype = 1) { //判断类名是否存在 if(!class_exists('\backend\logic\file\FileManager') || !class_exists('\common\models\ars\File')) { - return false; + return ['status' => false, 'info' => '操作失败']; } //需要新建的文件id @@ -34,11 +34,12 @@ class GoodsManager //删除文件 $class->deleteFile($del_file_id_arr); - if($create_file_res['status'] || $filetype == \common\models\ars\File::OWN_TYPE_GOODS_INDEX) { - $goodsModel->image = $create_file_res['first_file_id']; - $goodsModel->save(); + //记录第一张图片id + $first_file_id = 0; + if($create_file_res['status']) { + $first_file_id = $create_file_res['first_file_id']; } - return true; + return ['status' => true, 'info' => '操作成功', 'first_file_id' => $first_file_id]; } } \ No newline at end of file