public function get($f3) { global $smarty; // 生成 smarty 的缓存 id $smartyCacheId = 'Ajax|Category'; // 开启并设置 smarty 缓存时间 enableSmartyCache(true, bzf_get_option_value('smarty_cache_time_ajax_category')); if ($smarty->isCached('ajax_category.tpl', $smartyCacheId)) { goto out_display; } // 取得商品分类树形结构 $goodsCategoryService = new GoodsCategoryService(); $goodsCategoryTreeArray = $goodsCategoryService->fetchCategoryTreeArray(0); $smarty->assign('goodsCategoryTreeArray', $goodsCategoryTreeArray); out_display: $f3->expire(600); // 让客户端缓存 10 分钟 $smarty->display('ajax_category.tpl', $smartyCacheId); }
public function get($f3) { global $smarty; $goodsCategoryService = new GoodsCategoryService(); $goodsCategoryTreeArray = $goodsCategoryService->fetchCategoryTreeArray(0, true); if (empty($goodsCategoryTreeArray)) { goto out_display; } $goodsCategoryFlatArray = array(); $this->getCategoryFlatArray($goodsCategoryTreeArray, $goodsCategoryFlatArray); $smarty->assign('goodsCategoryFlatArray', $goodsCategoryFlatArray); $categoryGoodsCountArray = $goodsCategoryService->calcCategoryGoodsCount(); $categoryIdToGoodsCountArray = array(); foreach ($categoryGoodsCountArray as $categoryItem) { $categoryIdToGoodsCountArray[$categoryItem['cat_id']] = $categoryItem['goods_count']; } $smarty->assign('categoryIdToGoodsCountArray', $categoryIdToGoodsCountArray); out_display: $smarty->display('goods_category.tpl'); }
public function get($f3) { global $smarty; $smartyCacheId = 'EtaoFeed|' . md5(__NAMESPACE__ . '\\' . __CLASS__ . '_\\' . __METHOD__); // 判断是否有缓存 enableSmartyCache(true, 1800); // 缓存 30 分钟 if ($smarty->isCached('empty.tpl', $smartyCacheId)) { goto out_display; } // 取得商品分类树形结构 $goodsCategoryService = new GoodsCategoryService(); $goodsCategoryTreeArray = $goodsCategoryService->fetchCategoryTreeArray(0); $currentStamp = Time::localTimeStr(); $sellerId = EtaoFeedPlugin::getOptionValue('etaofeed_seller_id'); // 生成商品分类 XML $categoryXmlList = ''; foreach ($goodsCategoryTreeArray as $goodsCategoryItem) { $categoryXmlList .= $this->getGoodsCategoryXml($goodsCategoryItem); } $apiXml = <<<XML <?xml version="1.0" encoding="utf-8" ?> <root> <version>1.0</version> <modified>{$currentStamp}</modified> <seller_id>{$sellerId}</seller_id> <seller_cats>{$categoryXmlList}</seller_cats> </root> XML; $smarty->assign('outputContent', $apiXml); out_display: header('Content-Type:text/xml;charset=utf-8'); header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 //查询信息 $smarty->display('empty.tpl', $smartyCacheId); }
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['g.category_id'] = $validator->required('商品分类不能为空')->digits('分类id非法')->min(1, true, '分类id非法')->filter('ValidatorIntValue')->validate('category_id'); // 这里支持多品牌查询 $searchFormQuery['g.brand_id'] = array('=', $validator->validate('brand_id')); // 价格区间查询 $shopPriceMin = $validator->filter('ValidatorFloatValue')->validate('shop_price_min'); $shopPriceMin = null == $shopPriceMin ? null : Money::toStorage($shopPriceMin); $shopPriceMax = $validator->filter('ValidatorFloatValue')->validate('shop_price_max'); $shopPriceMax = null == $shopPriceMax ? null : Money::toStorage($shopPriceMax); $searchFormQuery['g.shop_price'] = array($shopPriceMin, $shopPriceMax); // 属性过滤 $filter = $validator->validate('filter'); // 排序 $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('g.sort_order', 'desc'); $orderByParam[] = array('g.goods_id', 'desc'); // 参数验证 if (!$this->validate($validator) || empty($searchFormQuery)) { goto out_fail; } $pageNo = isset($pageNo) && $pageNo > 0 ? $pageNo : 0; $pageSize = 45; // 每页固定显示 45 个商品 // 生成 smarty 的缓存 id $smartyCacheId = 'Goods|Category|' . md5(json_encode($searchFormQuery) . json_encode($orderByParam) . '_' . $filter . '_' . $pageNo . '_' . $pageSize); // 开启并设置 smarty 缓存时间 enableSmartyCache(true, bzf_get_option_value('smarty_cache_time_goods_search')); if ($smarty->isCached('goods_category.tpl', $smartyCacheId)) { goto out_display; } $goodsCategoryService = new GoodsCategoryService(); $category = $goodsCategoryService->loadCategoryById($searchFormQuery['g.category_id'], 1800); if ($category->isEmpty()) { $this->addFlashMessage('分类[' . $searchFormQuery['category_id'] . ']不存在'); goto out_fail; } $smarty->assign('category', $category); $metaData = json_decode($category['meta_data'], true); $metaFilterArray = @$metaData['filterArray']; // 1. 我们需要在左侧显示分类层级结构 $goodsCategoryTreeArray = $goodsCategoryService->fetchCategoryTreeArray($category['parent_meta_id'], false, 1800); $smarty->assign('goodsCategoryTreeArray', $goodsCategoryTreeArray); /** * 构造 Filter 数组,结构如下 * * array( * '商品品牌' => array( * filterKey => 'brand_id' * filterValueArray => array( array(value=>'13', text=>'品牌1'), ...) * ), * '颜色' => array( * filterKey => 'filter', * filterValueArray => array( array(value=>'13', text=>'品牌1'), ...) * ) * ) * */ $goodsFilterArray = array(); // filter 查询在这个条件下进行 $goodsFilterQueryCond = array_merge($this->searchExtraCondArray, array(array('g.category_id', '=', $searchFormQuery['g.category_id']))); // 2. 商品品牌查询 $goodsBrandIdArray = SearchHelper::search(SearchHelper::Module_Goods, 'distinct(g.brand_id)', array_merge($goodsFilterQueryCond, array(array('g.brand_id > 0'))), null, 0, 0); $brandIdArray = array_map(function ($elem) { return $elem['brand_id']; }, $goodsBrandIdArray); if (!empty($brandIdArray)) { $goodsBrandService = new GoodsBrandService(); $goodsBrandArray = $goodsBrandService->fetchBrandArrayByIdArray(array_unique(array_values($brandIdArray))); $filterBrandArray = array(); foreach ($goodsBrandArray as $brand) { $filterBrandArray[] = array('value' => $brand['brand_id'], 'text' => $brand['brand_name']); } if (!empty($filterBrandArray)) { $goodsFilterArray['品牌'] = array('filterKey' => 'brand_id', 'filterValueArray' => $filterBrandArray); } } // 3. 查询属性过滤 if (!empty($metaFilterArray)) { $goodsTypeService = new GoodsTypeService(); foreach ($metaFilterArray as $filterItem) { $goodsTypeAttrItem = $goodsTypeService->loadGoodsTypeAttrItemById($filterItem['attrItemId']); if ($goodsTypeAttrItem->isEmpty()) { continue; } // 取得商品属性值列表 $goodsAttrItemValueArray = SearchHelper::search(SearchHelper::Module_GoodsAttrGoods, 'min(ga.goods_attr_id) as goods_attr_id, ga.attr_item_value', array_merge($goodsFilterQueryCond, array(array('ga.attr_item_id', '=', $filterItem['attrItemId']))), null, 0, 0, 'ga.attr_item_value'); if (!empty($goodsAttrItemValueArray)) { $filterValueArray = array(); foreach ($goodsAttrItemValueArray as $itemValue) { $filterValueArray[] = array('value' => $itemValue['goods_attr_id'], 'text' => $itemValue['attr_item_value']); } $goodsFilterArray[$goodsTypeAttrItem['meta_name']] = array('filterKey' => 'filter', 'filterValueArray' => $filterValueArray); } else { // 如果这个属性完全没有值(没有一个商品设过任何值),我们弄一个空的 $goodsFilterArray[$goodsTypeAttrItem['meta_name']] = array('filterKey' => 'filter', 'filterValueArray' => array()); } } } // 赋值给模板 if (!empty($goodsFilterArray)) { $smarty->assign('goodsFilterArray', $goodsFilterArray); } // 4. 商品查询 if (!empty($metaFilterArray)) { // 构造 attrItemId $metaFilterTypeIdArray = array(); foreach ($metaFilterArray as $metaFilterItem) { $metaFilterTypeIdArray[] = $metaFilterItem['attrItemId']; } // 构造 filter 参数,注意 filter 参数在 GoodsGoodsAttr 中具体解析 // 合并查询参数 $searchParamArray = array_merge(QueryBuilder::buildSearchParamArray($searchFormQuery), $this->searchExtraCondArray, array(array('ga.filter', implode('.', $metaFilterTypeIdArray), $filter))); } else { // 合并查询参数 $searchParamArray = array_merge(QueryBuilder::buildSearchParamArray($searchFormQuery), $this->searchExtraCondArray); } $totalCount = SearchHelper::count(SearchHelper::Module_GoodsGoodsAttr, $searchParamArray); if ($totalCount <= 0) { goto out_display; // 没有商品,直接显示 } // 页号可能是用户乱输入的,我们需要检查 if ($pageNo * $pageSize >= $totalCount) { goto out_fail; // 返回首页 } $goodsArray = SearchHelper::search(SearchHelper::Module_GoodsGoodsAttr, 'g.goods_id, g.cat_id, g.goods_sn, g.goods_name, g.brand_id, g.goods_number, g.market_price' . ', g.shop_price, g.suppliers_id, g.virtual_buy_number, g.user_buy_number, g.user_pay_number' . ', (g.virtual_buy_number + g.user_pay_number) as total_buy_number', $searchParamArray, $orderByParam, $pageNo * $pageSize, $pageSize); if (empty($goodsArray)) { goto out_display; } $smarty->assign('goodsArray', $goodsArray); $smarty->assign('totalCount', $totalCount); $smarty->assign('pageNo', $pageNo); $smarty->assign('pageSize', $pageSize); // SEO 考虑,网页标题加上分类的名称 $smarty->assign('seo_title', $category['meta_name'] . ',' . $smarty->getTemplateVars('seo_title')); out_display: // 滑动图片广告 $goods_search_adv_slider = json_decode(bzf_get_option_value('goods_search_adv_slider'), true); if (!empty($goods_search_adv_slider)) { $smarty->assign('goods_search_adv_slider', $goods_search_adv_slider); } $smarty->display('goods_category.tpl', $smartyCacheId); return; out_fail: // 失败从这里返回 RouteHelper::reRoute($this, '/'); // 返回首页 }
/** * 列出整个商品分类树 * * @param $f3 */ public function ListCategoryTree($f3) { // 检查缓存 $cacheKey = md5(__NAMESPACE__ . '\\' . __CLASS__ . '\\' . __METHOD__); if ($f3->get('GET[nocache]')) { goto nocache; } $categoryTreeDisplayArray = $f3->get($cacheKey); if (!empty($categoryTreeDisplayArray)) { goto out; } nocache: // 没有缓存数据 $categoryService = new CategoryService(); $categoryTreeArray = $categoryService->fetchCategoryTreeArray(0, true); //构造显示数组 $categoryTreeDisplayArray = array(); function buildTreeDisplay(&$categoryDisplayArray, $categoryArray, $namePrefix) { foreach ($categoryArray as $categoryItem) { $categoryItem['meta_name'] = $namePrefix . $categoryItem['meta_name'] . ($categoryItem['meta_status'] ? '' : '(不显示)'); $categoryDisplayArray[] = $categoryItem; if (isset($categoryItem['child_list'])) { buildTreeDisplay($categoryDisplayArray, $categoryItem['child_list'], $namePrefix . '---------->'); } } } buildTreeDisplay($categoryTreeDisplayArray, $categoryTreeArray, ''); $f3->set($cacheKey, $categoryTreeDisplayArray, 300); //缓存 5 分钟 out: if (!$f3->get('GET[nocache]')) { $f3->expire(60); // 客户端缓存 1 分钟 } Ajax::header(); echo Ajax::buildResult(null, null, $categoryTreeDisplayArray); }
public function get($f3) { global $smarty; // 首先做参数合法性验证 $validator = new Validator($f3->get('GET')); $pageNo = $validator->digits('pageNo 参数非法')->min(0, true, 'pageNo 参数非法')->validate('pageNo'); // 搜索参数数组 $searchFormQuery = array(); $searchKeywords = $validator->validate('keywords'); $searchFormQuery['g.goods_name'] = $searchKeywords; // 这里支持多品牌查询 $searchFormQuery['g.brand_id'] = array('=', $validator->validate('brand_id')); // 价格区间查询 $shopPriceMin = $validator->filter('ValidatorFloatValue')->validate('shop_price_min'); $shopPriceMin = null == $shopPriceMin ? null : Money::toStorage($shopPriceMin); $shopPriceMax = $validator->filter('ValidatorFloatValue')->validate('shop_price_max'); $shopPriceMax = null == $shopPriceMax ? null : Money::toStorage($shopPriceMax); $searchFormQuery['g.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('g.sort_order', 'desc'); $orderByParam[] = array('g.goods_id', 'desc'); // 参数验证 if (!$this->validate($validator) || empty($searchFormQuery)) { goto out_fail; } $pageNo = isset($pageNo) && $pageNo > 0 ? $pageNo : 0; $pageSize = 45; // 每页固定显示 45 个商品 // 生成 smarty 的缓存 id $smartyCacheId = 'Goods|Search|' . md5(json_encode($searchFormQuery) . json_encode($orderByParam) . '_' . $pageNo . '_' . $pageSize); // 开启并设置 smarty 缓存时间 enableSmartyCache(true, bzf_get_option_value('smarty_cache_time_goods_search')); if ($smarty->isCached('goods_search.tpl', $smartyCacheId)) { goto out_display; } $goodsCategoryService = new GoodsCategoryService(); // 1. 我们需要在左侧显示分类层级结构 $goodsCategoryTreeArray = $goodsCategoryService->fetchCategoryTreeArray(0, false, 1800); $smarty->assign('goodsCategoryTreeArray', $goodsCategoryTreeArray); /** * 构造 Filter 数组,结构如下 * * array( * '商品品牌' => array( * filterKey => 'brand_id' * filterValueArray => array( array(value=>'13', text=>'品牌1'), ...) * ), * '颜色' => array( * filterKey => 'filter', * filterValueArray => array( array(value=>'13', text=>'品牌1'), ...) * ) * ) * */ $goodsFilterArray = array(); // filter 查询在这个条件下进行 $goodsFilterQueryCond = array_merge(QueryBuilder::buildSearchParamArray(array('g.goods_name' => $searchKeywords)), $this->searchExtraCondArray); // 2. 商品品牌查询 $goodsBrandIdArray = SearchHelper::search(SearchHelper::Module_Goods, 'distinct(g.brand_id)', array_merge($goodsFilterQueryCond, array(array('g.brand_id > 0'))), null, 0, 0); $brandIdArray = array_map(function ($elem) { return $elem['brand_id']; }, $goodsBrandIdArray); if (!empty($brandIdArray)) { $goodsBrandService = new GoodsBrandService(); $goodsBrandArray = $goodsBrandService->fetchBrandArrayByIdArray(array_unique(array_values($brandIdArray))); $filterBrandArray = array(); foreach ($goodsBrandArray as $brand) { $filterBrandArray[] = array('value' => $brand['brand_id'], 'text' => $brand['brand_name']); } if (!empty($filterBrandArray)) { $goodsFilterArray['品牌'] = array('filterKey' => 'brand_id', 'filterValueArray' => $filterBrandArray); } } if (!empty($goodsFilterArray)) { $smarty->assign('goodsFilterArray', $goodsFilterArray); } // 3. 商品属性过滤 TODO: 等以后扩展,看看 Search 怎么做属性过滤 // 4. 商品查询 // 构造 filter 参数,注意 filter 参数在 GoodsGoodsAttr 中具体解析 // 合并查询参数 $searchParamArray = array_merge(QueryBuilder::buildSearchParamArray($searchFormQuery), $this->searchExtraCondArray); $totalCount = SearchHelper::count(SearchHelper::Module_GoodsGoodsAttr, $searchParamArray); if ($totalCount <= 0) { goto out_display; // 没有商品,直接显示 } // 页号可能是用户乱输入的,我们需要检查 if ($pageNo * $pageSize >= $totalCount) { goto out_fail; // 返回首页 } $goodsArray = SearchHelper::search(SearchHelper::Module_GoodsGoodsAttr, 'g.goods_id, g.cat_id, g.goods_sn, g.goods_name, g.brand_id, g.goods_number, g.market_price' . ', g.shop_price, g.suppliers_id, g.virtual_buy_number, g.user_buy_number, g.user_pay_number' . ', (g.virtual_buy_number + g.user_pay_number) as total_buy_number', $searchParamArray, $orderByParam, $pageNo * $pageSize, $pageSize); if (empty($goodsArray)) { goto out_display; } $smarty->assign('goodsArray', $goodsArray); $smarty->assign('totalCount', $totalCount); $smarty->assign('pageNo', $pageNo); $smarty->assign('pageSize', $pageSize); // SEO 考虑,网页标题加上分类的名称 $smarty->assign('seo_title', '商品搜索,' . $searchKeywords . ',' . $smarty->getTemplateVars('seo_title')); out_display: // 滑动图片广告 $goods_search_adv_slider = json_decode(bzf_get_option_value('goods_search_adv_slider'), true); if (!empty($goods_search_adv_slider)) { $smarty->assign('goods_search_adv_slider', $goods_search_adv_slider); } $smarty->display('goods_search.tpl', $smartyCacheId); return; out_fail: // 失败从这里返回 RouteHelper::reRoute($this, '/'); // 返回首页 }