Authored by Rock Zhang

Merge branch 'develop' of git.dev.yoho.cn:web/yohobuy into develop

Conflicts:
	template/m.yohobuy.com/actions/channel/newsale/new.phtml
... ... @@ -57,16 +57,6 @@ class AbstractAction extends Controller_Abstract
{
return $this->_request->getPost($key, $default);
}
/**
* 关闭模板自动渲染
*
* @return void
*/
protected function disableView()
{
Dispatcher::getInstance()->autoRender(false);
}
/**
* 使用Memcache缓存
... ... @@ -83,5 +73,54 @@ class AbstractAction extends Controller_Abstract
return Cache::memcached($node, $childNode);
}
}
/**
* 关闭模板自动渲染
*
* @return void
*/
protected function disableView()
{
Dispatcher::getInstance()->autoRender(false);
}
/**
* 输出JSON数据到浏览器
*
* @return void
*/
protected function echoJson($json)
{
headers_sent() || header('Content-Type: application/json; charset=utf-8;');
echo $json;
}
/**
* 返回JSON数据
*
* @param int $code 状态编码
* @param string $message 提示信息
* @param mixed $data 数据内容
* @return json
*/
protected function returnJson($code, $message, $data)
{
return json_encode(array(
'code' => $code,
'message' => $message,
'data' => $data,
));
}
/**
* 判断是不是AJAX请求
*
* @return bool
*/
protected function isAjax()
{
return $this->_request->isXmlHttpRequest();
}
}
... ...
... ... @@ -87,10 +87,11 @@ class Yohobuy
*
* @param string $url 接口URL
* @param array $data 参数列表
* @param bool $returnJson 控制是否返回json格式数据
* @param int $timeout 超时时间
* @return mixed
*/
public static function get($url, $data = array(), $timeout = 5)
public static function get($url, $data = array(), $returnJson = false, $timeout = 5)
{
if (isset($data['private_key'])) {
unset($data['private_key']);
... ... @@ -104,7 +105,7 @@ class Yohobuy
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if (!empty($result)) {
if (!$returnJson && !empty($result)) {
$result = json_decode($result, true);
}
curl_close($ch);
... ... @@ -118,12 +119,13 @@ class Yohobuy
*
* @param string $url 接口URL
* @param array $data 参数列表
* @param bool $returnJson 控制是否返回json格式数据
* @param int $timeout 超时时间
* @param array $header
* @param array $cookie
* @return mixed
*/
public static function post($url, $data = array(), $timeout = 5, $header = array(), $cookie = array())
public static function post($url, $data = array(), $returnJson = false, $timeout = 5, $header = array(), $cookie = array())
{
$ch = curl_init($url);
... ... @@ -151,7 +153,7 @@ class Yohobuy
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$result = curl_exec($ch);
if (!empty($result)) {
if (!$returnJson && !empty($result)) {
$result = json_decode($result, true);
}
curl_close($ch);
... ... @@ -237,5 +239,23 @@ class Yohobuy
return $result;
}
/**
* rpc调用远程服务(YAR)
*
* @see http://php.net/manual/zh/yar-client.setopt.php
* @return array
*/
public static function yarClient($uri, $method, $data = array(), $timeout = 3000)
{
$client = new \Yar_Client($uri);
$client->SetOpt(YAR_OPT_PACKAGER, 'php');
$client->SetOpt(YAR_OPT_TIMEOUT, $timeout);
$client->SetOpt(YAR_OPT_CONNECT_TIMEOUT, $timeout);
$result = $client->$method($data);
return $result;
}
}
... ...
<?php
namespace LibModels\Wap\Product;
use Api\Sign;
... ... @@ -15,8 +16,9 @@ use Api\Yohobuy;
*/
class RecomData
{
/**
* 你可能喜欢的商品列表
* 你可能喜欢的BOYS或GIRLS的商品列表
*
* @param string $gender "1,3"表示男, "2,3"表示女
* @param string $channel 1表示男, 2表示女
... ... @@ -33,9 +35,46 @@ class RecomData
$param['limit'] = $limit;
$param['yh_channel'] = $channel;
$param['client_secret'] = Sign::getSign($param);
return Yohobuy::get(Yohobuy::API_URL, $param);
return Yohobuy::get(Yohobuy::API_URL, $param, true);
}
}
/**
* 你可能喜欢的潮童的商品列表
*
* @param int $page 分页第几页, 默认第1页
* @param int $limit 查询返回的最大限制数, 默认为50
* @return array
*/
public static function mayLikeKids($page = 1, $limit = 50)
{
$param = Yohobuy::param();
$param['method'] = 'app.search.kids';
$param['page'] = $page;
$param['limit'] = $limit;
$param['yh_channel'] = '3';
$param['client_secret'] = Sign::getSign($param);
return Yohobuy::get(Yohobuy::API_URL, $param, true);
}
/**
* 你可能喜欢的创意生活的新品到着和人气单品列表
*
* @param int $page 分页第几页, 默认第1页
* @param int $limit 查询返回的最大限制数, 默认为50
* @return array
*/
public static function mayLikeLifestyle($page = 1, $limit = 50)
{
$param = Yohobuy::param();
$param['method'] = 'app.search.lifeStyle';
$param['page'] = $page;
$param['limit'] = $limit;
$param['yh_channel'] = '4';
$param['client_secret'] = Sign::getSign($param);
return Yohobuy::get(Yohobuy::API_URL, $param, true);
}
}
... ...
... ... @@ -49,14 +49,14 @@ class Bootstrap extends Bootstrap_Abstract
Loader::getInstance()->registerLocalNameSpace(explode(',', $this->_config->application->namespaces));
}
/**
* 初始化插件
* @param Yaf_Dispatcher $dispatcher
*/
public function _initPlugin(Dispatcher $dispatcher)
{
}
// /**
// * 初始化插件
// * @param Yaf_Dispatcher $dispatcher
// */
// public function _initPlugin(Dispatcher $dispatcher)
// {
//
// }
/**
* 初始化路由
... ... @@ -87,15 +87,15 @@ class Bootstrap extends Bootstrap_Abstract
}
}
/**
* 初始化第三方包
* @param Dispatcher $dispatcher
*/
public function _initPackage(Dispatcher $dispatcher)
{
if ($this->_config->composer->autoload == 1) {
require $this->_config->composer->path . '/vendor/autoload.php';
}
}
// /**
// * 初始化第三方包
// * @param Dispatcher $dispatcher
// */
// public function _initPackage(Dispatcher $dispatcher)
// {
// if ($this->_config->composer->autoload == 1) {
// require $this->_config->composer->path . '/vendor/autoload.php';
// }
// }
}
... ...
<?php
use Action\AbstractAction;
use LibModels\Wap\Product\RecomData;
use Api\Yohobuy;
/**
* 频道选择
... ... @@ -11,5 +11,7 @@ class IndexController extends AbstractAction
{
$this->_view->assign('title', 'YOHO!有货');
$this->_view->display('index', array('test' => 'hello world'));
//$test = Yohobuy::yarClient('http://service.api.yohobuy.com/guang/service/v1/plustar/', 'getBrandInfo', array('id' => 289));
}
}
\ No newline at end of file
... ...
<?php
use Action\AbstractAction;
use LibModels\Wap\Product\RecomData;
/**
* 商品推荐相关
*
* @name RecomController
* @package Product
* @copyright yoho.inc
* @version 1.0 (2015-10-8 14:43:52)
* @author fei.hong <fei.hong@yoho.cn>
*/
class RecomController extends AbstractAction
{
/**
* 你可能喜欢的
*
* 备注:
* 调用位于男生(BOYS),女生(GIRLS),潮童(KIDS),创意生活(LifeStyle)页面的底部.
* 调用方式为AJAX,需要用JS遍历该数据集,替换图片URL地址中的{width},{height},{mode}.
* JS替换示例: str.replace("{width}", 300).replace("{height}", 300).replace("{mode}", 2);
*
* @param string gender "1,3"表示男, "2,3"表示女, 当channel为3时该参数可不传
* @param string channel 1表示男, 2表示女, 3表示潮童
* @return json
*/
public function maylikeAction()
{
if ($this->isAjax()) {
$data = '';
$gender = $this->get('gender', '1,3');
$channel = $this->get('channel', '1');
switch (strval($channel)) {
case '1': // 男(Boys)
case '2': // 女(Girls)
$data = RecomData::mayLike($gender, $channel);
break;
case '3': // 潮童(Kids)
$data = RecomData::mayLikeKids();
break;
case '4': // 创意生活(LifeStyle)
$data = RecomData::mayLikeLifestyle();
break;
}
$this->echoJson($data);
}
}
}
... ...