From f6142f18b2c252d9078604ce3ddd693db6a36033 Mon Sep 17 00:00:00 2001 From: linyaostalker <602604991@qq.com> Date: Tue, 26 Nov 2019 08:56:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E4=B8=8A=E4=BC=A0=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E5=8C=BA=E5=88=86=E5=88=A0=E9=99=A4=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E5=92=8C=E4=B8=8D=E4=BF=AE=E6=94=B9=E5=9B=BE=E7=89=87=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/controllers/CategoryController.php | 8 +- .../goods/src/controllers/GoodsController.php | 4 +- .../controllers/ShopcategoryController.php | 128 +++++++++++++++++- .../goods/src/logic/goods/GoodsManager.php | 10 +- .../goods/src/models/ars/ShopCategory.php | 5 +- .../goods/src/views/shopcategory/_form.php | 15 +- 6 files changed, 157 insertions(+), 13 deletions(-) diff --git a/vendor/antgoods/goods/src/controllers/CategoryController.php b/vendor/antgoods/goods/src/controllers/CategoryController.php index efedfec..10ff1b8 100644 --- a/vendor/antgoods/goods/src/controllers/CategoryController.php +++ b/vendor/antgoods/goods/src/controllers/CategoryController.php @@ -86,7 +86,7 @@ class CategoryController extends Controller $icon_image_id_str = $model->iconImageId; $model->save(); $goods_manager = new GoodsManager(); - $save_icon_image_res = $goods_manager->saveFile(explode(',', $icon_image_id_str), $model); + $save_icon_image_res = $goods_manager->saveFile(explode(',', $icon_image_id_str), $model, [], File::OWN_TYPE_CATEGORY_ICON); if($save_icon_image_res['status']){ $model->icon = $save_icon_image_res['first_file_id']; $model->save(); @@ -121,8 +121,8 @@ class CategoryController extends Controller $model->save(); $goods_manager = new GoodsManager(); $save_icon_image_res = $goods_manager->saveFile(explode(',', $icon_image_id_str), $model, explode(',', $icon_image_old_id_arr), File::OWN_TYPE_CATEGORY_ICON); - if($save_icon_image_res['status'] && $save_icon_image_res['first_file_id'] != 0){ - $model->image = $save_icon_image_res['first_file_id']; + if($save_icon_image_res['status'] && $save_icon_image_res['first_file_id'] !== 0){ + $model->icon = $save_icon_image_res['first_file_id']; $model->save(); } @@ -261,8 +261,8 @@ class CategoryController extends Controller $file_id_str = Yii::$app->request->get('fileidstr'); $file_id_arr = explode(',', $file_id_str); $data = \common\models\ars\File::find()->where(['id' => $file_id_arr])->all(); + $res = array(); if($data) { - $res = array(); $i = 0; foreach ($data as $key => $value) { $res[$i]['name'] = $value->alias; diff --git a/vendor/antgoods/goods/src/controllers/GoodsController.php b/vendor/antgoods/goods/src/controllers/GoodsController.php index 5b5e087..eaca006 100644 --- a/vendor/antgoods/goods/src/controllers/GoodsController.php +++ b/vendor/antgoods/goods/src/controllers/GoodsController.php @@ -130,7 +130,7 @@ class GoodsController extends Controller $goods_manager = new GoodsManager(); $save_cover_image_res = $goods_manager->saveFile(explode(',', $cover_image_id_str), $model, explode(',', $cover_image_old_id_arr)); $save_detail_image_res = $goods_manager->saveFile(explode(',', $detail_image_id_str), $model, explode(',', $detail_image_old_id_arr), File::OWN_TYPE_GOODS_DETAILS); - if($save_cover_image_res['status'] && $save_detail_image_res['status'] && $save_cover_image_res['first_file_id'] != 0){ + if($save_cover_image_res['status'] && $save_detail_image_res['status'] && $save_cover_image_res['first_file_id'] !== 0){ $model->image = $save_cover_image_res['first_file_id']; $model->save(); } @@ -267,8 +267,8 @@ class GoodsController extends Controller $file_id_str = Yii::$app->request->get('fileidstr'); $file_id_arr = explode(',', $file_id_str); $data = \common\models\ars\File::find()->where(['id' => $file_id_arr])->all(); + $res = array(); if($data) { - $res = array(); $i = 0; foreach ($data as $key => $value) { $res[$i]['name'] = $value->alias; diff --git a/vendor/antgoods/goods/src/controllers/ShopcategoryController.php b/vendor/antgoods/goods/src/controllers/ShopcategoryController.php index ef1feec..64c758a 100644 --- a/vendor/antgoods/goods/src/controllers/ShopcategoryController.php +++ b/vendor/antgoods/goods/src/controllers/ShopcategoryController.php @@ -2,6 +2,8 @@ namespace antgoods\goods\controllers; +use antgoods\goods\logic\goods\GoodsManager; +use common\models\ars\File; use Yii; use antgoods\goods\models\ars\ShopCategory; use antgoods\goods\models\searchs\ShopCategorySearch; @@ -29,6 +31,17 @@ class ShopcategoryController extends Controller ]; } + public function actions() + { + return [ + 'upload' => [ + 'class' => 'iron\actions\UploadAction', + 'path' => 'xls/', + 'maxSize' => 20480, + ] + ]; + } + /** * Lists all ShopCategory models. * @return mixed @@ -67,7 +80,18 @@ class ShopcategoryController extends Controller { $model = new ShopCategory(); - if ($model->load(Yii::$app->request->post()) && $model->save()) { + if ($model->load(Yii::$app->request->post())) { + + //类目图片上传保存处理 + $icon_image_id_str = $model->iconImageId; + $model->save(); + $goods_manager = new GoodsManager(); + $save_icon_image_res = $goods_manager->saveFile(explode(',', $icon_image_id_str), $model, [], File::OWN_TYPE_CATEGORY_ICON); + if($save_icon_image_res['status']){ + $model->icon = $save_icon_image_res['first_file_id']; + $model->save(); + } + return $this->redirect('index'); } @@ -86,8 +110,22 @@ class ShopcategoryController extends Controller public function actionUpdate($id) { $model = $this->findModel($id); + $model->iconImageId = $model->icon; + //记录已保存的类目图片id,用于修改 + $icon_image_old_id_arr = $model->icon; + + if ($model->load(Yii::$app->request->post())) { + + //类目图片上传保存处理 + $icon_image_id_str = $model->iconImageId; + $model->save(); + $goods_manager = new GoodsManager(); + $save_icon_image_res = $goods_manager->saveFile(explode(',', $icon_image_id_str), $model, explode(',', $icon_image_old_id_arr), File::OWN_TYPE_CATEGORY_ICON); + if($save_icon_image_res['status'] && $save_icon_image_res['first_file_id'] !== 0){ + $model->icon = $save_icon_image_res['first_file_id']; + $model->save(); + } - if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect('index'); } @@ -146,4 +184,90 @@ class ShopcategoryController extends Controller 'columns' => $searchModel->columns() ]); } + + /** + * 处理文件上传成功后回调保存到临时文件表中,并返回临时文件id + */ + public function actionSaveFile() + { + if(!class_exists('\common\models\ars\TemFile') || !class_exists('\backend\logic\file\FileManager')){ + return ''; + } + + $data = Yii::$app->request->get('data'); + $file_name = Yii::$app->request->get('fileName')[0]; + + if ($data['status'] == true) { + $model = new \common\models\ars\TemFile(); + $model->user_id = Yii::$app->user->identity->id; + $model->name = $file_name; + $file_manager = new \backend\logic\file\FileManager(); + $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']; + $model->save(); + return $model->id; + } + } + + /** + * @return string + * 点击删除按钮时同时删除字符串中的id + */ + public function actionImgIdDel() + { + //判断该类是否存在 + if(!class_exists('\common\models\ars\TemFile') || !class_exists('\common\models\ars\File')){ + return ''; + } + + $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 = \common\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 = \common\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; + } + + /** + * @return bool|false|string + * 加载已有的文件 + */ + public function actionImageFile() + { + //判断该类是否存在 + if(!class_exists('\common\models\ars\File')){ + return false; + } + + $file_id_str = Yii::$app->request->get('fileidstr'); + $file_id_arr = explode(',', $file_id_str); + $data = \common\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); + } } diff --git a/vendor/antgoods/goods/src/logic/goods/GoodsManager.php b/vendor/antgoods/goods/src/logic/goods/GoodsManager.php index c52e8d3..63696e5 100644 --- a/vendor/antgoods/goods/src/logic/goods/GoodsManager.php +++ b/vendor/antgoods/goods/src/logic/goods/GoodsManager.php @@ -36,8 +36,14 @@ class GoodsManager //记录第一张图片id $first_file_id = 0; - if($create_file_res['status']) { - $first_file_id = $create_file_res['first_file_id']; + + //查看修改数组是否为空 + if (!$newFileIdArr[0]) { + $first_file_id = null; + }else { + if ($create_file_res['status']) { + $first_file_id = $create_file_res['first_file_id']; + } } return ['status' => true, 'info' => '操作成功', 'first_file_id' => $first_file_id]; diff --git a/vendor/antgoods/goods/src/models/ars/ShopCategory.php b/vendor/antgoods/goods/src/models/ars/ShopCategory.php index 92fa87e..d6dc798 100644 --- a/vendor/antgoods/goods/src/models/ars/ShopCategory.php +++ b/vendor/antgoods/goods/src/models/ars/ShopCategory.php @@ -25,6 +25,8 @@ use yii\behaviors\TimestampBehavior; */ class ShopCategory extends \yii\db\ActiveRecord { + public $iconImageId; + public $iconImagePath; //是否显示is_show const IS_SHOW_DISPLAY = 0;//显示 const IS_SHOW_HIDE = 1;//隐藏 @@ -52,12 +54,11 @@ class ShopCategory extends \yii\db\ActiveRecord { return [ [['name'], 'required'], - [['pid', 'goods_count', 'sort_order', 'icon_type', 'is_show', 'is_delete'], 'integer'], + [['pid', 'goods_count', 'sort_order', 'icon', 'is_show', 'is_delete', 'iconImageId'], 'integer'], [['filter_attr'], 'string'], [['name'], 'string', 'max' => 60], [['keywords'], 'string', 'max' => 100], [['desc'], 'string', 'max' => 255], - [['icon'], 'string', 'max' => 64], ]; } diff --git a/vendor/antgoods/goods/src/views/shopcategory/_form.php b/vendor/antgoods/goods/src/views/shopcategory/_form.php index 97a9d30..cf80c31 100644 --- a/vendor/antgoods/goods/src/views/shopcategory/_form.php +++ b/vendor/antgoods/goods/src/views/shopcategory/_form.php @@ -3,6 +3,7 @@ use yii\helpers\Html; use yii\bootstrap4\ActiveForm; use blobt\widgets\Icheck; +use yii\helpers\Url; /* @var $this yii\web\View */ /* @var $model antgoods\goods\models\ars\ShopCategory */ @@ -21,7 +22,19 @@ use blobt\widgets\Icheck; field($model, 'sort_order')->textInput() ?> - field($model, 'icon')->textInput(['maxlength' => true]) ?> + field($model, 'iconImageId')->textInput()->label('') ?> + field($model, 'iconImagePath')->widget(\iron\widgets\Upload::className(), [ + 'url' => 'upload', + 'deleteUrl' => 'img-id-del', + 'dragdropWidth'=> 800, + 'afterSave' => 'save-file', + 'maxCount' => 1, + 'fillInAttribute' => 'iconImageId', + 'model' => $model, + 'previewConfig' => [ + 'url' => Url::to(['image-file', 'fileidstr' => $model->iconImageId]), + ], + ])->label('类目图片') ?> field($model, 'filter_attr')->textarea(['rows' => 6]) ?>