Explorar el Código

获取图库分类

shish hace 6 años
padre
commit
856f520964

+ 2 - 2
app/shop/controllers/PicController.php

@@ -21,9 +21,9 @@ class PicController extends BaseController
 		$categoryId = isset($get['categoryId']) && is_numeric($get['categoryId']) ? $get['categoryId'] : -1;
 		if ($categoryId != -1) {
 			$where['categoryId'] = $get['categoryId'];
-			$list = PicCategoryService::getPicList($where);
+			$list = PicCategoryService::getPicList($where,true);
 		} else {
-			$list = PicService::getPicList($where);
+			$list = PicService::getPicList($where,true);
 		}
 		util::success($list);
 	}

+ 17 - 0
biz/goods/classes/PicCategoryClass.php

@@ -14,4 +14,21 @@ class PicCategoryClass extends BaseClass
 	
 	public static $baseFile = '\biz\goods\models\PicCategory';
 	
+	//根据图库的所有分类
+	public static function getCategoryIdByPicId($id)
+	{
+		$picId = is_array($id) ? $id : [$id];
+		$list = self::getAllByCondition(['picId' => ['in', $picId]], null, 'picId,categoryId');
+		$data = [];
+		if (!empty($list)) {
+			foreach ($list as $key => $val) {
+				$picId = $val['picId'];
+				$categoryId = $val['categoryId'];
+				$data[$picId][] = $categoryId;
+			}
+			$data = is_array($id) ? $data : array_pop($data);
+		}
+		return $data;
+	}
+
 }

+ 13 - 5
biz/goods/classes/PicClass.php

@@ -11,18 +11,26 @@ use biz\base\classes\BaseClass;
 
 class PicClass extends BaseClass
 {
-
+	
 	public static $baseFile = '\biz\goods\models\Pic';
-
-	public static function groupPicBaseInfo($list)
+	
+	// $takeCategoryId 是否带上这个图片的所有分类
+	public static function groupPicBaseInfo($list, $takeCategoryId = false)
 	{
+		$categoryList = [];
+		if ($takeCategoryId) {
+			$picIds = array_column($list, 'id');
+			$categoryList = PicCategoryClass::getCategoryIdByPicId($picIds);
+		}
 		foreach ($list as $key => $val) {
 			$val = business::formatPic($val);
+			$id = $val['id'];
+			$val['categoryIdList'] = isset($categoryList[$id]) ? $categoryList[$id] : [];
 			$list[$key] = $val;
 		}
 		return $list;
 	}
-
+	
 	//排序最前面的值 shish 2019.12.8
 	public static function getTopNum($merchantId)
 	{
@@ -30,5 +38,5 @@ class PicClass extends BaseClass
 		$num = isset($top['inTurn']) ? $top['inTurn'] : 0;
 		return $num;
 	}
-
+	
 }

+ 2 - 2
biz/goods/services/PicCategoryService.php

@@ -26,9 +26,9 @@ class PicCategoryService extends BaseService
 	}
 	
 	//获取图库分类 shish 2019.12.8
-	public static function getPicHasCategory($id)
+	public static function getPicHasCategoryId($id)
 	{
-		$list = PicCategoryClass::getAllByCondition(['picId' => $id], null, '*', 'categoryId');
+		$list = PicCategoryClass::getAllByCondition(['picId' => $id], null, 'id', 'categoryId');
 		return !empty($list) ? array_keys($list) : [];
 	}
 

+ 7 - 7
biz/goods/services/PicService.php

@@ -9,20 +9,20 @@ use common\components\util;
 
 class PicService extends BaseService
 {
-
+	
 	public static $baseFile = '\biz\goods\classes\PicClass';
-
-	public static function getPicList($where)
+	
+	public static function getPicList($where, $takeCategoryId)
 	{
 		$data = PicClass::getList('*', $where, 'inTurn DESC,addTime DESC');
 		$list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
 		if (empty($list)) {
 			return $data;
 		}
-		$data['list'] = PicClass::groupPicBaseInfo($list);
+		$data['list'] = PicClass::groupPicBaseInfo($list, $takeCategoryId);
 		return $data;
 	}
-
+	
 	public static function addPic($data)
 	{
 		PicClass::addPic();
@@ -39,8 +39,8 @@ class PicService extends BaseService
 	public static function getPic($info)
 	{
 		$id = $info['id'];
-		$hasCategory = PicCategoryService::getPicHasCategory($id);
-		$info['categoryIdList'] = $hasCategory;
+		$hasCategoryId = PicCategoryService::getPicHasCategoryId($id);
+		$info['categoryIdList'] = $hasCategoryId;
 		$info = business::formatPic($info);
 		return $info;
 	}