Sfoglia il codice sorgente

分类下的商品

shish 6 anni fa
parent
commit
911cc546b2

+ 9 - 0
app/business/web/index.php

@@ -1,5 +1,14 @@
 <?php
 
+//跨域临时解决方案 shish 2019.12.1
+header('Access-Control-Allow-Origin:*');
+// 响应类型
+header('Access-Control-Allow-Methods:*');
+// 响应头设置
+header('Access-Control-Allow-Headers:content-type,token,id,source,account');
+header("Access-Control-Request-Headers: Origin, X-Requested-With, content-Type, Accept, Authorization");
+
+
 require(__DIR__ . '/../../../vendor/autoload.php');
 
 require(__DIR__ . '/../../../env.php');// Environment

+ 16 - 2
app/client/controllers/CategoryController.php

@@ -3,18 +3,32 @@
 namespace client\controllers;
 
 use biz\goods\services\CategoryService;
+use biz\goods\services\GoodsCategoryService;
 use Yii;
 use yii\web\Controller;
 use common\components\util;
 
 class CategoryController extends BaseController
 {
-
+	
 	//获取分类 shish 2019.12.2
 	public function actionGetList()
 	{
 		$return = CategoryService::getEnableList();
 		util::success($return);
 	}
-
+	
+	//取分类下商品 shish 2019.12.2
+	public function actionGetGoodsList()
+	{
+		$categoryId = Yii::$app->request->get('categoryId');
+		//没有分类默认取商家第一个分类
+		$categoryId = !empty($categoryId) ? $categoryId : CategoryService::getTopCategoryId();
+		$where = [];
+		$where['merchantId'] = $this->merchantId;
+		$where['cId'] = $categoryId;
+		$data = GoodsCategoryService::getGoodsList($where);
+		util::success($data);
+	}
+	
 }

+ 8 - 0
app/client/web/index.php

@@ -8,6 +8,14 @@ header('Access-Control-Allow-Methods:*');
 header('Access-Control-Allow-Headers:content-type,token,id,source,account');
 header("Access-Control-Request-Headers: Origin, X-Requested-With, content-Type, Accept, Authorization");
 
+//调用使用的输出方法
+function dd($out)
+{
+	echo '<pre>';
+	print_r($out);
+	Yii::$app->end();
+}
+
 require(__DIR__ . '/../../../vendor/autoload.php');
 
 require(__DIR__ . '/../../../env.php');// Environment

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

@@ -1,6 +1,8 @@
 <?php
 
 namespace biz\goods\classes;
+
+use common\components\business;
 use Yii;
 use biz\base\classes\BaseClass;
 
@@ -9,4 +11,15 @@ class GoodsCategoryClass extends BaseClass
 	
 	public static $baseFile = '\biz\goods\models\GoodsCategory';
 	
+	//组装商品基本信息 shish 2019.12.2
+	public static function groupGoodsBaseInfo($list)
+	{
+		foreach ($list as $key => $val) {
+			//大中小图片转化
+			$val = business::formatGoodsImg($val);
+			$list[$key] = $val;
+		}
+		return $list;
+	}
+	
 }

+ 9 - 0
biz/goods/services/CategoryService.php

@@ -27,5 +27,14 @@ 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');
+		$id = isset($cat['id']) ? $cat['id'] : 0;
+		return $id;
+	}
 	
 }

+ 11 - 42
biz/goods/services/GoodsCategoryService.php

@@ -3,7 +3,6 @@
 namespace biz\goods\services;
 
 use biz\base\services\BaseService;
-
 use biz\goods\classes\GoodsCategoryClass;
 use Yii;
 use linslin\yii2\curl;
@@ -13,48 +12,18 @@ class GoodsCategoryService extends BaseService
 	
 	public static $baseFile = '\biz\goods\classes\GoodsCategoryClass';
 	
