Browse Source

完成商品筛选属性的创建修改删除

wechat_public_accounts
linyaostalker 5 years ago
parent
commit
fce985429c
  1. 58
      vendor/antgoods/goods/src/assets/custom/sku.8d54a9feac4b1fce0c54.js
  2. 2
      vendor/antgoods/goods/src/controllers/GoodsController.php
  3. 120
      vendor/antgoods/goods/src/logic/goods/GoodsManager.php
  4. 3
      vendor/antgoods/goods/src/models/ars/FilterAttr.php
  5. 7
      vendor/antgoods/goods/src/views/goods/create.php
  6. 22
      vendor/antgoods/goods/src/views/goods/filter_attribute.php
  7. 8
      vendor/antgoods/goods/src/views/goods/update.php
  8. 21
      vendor/antgoods/goods/src/web/GoodsFilterAttributeAsset.php

58
vendor/antgoods/goods/src/assets/custom/sku.8d54a9feac4b1fce0c54.js
File diff suppressed because it is too large
View File

2
vendor/antgoods/goods/src/controllers/GoodsController.php

@ -124,10 +124,12 @@ class GoodsController extends Controller
} }
$attributeModel = GoodsManager::getAttribute($id); $attributeModel = GoodsManager::getAttribute($id);
$checkAttr = GoodsManager::getSkuInfo($id); $checkAttr = GoodsManager::getSkuInfo($id);
$filterAttributeModel = GoodsManager::getFilterAttribute($id);
return $this->render('update', [ return $this->render('update', [
'model' => $model, 'model' => $model,
'attributeModel' => $attributeModel, 'attributeModel' => $attributeModel,
'attrValue' => $checkAttr, 'attrValue' => $checkAttr,
'filterAttributeModel' => $filterAttributeModel,
]); ]);
} }

120
vendor/antgoods/goods/src/logic/goods/GoodsManager.php

@ -8,6 +8,7 @@ use antgoods\goods\models\ars\GoodsAttr;
use antgoods\goods\models\ars\Attribute; use antgoods\goods\models\ars\Attribute;
use antgoods\goods\models\ars\GoodsSku; use antgoods\goods\models\ars\GoodsSku;
use antgoods\goods\models\ars\Goods; use antgoods\goods\models\ars\Goods;
use antgoods\goods\models\ars\FilterAttr;
class GoodsManager class GoodsManager
{ {
@ -66,6 +67,7 @@ class GoodsManager
public static function updateGoods($data, $model, $coverImageOldIdStr = null, $detailImageOldIdStr = null) public static function updateGoods($data, $model, $coverImageOldIdStr = null, $detailImageOldIdStr = null)
{ {
$attribute = $data['attribute']; $attribute = $data['attribute'];
$filterAttribute = $data['filter_attribute'];
$tra = Yii::$app->db->beginTransaction(); $tra = Yii::$app->db->beginTransaction();
try { try {
if (!$model->save()) { if (!$model->save()) {
@ -84,6 +86,7 @@ class GoodsManager
throw new Exception('图片保存失败'); throw new Exception('图片保存失败');
} }
self::addAttributeOperating(['id' => $model->id, 'attribute' => $attribute]); self::addAttributeOperating(['id' => $model->id, 'attribute' => $attribute]);
self::addFilterAttributeOperating(['id' => $model->id, 'filterAttribute' => $filterAttribute]);
$tra->commit(); $tra->commit();
return ['status' => true]; return ['status' => true];
} catch (\yii\base\Exception $e) { } catch (\yii\base\Exception $e) {
@ -429,4 +432,121 @@ class GoodsManager
$value->delete(); $value->delete();
} }
} }
/**
* @param $data
* @return bool
* @throws Exception
* 创建修改商品筛选属性操作
*/
public static function addFilterAttributeOperating($data)
{
if (!$data['filterAttribute']) {
return true;
}
$data['filterAttribute'] = json_decode($data['filterAttribute'], true);
$oldFilterAttr = [];
$goodsFilterAttr = FilterAttr::find()->where(['goods_id' => $data['id'], 'is_delete' => FilterAttr::IS_DELETE_NO])->all();
if ($goodsFilterAttr) { //如果商品有旧的属性
if(count($data['filterAttribute']) == 0 && is_array($data['filterAttribute'])) { //如果传上来的是空数组,删除该商品下的全部属性
self::delFilterAttribute($goodsFilterAttr);
return true;
}
foreach ($goodsFilterAttr as $key => $value) { //把旧的商品属性保存到一个数组
$oldFilterAttr[$value->id] = $value->attr_value;
}
}
$newAttr = self::addFilterAttribute($data['filterAttribute'], $data['id']); //添加新的商品属性
$delAttr = array_diff(array_keys($oldFilterAttr), array_keys($newAttr)); //找出需要删除的goodsAttrId
if (!$delAttr) {
return true;
}
foreach ($delAttr as $value) {
$model = FilterAttr::find()->where(['id' => $value, 'is_delete' => FilterAttr::IS_DELETE_NO])->One();
if ($model) {
$model->is_delete = FilterAttr::IS_DELETE_YES;
if (!$model->save()) {
throw new Exception('goodsAttribute delete false');
}
}
}
}
/**
* @param $goodsAttr
* @throws Exception
* 删除商品筛选属性
*/
public static function delFilterAttribute($goodsFilterAttr)
{
foreach ($goodsFilterAttr as $key => $value) {
$value->is_delete = FilterAttr::IS_DELETE_YES;
if (!$value->save()) {
throw new Exception('goods attribute delete false');
}
}
}
/**
* @param $attribute
* @param $goodsId
* @return array
* @throws Exception
* 保存商品筛选属性
*/
public static function addFilterAttribute($attribute, $goodsId)
{
$newAttr = [];
if (!$attribute) {
return [];
}
foreach ($attribute as $value) {
foreach ($value['value'] as $k => $v) {
$goodsFilterAttrModel = new FilterAttr();
$goodsFilterAttrModel->attr_id = $value['id'];
$attr = FilterAttr::find()->where(['goods_id' => $goodsId, 'attr_id' => $value['id'], 'attr_value' => $v, 'is_delete' => FilterAttr::IS_DELETE_NO])->one();
if ($attr) {
$newAttr[$attr->id] = $attr->attr_value; //原有的数据
} else {
$goodsFilterAttrModel->goods_id = $goodsId;
$goodsFilterAttrModel->attr_value = $v;
if (!$goodsFilterAttrModel->save()) {
throw new Exception('goodsAttribute save false');
}
$newAttr[$goodsFilterAttrModel->id] = $goodsFilterAttrModel->attr_value; //新增的数据
}
}
}
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;
}
} }

