shish пре 6 година
родитељ
комит
b38171e1cf

+ 11 - 7
app/business/controllers/GoodsController.php

@@ -28,10 +28,12 @@ class GoodsController extends BaseController
 	{
 		$data = Yii::$app->request->post();
 		$data['merchantId'] = $this->merchantId;
-		$info = GoodsService::addGoods($data);
+		$return = GoodsService::addGoods($data);
+		$id = $return['id'];
+		$info = GoodsService::getById($id);
 		util::success($info);
 	}
-
+	
 	//获取商品详情 shish 2019.12.7
 	public function actionDetail()
 	{
@@ -43,12 +45,14 @@ class GoodsController extends BaseController
 	//更新商品 shish 2019.12.7
 	public function actionUpdate()
 	{
-		$data = Yii::$app->request->get();
-		$id = $data['id'];
-		$re = GoodsService::updateById($id, $data);
-		util::success($re);
+		$data = Yii::$app->request->post();
+		$id = isset($data['id']) ? $data['id'] : 0;
+		unset($data['id']);
+		GoodsService::valid($id,$this->merchantId);
+		GoodsService::updateGoods($id, $data);
+		util::ok();
 	}
-	
+
 	//删除商品 shish 2019.12.7
 	public function actionDelete()
 	{

+ 6 - 0
biz/goods/classes/GoodsCategoryClass.php

@@ -11,4 +11,10 @@ class GoodsCategoryClass extends BaseClass
 
 	public static $baseFile = '\biz\goods\models\GoodsCategory';
 
+	//删除商品的分类 shish 2019.12.7
+	public static function delGoodsCategory($goodsId)
+	{
+		self::deleteByCondition(['gId'=>$goodsId]);
+	}
+
 }

+ 42 - 2
biz/goods/classes/GoodsClass.php

@@ -76,10 +76,9 @@ class GoodsClass extends BaseClass
 			$addCategory[] = ['gId' => $id, 'cId' => $categoryId, 'merchantId' => $data['merchantId']];
 		}
 		GoodsCategoryClass::batchAdd($addCategory);
-		
 		//处理款式
 		if (!empty($goodsStyleList) && $data['goodsStyle'] == 1) {
-			$goodsStyleList = [['id' => 0, 'styleName' => '红色', 'price' => 0.1, 'picture' => '']];
+			$goodsStyleList = [['styleName' => '红色', 'price' => 0.1, 'picture' => '']];
 			$addStyle = [];
 			foreach ($goodsStyleList as $style) {
 				$addStyle[] = ['styleName' => $style['styleName'], 'goodsId' => $id, 'picture' => $style['picture'], 'price' => $style['price']];
@@ -88,5 +87,46 @@ class GoodsClass extends BaseClass
 		}
 		return $goodsInfo;
 	}
+	
+	//更新商品 shish 2019.12.7
+	public static function updateGoods($id, $data)
+	{
+		$categoryIdList = isset($data['categoryIdList']) && !empty($data['categoryIdList']) ? $data['categoryIdList'] : [];
+		unset($data['categoryIdList']);
+		if (empty($categoryIdList)) {
+			util::fail('没有选择分类');
+		}
+		$goodsStyleList = isset($data['goodsStyleList']) && !empty($data['goodsStyleList']) ? $data['goodsStyleList'] : [];
+		unset($data['goodsStyleList']);
+		
+		//处理分类
+		$merchantId = $data['merchantId'];
+		$merchantHasCategoryIdList = CategoryClass::getCategoryIdList($merchantId);
+		$diff = array_diff($categoryIdList, $merchantHasCategoryIdList);
+		if (!empty($diff)) {
+			util::fail('您使用了别人的分类');
+		}
 
+		//先删除分类
+		GoodsCategoryClass::delGoodsCategory($id);
+		$addCategory = [];
+		foreach ($categoryIdList as $categoryId) {
+			$addCategory[] = ['gId' => $id, 'cId' => $categoryId, 'merchantId' => $data['merchantId']];
+		}
+		//再添加分类
+		GoodsCategoryClass::batchAdd($addCategory);
+
+		//处理款式
+		if (!empty($goodsStyleList) && $data['goodsStyle'] == 1) {
+			$goodsStyleList = [['id' => 0, 'styleName' => '红色', 'price' => 0.1, 'picture' => '','act'=>'']];
+			$addStyle = [];
+			foreach ($goodsStyleList as $style) {
+				$addStyle[] = ['styleName' => $style['styleName'], 'goodsId' => $id, 'picture' => $style['picture'], 'price' => $style['price']];
+			}
+			GoodsStyleClass::batchAdd($addStyle);
+		}
+
+		self::updateById($id, $data);
+	}
+	
 }

+ 15 - 0
biz/goods/services/GoodsService.php

@@ -52,5 +52,20 @@ class GoodsService extends BaseService
 	{
 		return GoodsClass::addGoods($data);
 	}
+	
+	//更新商品 shish 2019.12.7
+	public static function updateGoods($id, $data)
+	{
+		return GoodsClass::updateGoods($id, $data);
+	}
+	
+	//验证对此商品的操作权限 shish 2019.12.7
+	public static function valid($id, $merchantId)
+	{
+		$goods = GoodsClass::getById($id);
+		if ($goods['merchantId'] != $merchantId) {
+			util::fail('您没有权限操作');
+		}
+	}
 
 }