Authored by whb

webplugin方式

<?php
namespace WebPlugin\DataProcess;
use WebPlugin\Images;
use WebPlugin\Helpers;
use WebPlugin\Cache;
use Configs\CacheConfig;
use LibModels\Web\Product\SearchData;
/**
* web版通用处理器
*/
class Process
{
/**
*
* @param array $data
* @param string $type (boys, girls, kids, lifestyle)
*/
public static function getContent(array &$data, $type = '')
{
// 组合处理数据
$result = self::mergeProcess($data, $type);
foreach ($data as $key => $val) {
$fun = $val['template_name'];
if (empty($val['data']) || ! is_callable("self::$fun")) {
continue;
}
// 单个处理数据
$build = self::$fun($val, $type);
if (! empty($build)) {
$result[$key] = $build;
unset($data[$key]);
}
}
return $result;
}
/**
* 热门分类处理
*
* @param array $data
* @param string $type
* @return array
*/
public static function hotCategory(array $data, $type)
{
$data = $data['data'];
$result = $temp = array();
$temp = array(
'name' => $data['name'],
'navs' => array(),
'tplrecommend' => array()
);
foreach ($data['navs']['list'] as $val) {
$temp['navs'][] = array(
'id' => '',
'href' => $val['url'],
'name' => $val['name']
);
}
foreach ($data['menuNav']['blocks'] as $val) {
$val['img'] = Images::getImageUrl($val['img'], 185, 76, 1);
$val['url'] = Helpers::transUrl($val['url'], $type);
$temp['tplrecommend']['keyword'][] = array(
'href' => $val['url'],
'name' => $val['title'],
'img' => $val['img']
);
}
foreach ($data['menuNav']['list'] as $val) {
if (empty($val['name']) && empty($val['url'])) {
continue;
}
$val['url'] = Helpers::transUrl($val['url'], $type);
$temp['tplrecommend']['category'][] = array(
'name' => $val['name'],
'href' => $val['url']
);
}
foreach ($data['imgs'] as $key => $val) {
$val['url'] = Helpers::transUrl($val['url'], $type);
if ($key == 0) {
$val['img'] = Images::getImageUrl($val['img'], 377, 504, 1); // 大图
$temp['tplrecommend']['brands'][] = array(
'href' => $val['url'],
'name' => $val['title'],
'img' => $val['img']
);
} else {
$val['img'] = Images::getImageUrl($val['img'], 185, 504, 1); // 小图
$temp['tplrecommend']['types'][] = array(
'href' => $val['url'],
'name' => $val['title'],
'img' => $val['img']
);
}
}
$result['recommend'] = $temp;
return $result;
}
/**
* banner处理
*
* @param array $data
* @param string $type
* @return array
*/
public static function focus(array $data, $type)
{
$result = array();
$temp = array();
$width = 1150;
$height = 450;
if ($data['focus_type'] == 1) {
foreach ($data['data'] as $val) {
$ret = array();
$val['src'] = Images::getImageUrl($val['src'], $width, $height, 1);
$val['url'] = Helpers::transUrl($val['url'], $type);
$ret = array (
'href' => $val['url'],
'img' => $val['src'],
);
if(!empty($val['bgColor'])) {
$ret['bgColor'] = $val['bgColor'];
}
$temp[] = $ret;
}
$result['slide']['list'] = $temp;
}
return $result;
}
/**
* boys焦点图处理
*
* @param array $data
* @param string $type
* @return array
*/
public static function recommend_content_three(array $data, $type)
{
$result = array();
$temp = array();
if ($data['template_name'] == 'recommend_content_three') {
foreach ($data['data']['big_image'] as $val) {
$width = 1150;
$height = 450;
$val['src'] = Images::getImageUrl($val['src'], $width, $height, 1);
$val['url'] = Helpers::transUrl($val['url'], $type);
$temp['list'][] = array(
'href' => $val['url'],
'img' => $val['src'],
// 'bgColor'=> $val[]
);
}
foreach ($data['data']['list'] as $val) {
$width = 138;
$height = 54;
$val['src'] = Images::getImageUrl($val['src'], $width, $height, 1);
$val['url'] = Helpers::transUrl($val['url'], $type);
$temp['pagination'][] = array(
'href' => $val['url'],
'img' => $val['src']
);
}
$result['slide'] = $temp;
}
return $result;
}
/**
* 广告位
*
* @param array $data
* @param string $type
* @return array
*/
public static function single_image(array $data, $type)
{
$result = array();
$temp = array();
if ($data['template_name'] == 'single_image') {
$temp = array(
'href' => '',
'img' => '',
'name' => ''
);
if (isset($data['data'][0])) {
$val = $data['data'][0];
$temp['img'] = Images::getImageUrl($val['src'], 1150, 129, 1);
$temp['href'] = $val['url'];
$temp['name'] = $val['title'];
$result['adbanner'] = $temp;
}
}
return $result;
}
/**
* 左右banner
*
* @param array $data
* @param string $type
*/
public static function debrisSlider(array $data, $type)
{
$data = $data['data'];
$result = array(
'debrisSlider' => array(
'left' => array(),
'center' => array(),
'right' => array()
)
);
foreach ($data['left'] as $val) {
$val['img'] = Images::getImageUrl($val['img'], 280, 265, 1);
$result['debrisSlider']['left'][] = array(
'img' => $val['img'],
'href' => $val['url'],
'name' => $val['title']
);
}
foreach ($data['center'] as $val) {
$val['img'] = Images::getImageUrl($val['img'], 570, 633, 1);
$result['debrisSlider']['center'][] = array(
'img' => $val['img'],
'href' => $val['url'],
'name' => $val['title']
);
}
foreach ($data['right'] as $val) {
$val['img'] = Images::getImageUrl($val['img'], 280, 449, 1);
$result['debrisSlider']['right'][] = array(
'img' => $val['img'],
'href' => $val['url'],
'name' => $val['title']
);
}
return $result;
}
/**
* 组合数据处理
*
* @param array $data
* @param string $type
* @return array
*/
public static function mergeProcess(array &$data, $type)
{
$result = array();
$mergeProcessList = array(
'mergeSinglehotData',
'mergePreferenceBrandsData',
'mergeAccordionData',
'mergeCategoryData',
'mergeNewReportData',
'mergeSinglehotData2',
'mergeHotCategoryData',
'mergeGirlkidsData',
'mergeHotBrandsData'
);
foreach ($data as $key => $val) {
foreach ($mergeProcessList as $merge) {
if (isset($data[$key])) {
$temp = self::$merge($key, $data, $type);
if (! empty($temp)) {
$result[$key] = $temp;
}
}
}
}
return $result;
}
/**
* 组合人气单品 [
* template: text & textNav & goods & floor ||
* textNav & goods & floor ||
* text & textNav & goods & app_icon_list
* ]
*
* @param string $key
* @param array $data
* @param string $type
* @return array
*/
public static function mergeSinglehotData($key, array &$data, $type)
{
$result = array();
if ((isset($data[$key]) && isset($data[$key + 2])) || (isset($data[$key]) && isset($data[$key + 3])))
{
$title = '';
$source = $goods = $floor = array();
if ($data[$key]['template_name'] == 'textNav' && $data[$key + 1]['template_name'] == 'goods'
&& $data[$key + 2]['template_name'] == 'floor')
{
$title = '人气单品 TOP100';
$source = $data[$key];
$goods = $data[$key + 1];
$floor = $data[$key + 2];
unset($data[$key], $data[$key + 1], $data[$key + 2]);
} else if ($data[$key]['template_name'] == 'text' && $data[$key + 1]['template_name'] == 'textNav'
&& $data[$key + 2]['template_name'] == 'goods' && $data[$key + 3]['template_name'] == 'floor')
{
$title = $data[$key]['data']['text'];
$source = $data[$key + 1];
$goods = $data[$key + 2];
$floor = $data[$key + 3];
unset($data[$key], $data[$key + 1], $data[$key + 2], $data[$key + 3]);
} else if ($data[$key]['template_name'] == 'text' && $data[$key + 1]['template_name'] == 'textNav'
&& $data[$key + 2]['template_name'] == 'goods' && $data[$key + 3]['template_name'] == 'app_icon_list')
{
$title = $data[$key]['data']['text'];
$source = $data[$key + 1];
$goods = $data[$key + 2];
$floor = $data[$key + 3];
unset($data[$key], $data[$key + 1], $data[$key + 2], $data[$key + 3]);
}
if (! empty($source) && ! empty($floor))
{
$result = array(
'singlehot' => array(
'name' => '',
'imgHot' => array(),
'brands' => array(),
'navs'=> array(),
)
);
// text模版
$result['singlehot']['name'] = $title;
//textNav模版
$result['singlehot']['navs'] = self::mergeNavProcess($source, $type);
// goods模版
$result['singlehot']['imgHot'] = self::mergeGoodsProcess($goods, $type);
// floor模版
foreach ($floor['data'] as $val) {
$val['src'] = Images::getImageUrl($val['src'], 185, 86, 1);
$val['url'] = Helpers::transUrl($val['url'], $type);
$result['singlehot']['brands'][] = array(
'href' => $val['url'],
'img' => $val['src'],
'name' => $val['title']
);
}
}
}
return $result;
}
/**
* 组合人气单品 [template: text & small_pic & floor ]
*
* @return array
*/
public static function mergeSinglehotData2($key, array &$data, $type)
{
$result = array();
if (isset($data[$key]) && isset($data[$key + 2])) {
if ($data[$key]['template_name'] == 'text' && $data[$key + 1]['template_name'] == 'small_pic'
&& $data[$key + 2]['template_name'] == 'floor')
{
$result = array(
'singlehot' => array(
'name' => '',
'imgHot' => array()
)
);
// text
$result['singlehot']['name'] = $data[$key]['data']['text'];
$len = 10;
$list = array();
// small_pic
foreach ($data[$key + 1]['data'] as &$val) {
$w = 378;
$h = 248;
$val['src'] = Images::getImageUrl($val['src'], $w, $h, 1);
}
//floor
foreach ($data[$key + 2]['data'] as &$val)
{
$w = 185;
$h = 248;
$val['src'] = Images::getImageUrl($val['src'], $w, $h, 1);
}
for ($i = 0; $i < $len; $i ++)
{
$pos = $i;
if ($i == 1)
{
$val = $data[$key + 1]['data'][0]; // 第二个是大图
}
else if ($i == $len - 1)
{
$val = $data[$key + 1]['data'][1]; // 最后一个是大图
}
else
{
if ($pos > 1) // 小图
{
$pos = $pos - 1;
}
$val = $data[$key + 2]['data'][$pos];
}
$result['singlehot']['imgHot'][] = array(
'href' => $val['url'],
'img' => $val['src'],
'name' => $val['alt']
);
}
unset($data[$key], $data[$key + 1], $data[$key + 2]);
}
}
return $result;
}
/**
* 组合优选品牌数据 [ template: text & focus & floor]
*
* @param string $key
* @param array $data
* @param string $type
* @return array
*/
public static function mergePreferenceBrandsData($key, array &$data, $type)
{
$result = array();
if (isset($data[$key]) && isset($data[$key + 2])) {
if ($data[$key]['template_name'] == 'text' && $data[$key + 1]['template_name'] == 'focus'
&& ($data[$key + 2]['template_name'] == 'floor' ||
$data[$key + 2]['template_name'] == 'app_icon_list'))
{
$result = array(
'preferenceBrands' => array(
'name' => '',
'imgBrand' => array(),
'brandUrl' => ''
)
);
// text模版
$result['preferenceBrands']['name'] = $data[$key]['data']['text'];
// focus模版
foreach ($data[$key + 1]['data'] as $val) {
if($type == 'boys') {
$width = 378; $height = 175;
} else{
$width = 320;$height = 430;
}
$val['src'] = Images::getImageUrl($val['src'], $width, $height, 1);
$val['url'] = Helpers::transUrl($val['url'], $type);
$result['preferenceBrands']['imgBrand'][] = array(
'href' => $val['url'],
'img' => $val['src']
);
}
// floor模版
$floor = array(
'logoBrand' => '',
'moreBrand' => ''
);
foreach ($data[$key + 2]['data'] as $val) {
$width = 185;
$height = 86;
$val['src'] = Images::getImageUrl($val['src'], $width, $height, 2);
$val['url'] = Helpers::transUrl($val['url'], $type);
$floor['logoBrand'][] = array(
'href' => $val['url'],
'img' => $val['src']
);
}
// textNav模版
if (isset($data[$key + 3]['data'][0]['url']) && $data[$key + 3]['template_name'] == 'textNav') {
$floor['moreBrand'] = $data[$key + 3]['data'][0]['url'];
unset($data[$key + 3]);
}
$type_key = sprintf("%s_%s", $type, $key + 2);
$cacheKey = sprintf("%s_%s", CacheConfig::KEY_INDEX_BRANDS_LIST_DATA, $type_key);
Cache::set($cacheKey, $floor, 7200);
$result['preferenceBrands']['brandUrl'] = '/common/getIndexResourceBrand?type=' . $type_key;
unset($data[$key], $data[$key + 1], $data[$key + 2]);
}
}
return $result;
}
/**
* 组合girlkids数据 [ template: text & textNav & goods]
*
* @param string $key
* @param array $data
* @param string $type
* @return array
*/
public static function mergeGirlkidsData($key, array &$data, $type)
{
$result = array();
if (isset($data[$key]) && isset($data[$key + 2]))
{
if ($data[$key]['template_name'] == 'text' && $data[$key + 2]['template_name'] == 'goods')
{
$result = array(
'girlkids' => array(
'name' => '',
'imgHot' => array()
)
);
// text
$result['girlkids']['name'] = $data[$key]['data']['text'];
// goods
$result['girlkids']['imgHot'] = self::mergeGoodsProcess($data[$key + 2], $type);
unset($data[$key], $data[$key + 1], $data[$key + 2]);
}
}
return $result;
}
/**
* 组合accordion数据 [ template: text & textNav & focus ]
*
* @param string $key
* @param array $data
* @param string $type
* @return array
*/
public static function mergeAccordionData($key, array &$data, $type)
{
$result = array();
if (isset($data[$key]) && isset($data[$key + 2]))
{
if ($data[$key]['template_name'] == 'text' && $data[$key + 1]['template_name'] == 'textNav'
&& $data[$key + 2]['template_name'] == 'focus')
{
$result = array(
'accordion' => array(
'name' => '',
'navs' => array(),
'slide' => array()
)
);
$result['accordion']['name'] = $data[$key]['data']['text'];
// textNav模版
if (! empty($data[$key + 1]['data'])) {
$result['accordion']['navs'] = self::mergeNavProcess($data[$key + 1], $type);
}
// focus模版
foreach ($data[$key + 2]['data'] as $val) {
$val['src'] = Images::getImageUrl($val['src'], 650, 400, 1);
$result['accordion']['slide'][] = array(
'href' => $val['url'],
'img' => $val['src'],
'name' => $val['title']
);
}
unset($data[$key], $data[$key + 1], $data[$key + 2]);
}
}
return $result;
}
/**
* 组合boys最新速报数据 [ template: text & single_image & floor & single_image]
*
* @param string $key
* @param array $data
* @param string $type
* @return array
*/
public static function mergeNewReportData($key, array &$data, $type)
{
$result = array();
if (isset($data[$key]) && isset($data[$key + 3]))
{
if ($data[$key]['template_name'] == 'text' && $data[$key + 1]['template_name'] == 'single_image'
&& $data[$key + 2]['template_name'] == 'floor' &&
$data[$key + 3]['template_name'] == 'single_image')
{
$result = array(
'newReport' => array(
'name' => '',
'list' => array()
)
);
// text
$result['newReport']['name'] = $data[$key]['data']['text'];
// single_image & floor & single_image
$list = array_merge($data[$key + 1]['data'], array_slice($data[$key + 2]['data'], 0, 6), $data[$key + 3]['data']);
foreach ($list as $listKey => $val) {
$width = 185;
$height = 248;
$w = 377;
$h = 504;
if ($listKey == 0 || $listKey == 7) {
$val['src'] = Images::getImageUrl($val['src'], $w, $h, 1);
$result['newReport']['list'][] = array(
'href' => $val['url'],
'img' => $val['src']
);
} else {
$val['src'] = Images::getImageUrl($val['src'], $width, $height, 1);
$result['newReport']['list'][] = array(
'href' => $val['url'],
'img' => $val['src']
);
}
}
unset($data[$key], $data[$key + 1], $data[$key + 2], $data[$key + 3]);
}
}
return $result;
}
/**
* 组合category数据 [template: text & textNav & floor ]
*
* @param array $data
* @param string $type
* @return array
*/
public static function mergeCategoryData($key, array &$data, $type)
{
$result = array();
if (isset($data[$key]) && isset($data[$key + 2])) {
if ($data[$key]['template_name'] == 'text' && $data[$key + 1]['template_name'] == 'textNav'
&& $data[$key + 2]['template_name'] == 'floor') {
$result = array(
'category' => array(
'name' => '',
'navs' => array(),
'list' => array()
)
);
// text模版
$result['category']['name'] = $data[$key]['data']['text'];
// textNav模版
if (! empty($data[$key + 1]['data'])) {
$result['category']['navs'] = self::mergeNavProcess($data[$key + 1], $type);
}
// floor模版
foreach ($data[$key + 2]['data'] as $pos => $val) {
$width = 185; $height = 510;
if($pos == 1) {
$width = 377; $height = 504;
}
$val['src'] = Images::getImageUrl($val['src'], $width, $height, 2);
$val['url'] = Helpers::transUrl($val['url'], $type);
$result['category']['list'][] = array(
'href' => $val['url'],
'img' => $val['src'],
'name' => $val['title']
);
}
unset($data[$key], $data[$key + 1], $data[$key + 2]);
}
}
return $result;
}
/**
* 组合HotCategory数据 [template: hotCategory & floor] 首页使用
*
* @param string $key
* @param array $data
* @param string $type
* @return array
*/
public static function mergeHotCategoryData($key, &$data, $type)
{
$result = array();
if (isset($data[$key]) && isset($data[$key + 1]))
{
if ($data[$key]['template_name'] == 'hotCategory' && $data[$key + 1]['template_name'] == 'floor')
{
$result = array(
'name' => '',
'navs' => array(),
'tplrecommend' => array(
'keyword' => array(),
'category' => array(),
'brands' => array(),
'types' => array(),
'products' => array()
)
);
// hotCategory模版
$temp = self::hotCategory($data[$key], $type);
$temp = $temp['recommend'];
$result['name'] = $temp['name'];
$result['navs'] = $temp['navs'];
$result['tplrecommend']['keyword'] = $temp['tplrecommend']['keyword'];
$result['tplrecommend']['category'] = $temp['tplrecommend']['category'];
foreach ($data[$key]['data']['imgs'] as $key2 => $val)
{
$val['url'] = Helpers::transUrl($val['url'], $type);
if ($key2 == 0 || $key2 == 4) // 两个大图
{
$val['img'] = Images::getImageUrl($val['img'], 378, 248, 1); // 大图
$result['tplrecommend']['brands'][] = array(
'href' => $val['url'],
'name' => $val['title'],
'img' => $val['img']
);
} else {
$val['img'] = Images::getImageUrl($val['img'], 185, 248, 1); // 小图
$result['tplrecommend']['types'][] = array(
'href' => $val['url'],
'name' => $val['title'],
'img' => $val['img']
);
}
}
// floor模版
foreach ($data[$key + 1]['data'] as $val)
{
$val['src'] = Images::getImageUrl($val['src'], 222, 298, 1); // 小图
$result['tplrecommend']['products'][] = array(
'href' => $val['url'],
'name' => $val['title'],
'img' => $val['src']
);
}
unset($data[$key], $data[$key + 1]);
$result = array(
'recommend' => $result
);
}
}
return $result;
}
/**
* 组合之nav处理
*
* @param array $data
* @param string $type
* @return array
*/
public static function mergeNavProcess(array $data, $type)
{
$result = array();
foreach ($data['data'] as $val) {
if (empty($val))
continue;
$result[] = array(
'href' => $val['url'],
'name' => $val['name']
);
}
return $result;
}
/**
* 组合之商品处理
*
* @param array $data
* @param string $type
* @return array
*/
public static function mergeGoodsProcess(array $data, $type)
{
$result = array();
$skns = '';
foreach ($data['data'] as $val) {
$skns .= $val['id'] . ' ';
}
$params = array(
'query' => rtrim($skns),
'client_type' => 'web'
);
$goodsList = SearchData::searchElasticByCondition($params, false);
$goodsList = $goodsList['data']['product_list'];
$pos = 0;
foreach ($goodsList as $goods)
{
$oneGoods = array();
$pos ++;
if ($pos > 12) {
break;
}
if (empty($goods)) {
continue;
}
$oneGoods['img'] = Images::getImageUrl($goods['goods_list'][0]['images_url'], 280, 373, 1);
$oneGoods['name'] = $goods['product_name'];
$oneGoods['price'] = $goods['sales_price'];
$url = 'http://item.yohobuy.com/product/pro_' .
$goods['product_id'] . '_' . $goods['goods_list'][0]['goods_id']
. '/' . $goods['cn_alphabet'] . '.html';
$oneGoods['href'] = Helpers::transUrl($url, $type);
$result[$goods['product_skn']] = $oneGoods;
}
// 排序
uksort($result, function ($a, $b) use($skns) {
$skn_arr = explode(' ', $skns);
return array_search($a, $skn_arr) > array_search($b, $skn_arr);
});
$pos = 1;
//添加TOP标示
foreach($result as &$val) {
if($pos < 4) {
$val['tip'] = 'TOP'.$pos;
}
else {
break;
}
$pos ++;
}
return $result;
}
/**
* 热门品牌数据处理
*
* @param array $data
* @param string $type
*/
public static function mergeHotBrandsData($key,array &$data,$type)
{
$result = array();
$temp = array();
if ($data[$key]['template_name'] == 'custom_brands' || $data[$key]['template_name'] == 'kids_brands')
{
$result = array(
'hotBrands' => array(
'name' => '',
'brandUrl' => ''
)
);
// text模版
$result['hotBrands']['name'] = '热门品牌';
//floor模板
$floor = array(
'logoBrand' => '',
'moreBrand' => ''
);
$brands = $data[$key]['data']['list'];
foreach ($brands as $val)
{
$width = 185;
$height = 86;
$val['src'] = Images::getImageUrl($val['src'], $width, $height, 2);
$val['url'] = Helpers::transUrl($val['url'], $type);
$floor['logoBrand'][] = array(
'href' => $val['url'],
'img' => $val['src']
);
}
$floor['moreBrand'] = 'http://yohobuy.com/brands';
$type_key = sprintf("%s_%s", $type, $key);
$cacheKey = sprintf("%s_%s", CacheConfig::KEY_INDEX_BRANDS_LIST_DATA, $type_key);
Cache::set($cacheKey, $floor, 7200);
$result['hotBrands']['brandUrl'] = '/common/getIndexResourceBrand?type=' . $type_key;
}
return $result;
}
}
\ No newline at end of file
... ...