-	//分页取分类下的商品,包括商品明细 shish 2019.8.28
-	public static function getGoodsList($categoryId)
-	{
-		$select = '*';
-		$order = 'inTurn desc';
-		$merchantId = Yii::$app->params['merchantId'];
-		$where = ['merchantId' => $merchantId, 'cId' => $categoryId];
-		$return = self::getList($select, $where, $order);
-		$list = $return['list'];
-		$ids = array_column($list, 'gId');
-		$goodsList = [];
-		if (!empty($ids)) {
-			$goodsList = GoodsService::getByIds($ids);
-			foreach ($goodsList as $key => $val) {
-				//取出较小的封面图 shish 2019.8.3
-				$url = $val['cover'];
-				$pos = strrpos($url, '.');
-				$pre = substr($url, 0, $pos);
-				$ext = substr($url, ($pos + 1));
-				$middleCover = $pre . '_200.' . $ext;
-				$goodsList[$key]['middleCover'] = $middleCover;
-			}
-		}
-		$return['list'] = $goodsList;
-		return $return;
-	}
-
-	//取出商家分类下的商品id
-	public static function getCategoryIdList()
+	//分类下的商品 shish 2019.12.2
+	public static function getGoodsList($where)
 	{
-		$merchantId = Yii::$app->params['merchantId'];
-		$all = GoodsCategoryClass::getAllByCondition(['merchantId' => $merchantId], 'inTurn DESC');
-		if (empty($all)) {
-			return [];
-		}
-		$ids = [];
-		foreach ($all as $category) {
-			$cId = $category['cId'];
-			$gId = $category['gId'];
-			$ids[$cId][] = $gId;
+		$data = GoodsCategoryClass::getList('*', $where, 'inTurn DESC');
+		$list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
+		if (empty($list)) {
+			return $data;
 		}
-		return $ids;
+		$ids = array_column($list, 'gId');
+		$list = GoodsService::getAllByCondition(['id' => ['in', $ids]], 'inTurn DESC', 'goodsName,shopImg,price,sold');
+		$data['list'] = GoodsCategoryClass::groupGoodsBaseInfo($list);
+		return $data;
 	}
-
+	
 }

+ 31 - 0
common/components/business.php

@@ -33,4 +33,35 @@ class business
 		return $object;
 	}
 	
+	//对商品表shopImg字段的转化 shish 2019.12.2
+	public static function formatGoodsImg($info)
+	{
+		$shopImg = isset($info['shopImg']) ? $info['shopImg'] : [];
+		unset($info['shopImg']);
+		//没有保存图片返回空
+		if (empty($shopImg)) {
+			$info['imgList'] = [];
+			return $info;
+		}
+		$arr = json_decode($shopImg, true);
+		//图片内容转化失败返回空
+		if (is_array($arr) == false) {
+			$info['imgList'] = [];
+			return $info;
+		}
+		$imgList = [];
+		$prefixImgUrl = Yii::$app->params['imgUrl'];
+		foreach ($arr as $imgUrl) {
+			$pos = strrpos($imgUrl, '.');
+			$pre = substr($imgUrl, 0, $pos);
+			$ext = substr($imgUrl, ($pos + 1));
+			$middleImg = $pre . '_200.' . $ext;
+			$normal = $prefixImgUrl . $imgUrl;
+			$middle = $prefixImgUrl . $middleImg;
+			$imgList[] = ['normal' => $normal, 'middle' => $middle];
+		}
+		$info['imgList'] = $imgList;
+		return $info;
+	}
+	
 }

+ 3 - 0
sql.txt

@@ -15,6 +15,9 @@ ALTER TABLE xhCategory ADD `status` TINYINT NOT NULL DEFAULT 0 COMMENT '状态 0
 ALTER TABLE xhCategory DROP INDEX `indexShow`;
 ALTER TABLE xhCategory DROP INDEX `menuShow`;
 ALTER TABLE xhCategory ADD INDEX merchantId(merchantId,`status`);
+ALTER TABLE xhGoodsCategory DROP INDEX merchantId;
+ALTER TABLE xhGoodsCategory ADD INDEX merchantId(merchantId,cId);
+ALTER TABLE xhGoods DROP COLUMN `cover`;
 
 2019.12.2 13:00 shish 广告管理
 DROP TABLE IF EXISTS xhAd;