Goods.php 3.68 KB
<?php
/**
 * 商品相关
 */
header("Content-type: text/html; charset=utf-8");

class YHMApi_App_V2_Goods extends YHMApi_App_V1_Goods
{
	/**
	 * 获取品类列表数据
	 * 
	 * @return json
	 */
	public static function getCategoryList(array $params, $fields = '*') 
	{
		$categoryList = YHMProduct_Models_Category_Client::getCategoryList(3, 0, 1000);
		$key = 'getCategoryList_key';
		$list = Q_Cache::factory('Memcached')->get($key);
		if(empty($list)) 
		{
			foreach ( $categoryList as $category) 
			{
				// 商品父类
				if (empty ( $category ['parent_id'] )) 
				{
					$category ['sub_class'] = array ();
					if (empty ( $list [$category ['id']] )) 
					{
						$list [$category ['id']] = array ();
					}
					$list [$category ['id']] += $category;
				} 
				else 
				{
					$list [$category ['parent_id']] ['sub_class'] [] = $category;
				}
			}
			$list = array_values($list);
			$list = Q_Cache::factory('Memcached')->set($key, $list, 86400);
		}
		return self::result(200, '品类列表.', $list);
	}
   
	/**
	 * 获取首页最新商品
	 * 
	 * @param array $params
	 * @param string $fields
	 */
	public static function getNewestIndexList(array $params, $fields = '*')
	{
		if(!isset($params ['num']))
		{
			$params ['num'] = 8;
		}
		if(!isset($params ['offset']))
		{
			$params ['offset'] = 0;
		}
		$params ['viewNum'] = $params ['num'];
		$params ['page'] = intval($params ['offset'] / $params ['num']) + 1;
		if (!empty($params['uid'])) 
		{
			$params['searchuid'] = $params['uid'];
		}
		$params ['stocknumber'] = 1; //库存数大于1的商品
		$params ['status'] = 1;
		$resultList = array();
		try {
			$searchObj = new YHMSearch_Goods ();
			$skcList = $searchObj->getList($params);
			if (!empty($skcList)) 
			{
				foreach ($skcList as $skc => $goodsName)
				{
					$productInfo = YHMProduct_Models_Goods_Client::getOneByProductSkc($skc);
					if(!empty($productInfo))
					{
						$goodsImages = YHMProduct_Models_Goodsimages_Client::getByProductSkc($skc);
						// 是否收藏商品
						if (!empty($params ['uid'])) 
						{
							$favorite = YHMPassport_Models_Favorite_Goods_Client::findIsFavoriteBySkc($params ['uid'], $params ['product_skc']);
						}
						$favorite_num = YHMPassport_Models_Favorite_Goods_Client::getFavoriteTotal($skc);
						$is_favorite = 'N';
						if (!empty($favorite)) {
							$is_favorite = 'Y';
						}
						//商品描述 goods_describe
						$goods_describe = YHMProduct_Models_Goods_Client::getGoodsDescribeBySkc($skc);
						$imageList = array();
						foreach($goodsImages as $goodsImage)
						{
							$imageList[] = array(
									'is_default' => $goodsImage['is_default'],
									'image_url'=> YHMUpload_Images::template($goodsImage ['image_path'], 'yhfair-product'),
									'id' => $goodsImage ['id'],
							);
						}
						$is_deleted = 'N';
						if ($productInfo['status'] == 0) 
						{
							$is_deleted = 'Y';
						}
						$storeInfo = self::_getStoreForList($productInfo['store_id'] , false);
						unset($storeInfo['newest_goods_list']);
						$resultList[] = array(
								'image_list' => $imageList,
								'product_skc' => $skc,
								'is_favorite' => $is_favorite, // 是否已收藏,
								'favorite_num'=> $favorite_num,//收藏数
								'area_code'=> $productInfo['area_code'],
								'goods_name'=> $productInfo['goods_name'],
								'goods_describe' => $goods_describe['goods_describe'],
								'create_time' => $productInfo['create_time'],
								'sale_price' => $productInfo['sale_price'],
								'is_deleted'  => $is_deleted)
						         + $storeInfo;
					}
				}
			}
		} catch (Exception $e) 
		{
			return self::result(500, '搜索服务异常');
		}
		return self::result(200, '首页最新商品', $resultList);
	}
}