Authored by Rock Zhang

添加新品到着有关代码

@@ -167,7 +167,7 @@ class Yohobuy @@ -167,7 +167,7 @@ class Yohobuy
167 * @param array $options CURL设置项 167 * @param array $options CURL设置项
168 * @return array 168 * @return array
169 */ 169 */
170 - public function loop($urlList = array(), $options = array()) 170 + public static function getMulti($urlList = array(), $options = array())
171 { 171 {
172 $result = array(); 172 $result = array();
173 $response = array(); 173 $response = array();
1 <?php 1 <?php
  2 +namespace LibModels\wap\Channel;
2 3
3 -/*  
4 - * To change this license header, choose License Headers in Project Properties.  
5 - * To change this template file, choose Tools | Templates  
6 - * and open the template in the editor. 4 +use Api\Yohobuy;
  5 +use Api\Sign;
  6 +use Plugin\Helpers;
  7 +
  8 +/**
  9 + * 新品到着接口操作类
  10 + *
  11 + * @name NewsaleData
  12 + * @package Library/LibModels/Channel
  13 + * @copyright yoho.inc
  14 + * @version 1.0 (2015-10-8)
  15 + * @author gtskk <rocky.zhang@yoho.cn>
  16 + */
  17 +class NewsaleData
  18 +{
  19 +
  20 + /**
  21 + * 获取新品到着焦点图数据
  22 + * @param string $contentCode 内容位置码
  23 + * @return array 新品到着焦点图有关数据
  24 + */
  25 + public static function getNewsaleFocus($contentCode)
  26 + {
  27 + // 构建必传参数
  28 + $param = Yohobuy::param();
  29 +
  30 + $param['content_code'] = $contentCode;
  31 + $param['client_secret'] = Sign::getSign($param);
  32 +
  33 + return Yohobuy::get(Yohobuy::SERVICE_URL.'operations/api/v5/resource/get', $param);
  34 + }
  35 +
  36 + /**
  37 + * 获取新品到着商品数据
  38 + *
  39 + * @param string $gender "1,3"表示男, "2,3"表示女, "1,2,3"表示全部
  40 + * @param string $channel 1表示男, 2表示女
  41 + * @param integer $limit 查询返回的最大限制数, 默认为50
  42 + * @param integer $page 分页第几页, 默认第1页
  43 + * @return array 新品到着商品数据
7 */ 44 */
  45 + public static function getNewsaleProducts($gender, $channel, $limit = 50, $page = 1)
  46 + {
  47 + $param = Yohobuy::param();
  48 + $param['method'] = 'app.search.newProduct';
  49 + $param['gender'] = $gender;
  50 + $param['page'] = $page;
  51 + $param['limit'] = $limit;
  52 + $param['yh_channel'] = $channel;
  53 + $param['client_secret'] = Sign::getSign($param);
  54 +
  55 + // 构建url地址列表
  56 + $urlList = array();
  57 + $param['dayLimit'] = 1;
  58 + $urlList['new'] = Yohobuy::httpBuildQuery(Yohobuy::API_URL,$param);
  59 + $param['dayLimit'] = 2;
  60 + $urlList['week'] = Yohobuy::httpBuildQuery(Yohobuy::API_URL,$param);
  61 + $param['dayLimit'] = 3;
  62 + $urlList['sale'] = Yohobuy::httpBuildQuery(Yohobuy::API_URL,$param);
  63 + // var_dump($urlList);exit;
  64 +
  65 + return Yohobuy::getMulti($urlList);
  66 + }
  67 +
  68 + /**
  69 + * 筛选新品到着商品
  70 + *
  71 + * @param
  72 + * @param array $selectParams 筛选条件参数,可传递的条件参数有:
  73 + * string gender "1,3"表示男, "2,3"表示女, "1,2,3"表示全部
  74 + * integer brand 品牌Id
  75 + * integer sort 品类Id
  76 + * integer color 颜色Id
  77 + * integer size 尺码Id
  78 + * string price 价格
  79 + * string p_d 折扣
  80 + * @param string $channel 1表示男, 2表示女
  81 + * @param integer $dayLimit 限制读取多少天,默认为1天
  82 + * @param integer $limit 查询返回的最大限制数, 默认为50
  83 + * @param integer $page 分页第几页, 默认第1页
  84 + * @return array 筛选出来的新品到着商品
  85 + */
  86 + public static function selectNewsaleProducts(array $selectParams, $channel, $dayLimit = 1, $limit = 50, $page = 1)
  87 + {
  88 + $selectItems = array(
  89 + 'gender',
  90 + 'brand',
  91 + 'sort',
  92 + 'color',
  93 + 'size',
  94 + 'price',
  95 + 'p_d'
  96 + );
  97 +
  98 + $param = Yohobuy::param();
  99 + $param['method'] = 'app.search.newProduct';
  100 + $param['dayLimit'] = $dayLimit;
  101 + $param['page'] = $page;
  102 + $param['limit'] = $limit;
  103 + $param['yh_channel'] = $channel;
  104 +
  105 + // 拉取筛选参数
  106 + $queriedParams = Helpers::array_get($selectParams, $selectItems);
  107 + $param = array_merge($param, $queriedParams);
  108 + $param['client_secret'] = Sign::getSign($param);
  109 +
  110 + return Yohobuy::get(Yohobuy::API_URL, $param);
  111 + }
8 112
  113 +}
  1 +<?php
  2 +
  3 +namespace Plugin;
  4 +
  5 +/**
  6 + * 辅助类
  7 + */
  8 +class Helpers
  9 +{
  10 +
  11 + /**
  12 + * 根据尺寸获得图片url
  13 + * @param string $fileName 文件名
  14 + * @param integer $width 图片宽度
  15 + * @param integer $height 图片高度
  16 + * @param integer $mode 模式
  17 + * @return string 图片地址
  18 + */
  19 + public static function getImageUrl($fileName, $width, $height, $mode = 1){
  20 + return str_replace('{width}', $width, str_replace('{height}', $height, str_replace('{mode}', $mode, $fileName)));
  21 + }
  22 +
  23 + /**
  24 + * 从数组中中获取指定键值对应的元素
  25 + * @param array $array 需要获取的数组
  26 + * @param array|string $keys 指定键值,数组或者字符串
  27 + * @return array 获取的数据
  28 + */
  29 + public static function array_get($array, $keys)
  30 + {
  31 + return array_intersect_key($array, array_flip((array) $keys));
  32 + }
  33 +}
  1 +{{>layout/header}}
  2 +
  3 +<code>
  4 +{{#focus}}
  5 +<ul>
  6 +{{#each data}}
  7 +{{#data}}
  8 +<li><img src="{{src}}" alt="{{title}}"></li>
  9 +{{/data}}
  10 +{{/each}}
  11 +</ul>
  12 +{{/focus}}
  13 +</code>
  14 +
  15 +{{>layout/footer}}
1 <?php 1 <?php
2 2
3 -/*  
4 - * To change this license header, choose License Headers in Project Properties.  
5 - * To change this template file, choose Tools | Templates  
6 - * and open the template in the editor. 3 +use Action\AbstractAction;
  4 +use LibModels\wap\Channel\NewsaleData as Newsale;
  5 +use Plugin\Helpers;
  6 +/**
  7 + * 新品到着
7 */ 8 */
  9 +class NewsaleController extends AbstractAction
  10 +{
  11 + public function indexAction()
  12 + {
  13 + $this->_view->assign('title', '新品到着');
8 14
  15 + // 新品到着顶部焦点图
  16 + $focusData = Newsale::getNewsaleFocus('a7989369aa86681c678bc40f171b8f1d');
  17 + // 新品到着商品数据
  18 + $productsData = Newsale::getNewsaleProducts('1,3', 1, 60);
  19 + var_dump($productsData);
  20 +
  21 + // 处理返回的数据
  22 + $focus = array();
  23 + $products = array();
  24 + if($focusData['code'] === 200)
  25 + {
  26 + foreach ($focusData['data']['data'] as $single)
  27 + {
  28 + $single['src'] = Helpers::getImageUrl($val['src'], 375, 667, 1);
  29 + $focus[] = $single;
  30 + }
  31 + }
  32 + if($productsData['code'] === 200)
  33 + {
  34 + $products = $productsData['data'];
  35 + foreach ($products['product_list'] as &$val) {
  36 + $val['default_images'] = Helpers::getImageUrl($val['default_images'], 290, 386);
  37 + }
  38 + }
  39 + var_dump($products);exit;
  40 +
  41 + $this->_view->display('new', compact('focus', 'products'));
  42 + }
  43 +}