/** * 取得 goods 和 goods_promote 的 join 结果,同时取得 goods 对应的所有商品 * 由于这个函数的代价非常大(几千个商品,上万个图片),所以我们一定要做缓存 * * @return array array('goods' => $goodsArray, 'goodsIdToGalleryArray' => $goodsIdToGalleryArray) * * @param array $queryCondArray 查询条件 * @param string $sort * @param int $offset * @param int $limit * @param int $ttl 缓存时间 * * @return array */ public function fetchGoodsGalleryPromote($queryCondArray, $sort, $offset, $limit, $ttl = 0) { // 首先做参数验证 $validator = new Validator(array('sort' => $sort, 'offset' => $offset, 'limit' => $limit, 'ttl' => $ttl)); $offset = $validator->digits()->min(0)->validate('offset'); $limit = $validator->digits()->min(0)->validate('limit'); $ttl = $validator->digits()->min(0)->validate('ttl'); $sort = $validator->validate('sort'); // 查询商品信息 $condArray = array(); $condArray[] = array('g.goods_id = gp.goods_id'); $formQuery = array(); $formQuery['is_delete'] = 0; $formQuery['is_on_sale'] = 1; $formQuery['is_alone_sale'] = 1; if (!empty($queryCondArray)) { $condArray = array_merge($condArray, $queryCondArray); } $condArray = array_merge($condArray, QueryBuilder::buildQueryCondArray($formQuery)); $goodsArray = $this->_fetchArray(array('goods' => 'g', 'goods_promote' => 'gp'), 'g.system_tag_list, g.goods_name, g.goods_name_short, g.brand_id, g.goods_number, g.market_price, g.shop_price, g.cat_id' . ', g.sort_order, g.goods_brief, g.seo_keyword, g.goods_notice , g.virtual_buy_number, g.user_pay_number' . ', gp.* ', $condArray, array('order' => $sort), $offset, $limit, $ttl); // options // 如果没有数据就退出 if (empty($goodsArray)) { return array(); } // 查询商品图片 $goodsIdArray = array(); foreach ($goodsArray as $goodsItem) { $goodsIdArray[] = $goodsItem['goods_id']; } $goodsGalleryService = new GoodsGalleryService(); $goodsGalleryArray = $goodsGalleryService->fetchGoodsGalleryArrayByGoodsIdArray($goodsIdArray, $ttl); // 建立 goods_id --> goods_gallery 的反查表 $goodsIdToGalleryArray = array(); foreach ($goodsGalleryArray as $goodsGalleryItem) { if (!isset($goodsIdToGalleryArray[$goodsGalleryItem['goods_id']])) { $goodsIdToGalleryArray[$goodsGalleryItem['goods_id']] = array(); } $goodsIdToGalleryArray[$goodsGalleryItem['goods_id']][] = $goodsGalleryItem; } return array('goods' => $goodsArray, 'goodsIdToGalleryArray' => $goodsIdToGalleryArray); }
public function get($f3) { global $smarty; // 首先做参数合法性验证 $validator = new Validator($f3->get('GET')); $pageNo = $validator->digits('pageNo 参数非法')->min(0, true, 'pageNo 参数非法')->validate('pageNo'); // 搜索参数数组 $searchFormQuery = array(); $searchFormQuery['category_id'] = $validator->digits('分类id非法')->min(1, true, '分类id非法')->filter('ValidatorIntValue')->validate('category_id'); $searchFormQuery['suppliers_id'] = $validator->digits('供货商id非法')->min(1, true, '供货商id非法')->filter('ValidatorIntValue')->validate('suppliers_id'); $searchFormQuery['goods_name'] = $validator->validate('goods_name'); // 价格区间查询 $shopPriceMin = $validator->filter('ValidatorFloatValue')->validate('shop_price_min'); $shopPriceMax = $validator->filter('ValidatorFloatValue')->validate('shop_price_max'); $searchFormQuery['shop_price'] = array($shopPriceMin, $shopPriceMax); // 排序 $orderBy = $validator->oneOf(array('', 'total_buy_number', 'shop_price', 'add_time'))->validate('orderBy'); $orderDir = $validator->oneOf(array('', 'asc', 'desc'))->validate('orderDir'); $orderByParam = array(); if (!empty($orderBy)) { $orderByParam = array(array($orderBy, $orderDir)); } //增加一些我们的缺省排序 $orderByParam[] = array('sort_order', 'desc'); $orderByParam[] = array('goods_id', 'desc'); // 参数验证 if (!$this->validate($validator) || empty($searchFormQuery)) { goto out_fail; } $pageNo = isset($pageNo) && $pageNo > 0 ? $pageNo : 0; $pageSize = 10; // 每页固定显示 10 个商品 // 生成 smarty 的缓存 id $smartyCacheId = 'Goods|Search|' . md5(json_encode($searchFormQuery) . json_encode($orderByParam) . '_' . $pageNo . '_' . $pageSize); // 开启并设置 smarty 缓存时间 enableSmartyCache(true, MobileThemePlugin::getOptionValue('smarty_cache_time_goods_search')); if ($smarty->isCached('goods_search.tpl', $smartyCacheId)) { goto out_display; } // 合并查询参数 $searchParamArray = array_merge(QueryBuilder::buildSearchParamArray($searchFormQuery), $this->searchExtraCondArray); $totalCount = SearchHelper::count(SearchHelper::Module_Goods, $searchParamArray); if ($totalCount <= 0) { goto out_display; // 没有商品,直接显示 } // 页号可能是用户乱输入的,我们需要检查 if ($pageNo * $pageSize >= $totalCount) { goto out_fail; // 返回首页 } $goodsArray = SearchHelper::search(SearchHelper::Module_Goods, $this->searchFieldSelector, $searchParamArray, $orderByParam, $pageNo * $pageSize, $pageSize); if (empty($goodsArray)) { goto out_display; } // 取得 商品ID 列表 $goodsIdArray = array(); foreach ($goodsArray as $goodsItem) { $goodsIdArray[] = $goodsItem['goods_id']; } // 取得商品的图片 $goodsGalleryService = new GoodsGalleryService(); $goodsGalleryArray = $goodsGalleryService->fetchGoodsGalleryArrayByGoodsIdArray($goodsIdArray); $currentGoodsId = -1; $goodsThumbImageArray = array(); $goodsImageArray = array(); foreach ($goodsGalleryArray as $goodsGalleryItem) { if ($currentGoodsId == $goodsGalleryItem['goods_id']) { continue; //每个商品我们只需要一张图片,跳过其它的图片 } $currentGoodsId = $goodsGalleryItem['goods_id']; // 新的商品 id $goodsThumbImageArray[$currentGoodsId] = RouteHelper::makeImageUrl($goodsGalleryItem['thumb_url']); $goodsImageArray[$currentGoodsId] = RouteHelper::makeImageUrl($goodsGalleryItem['img_url']); } // 赋值给模板 $smarty->assign('totalCount', $totalCount); $smarty->assign('pageNo', $pageNo); $smarty->assign('pageSize', $pageSize); $smarty->assign('goodsArray', $goodsArray); $smarty->assign('goodsThumbImageArray', $goodsThumbImageArray); $smarty->assign('goodsImageArray', $goodsImageArray); out_display: $smarty->display('goods_search.tpl', $smartyCacheId); return; out_fail: // 失败从这里返回 RouteHelper::reRoute($this, '/'); // 返回首页 }
public function get($f3) { global $smarty; // 生成 smarty 的缓存 id $smartyCacheId = 'Mobile|Index'; // 开启并设置 smarty 缓存时间 enableSmartyCache(true, MobileThemePlugin::getOptionValue('smarty_cache_time_goods_index')); // 缓存页面 if ($smarty->isCached('mobile_index.tpl', $smartyCacheId)) { goto out_display; } $categoryLevel = 3; // 取得分类下面 3 层的商品 $categoryGoodsSzie = 4; // 每个分类下面取 4 个商品 // 取得分类列表 $categoryService = new CategoryService(); $categoryArray = $categoryService->fetchCategoryArray(0); //取得分类 // 取得商品列表 $categoryGoodsArray = array(); $goodsIdArray = array(); foreach ($categoryArray as $category) { // 分类下面 3 层子分类, 取 4 个商品 $goodsArray = $categoryService->fetchGoodsArray($category['meta_id'], $categoryLevel, PluginHelper::SYSTEM_MOBILE, 0, $categoryGoodsSzie); if (!empty($goodsArray)) { $categoryGoodsArray[$category['meta_id']] = $goodsArray; foreach ($goodsArray as $goodsItem) { $goodsIdArray[] = $goodsItem['goods_id']; } } } // 网站完全没有商品? 不缓存 if (empty($goodsIdArray)) { goto out_display; } // 取得商品的图片 $goodsGalleryService = new GoodsGalleryService(); $goodsGalleryArray = $goodsGalleryService->fetchGoodsGalleryArrayByGoodsIdArray($goodsIdArray); $currentGoodsId = -1; $goodsThumbImageArray = array(); $goodsImageArray = array(); foreach ($goodsGalleryArray as $goodsGalleryItem) { if ($currentGoodsId == $goodsGalleryItem['goods_id']) { continue; //每个商品我们只需要一张图片,跳过其它的图片 } $currentGoodsId = $goodsGalleryItem['goods_id']; // 新的商品 id $goodsThumbImageArray[$currentGoodsId] = RouteHelper::makeImageUrl($goodsGalleryItem['thumb_url']); $goodsImageArray[$currentGoodsId] = RouteHelper::makeImageUrl($goodsGalleryItem['img_url']); } // 赋值给模板 $smarty->assign('categoryArray', $categoryArray); $smarty->assign('categoryGoodsArray', $categoryGoodsArray); $smarty->assign('goodsThumbImageArray', $goodsThumbImageArray); $smarty->assign('goodsImageArray', $goodsImageArray); out_display: $smarty->assign('seo_title', $smarty->getTemplateVars('seo_title') . ',' . $f3->get('HOST')); $smarty->display('mobile_index.tpl', $smartyCacheId); }
private function getGoodsArrayXml($goodsArray) { $itemXmlList = ''; // 没有商品,退出 if (empty($goodsArray)) { goto out_output; } // 查询商品图片 $goodsIdArray = array(); foreach ($goodsArray as $goodsItem) { $goodsIdArray[] = $goodsItem['goods_id']; } $goodsGalleryService = new GoodsGalleryService(); $goodsGalleryArray = $goodsGalleryService->fetchGoodsGalleryArrayByGoodsIdArray($goodsIdArray); // 建立 goods_id --> goods_gallery 的反查表 $goodsIdToGalleryArray = array(); foreach ($goodsGalleryArray as $goodsGalleryItem) { if (!isset($goodsIdToGalleryArray[$goodsGalleryItem['goods_id']])) { $goodsIdToGalleryArray[$goodsGalleryItem['goods_id']] = array(); } $goodsIdToGalleryArray[$goodsGalleryItem['goods_id']][] = $goodsGalleryItem; } // 生成 商品列表 foreach ($goodsArray as $goodsItem) { $itemXmlList .= $this->getGoodsItemXml($goodsItem, $goodsIdToGalleryArray); } out_output: $apiXml = <<<XML <?xml version="1.0" encoding="utf-8" ?> <items> {$itemXmlList} </items> XML; return $apiXml; }