From bfdfd4304c313d3e5a6fc9f473085b3102e1bd3a Mon Sep 17 00:00:00 2001 From: linyaostalker <602604991@qq.com> Date: Wed, 11 Dec 2019 08:53:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=95=86=E5=93=81=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E8=8E=B7=E5=8F=96=E5=B1=9E=E6=80=A7=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../goods/controllers/AttributeController.php | 10 +-- .../goods/controllers/BrandController.php | 3 +- .../goods/controllers/CategoryController.php | 5 +- .../goods/controllers/GoodsController.php | 7 +- .../goods/logic/goods/GoodsManager.php | 70 ++++++------------- composer.json | 3 +- 6 files changed, 37 insertions(+), 61 deletions(-) diff --git a/backend/modules/goods/controllers/AttributeController.php b/backend/modules/goods/controllers/AttributeController.php index 41f3796..0ff2522 100755 --- a/backend/modules/goods/controllers/AttributeController.php +++ b/backend/modules/goods/controllers/AttributeController.php @@ -8,6 +8,7 @@ use backend\modules\goods\models\searchs\AttributeSearch; use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; +use iron\widget\Excel; /** * AttributeController implements the CRUD actions for Attribute model. @@ -70,10 +71,11 @@ class AttributeController extends Controller $model->cat_id = 0; if ($model->load(Yii::$app->request->post()) && $model->validate()) { - $model->value = str_replace(',', ',', $model->value); + $value = $model->value; + $model->value = str_replace(',', ',', $value); $array = explode(',', $model->value); if (count($array) != count(array_unique($array))) { - \Yii::$app->getSession()->setFlash('error', '不能有相同的属性值'); + Yii::$app->getSession()->setFlash('error', '不能有相同的属性值'); return $this->render('create', ['model' => $model]); } if ($model->save()) { @@ -101,7 +103,7 @@ class AttributeController extends Controller $model->value = str_replace(',', ',', $model->value); $array = explode(',', $model->value); if (count($array) != count(array_unique($array))) { - \Yii::$app->getSession()->setFlash('error', '不能有相同的属性值'); + Yii::$app->getSession()->setFlash('error', '不能有相同的属性值'); return $this->render('create', ['model' => $model]); } if ($model->save()) { @@ -159,7 +161,7 @@ class AttributeController extends Controller } else { $dataProvider = $searchModel->search($params); } - \iron\widget\Excel::export([ + Excel::export([ 'models' => $dataProvider->getModels(), 'format' => 'Xlsx', 'asAttachment' => true, diff --git a/backend/modules/goods/controllers/BrandController.php b/backend/modules/goods/controllers/BrandController.php index c96b99f..99bc76b 100755 --- a/backend/modules/goods/controllers/BrandController.php +++ b/backend/modules/goods/controllers/BrandController.php @@ -8,6 +8,7 @@ use backend\modules\goods\models\searchs\BrandSearch; use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; +use iron\widget\Excel; /** * BrandController implements the CRUD actions for Brand model. @@ -140,7 +141,7 @@ class BrandController extends Controller } else { $dataProvider = $searchModel->search($params); } - \iron\widget\Excel::export([ + Excel::export([ 'models' => $dataProvider->getModels(), 'format' => 'Xlsx', 'asAttachment' => true, diff --git a/backend/modules/goods/controllers/CategoryController.php b/backend/modules/goods/controllers/CategoryController.php index 305db28..6fc6aae 100755 --- a/backend/modules/goods/controllers/CategoryController.php +++ b/backend/modules/goods/controllers/CategoryController.php @@ -72,9 +72,8 @@ class CategoryController extends Controller } /** - * Creates a new Category model. - * If creation is successful, the browser will be redirected to the 'view' page. - * @return mixed + * @return string|yii\web\Response + * @throws Exception */ public function actionCreate() { diff --git a/backend/modules/goods/controllers/GoodsController.php b/backend/modules/goods/controllers/GoodsController.php index 91d07c6..4808b8c 100755 --- a/backend/modules/goods/controllers/GoodsController.php +++ b/backend/modules/goods/controllers/GoodsController.php @@ -2,6 +2,7 @@ namespace backend\modules\goods\controllers; +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; @@ -139,9 +140,11 @@ class GoodsController extends Controller $model->uniform_postage /= 100; $model->market_price /= 100; $model->price /= 100; - $attributeModel = GoodsManager::getAttribute($id); + $goodsAttributes = GoodsAttr::find()->where(['goods_id' => $id, 'is_delete' => GoodsAttr::IS_DELETE_NO])->andWhere(['!=', 'attr_id', 0])->all(); + $attributeModel = GoodsManager::getAttribute($goodsAttributes); $checkAttr = GoodsManager::getSkuInfo($id); - $filterAttributeModel = GoodsManager::getFilterAttribute($id); + $goodsFilterAttributes = FilterAttr::find()->where(['goods_id' => $id, 'is_delete' => FilterAttr::IS_DELETE_NO])->andWhere(['!=', 'attr_id', 0])->all(); + $filterAttributeModel = GoodsManager::getAttribute($goodsFilterAttributes); $judgeGoodsCategory = GoodsManager::judgeGoodsCategory($model); return $this->render('update', [ 'model' => $model, diff --git a/backend/modules/goods/logic/goods/GoodsManager.php b/backend/modules/goods/logic/goods/GoodsManager.php index 1eb735b..d36f933 100755 --- a/backend/modules/goods/logic/goods/GoodsManager.php +++ b/backend/modules/goods/logic/goods/GoodsManager.php @@ -3,14 +3,15 @@ namespace backend\modules\goods\logic\goods; use backend\modules\file\models\ars\File; use Yii; -use yii\base\Exception; +use yii\db\Exception; use backend\modules\goods\models\ars\GoodsAttr; use backend\modules\goods\models\ars\Attribute; use backend\modules\goods\models\ars\GoodsSku; use backend\modules\goods\models\ars\Goods; use backend\modules\goods\models\ars\FilterAttr; -use backend\modules\goods\models\ars\Category; use yii\web\HttpException; +use backend\modules\file\logic\file\FileManager; +use yii\web\ServerErrorHttpException; class GoodsManager { @@ -31,7 +32,7 @@ class GoodsManager $createFileIdArr = array_diff($newFileIdArr, $oldFileIdArr); //创建文件 - $class = new \backend\modules\file\logic\file\FileManager(); + $class = new FileManager(); $createFileRes = $class->saveTemFileToFile($createFileIdArr, $goodsModel->id, $fileType); //需要删除的文件id @@ -54,15 +55,15 @@ class GoodsManager $tra->commit(); return ['status' => true, 'info' => '操作成功', 'first_file_id' => $firstFileId]; - } catch (\Exception $e) { + } catch (Exception $e) { $tra->rollBack(); - throw new \Exception($e->getMessage()); + throw new ServerErrorHttpException($e->getMessage()); } } /** * @param $data - * @param $model + * @param Goods|$model * @param null $coverImageOldIdStr * @param null $detailImageOldIdStr * @return array @@ -82,7 +83,7 @@ class GoodsManager throw new \Exception('商品保存失败'); } $saveCoverImageRes = self::saveFile(explode(',', $model->coverImageId), $model, explode(',', $coverImageOldIdStr), File::OWN_TYPE_GOODS_INDEX); - $saveDetailImageRes = self::saveFile(explode(',', $model->detailImageId), $model, explode(',', $detailImageOldIdStr), File::OWN_TYPE_GOODS_DETAILS); + self::saveFile(explode(',', $model->detailImageId), $model, explode(',', $detailImageOldIdStr), File::OWN_TYPE_GOODS_DETAILS); if($saveCoverImageRes['first_file_id'] !== 0) { $model->image = $saveCoverImageRes['first_file_id']; if (!$model->save()) { @@ -198,33 +199,32 @@ class GoodsManager } /** - * @param $id - * @return Attribute|array|null + * @param GoodsAttr|FilterAttr|$attributes + * @return array * 获取属性信息 */ - public static function getAttribute($id) + public static function getAttribute($attributes) { - $goodsAttributes = GoodsAttr::find()->where(['goods_id' => $id, 'is_delete' => GoodsAttr::IS_DELETE_NO])->andWhere(['!=', 'attr_id', 0])->all(); - $filter = []; - $goodsAttributeModel = []; - if (!$goodsAttributes) { - return $goodsAttributeModel; + $filter = array(); + $attributeModel = array(); + if (!$attributes) { + return $attributeModel; } - foreach ($goodsAttributes as $key => $value) { + foreach ($attributes as $key => $value) { $attribute = Attribute::findOne($value->attr_id); if (!in_array($attribute->name, $filter)) { $filter[] = $attribute->name; $attribute = ['name' => $attribute->name, 'id' => $attribute->id, 'value' => [$value->attr_value]]; - $goodsAttributeModel[] = $attribute; + $attributeModel[] = $attribute; } else { - foreach ($goodsAttributeModel as $k => $v) { + foreach ($attributeModel as $k => $v) { if ($v['name'] == $attribute->name) { - $goodsAttributeModel[$k]['value'][] = $value->attr_value; + $attributeModel[$k]['value'][] = $value->attr_value; } } } } - return $goodsAttributeModel; + return $attributeModel; } /** @@ -534,36 +534,6 @@ class GoodsManager return $newAttr; } - /** - * @param $id - * @return Attribute|array|null - * 获取筛选属性信息 - */ - public static function getFilterAttribute($id) - { - $goodsFilterAttributes = FilterAttr::find()->where(['goods_id' => $id, 'is_delete' => FilterAttr::IS_DELETE_NO])->andWhere(['!=', 'attr_id', 0])->all(); - $filter = []; - $goodsFilterAttributeModel = []; - if (!$goodsFilterAttributes) { - return $goodsFilterAttributeModel; - } - foreach ($goodsFilterAttributes as $key => $value) { - $attribute = Attribute::findOne($value->attr_id); - if (!in_array($attribute->name, $filter)) { - $filter[] = $attribute->name; - $attribute = ['name' => $attribute->name, 'id' => $attribute->id, 'value' => [$value->attr_value]]; - $goodsFilterAttributeModel[] = $attribute; - } else { - foreach ($goodsFilterAttributeModel as $k => $v) { - if ($v['name'] == $attribute->name) { - $goodsFilterAttributeModel[$k]['value'][] = $value->attr_value; - } - } - } - } - return $goodsFilterAttributeModel; - } - /** * @param $goodsModel * @return bool diff --git a/composer.json b/composer.json index c79317a..9b4dad7 100755 --- a/composer.json +++ b/composer.json @@ -20,7 +20,8 @@ "kartik-v/yii2-editable": "^1.7@dev", "kartik-v/yii2-widget-depdrop": "dev-master", "antkaz/yii2-vue": "dev-master", - "xj/yii2-babel": "dev-master" + "xj/yii2-babel": "dev-master", + "ext-json": "*" }, "repositories": { "asset_packagist": {