3
vendor/antgoods/goods/src/models/ars/FilterAttr.php

@ -18,6 +18,9 @@ use yii\behaviors\TimestampBehavior;
*/ */
class FilterAttr extends \yii\db\ActiveRecord class FilterAttr extends \yii\db\ActiveRecord
{ {
//是否删除is_delete
const IS_DELETE_NO = 0;//未删除
const IS_DELETE_YES = 1;//已删除
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

7
vendor/antgoods/goods/src/views/goods/create.php

@ -27,6 +27,13 @@ Yii::$app->params['bsVersion'] = '4.x';
'form' => $form 'form' => $form
]), ]),
], ],
[
'label' => '<i class="fas fa-list-alt"></i> 筛选规格',
'content' => $this->render('filter_attribute', [
'filterModel' => [],
'filterAttrValue' => [],
]),
],
[ [
'label' => '<i class="fas fa-list-alt"></i> 商品规格', 'label' => '<i class="fas fa-list-alt"></i> 商品规格',
'content' => $this->render('attribute', [ 'content' => $this->render('attribute', [

22
vendor/antgoods/goods/src/views/goods/filter_attribute.php

@ -0,0 +1,22 @@
<?php
use antgoods\goods\models\ars\Attribute;
use antgoods\goods\web\GoodsFilterAttributeAsset;
$param = [];
$value = [];
$attrId = [];
$allFilterAttr = Attribute::find()->where(['type' => Attribute::TYPE_ATTR])->asArray()->all();
GoodsFilterAttributeAsset::register($this);
?>
<div class="content-box">
<input type="hidden" id="attribute" name="attribute"/>
<div id="filter-choose"></div>
</div>
<script>
var csrf = "<?= Yii::$app->request->csrfToken ?>";
var allFilterAttr = <?= isset($allFilterAttr) ? json_encode($allFilterAttr) : [] ?>; // 所有数据
var currentFilterAttr = <?= isset($filterModel) ? json_encode($filterModel) : [] ?>; // 默认数据(之前已选择的数据)
var canNotDeleteFilterAttr =<?=isset($filterAttrValue) ? json_encode($filterAttrValue) : [] ?>; // 已设置sku的值,不可删除
</script>

8
vendor/antgoods/goods/src/views/goods/update.php

@ -28,11 +28,17 @@ Yii::$app->params['bsVersion'] = '4.x';
'form' => $form 'form' => $form
]), ]),
], ],
[
'label' => '<i class="fas fa-list-alt"></i> 筛选规格',
'content' => $this->render('filter_attribute', [
'filterModel' => $filterAttributeModel,
'filterAttrValue' => [],
]),
],
[ [
'label' => '<i class="fas fa-list-alt"></i> 商品规格', 'label' => '<i class="fas fa-list-alt"></i> 商品规格',
'content' => $this->render('attribute', [ 'content' => $this->render('attribute', [
'model' => $attributeModel, 'model' => $attributeModel,
'attrValue' => [],
'attrValue' => $attrValue, 'attrValue' => $attrValue,
]), ]),
], ],

21
vendor/antgoods/goods/src/web/GoodsFilterAttributeAsset.php

@ -0,0 +1,21 @@
<?php
namespace antgoods\goods\web;
use yii\web\AssetBundle;
/**
* Main backend application asset bundle.
*/
class GoodsFilterAttributeAsset extends AssetBundle
{
public $sourcePath = "@antgoods/goods/src/assets";
public $css = [
];
public $js = [
'custom/sku.8d54a9feac4b1fce0c54.js'
];
public $depends = [
'yii\web\YiiAsset',
];
}
Loading…
Cancel
Save