From e0f21480a8b7e29362ecef8abebe1846bac25e9f Mon Sep 17 00:00:00 2001 From: linyaostalker <602604991@qq.com> Date: Wed, 11 Dec 2019 20:06:03 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=E9=87=8D=E6=9E=84=E8=A7=84=E6=A0=BC?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=92=8C=E7=BC=96=E8=BE=91=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../goods/controllers/AttributeController.php | 19 +++--------------- .../goods/logic/goods/GoodsManager.php | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/backend/modules/goods/controllers/AttributeController.php b/backend/modules/goods/controllers/AttributeController.php index 0ff2522..67e7ba9 100755 --- a/backend/modules/goods/controllers/AttributeController.php +++ b/backend/modules/goods/controllers/AttributeController.php @@ -2,6 +2,7 @@ namespace backend\modules\goods\controllers; +use backend\modules\goods\logic\goods\GoodsManager; use Yii; use backend\modules\goods\models\ars\Attribute; use backend\modules\goods\models\searchs\AttributeSearch; @@ -71,14 +72,7 @@ class AttributeController extends Controller $model->cat_id = 0; if ($model->load(Yii::$app->request->post()) && $model->validate()) { - $value = $model->value; - $model->value = str_replace(',', ',', $value); - $array = explode(',', $model->value); - if (count($array) != count(array_unique($array))) { - Yii::$app->getSession()->setFlash('error', '不能有相同的属性值'); - return $this->render('create', ['model' => $model]); - } - if ($model->save()) { + if (GoodsManager::updateAttribute($model)) { return $this->redirect(['index']); } } @@ -100,16 +94,9 @@ class AttributeController extends Controller $model = $this->findModel($id); if ($model->load(Yii::$app->request->post()) && $model->validate()) { - $model->value = str_replace(',', ',', $model->value); - $array = explode(',', $model->value); - if (count($array) != count(array_unique($array))) { - Yii::$app->getSession()->setFlash('error', '不能有相同的属性值'); - return $this->render('create', ['model' => $model]); - } - if ($model->save()) { + if (GoodsManager::updateAttribute($model)) { return $this->redirect(['index']); } - return $this->redirect('index'); } return $this->render('update', [ diff --git a/backend/modules/goods/logic/goods/GoodsManager.php b/backend/modules/goods/logic/goods/GoodsManager.php index 174f55b..3d8f830 100755 --- a/backend/modules/goods/logic/goods/GoodsManager.php +++ b/backend/modules/goods/logic/goods/GoodsManager.php @@ -557,4 +557,24 @@ class GoodsManager } return false; //否则返回false,表示后台分类可以修改 } + + /** + * @param Attribute|$attrModel + * @return bool + * 编辑规格属性 + */ + public static function updateAttribute($attrModel) + { + $attrModel->value = str_replace(',', ',', $attrModel->value); + $attrValue = explode(',', $attrModel->value); + if (count($attrValue) != count(array_unique($attrValue))) { + Yii::$app->getSession()->setFlash('error', '不能有相同的属性值'); + return false; + } + if (!$attrModel->save()) { + Yii::$app->getSession()->setFlash('error', '保存失败'); + return false; + } + return true; + } } \ No newline at end of file