From d0064a23c262625b1b772026b0147658a8b51c3d Mon Sep 17 00:00:00 2001 From: linyaostalker <602604991@qq.com> Date: Thu, 12 Dec 2019 14:20:14 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=E9=87=8D=E6=9E=84=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0ajax=E5=80=9F=E5=8F=A3=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=B9=B6=E4=BC=98=E5=8C=96=EF=BC=8C=E5=AE=8C=E6=88=90=E5=90=8E?= =?UTF-8?q?=E5=8F=B0=E5=95=86=E5=93=81=E5=88=86=E7=B1=BB=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E5=99=A8=E4=BB=A3=E7=A0=81=E9=87=8D=E6=9E=84=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/file/logic/file/FileManager.php | 70 +++++++++++++++++-- .../goods/controllers/CategoryController.php | 64 ++++------------- .../goods/controllers/GoodsController.php | 67 +++--------------- .../controllers/ShopCategoryController.php | 49 ++----------- .../goods/logic/goods/GoodsManager.php | 2 +- 5 files changed, 98 insertions(+), 154 deletions(-) diff --git a/backend/modules/file/logic/file/FileManager.php b/backend/modules/file/logic/file/FileManager.php index a5e1d66..5c3e534 100755 --- a/backend/modules/file/logic/file/FileManager.php +++ b/backend/modules/file/logic/file/FileManager.php @@ -21,7 +21,7 @@ class FileManager const TYPE_WORD = 4;//word文本 const TYPE_TXT = 5;//txt文本 - private $extension = [ + public static $extension = [ self::TYPE_IMAGE => ['jpg', 'png', 'jpeg'], self::TYPE_VIDEO => ['mp4'], self::TYPE_EXCEL => [], @@ -34,9 +34,9 @@ class FileManager * @return int|string * 根据文件拓展名在$extension中查找对应的文件类型,若不存在则返回self::TYPE_NONE */ - public function searchType($keyword) + public static function searchType($keyword) { - foreach($this->extension as $key => $type){ + foreach(self::$extension as $key => $type){ if (in_array($keyword, $type)) { return $key; } @@ -132,7 +132,7 @@ class FileManager * @param $fileIdStrName //表单中临时保存的文件id字符串组合(以,隔开) * @param string $fileNameInModel //需要保存到数据库中的数据表字段名称 * @param $fileOldIdStr //数据库中已经保存的文件id字符串组合(以,隔开) - * @param File|$fileType //File模型中定义的own_type常量 + * @param $fileType //File模型中定义的own_type常量 * @return bool * @throws Exception * 数据模型保存文件操作 @@ -154,4 +154,66 @@ class FileManager } return true; } + + /** + * @param $fileName + * @param $formData + * @return int + * 保存临时文件操作 + */ + public static function saveTemFile($fileName, $formData) + { + $temFileModel = new TemFile(); + $temFileModel->user_id = Yii::$app->user->identity->id; + $temFileModel->name = $fileName; + $temFileModel->type = FileManager::searchType(pathinfo($formData['path'])['extension']); + $temFileModel->alias = $formData['alias']; + $temFileModel->path = $formData['path']; + $temFileModel->save(); + return $temFileModel->id; + } + + /** + * @param $data //ajax接收的数据 + * @return string + * 上传插件点击删除按钮时处理文件id字符串(以,分隔) + */ + public static function dealFileIdStrInDel($data) + { + $imgIdArr = explode(',', $data['imgid']); + if(isset($data['data']['alias'])) { + $temFile = TemFile::findOne(['alias' => $data['data']['alias']]); + if ($temFile) { + $imgIdArr = array_diff($imgIdArr, [$temFile->id]); + } + }else{ + foreach ($data as $key => $value) { + $temFile = File::findOne(['alias' => $value]); + if ($temFile) { + $imgIdArr = array_diff($imgIdArr, [$temFile->id]); + } + } + } + return implode(',', $imgIdArr); + } + + /** + * @param $fileIdStr + * @return false|string + * 加载已有的文件数据 + */ + public static function loadExitFile($fileIdStr) + { + $fileIdArr = explode(',', $fileIdStr); + $files = File::find()->where(['id' => $fileIdArr])->all(); + $fileInfo = array(); + if($files) { + foreach ($files as $key => $file) { + $fileInfo[$key]['name'] = $file->alias; + $fileInfo[$key]['path'] = Yii::$app->request->hostInfo . '/' . $file->path; + $fileInfo[$key]['size'] = filesize($file->path); + } + } + return json_encode($fileInfo); + } } \ No newline at end of file diff --git a/backend/modules/goods/controllers/CategoryController.php b/backend/modules/goods/controllers/CategoryController.php index 0233de9..40dd420 100755 --- a/backend/modules/goods/controllers/CategoryController.php +++ b/backend/modules/goods/controllers/CategoryController.php @@ -2,8 +2,8 @@ namespace backend\modules\goods\controllers; -use backend\modules\goods\logic\goods\GoodsManager; use backend\modules\file\models\ars\File; +use Exception; use Yii; use backend\modules\goods\models\ars\Category; use backend\modules\goods\models\searchs\CategorySearch; @@ -11,6 +11,8 @@ use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; use backend\modules\file\logic\file\FileManager; +use yii\web\Response; +use iron\widget\Excel; /** * CategoryController implements the CRUD actions for Category model. @@ -73,7 +75,7 @@ class CategoryController extends Controller } /** - * @return string|yii\web\Response + * @return string|Response * @throws Exception */ public function actionCreate() @@ -81,16 +83,13 @@ class CategoryController extends Controller $model = new Category(); $model->is_show = Category::IS_SHOW_DISPLAY; $model->sort_order = 0; - if ($model->load(Yii::$app->request->post()) && $model->validate()) { //类目图片上传保存处理 $res = FileManager::saveFileInModel($model, 'iconImageId', '', File::OWN_TYPE_CATEGORY_ICON, 'icon'); - if ($res) { return $this->redirect('index'); } } - return $this->render('create', [ 'model' => $model, ]); @@ -102,6 +101,7 @@ class CategoryController extends Controller * @param integer $id * @return mixed * @throws NotFoundHttpException if the model cannot be found + * @throws Exception */ public function actionUpdate($id) { @@ -109,16 +109,13 @@ class CategoryController extends Controller $model->iconImageId = $model->icon; //记录已保存的类目图片id,用于修改 $icon_image_old_id_arr = $model->icon; - if ($model->load(Yii::$app->request->post()) && $model->validate()) { //类目图片上传保存处理 $res = FileManager::saveFileInModel($model, 'iconImageId', $icon_image_old_id_arr, File::OWN_TYPE_CATEGORY_ICON, 'icon'); - if ($res) { return $this->redirect('index'); } } - return $this->render('update', [ 'model' => $model, ]); @@ -169,7 +166,7 @@ class CategoryController extends Controller } else { $dataProvider = $searchModel->search($params); } - \iron\widget\Excel::export([ + Excel::export([ 'models' => $dataProvider->getModels(), 'format' => 'Xlsx', 'asAttachment' => true, @@ -184,19 +181,12 @@ class CategoryController extends Controller public function actionSaveFile() { $data = Yii::$app->request->get('data'); - $file_name = Yii::$app->request->get('fileName')[0]; + $fileName = Yii::$app->request->get('fileName')[0]; if ($data['status'] == true) { - $model = new \backend\modules\file\models\ars\TemFile(); - $model->user_id = Yii::$app->user->identity->id; - $model->name = $file_name; - $file_manager = new \backend\modules\file\logic\file\FileManager(); - $model->type = $file_manager->searchType(pathinfo($data['path'])['extension']); - $model->alias = $data['alias']; - $model->path = $data['path']; - $model->save(); - return $model->id; + return FileManager::saveTemFile($fileName, $data); } + return false; } /** @@ -205,24 +195,8 @@ class CategoryController extends Controller */ public function actionImgIdDel() { - $img_id = Yii::$app->request->get('imgid'); - $img_id_arr = explode(',', $img_id); - if(isset(Yii::$app->request->get('data')['alias'])) { - $alias = Yii::$app->request->get('data')['alias']; - $tem_file = \backend\modules\file\models\ars\TemFile::findOne(['alias' => $alias]); - if ($tem_file) { - $img_id_arr = array_diff($img_id_arr, [$tem_file->id]); - } - }else{ - foreach (Yii::$app->request->get() as $key => $value) { - $tem_file = \backend\modules\file\models\ars\File::findOne(['alias' => $value]); - if ($tem_file) { - $img_id_arr = array_diff($img_id_arr, [$tem_file->id]); - } - } - } - $img_id_str = implode(',', $img_id_arr); - return $img_id_str; + $data = Yii::$app->request->get(); + return FileManager::dealFileIdStrInDel($data); } /** @@ -231,19 +205,7 @@ class CategoryController extends Controller */ public function actionImageFile() { - $file_id_str = Yii::$app->request->get('fileidstr'); - $file_id_arr = explode(',', $file_id_str); - $data = \backend\modules\file\models\ars\File::find()->where(['id' => $file_id_arr])->all(); - $res = array(); - if($data) { - $i = 0; - foreach ($data as $key => $value) { - $res[$i]['name'] = $value->alias; - $res[$i]['path'] = Yii::$app->request->hostInfo . '/' . $value->path; - $res[$i]['size'] = filesize($value->path); - $i++; - } - } - return json_encode($res); + $fileIdStr = Yii::$app->request->get('fileIdStr'); + return FileManager::loadExitFile($fileIdStr); } } diff --git a/backend/modules/goods/controllers/GoodsController.php b/backend/modules/goods/controllers/GoodsController.php index 5430eee..acb8491 100755 --- a/backend/modules/goods/controllers/GoodsController.php +++ b/backend/modules/goods/controllers/GoodsController.php @@ -2,23 +2,19 @@ namespace backend\modules\goods\controllers; +use backend\modules\file\logic\file\FileManager; use backend\modules\goods\models\ars\FilterAttr; use backend\modules\goods\models\ars\GoodsAttr; -use backend\modules\goods\models\ars\GoodsSku; -use backend\models\ars\OrderGoods; -use backend\modules\file\models\ars\TemFile; use backend\modules\shop\logic\ShopManager; -use MongoDB\Driver\Manager; +use Exception; use Yii; use backend\modules\goods\models\ars\Goods; use backend\modules\goods\models\searchs\GoodsSearch; use yii\web\Controller; -use yii\web\HttpException; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; use backend\modules\goods\logic\goods\GoodsManager; use backend\modules\file\models\ars\File; -use yii\web\Response; use backend\modules\goods\models\ars\Attribute; /** @@ -93,6 +89,7 @@ class GoodsController extends Controller * Creates a new Goods model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed + * @throws Exception */ public function actionCreate() { @@ -108,8 +105,6 @@ class GoodsController extends Controller if ($res) { return $this->redirect('index'); } - } else { - $model->ruleVerify = 1; } return $this->render('create', [ 'model' => $model, @@ -122,6 +117,7 @@ class GoodsController extends Controller * @param integer $id * @return mixed * @throws NotFoundHttpException if the model cannot be found + * @throws Exception */ public function actionUpdate($id) { @@ -215,18 +211,10 @@ class GoodsController extends Controller public function actionSaveFile() { $data = Yii::$app->request->get('data'); - $file_name = Yii::$app->request->get('fileName')[0]; + $fileName = Yii::$app->request->get('fileName')[0]; if ($data['status'] == true) { - $model = new \backend\modules\file\models\ars\TemFile(); - $model->user_id = Yii::$app->user->identity->id; - $model->name = $file_name; - $file_manager = new \backend\modules\file\logic\file\FileManager(); - $model->type = $file_manager->searchType(pathinfo($data['path'])['extension']); - $model->alias = $data['alias']; - $model->path = $data['path']; - $model->save(); - return $model->id; + return FileManager::saveTemFile($fileName, $data); } } @@ -236,24 +224,8 @@ class GoodsController extends Controller */ public function actionImgIdDel() { - $img_id = Yii::$app->request->get('imgid'); - $img_id_arr = explode(',', $img_id); - if(isset(Yii::$app->request->get('data')['alias'])) { - $alias = Yii::$app->request->get('data')['alias']; - $tem_file = \backend\modules\file\models\ars\TemFile::findOne(['alias' => $alias]); - if ($tem_file) { - $img_id_arr = array_diff($img_id_arr, [$tem_file->id]); - } - }else{ - foreach (Yii::$app->request->get() as $key => $value) { - $tem_file = \backend\modules\file\models\ars\File::findOne(['alias' => $value]); - if ($tem_file) { - $img_id_arr = array_diff($img_id_arr, [$tem_file->id]); - } - } - } - $img_id_str = implode(',', $img_id_arr); - return $img_id_str; + $data = Yii::$app->request->get(); + return FileManager::dealFileIdStrInDel($data); } /** @@ -262,25 +234,8 @@ class GoodsController extends Controller */ public function actionImageFile() { - $rule_verify = Yii::$app->request->get('ruleverify'); - $file_id_str = Yii::$app->request->get('fileidstr'); - $file_id_arr = explode(',', $file_id_str); - if ($rule_verify == 1) { - $data = \backend\modules\file\models\ars\TemFile::find()->where(['id' => $file_id_arr])->all(); - } else { - $data = \backend\modules\file\models\ars\File::find()->where(['id' => $file_id_arr])->all(); - } - $res = array(); - if($data) { - $i = 0; - foreach ($data as $key => $value) { - $res[$i]['name'] = $value->alias; - $res[$i]['path'] = Yii::$app->request->hostInfo . '/' . $value->path; - $res[$i]['size'] = filesize($value->path); - $i++; - } - } - return json_encode($res); + $fileIdStr = Yii::$app->request->get('fileIdStr'); + return FileManager::loadExitFile($fileIdStr); } /** @@ -322,7 +277,7 @@ class GoodsController extends Controller GoodsManager::deleteSku($res['type'], $data, $goodsId); $tra->commit(); return ['status' => true]; - } catch (\Exception $e) { + } catch (Exception $e) { $tra->rollBack(); return ['status' => false, 'info' => $e->getMessage()]; } diff --git a/backend/modules/goods/controllers/ShopCategoryController.php b/backend/modules/goods/controllers/ShopCategoryController.php index c0feeec..41cd6a2 100755 --- a/backend/modules/goods/controllers/ShopCategoryController.php +++ b/backend/modules/goods/controllers/ShopCategoryController.php @@ -2,6 +2,7 @@ namespace backend\modules\goods\controllers; +use backend\modules\file\logic\file\FileManager; use backend\modules\goods\logic\goods\GoodsManager; use backend\modules\file\models\ars\File; use Yii; @@ -205,18 +206,10 @@ class ShopCategoryController extends Controller public function actionSaveFile() { $data = Yii::$app->request->get('data'); - $file_name = Yii::$app->request->get('fileName')[0]; + $fileName = Yii::$app->request->get('fileName')[0]; if ($data['status'] == true) { - $model = new \backend\modules\file\models\ars\TemFile(); - $model->user_id = Yii::$app->user->identity->id; - $model->name = $file_name; - $file_manager = new \backend\modules\file\logic\file\FileManager(); - $model->type = $file_manager->searchType(pathinfo($data['path'])['extension']); - $model->alias = $data['alias']; - $model->path = $data['path']; - $model->save(); - return $model->id; + return FileManager::saveTemFile($fileName, $data); } } @@ -226,24 +219,8 @@ class ShopCategoryController extends Controller */ public function actionImgIdDel() { - $img_id = Yii::$app->request->get('imgid'); - $img_id_arr = explode(',', $img_id); - if(isset(Yii::$app->request->get('data')['alias'])) { - $alias = Yii::$app->request->get('data')['alias']; - $tem_file = \backend\modules\file\models\ars\TemFile::findOne(['alias' => $alias]); - if ($tem_file) { - $img_id_arr = array_diff($img_id_arr, [$tem_file->id]); - } - }else{ - foreach (Yii::$app->request->get() as $key => $value) { - $tem_file = \backend\modules\file\models\ars\File::findOne(['alias' => $value]); - if ($tem_file) { - $img_id_arr = array_diff($img_id_arr, [$tem_file->id]); - } - } - } - $img_id_str = implode(',', $img_id_arr); - return $img_id_str; + $data = Yii::$app->request->get(); + return FileManager::dealFileIdStrInDel($data); } /** @@ -252,19 +229,7 @@ class ShopCategoryController extends Controller */ public function actionImageFile() { - $file_id_str = Yii::$app->request->get('fileidstr'); - $file_id_arr = explode(',', $file_id_str); - $data = \backend\modules\file\models\ars\File::find()->where(['id' => $file_id_arr])->all(); - $res = array(); - if($data) { - $i = 0; - foreach ($data as $key => $value) { - $res[$i]['name'] = $value->alias; - $res[$i]['path'] = Yii::$app->request->hostInfo . '/' . $value->path; - $res[$i]['size'] = filesize($value->path); - $i++; - } - } - return json_encode($res); + $fileIdStr = Yii::$app->request->get('fileIdStr'); + return FileManager::loadExitFile($fileIdStr); } } diff --git a/backend/modules/goods/logic/goods/GoodsManager.php b/backend/modules/goods/logic/goods/GoodsManager.php index 3d8f830..ae19697 100755 --- a/backend/modules/goods/logic/goods/GoodsManager.php +++ b/backend/modules/goods/logic/goods/GoodsManager.php @@ -200,7 +200,7 @@ class GoodsManager } /** - * @param GoodsAttr|FilterAttr|$attributes + * @param $attributes * @return array * 获取属性信息 */