shish 6 éve
szülő
commit
ea7cca01ca

+ 7 - 9
app/client/controllers/MainController.php

@@ -3,6 +3,7 @@
 namespace client\controllers;
 
 use biz\goods\services\CategoryService;
+use biz\goods\services\GoodsCategoryService;
 use Yii;
 use yii\web\Controller;
 use common\components\util;
@@ -17,15 +18,12 @@ class MainController extends BaseController
 			['adImg' => 'http://img.fjglhs.com/images/glhs/glhs_banner2.jpg', 'url' => 'http://www.baidu.com'],
 			['adImg' => 'http://img.fjglhs.com/images/glhs/glhs_banner3.jpg', 'url' => 'http://www.baidu.com'],
 		];
-		$data = [];
+		$category = CategoryService::getTopThreeCategory();
+		if (!empty($category)) {
+			$category = GoodsCategoryService::getAppointCategoryList($category);
+		}
+		$data = ['ad' => $ad, 'category' => $category];
 		util::success($data);
 	}
-
-	//取分类 shish 2019.12.2
-	public function actionGetCategory()
-	{
-		$menu = CategoryService::getMenu();
-		util::success($menu);
-	}
-
+	
 }

+ 13 - 2
biz/goods/services/CategoryService.php

@@ -27,14 +27,25 @@ class CategoryService extends BaseService
 		}
 		return $cat;
 	}
-
+	
 	//获取商家最靠前的分类id shish 2019.12.2
 	public static function getTopCategoryId()
 	{
 		$merchantId = Yii::$app->params['merchantId'];
-		$cat = CategoryClass::getByCondition(['merchantId' => $merchantId], false, 'inTurn DESC');
+		$cat = CategoryClass::getByCondition(['merchantId' => $merchantId, 'status' => 1], false, 'inTurn DESC');
 		$id = isset($cat['id']) ? $cat['id'] : 0;
 		return $id;
 	}
 	
+	//获取前三个分类信息 shish 2019.12.2
+	public static function getTopThreeCategory()
+	{
+		$merchantId = Yii::$app->params['merchantId'];
+		$cat = CategoryClass::getLimitList('id,categoryName', ['merchantId' => $merchantId, 'status' => 1], 3, 'inTurn DESC');
+		if (empty($cat)) {
+			return [];
+		}
+		return $cat;
+	}
+	
 }

+ 43 - 1
biz/goods/services/GoodsCategoryService.php

@@ -12,7 +12,7 @@ class GoodsCategoryService extends BaseService
 	
 	public static $baseFile = '\biz\goods\classes\GoodsCategoryClass';
 	
-	//分类下的商品 shish 2019.12.2
+	//查询一个分类下的商品,有分页 shish 2019.12.2
 	public static function getGoodsList($where)
 	{
 		$data = GoodsCategoryClass::getList('*', $where, 'inTurn DESC');
@@ -26,4 +26,46 @@ class GoodsCategoryService extends BaseService
 		return $data;
 	}
 	
+	//查询指定分类下的商品,没有分页,首页使用 shish 2019.12.2
+	public static function getAppointCategoryList($category)
+	{
+		$ids = array_column($category, 'id');
+		//取出分类下的商品id
+		$list = GoodsCategoryClass::getAllByCondition(['cId' => ['in', $ids]], null, '*');
+		if (empty($list)) {
+			return $category;
+		}
+		$goodsList = [];
+		$categoryGoods = [];
+		foreach ($list as $val) {
+			$goodsList[] = $val['gId'];
+			$cId = $val['cId'];
+			$categoryGoods[$cId][] = $val['gId'];
+		}
+		//查出商品信息
+		$goodsInfoList = GoodsService::getAllByCondition(['id' => ['in', $goodsList]], 'inTurn DESC', 'goodsName,shopImg,price,sold,id', 'id');
+		$goodsInfoList = GoodsCategoryClass::groupGoodsBaseInfo($goodsInfoList);
+
+		//每个分类取的商品数
+		$getNum = [6, 2, 4];
+
+		foreach ($category as $key => $val) {
+			$id = $val['id'];
+			//当前分类下有哪些商品id,并组合商品信息
+			$categoryGoodsIdList = isset($categoryGoods[$id]) ? $categoryGoods[$id] : [];
+			$info = [];
+			if (!empty($categoryGoodsIdList)) {
+				$currentGetNum = isset($getNum[$key]) ? $getNum[$key] : 2;
+				$categoryGoodsIdList = array_slice($categoryGoodsIdList, 0, $currentGetNum);
+				foreach ($categoryGoodsIdList as $currentGoodsId) {
+					if (isset($goodsInfoList[$currentGoodsId])) {
+						$info[] = $goodsInfoList[$currentGoodsId];
+					}
+				}
+			}
+			$category[$key]['goodsInfoList'] = $info;
+		}
+		return $category;
+	}
+	
 }