From cb1f7310ea8d898bf4698c67860510f7c159b55c Mon Sep 17 00:00:00 2001 From: linyaostalker <602604991@qq.com> Date: Fri, 22 Nov 2019 19:55:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BF=9D=E5=AD=98=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E6=96=87=E4=BB=B6=E9=80=BB=E8=BE=91=EF=BC=8C=E6=A0=B9?= =?UTF-8?q?=E6=8D=AEarray=5Fdiff=E6=96=B9=E6=B3=95=E5=AF=B9=E6=AF=94?= =?UTF-8?q?=E6=97=A7=E6=95=B0=E6=8D=AE=E6=95=B0=E7=BB=84=E5=92=8C=E6=96=B0?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=95=B0=E7=BB=84,=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=92=8C=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/logic/file/FileManager.php | 27 +++++++++++++++---- .../goods/src/logic/goods/GoodsManager.php | 23 +++++++++++----- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/backend/logic/file/FileManager.php b/backend/logic/file/FileManager.php index d8e054c..d164216 100644 --- a/backend/logic/file/FileManager.php +++ b/backend/logic/file/FileManager.php @@ -29,7 +29,8 @@ class FileManager * @return bool|int|string * 根据文件拓展名在$extension中查找对应的文件类型,若不存在则返回false */ - public function searchType($array, $keyword){ + public function searchType($array, $keyword) + { foreach($array as $key => $value){ foreach ($value as $k => $v) { if($v == $keyword){ @@ -47,16 +48,15 @@ class FileManager * @return array * 根据临时文件id(字符串)将临时文件保存在文件中 */ - public function saveTemFileToFile($temFIleIdStr, $ownId, $ownType) + public function saveTemFileToFile($temFIleIdArr, $ownId, $ownType) { - if(!$temFIleIdStr || !$ownId) { + if(!$temFIleIdArr || !$ownId) { return ['status' => false, 'info' => '参数错误']; } - $img_id_arr = explode(',', $temFIleIdStr); $i = 1; $first_file_id = 0; - foreach ($img_id_arr as $key => $value) { + foreach ($temFIleIdArr as $key => $value) { $tem_file = TemFile::findOne($value); if(!$tem_file) { @@ -99,4 +99,21 @@ class FileManager return ['status' => false]; } } + + /** + * @param $fileIdArr + * 删除file表中的文件 + */ + public function deleteFile($fileIdArr) + { + if($fileIdArr){ + foreach ($fileIdArr as $key => $value) { + $file_model = File::findOne($value); + if($file_model){ + $file_model->is_delete = File::IS_DELETE_YES; + $file_model->save(); + } + } + } + } } \ No newline at end of file diff --git a/vendor/antgoods/goods/src/logic/goods/GoodsManager.php b/vendor/antgoods/goods/src/logic/goods/GoodsManager.php index b1410d8..4f69054 100644 --- a/vendor/antgoods/goods/src/logic/goods/GoodsManager.php +++ b/vendor/antgoods/goods/src/logic/goods/GoodsManager.php @@ -7,24 +7,35 @@ namespace antgoods\goods\logic\goods; class GoodsManager { /** - * @param $fileIdStr + * @param $newFileIdArr + * @param array $oldFileIdArr * @param $goodsModel * @param int $filetype * @return bool - * 判断file逻辑文件FileManager和file模型文件是否存在,若存在,则执行文件保存操作 + * 保存新文件,删除不需要的文件操作 */ - public function saveFile($fileIdStr, $goodsModel, $filetype = 1) + public function saveFile($newFileIdArr, $oldFileIdArr = [], $goodsModel, $filetype = 1) { //判断类名是否存在 if(!class_exists('\backend\logic\file\FileManager') || !class_exists('\common\models\ars\File')) { return false; } + //需要新建的文件id + $create_file_id_arr = array_diff($newFileIdArr, $oldFileIdArr); + + //创建文件 $class = new \backend\logic\file\FileManager(); - $res = $class->saveTemFileToFile($fileIdStr, $goodsModel->id, $filetype); + $create_file_res = $class->saveTemFileToFile($create_file_id_arr, $goodsModel->id, $filetype); + + //需要删除的文件id + $del_file_id_arr = array_diff($oldFileIdArr, $newFileIdArr); + + //删除文件 + $class->deleteFile($del_file_id_arr); - if($res['status'] || $filetype == \common\models\ars\File::OWN_TYPE_GOODS_INDEX) { - $goodsModel->image = $res['first_file_id']; + if($create_file_res['status'] || $filetype == \common\models\ars\File::OWN_TYPE_GOODS_INDEX) { + $goodsModel->image = $create_file_res['first_file_id']; $goodsModel->save(); }