Authored by hf

code review by hf: do add web group file

Too many changes to show.

To preserve performance only 14 of 14+ files are displayed.

<?php
/**
* 所有Controller控制器的基类
*
* @name AbstractAction
* @package library
* @copyright yoho.inc
* @version 1.0 (2015-9-15 11:55:25)
* @author fei.hong <fei.hong@yoho.cn>
*/
namespace Action;
use Yaf\Controller_Abstract;
use Yaf\Dispatcher;
use Plugin\Cache;
use Plugin\Helpers;
use Hood\Session;
class AbstractAction extends Controller_Abstract
{
/**
* HTTP请求对象
*
* @var object
*/
protected $_request;
/**
* 用户相关信息
*/
protected $_vip;
protected $_uid = 0;
protected $_uname = '';
protected $_usession = '';
protected $_useSession = true;
/**
* 存放模板数据
*
* @var array
*/
protected $_data;
/**
* 初始化
*/
public function init()
{
$this->_request = $this->getRequest();
// 设置环境变量
switch (APPLICATION_ENV) {
case 'production': // 生产
$this->_view->assign('rlsEnv', true);
$this->_useSession = true;
break;
case 'preview': // 预览
$this->_view->assign('preEnv', true);
$this->_useSession = true;
break;
case 'testing': // 测试
$this->_view->assign('testEnv', true);
$this->_useSession = true;
break;
case 'develop': // 开发
default:
$this->_view->assign('devEnv', true);
$this->_useSession = false;
break;
}
}
/**
* 封装一下获取get参数
*
* @param String $key
* @param mixed $default
* @return mixed
*/
protected function get($key = null, $default = null)
{
if (null === $key) {
return $_GET;
}
return $this->_request->getQuery($key, $default);
}
/**
* 封装一下获取post参数
*
* @param String $key
* @param mixed $default
* @return mixed
*/
protected function post($key = null, $default = null)
{
if (null === $key) {
return $_POST;
}
return $this->_request->getPost($key, $default);
}
/**
* 封装一下获取YAF内部的参数
*
* @param String $key
* @param mixed $default
* @return mixed
*/
protected function param($key, $default = null)
{
return $this->_request->getParam($key, $default);
}
/**
* 封装一下获取服务器的参数
*
* @param String $key
* @param mixed $default
* @return mixed
*/
protected function server($key, $default = null)
{
return $this->_request->getServer($key, $default);
}
/**
* 关闭模板自动渲染
*
* @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;');
if (is_array($json)) {
$json = json_encode($json);
}
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();
}
/**
* 跳转到错误页面
*/
protected function error()
{
headers_sent() || header('Location: /error.html');
exit();
}
/**
* 跳转到指定的URL
*
* @param string $url 链接地址
* @return void
*/
protected function go($url)
{
headers_sent() || header('Location: ' . $url);
exit();
}
/**
* 设置Cookie
*
* @param string $name cookie的名字
* @param string $value cookie的值
* @param integer $expire cookie过期时间
* @param integer $path cookie可用的路径
* @param string $domain cookie可用域名
*/
protected function setCookie($name, $value, $expire = 0, $path = '/', $domain = '.yohobuy.com')
{
setcookie($name, $value, $expire, $path, $domain);
}
/**
* 返回Cookie变量
*
* @param string $name cookie名称
* @param string $default 未获取到返回的默认值
* @return string 获取到的cookie值
*/
protected function getCookie($name, $default = '')
{
return $this->_request->getCookie($name, $default);
}
/**
* 设置缓存
*
* @param string $key 键名
* @param mixed $value 需要缓存的数据
* @param int $expire 缓存有效期(单位秒, 0表示永久)
* @return void
*/
protected function setCache($key, $value, $expire)
{
Cache::set($key, $value, $expire);
}
/**
* 获取缓存
*
* @param string $key 键名
* @param bool $isMaster 控制是到主服务器取,还是到从服务器取缓存
* @return mixed
*/
protected function getCache($key, $isMaster = true)
{
if ($isMaster) {
return Cache::get($key, 'master');
} else {
return Cache::get($key, 'slave');
}
}
/**
* 设置Session
*
* @param string $name 名称
* @param mixed $value 值
* @return void
*/
public function setSession($name, $value)
{
if ($this->_useSession) {
Session::start('yohobuy_session', null, 'yohobuy.com')->__set($name, $value);
}
}
/**
* 获取Session
*
* @param string $name 名称
* @return mixed
*/
public function getSession($name)
{
if ($this->_useSession) {
return Session::start('yohobuy_session', null, 'yohobuy.com')->__get($name);
} else {
return '';
}
}
/**
* 获取当前登录的用户ID
*
* @param bool $useSession (true:从服务端session中检查, false:从客户端cookie中检查)
* @return int
*/
protected function getUid($useSession = false)
{
// 控制是否启用SESSION
if (!$this->_useSession) {
$useSession = false;
}
//$useSession = false;
if (!$this->_uid) {
$cookie = $this->getCookie('_UID');
// 兼容老的
if (!empty($cookie)) {
$cookieList = explode('::', $cookie);
if (isset($cookieList[1]) && is_numeric($cookieList[1])) {
if ($useSession) {
$token = $this->getSession('_TOKEN');
if (empty($token)) {
$token = $this->getCookie('_TOKEN');
}
if ($token === Helpers::makeToken($cookieList[1])) {
$this->_uid = $cookieList[1];
}
} else {
$this->_uid = $cookieList[1];
}
$this->_uname = $cookieList[0];
$this->_usession = $cookieList[3];
$this->_vip = $cookieList[2];
}
}
// 新的, 如果老站没有同步成功,再尝试从SESSION获取
elseif ($useSession) {
$uid = $this->getSession('_LOGIN_UID');
if (!empty($uid)) {
$this->_uid = $uid;
}
}
}
return $this->_uid;
}
/**
* 获取客户端唯一标识
*
* @return string
*/
protected function getUdid()
{
$udid = '';
$realIP = $this->_request->getServer('HTTP_X_REAL_IP');
if ($realIP) {
$udid = md5($realIP);
} else {
$realIP = $this->_request->getServer('REMOTE_ADDR', '');
$udid = md5($realIP);
}
return $udid;
}
/*
* 设置网站SEO的标题
*
* @param string $title 标题
* @param string $sign 连接的字符串
* @param bool $showMore 是否显示更多内容
* @return void
*/
protected function setTitle($title, $showMore = true, $sign = ' | ')
{
$this->_view->assign('title_more', $showMore);
$this->_view->assign('title', $title . $sign);
}
/**
* 设置网站SEO的关键词
*
* @param string $keywords 关键词,多个之间用","逗号分隔
* @return void
*/
protected function setKeywords($keywords)
{
$this->_view->assign('keywords', rtrim($keywords, ',') . ',');
}
/**
* 设置网站SEO的描述内容
*
* @param string $description 描述内容
* @param bool $showMore 是否显示更多内容
* @param string $sign 连接的字符串
* @return void
*/
protected function setDescription($description, $showMore = true, $sign = ' ')
{
$this->_view->assign('description_more', $showMore);
$this->_view->assign('description', $description . $sign);
}
/**
* 设置网站导航头部信息
*
* @param string $title 头部标题
* @param mixed $backUrl 返回的链接
* @param string $homeUrl 返回首页的链接
* @return void
*/
protected function setNavHeader($title = '', $backUrl = true, $homeUrl = '/', $navBtn = false)
{
$header = array();
// // 判断是否是微信浏览器浏览
// if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') === false) {
// $header['isShow'] = true;
// }
if ($backUrl) {
$header['navBack'] = true;
if (is_string($backUrl) && $backUrl != '') {
$header['backUrl'] = $backUrl;
}
}
if (!empty($title)) {
$header['navTitle'] = $title;
}
if (!empty($homeUrl)) {
$header['navHome'] = $homeUrl . '?go=1&t=' . time();
}
if ($navBtn) {
$header['navBtn'] = true;
}
// 默认使用男生的头部
$header['boys'] = true;
$controller = $this->getRequest()->getControllerName();
if ($controller !== 'Home') {
// 根据COOKIE记录的频道进行导航定位
$channel = Helpers::getChannelByCookie();
switch ($channel) {
default:
case 1:
$header['boys'] = true;
break;
case 2:
$header['girls'] = true;
break;
case 3:
$header['kids'] = true;
break;
case 4:
$header['lifeStyle'] = true;
break;
}
}
$this->_view->assign('pageHeader', $header);
}
/**
* 设置网站导航底部信息
*
* @return void
*/
protected function setNavFooterTab()
{
$this->_view->assign('showFooterTab', array(
'indexUrl' => Helpers::url('/?go=1'), //首页
'categoryUrl' => Helpers::url('/cate'), // 分类
'guangUrl' => Helpers::url('', null, 'guang'), // 逛首页
'shoppingCartUrl' => Helpers::url('/cart/index/index'), // 购物车
'mineUrl' => Helpers::url('/home'), // 个人中心
));
}
/**
* 设置侧边栏信息
*
* @param string $guangChoosed 逛默认选中项 "all"表示全部,"boys":只看男生,"girls":只看女生
* @return void
*/
protected function setNavSide($guangChoosed = 'all')
{
$this->_view->assign('sideNav', \Index\SideModel::getLeftNav($guangChoosed));
}
/**
* 设置最后修改时间
*
* @param string $modifiedTime 修改时间戳
* @param type $notModifiedExit 是否在没有修改时返回304状态
* @return void
*/
public static function setLastModified($modifiedTime, $notModifiedExit = true)
{
$modifiedTime = date('D, d M Y H:i:s ', $modifiedTime) . 'GMT';
if ($notModifiedExit && isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $modifiedTime == $_SERVER['HTTP_IF_MODIFIED_SINCE']) {
header('HTTP/1.1 304 Not Modified');
exit();
}
header('Last-Modified: ' . $modifiedTime);
}
/**
* 设置浏览器的缓存
*
* @param int $seconds 单位是秒
* @return void
*/
public static function setExpires($seconds = 180)
{
$time = date('D, d M Y H:i:s ', time() + $seconds) . 'GMT';
header('Expires: ' . $time);
}
}
<?php
/**
* 所有Controller控制器的基类
*
* @name AbstractAction
* @package library
* @copyright yoho.inc
* @version 1.0 (2015-9-15 11:55:25)
* @author fei.hong <fei.hong@yoho.cn>
*/
namespace Action;
use Yaf\Controller_Abstract;
use Yaf\Dispatcher;
use Plugin\Cache;
use Plugin\Helpers;
use Hood\Session;
class AbstractAction extends Controller_Abstract
{
/**
* HTTP请求对象
*
* @var object
*/
protected $_request;
/**
* 用户相关信息
*/
protected $_vip;
protected $_uid = 0;
protected $_uname = '';
protected $_usession = '';
protected $_useSession = true;
/**
* 存放模板数据
*
* @var array
*/
protected $_data;
/**
* 初始化
*/
public function init()
{
$this->_request = $this->getRequest();
// 设置环境变量
switch (APPLICATION_ENV) {
case 'production': // 生产
$this->_view->assign('rlsEnv', true);
$this->_useSession = true;
break;
case 'preview': // 预览
$this->_view->assign('preEnv', true);
$this->_useSession = true;
break;
case 'testing': // 测试
$this->_view->assign('testEnv', true);
$this->_useSession = true;
break;
case 'develop': // 开发
default:
$this->_view->assign('devEnv', true);
$this->_useSession = false;
break;
}
}
/**
* 封装一下获取get参数
*
* @param String $key
* @param mixed $default
* @return mixed
*/
protected function get($key = null, $default = null)
{
if (null === $key) {
return $_GET;
}
return $this->_request->getQuery($key, $default);
}
/**
* 封装一下获取post参数
*
* @param String $key
* @param mixed $default
* @return mixed
*/
protected function post($key = null, $default = null)
{
if (null === $key) {
return $_POST;
}
return $this->_request->getPost($key, $default);
}
/**
* 封装一下获取YAF内部的参数
*
* @param String $key
* @param mixed $default
* @return mixed
*/
protected function param($key, $default = null)
{
return $this->_request->getParam($key, $default);
}
/**
* 封装一下获取服务器的参数
*
* @param String $key
* @param mixed $default
* @return mixed
*/
protected function server($key, $default = null)
{
return $this->_request->getServer($key, $default);
}
/**
* 关闭模板自动渲染
*
* @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;');
if (is_array($json)) {
$json = json_encode($json);
}
echo $json;
}
/**
* 返回JSON数据
*
* @param int $code 状态编码
* @param string $message 提示信息
* @param mixed $data 数据内容
* @return json
*/
protected function returnJson($code, $message, $data)
{
headers_sent() || header('Content-Type: application/json; charset=utf-8;');
return json_encode(array(
'code' => $code,
'message' => $message,
'data' => $data,
));
}
/**
* JSON输出
* @param $code
* @param null $message
* @param null $data
*/
protected function helpJsonResult($code, $message = null, $data = null)
{
header('Content-type: application/json');
echo json_encode(array('code' => $code, 'message' => $message, 'data' => $data));
exit();
}
/**
* JSONP Callback输出,用于远程调用
* @param $callbackString
* @param $code
* @param null $message
* @param null $data
*/
protected function helpJsonCallbackResult($callbackString, $code, $message = null, $data = null)
{
header('Content-type: application/json');
echo $callbackString . "(";
echo json_encode(array('code' => $code, 'message' => $message, 'data' => $data));
echo ")";
exit();
}
/**
* 判断是不是AJAX请求
*
* @return bool
*/
protected function isAjax()
{
return $this->_request->isXmlHttpRequest();
}
/**
* 跳转到错误页面
*/
protected function error()
{
headers_sent() || header('Location: /error.html');
exit();
}
/**
* 跳转到指定的URL
*
* @param string $url 链接地址
* @return void
*/
protected function go($url)
{
headers_sent() || header('Location: ' . $url);
exit();
}
/**
* 设置Cookie
*
* @param string $name cookie的名字
* @param string $value cookie的值
* @param integer $expire cookie过期时间
* @param integer $path cookie可用的路径
* @param string $domain cookie可用域名
*/
protected function setCookie($name, $value, $expire = 0, $path = '/', $domain = '.yohobuy.com')
{
setcookie($name, $value, $expire, $path, $domain);
}
/**
* 返回Cookie变量
*
* @param string $name cookie名称
* @param string $default 未获取到返回的默认值
* @return string 获取到的cookie值
*/
protected function getCookie($name, $default = '')
{
return $this->_request->getCookie($name, $default);
}
/**
* 设置缓存
*
* @param string $key 键名
* @param mixed $value 需要缓存的数据
* @param int $expire 缓存有效期(单位秒, 0表示永久)
* @return void
*/
protected function setCache($key, $value, $expire)
{
Cache::set($key, $value, $expire);
}
/**
* 获取缓存
*
* @param string $key 键名
* @param bool $isMaster 控制是到主服务器取,还是到从服务器取缓存
* @return mixed
*/
protected function getCache($key, $isMaster = true)
{
if ($isMaster) {
return Cache::get($key, 'master');
} else {
return Cache::get($key, 'slave');
}
}
/**
* 设置Session
*
* @param string $name 名称
* @param mixed $value 值
* @return void
*/
public function setSession($name, $value)
{
if ($this->_useSession) {
Session::start('yohobuy_session', null, 'yohobuy.com')->__set($name, $value);
}
}
/**
* 获取Session
*
* @param string $name 名称
* @return mixed
*/
public function getSession($name)
{
if ($this->_useSession) {
return Session::start('yohobuy_session', null, 'yohobuy.com')->__get($name);
} else {
return '';
}
}
/**
* 获取当前登录的用户ID
*
* @param bool $useSession (true:从服务端session中检查, false:从客户端cookie中检查)
* @return int
*/
protected function getUid($useSession = false)
{
// 控制是否启用SESSION
if (!$this->_useSession) {
$useSession = false;
}
$useSession = false;
if (!$this->_uid) {
$cookie = $this->getCookie('_UID');
if (!empty($cookie)) {
$cookieList = explode('::', $cookie);
if (isset($cookieList[1]) && is_numeric($cookieList[1])) {
if ($useSession) {
$token = $this->getSession('_TOKEN');
if (empty($token)) {
$token = $this->getCookie('_TOKEN');
}
if ($token === Helpers::makeToken($cookieList[1])) {
$this->_uid = $cookieList[1];
}
} else {
$this->_uid = $cookieList[1];
}
$this->_uname = $cookieList[0];
$this->_usession = $cookieList[3];
$this->_vip = $cookieList[2];
}
}
}
return $this->_uid;
}
/**
* 获取客户端唯一标识
*
* @return string
*/
protected function getUdid()
{
$udid = '';
$realIP = $this->_request->getServer('HTTP_X_REAL_IP');
if ($realIP) {
$udid = md5($realIP);
} else {
$realIP = $this->_request->getServer('REMOTE_ADDR', '');
$udid = md5($realIP);
}
return $udid;
}
/*
* 设置网站SEO的标题
*
* @param string $title 标题
* @param string $sign 连接的字符串
* @param bool $showMore 是否显示更多内容
* @return void
*/
protected function setTitle($title, $showMore = true, $sign = ' | ')
{
$this->_view->assign('title_more', $showMore);
$this->_view->assign('title', $title . $sign);
}
/**
* 设置网站SEO的关键词
*
* @param string $keywords 关键词,多个之间用","逗号分隔
* @return void
*/
protected function setKeywords($keywords)
{
$this->_view->assign('keywords', rtrim($keywords, ',') . ',');
}
/**
* 设置网站SEO的描述内容
*
* @param string $description 描述内容
* @param string $sign 连接的字符串
* @param bool $showMore 是否显示更多内容
* @return void
*/
protected function setDescription($description, $showMore = true, $sign = ' ')
{
$this->_view->assign('description_more', $showMore);
$this->_view->assign('description', $description . $sign);
}
/**
* 设置网站导航头部信息
*
* @param string $title 头部标题
* @param mixed $backUrl 返回的链接
* @param string $homeUrl 返回首页的链接
* @return void
*/
protected function setNavHeader($title = '', $backUrl = true, $homeUrl = '/', $navBtn = false)
{
$header = array();
// // 判断是否是微信浏览器浏览
// if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') === false) {
// $header['isShow'] = true;
// }
if ($backUrl) {
$header['navBack'] = true;
if (is_string($backUrl) && $backUrl != '') {
$header['backUrl'] = $backUrl;
}
}
if (!empty($title)) {
$header['navTitle'] = $title;
}
if (!empty($homeUrl)) {
$header['navHome'] = $homeUrl . '?go=1&t=' . time();
}
if ($navBtn) {
$header['navBtn'] = true;
}
// 默认使用男生的头部
$header['boys'] = true;
$controller = $this->getRequest()->getControllerName();
if ($controller !== 'Home') {
// 根据COOKIE记录的频道进行导航定位
$channel = Helpers::getChannelByCookie();
switch ($channel) {
default:
case 1:
$header['boys'] = true;
break;
case 2:
$header['girls'] = true;
break;
case 3:
$header['kids'] = true;
break;
case 4:
$header['lifeStyle'] = true;
break;
}
}
$this->_view->assign('pageHeader', $header);
}
/**
* 设置网站导航底部信息
*
* @return void
*/
protected function setNavFooterTab()
{
$this->_view->assign('showFooterTab', array(
'indexUrl' => Helpers::url('/?go=1'), //首页
'categoryUrl' => Helpers::url('/cate'), // 分类
'guangUrl' => Helpers::url('', null, 'guang'), // 逛首页
'shoppingCartUrl' => Helpers::url('/cart/index/index'), // 购物车
'mineUrl' => Helpers::url('/home'), // 个人中心
));
}
/**
* 设置侧边栏信息
*
* @param string $guangChoosed 逛默认选中项 "all"表示全部,"boys":只看男生,"girls":只看女生
* @return void
*/
protected function setNavSide($guangChoosed = 'all')
{
$this->_view->assign('sideNav', \Index\SideModel::getLeftNav($guangChoosed));
}
/**
* 设置最后修改时间
*
* @param string $modifiedTime 修改时间戳
* @param type $notModifiedExit 是否在没有修改时返回304状态
* @return void
*/
public static function setLastModified($modifiedTime, $notModifiedExit = true)
{
$modifiedTime = date('D, d M Y H:i:s ', $modifiedTime) . 'GMT';
if ($notModifiedExit && isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $modifiedTime == $_SERVER['HTTP_IF_MODIFIED_SINCE']) {
header('HTTP/1.1 304 Not Modified');
exit();
}
header('Last-Modified: ' . $modifiedTime);
}
/**
* 设置浏览器的缓存
*
* @param int $seconds 单位是秒
* @return void
*/
public static function setExpires($seconds = 180)
{
$time = date('D, d M Y H:i:s ', time() + $seconds) . 'GMT';
header('Expires: ' . $time);
}
}
... ...
<?php
/**
* 有货相关接口类
*
* @name Yohobuy
* @package library/Api
* @copyright yoho.inc
* @version 1.0 (2015-9-30 16:42:51)
* @author fei.hong <fei.hong@yoho.cn>
*/
namespace Api;
use Plugin\Cache;
class Yohobuy
{
/* 正式环境 */
// const API_URL = 'http://api2.open.yohobuy.com/';
// const API_URL2 = 'http://api.open.yohobuy.com/';
// const SERVICE_URL = 'http://service.api.yohobuy.com/';
// const YOHOBUY_URL = 'http://www.yohobuy.com/';
// const API_URL = 'http://apih5.yoho.cn/';
// const API_URL2 = 'http://apih5.yoho.cn/';
// const SERVICE_URL = 'http://serviceh5.yoho.cn/';
// const YOHOBUY_URL = 'http://www.yohobuy.com/';
// const API_OLD = 'http://api2.open.yohobuy.com/';
// /* 测试环境 */
// const API_URL = 'http://testapi.yoho.cn:28078/';
// const SERVICE_URL = 'http://testservice.yoho.cn:28077/';
// const YOHOBUY_URL = 'http://www.yohobuy.com/';
// const API_OLD = 'http://test2.open.yohobuy.com/';
/* 预览环境 */
const API_URL = 'http://preapi.yoho.cn/';
const API_URL2 = 'http://preapi.yoho.cn/';
const SERVICE_URL = 'http://serviceh5.yoho.cn/';
const YOHOBUY_URL = 'http://www.yohobuy.com/';
const API_OLD = 'http://api2.open.yohobuy.com/';
/**
* 私钥列表
*
* @var array
*/
public static $privateKeyList = array(
'android' => 'fd4ad5fcfa0de589ef238c0e7331b585',
'iphone' => 'a85bb0674e08986c6b115d5e3a4884fa',
'ipad' => 'ad9fcda2e679cf9229e37feae2cdcf80',
'web' => '0ed29744ed318fd28d2c07985d3ba633',
);
/**
* 取得当前的客户端类型
*/
public static function clientType()
{
// 苹果设备
if (strstr($_SERVER['HTTP_USER_AGENT'], 'iPhone')) {
return 'iphone';
}
// 苹果IPAD
elseif (strstr($_SERVER['HTTP_USER_AGENT'], 'iPad')) {
return 'ipad';
}
elseif (stristr($_SERVER['HTTP_USER_AGENT'], 'android')) {
return 'android';
}
// 其它
else {
return 'web';
}
}
/**
* 取得当前的IP地址
*
* @param bool $int 返回int类型的ip地址,默认是
* @return mixed 当前的IP地址
*/
public static function ip($int = true)
{
if (isset($_SERVER['HTTP_CLIENT_IP']) && $_SERVER['HTTP_CLIENT_IP']) {
$onlineip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) {
$onlineip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$onlineip = $_SERVER['REMOTE_ADDR'];
}
return $int ? ip2long($onlineip) : $onlineip;
}
/**
* 取得公共的参数
*
* @return array
*/
public static function param()
{
$clientType = self::clientType();
$param = array(
'app_version' => '3.8.2',
'client_type' => $clientType,
'os_version' => 'yohobuy:h5',
'private_key' => self::$privateKeyList[$clientType],
'screen_size' => '720x1280',
'v' => '7',
);
return $param;
}
/**
* 构建URL
*
* @param string $url
* @param array $data
* @return string
*/
public static function httpBuildQuery($url, $data)
{
// 销毁私钥参数
if (isset($data['private_key'])) {
unset($data['private_key']);
}
if (strstr($url, '?') !== false) {
$url .= '&' . http_build_query($data, null, '&');
} else {
$url .= '?' . http_build_query($data, null, '&');
}
return $url;
}
/**
* get方式调用接口
*
* @param string $url 接口URL
* @param array $data 参数列表
* @parma mixed $cache 控制是否启用接口数据的缓存(时间单位为秒). 如3600表示缓存1小时, false表示不缓存
* @param bool $returnJson 控制是否返回json格式数据
* @param int $timeout 超时时间
* @return mixed
*/
public static function get($url, $data = array(), $cache = false, $returnJson = false, $timeout = 5)
{
// 销毁私钥参数
if (isset($data['private_key'])) {
unset($data['private_key']);
}
if (!empty($data)) {
$url = self::httpBuildQuery($url, $data);
}
/* 开启缓存的情况 */
if ($cache && USE_CACHE) {
// 先尝试获取一级缓存(master), 有数据则直接返回.
$result = Cache::get($url, 'master');
if (!empty($result)) {
return $result;
}
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if (!$returnJson && !empty($result)) {
$result = json_decode($result, true);
}
curl_close($ch);
$data = array();
/* 开启缓存的情况 */
if ($cache && USE_CACHE) {
// 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
if (empty($result)) {
$result = Cache::get($url, 'slave');
}
// 接口调用成功时,这里会设置一级(master)和二级(slave)的缓存数据.
else {
Cache::set($url, $result, $cache);
}
}
return $result;
}
/**
* post提交数据
*
* @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(), $returnJson = false, $timeout = 10, $header = array(), $cookie = array())
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
if (!empty($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
} else {
curl_setopt($ch, CURLOPT_HEADER, 0);
}
if (!empty($cookie)) {
$cookie_str = array();
foreach ($cookie as $key => $val) {
$cookie_str[] = urlencode($key) . '=' . urlencode($val);
}
curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookie_str));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
// 销毁私钥参数
if (isset($data['private_key'])) {
unset($data['private_key']);
}
if (!empty($data)) {
$str = http_build_query($data, null, '&');
// 新加支持application/x-www-form-urlencoded调用方式
//curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
}
$result = curl_exec($ch);
if (!$returnJson && !empty($result)) {
$result = json_decode($result, true);
}
curl_close($ch);
$data = array();
return $result;
}
/**
* 批量调用接口
*
* @param array $urlList 接口列表
* @param array $options CURL设置项
* @parma mixed $cache 控制是否启用接口数据的缓存(时间单位为秒). 如3600表示缓存1小时, false表示不缓存
* @param int $timeout 超时时间,单位是秒
* @return array
*/
public static function getMulti($urlList = array(), $options = array(), $cache = false, $timeout = 5)
{
/* 开启缓存的情况 */
if ($cache && USE_CACHE) {
$key = md5(implode(',', array_values($urlList)));
// 先尝试获取一级缓存(master), 有数据则直接返回.
$result = Cache::get($key, 'master');
if (!empty($result)) {
return $result;
}
}
$result = array();
$response = array();
$running = 0;
$data = '';
$error = '';
$defaultOptions = array(
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => $timeout,
CURLOPT_TIMEOUT => $timeout,
CURLOPT_NOSIGNAL => 1, //忽略所有的curl传递给php的信号,减少并发crash
);
$mh = curl_multi_init();
$ch = array();
// 应用CURL配置
if (empty($options)) {
$options = $defaultOptions;
} else {
$options = array_merge($defaultOptions, $options);
}
// 添加子链接句柄
foreach ($urlList as $name => $api) {
$ch[$name] = curl_init($api);
curl_setopt_array($ch[$name], $options);
curl_multi_add_handle($mh, $ch[$name]);
$result[$name] = array();
}
// 调用API接口
do {
do {
$status = curl_multi_exec($mh, $running);
} while ($status == CURLM_CALL_MULTI_PERFORM);
if ($status != CURLM_OK) {
break;
}
if ($running > 0) {
curl_multi_select($mh, 0.5);
}
} while ($running);
// 获取API接口响应的结果
foreach ($urlList as $name => $api) {
$error = curl_error($ch[$name]);
if ($error != '') {
continue;
}
$data = curl_multi_getcontent($ch[$name]);
if (!$data) {
continue;
}
$response = json_decode($data, true);
if (empty($response['data'])) {
continue;
}
$result[$name] = $response['data'];
curl_multi_remove_handle($mh, $ch[$name]);
curl_close($ch[$name]);
}
curl_multi_close($mh);
/* 开启缓存的情况 */
if ($cache && USE_CACHE) {
// 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
if (empty($result[$name])) {
$result = Cache::get($key, 'slave');
}
// 接口调用成功时,这里会设置一级(master)和二级(slave)的缓存数据.
else {
Cache::set($key, $result, $cache);
}
}
return $result;
}
/**
* rpc调用远程服务(YAR)
*
* @see http://php.net/manual/zh/yar-client.setopt.php
* @param string $uri
* @param string $method
* @param array $parameters
* @param mixed $cache 控制是否启用接口数据的缓存(时间单位为秒). 如3600表示缓存1小时, false表示不缓存
* @param int $timeout
* @return array
*/
public static function yarClient($uri, $method, $parameters = array(), $cache = false, $timeout = 3000)
{
/* 开启缓存的情况 */
if ($cache && USE_CACHE) {
$key = self::httpBuildQuery($uri . $method, $parameters);
// 先尝试获取一级缓存(master), 有数据则直接返回.
$result = Cache::get($key, 'master');
if (!empty($result)) {
return $result;
}
}
$client = new \Yar_Client($uri);
$client->SetOpt(YAR_OPT_PACKAGER, 'php');
$client->SetOpt(YAR_OPT_TIMEOUT, $timeout);
$client->SetOpt(YAR_OPT_CONNECT_TIMEOUT, $timeout);
try {
$result = call_user_func_array(array($client, $method), $parameters);
} catch (\Exception $e) {
$result = array();
}
/* 开启缓存的情况 */
if ($cache && USE_CACHE) {
// 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
if (empty($result)) {
$result = Cache::get($key, 'slave');
}
// 接口调用成功时,这里会设置一级(master)和二级(slave)的缓存数据.
else {
Cache::set($key, $result, $cache);
}
}
return $result;
}
/**
* 并行(异步)调用远程服务
*
* @see http://php.net/manual/zh/class.yar-concurrent-client.php
* @param string $uri
* @param string $method
* @param array $parameter
* @param callable $callback
* @param int $timeout
* @return void
*/
public static function yarConcurrentCall($uri, $method, $parameters, $callback, $timeout = 3000)
{
\Yar_Concurrent_Client::call($uri, $method, $parameters, $callback, null, array(
YAR_OPT_PACKAGER => 'php',
YAR_OPT_TIMEOUT => $timeout,
YAR_OPT_CONNECT_TIMEOUT => $timeout
));
}
public static function yarConcurrentLoop($callback = null)
{
\Yar_Concurrent_Client::loop($callback);
}
/**
* 提交json格式数据请求java有关接口
*
* @param string $url 接口URL
* @param array $data 参数列表
* @param bool $returnJson 控制是否返回json格式数据
* @param int $timeout 超时时间
* @param array $cookie
* @return mixed
*/
public static function jsonPost($url, $data = array(), $returnJson = false, $timeout = 10, $cookie = array())
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
if (!empty($cookie)) {
$cookie_str = array();
foreach ($cookie as $key => $val) {
$cookie_str[] = urlencode($key) . '=' . urlencode($val);
}
curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookie_str));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (!empty($data)) {
$data_string = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
// 设置json的Header
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string)
));
}
$result = curl_exec($ch);
if (!$returnJson && !empty($result)) {
$result = json_decode($result, true);
}
curl_close($ch);
$data = array();
return $result;
}
}
<?php
/**
* 有货相关接口类
*
* @name Yohobuy
* @package library/Api
* @copyright yoho.inc
* @version 1.0 (2015-9-30 16:42:51)
* @author fei.hong <fei.hong@yoho.cn>
*/
namespace Api;
use Plugin\Cache;
class Yohobuy
{
/* 正式环境 */
// const API_URL = 'http://api2.open.yohobuy.com/';
// const API_URL2 = 'http://api.open.yohobuy.com/';
// const SERVICE_URL = 'http://service.api.yohobuy.com/';
// const YOHOBUY_URL = 'http://www.yohobuy.com/';
// const API_URL = 'http://apih5.yoho.cn/';
// const API_URL2 = 'http://apih5.yoho.cn/';
// const SERVICE_URL = 'http://serviceh5.yoho.cn/';
// const YOHOBUY_URL = 'http://www.yohobuy.com/';
// const API_OLD = 'http://api2.open.yohobuy.com/';
// /* 测试环境 */
// const API_URL = 'http://testapi.yoho.cn:28078/';
// const SERVICE_URL = 'http://testservice.yoho.cn:28077/';
// const YOHOBUY_URL = 'http://www.yohobuy.com/';
// const API_OLD = 'http://test2.open.yohobuy.com/';
/* 预览环境 */
// const API_URL = 'http://preapi.yoho.cn/';
// const API_URL2 = 'http://preapi.yoho.cn/';
// const SERVICE_URL = 'http://serviceh5.yoho.cn/';
// const YOHOBUY_URL = 'http://www.yohobuy.com/';
// const API_OLD = 'http://api2.open.yohobuy.com/';
/*PC重构地址*/
const API_URL = 'http://test2.open.yohobuy.com/';
const SERVICE_URL = 'http://test.service.api.yohobuy.com/';
const YOHOBUY_URL = 'http://www.yohobuy.com/';
/**
* 私钥列表
*
* @var array
*/
public static $privateKeyList = array(
'android' => 'fd4ad5fcfa0de589ef238c0e7331b585',
'iphone' => 'a85bb0674e08986c6b115d5e3a4884fa',
'ipad' => 'ad9fcda2e679cf9229e37feae2cdcf80',
'web' => '0ed29744ed318fd28d2c07985d3ba633',
);
/**
* 取得当前的客户端类型
*/
public static function clientType()
{
// 苹果设备
if (strstr($_SERVER['HTTP_USER_AGENT'], 'iPhone')) {
return 'iphone';
}
// 苹果IPAD
elseif (strstr($_SERVER['HTTP_USER_AGENT'], 'iPad')) {
return 'ipad';
}
elseif (stristr($_SERVER['HTTP_USER_AGENT'], 'android')) {
return 'android';
}
// 其它
else {
return 'web';
}
}
/**
* 取得当前的IP地址
*
* @param bool $int 返回int类型的ip地址,默认是
* @return mixed 当前的IP地址
*/
public static function ip($int = true)
{
if (isset($_SERVER['HTTP_CLIENT_IP']) && $_SERVER['HTTP_CLIENT_IP']) {
$onlineip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) {
$onlineip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$onlineip = $_SERVER['REMOTE_ADDR'];
}
return $int ? ip2long($onlineip) : $onlineip;
}
/**
* 取得公共的参数
*
* @return array
*/
public static function param()
{
$clientType = self::clientType();
$param = array(
'app_version' => '3.8.2',
'client_type' => $clientType,
'os_version' => 'yohobuy:h5',
'private_key' => self::$privateKeyList[$clientType],
'screen_size' => '720x1280',
'v' => '7',
);
return $param;
}
/**
* 构建URL
*
* @param string $url
* @param array $data
* @return string
*/
public static function httpBuildQuery($url, $data)
{
// 销毁私钥参数
if (isset($data['private_key'])) {
unset($data['private_key']);
}
if (strstr($url, '?') !== false) {
$url .= '&' . http_build_query($data, null, '&');
} else {
$url .= '?' . http_build_query($data, null, '&');
}
return $url;
}
/**
* get方式调用接口
*
* @param string $url 接口URL
* @param array $data 参数列表
* @parma mixed $cache 控制是否启用接口数据的缓存(时间单位为秒). 如3600表示缓存1小时, false表示不缓存
* @param bool $returnJson 控制是否返回json格式数据
* @param int $timeout 超时时间
* @return mixed
*/
public static function get($url, $data = array(), $cache = false, $returnJson = false, $timeout = 5)
{
// 销毁私钥参数
if (isset($data['private_key'])) {
unset($data['private_key']);
}
if (!empty($data)) {
$url = self::httpBuildQuery($url, $data);
}
/* 开启缓存的情况 */
if ($cache && USE_CACHE) {
// 先尝试获取一级缓存(master), 有数据则直接返回.
$result = Cache::get($url, 'master');
if (!empty($result)) {
return $result;
}
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if (!$returnJson && !empty($result)) {
$result = json_decode($result, true);
}
curl_close($ch);
$data = array();
/* 开启缓存的情况 */
if ($cache && USE_CACHE) {
// 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
if (empty($result)) {
$result = Cache::get($url, 'slave');
}
// 接口调用成功时,这里会设置一级(master)和二级(slave)的缓存数据.
else {
Cache::set($url, $result, $cache);
}
}
return $result;
}
/**
* post提交数据
*
* @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(), $returnJson = false, $timeout = 10, $header = array(), $cookie = array())
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
if (!empty($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
} else {
curl_setopt($ch, CURLOPT_HEADER, 0);
}
if (!empty($cookie)) {
$cookie_str = array();
foreach ($cookie as $key => $val) {
$cookie_str[] = urlencode($key) . '=' . urlencode($val);
}
curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookie_str));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
// 销毁私钥参数
if (isset($data['private_key'])) {
unset($data['private_key']);
}
if (!empty($data)) {
$str = http_build_query($data, null, '&');
// 新加支持application/x-www-form-urlencoded调用方式
//curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
}
$result = curl_exec($ch);
if (!$returnJson && !empty($result)) {
$result = json_decode($result, true);
}
curl_close($ch);
$data = array();
return $result;
}
/**
* 批量调用接口
*
* @param array $urlList 接口列表
* @param array $options CURL设置项
* @parma mixed $cache 控制是否启用接口数据的缓存(时间单位为秒). 如3600表示缓存1小时, false表示不缓存
* @param int $timeout 超时时间,单位是秒
* @return array
*/
public static function getMulti($urlList = array(), $options = array(), $cache = false, $timeout = 5)
{
/* 开启缓存的情况 */
if ($cache && USE_CACHE) {
$key = md5(implode(',', array_values($urlList)));
// 先尝试获取一级缓存(master), 有数据则直接返回.
$result = Cache::get($key, 'master');
if (!empty($result)) {
return $result;
}
}
$result = array();
$response = array();
$running = 0;
$data = '';
$error = '';
$defaultOptions = array(
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => $timeout,
CURLOPT_TIMEOUT => $timeout,
CURLOPT_NOSIGNAL => 1, //忽略所有的curl传递给php的信号,减少并发crash
);
$mh = curl_multi_init();
$ch = array();
// 应用CURL配置
if (empty($options)) {
$options = $defaultOptions;
} else {
$options = array_merge($defaultOptions, $options);
}
// 添加子链接句柄
foreach ($urlList as $name => $api) {
$ch[$name] = curl_init($api);
curl_setopt_array($ch[$name], $options);
curl_multi_add_handle($mh, $ch[$name]);
$result[$name] = array();
}
// 调用API接口
do {
do {
$status = curl_multi_exec($mh, $running);
} while ($status == CURLM_CALL_MULTI_PERFORM);
if ($status != CURLM_OK) {
break;
}
if ($running > 0) {
curl_multi_select($mh, 0.5);
}
} while ($running);
// 获取API接口响应的结果
foreach ($urlList as $name => $api) {
$error = curl_error($ch[$name]);
if ($error != '') {
continue;
}
$data = curl_multi_getcontent($ch[$name]);
if (!$data) {
continue;
}
$response = json_decode($data, true);
if (empty($response['data'])) {
continue;
}
$result[$name] = $response['data'];
curl_multi_remove_handle($mh, $ch[$name]);
curl_close($ch[$name]);
}
curl_multi_close($mh);
/* 开启缓存的情况 */
if ($cache && USE_CACHE) {
// 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
if (empty($result[$name])) {
$result = Cache::get($key, 'slave');
}
// 接口调用成功时,这里会设置一级(master)和二级(slave)的缓存数据.
else {
Cache::set($key, $result, $cache);
}
}
return $result;
}
/**
* rpc调用远程服务(YAR)
*
* @see http://php.net/manual/zh/yar-client.setopt.php
* @param string $uri
* @param string $method
* @param array $parameters
* @param mixed $cache 控制是否启用接口数据的缓存(时间单位为秒). 如3600表示缓存1小时, false表示不缓存
* @param int $timeout
* @return array
*/
public static function yarClient($uri, $method, $parameters = array(), $cache = false, $timeout = 3000)
{
/* 开启缓存的情况 */
if ($cache && USE_CACHE) {
$key = self::httpBuildQuery($uri . $method, $parameters);
// 先尝试获取一级缓存(master), 有数据则直接返回.
$result = Cache::get($key, 'master');
if (!empty($result)) {
return $result;
}
}
$client = new \Yar_Client($uri);
$client->SetOpt(YAR_OPT_PACKAGER, 'php');
$client->SetOpt(YAR_OPT_TIMEOUT, $timeout);
$client->SetOpt(YAR_OPT_CONNECT_TIMEOUT, $timeout);
try {
$result = call_user_func_array(array($client, $method), $parameters);
} catch (\Exception $e) {
$result = array();
}
/* 开启缓存的情况 */
if ($cache && USE_CACHE) {
// 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
if (empty($result)) {
$result = Cache::get($key, 'slave');
}
// 接口调用成功时,这里会设置一级(master)和二级(slave)的缓存数据.
else {
Cache::set($key, $result, $cache);
}
}
return $result;
}
/**
* 并行(异步)调用远程服务
*
* @see http://php.net/manual/zh/class.yar-concurrent-client.php
* @param string $uri
* @param string $method
* @param array $parameter
* @param callable $callback
* @param int $timeout
* @return void
*/
public static function yarConcurrentCall($uri, $method, $parameters, $callback, $timeout = 3000)
{
\Yar_Concurrent_Client::call($uri, $method, $parameters, $callback, null, array(
YAR_OPT_PACKAGER => 'php',
YAR_OPT_TIMEOUT => $timeout,
YAR_OPT_CONNECT_TIMEOUT => $timeout
));
}
public static function yarConcurrentLoop($callback = null)
{
\Yar_Concurrent_Client::loop($callback);
}
/**
* 提交json格式数据请求java有关接口
*
* @param string $url 接口URL
* @param array $data 参数列表
* @param bool $returnJson 控制是否返回json格式数据
* @param int $timeout 超时时间
* @param array $cookie
* @return mixed
*/
public static function jsonPost($url, $data = array(), $returnJson = false, $timeout = 10, $cookie = array())
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
if (!empty($cookie)) {
$cookie_str = array();
foreach ($cookie as $key => $val) {
$cookie_str[] = urlencode($key) . '=' . urlencode($val);
}
curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookie_str));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (!empty($data)) {
$data_string = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
// 设置json的Header
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string)
));
}
$result = curl_exec($ch);
if (!$returnJson && !empty($result)) {
$result = json_decode($result, true);
}
curl_close($ch);
$data = array();
return $result;
}
}
... ...
<?php
namespace Configs;
/**
* 缓存配置文件
*/
class CacheConfig
{
/* 公共的数据缓存 */
const KEY_COMMON_SIDE_NAV = 'key_common_side_nav'; // 公共的侧边栏
const KEY_COMMON_CATEGORY_CLASSES = 'key_common_category_classes'; // 公共的品类数据
const KEY_COMMON_CATEGORY_CLASSES_NAMES = 'key_common_category_classes_names'; // 公共的品类名称列表
/* 控制器方法中的数据缓存 */
const KEY_ACTION_INDEX_INDEX = 'key_action_index_index'; // 频道选择
const KEY_ACTION_BOYS_INDEX = 'key_action_boys_index'; // 男生首页
const KEY_ACTION_GIRLS_INDEX = 'key_action_girls_index'; // 女生首页
const KEY_ACTION_KIDS_INDEX = 'key_action_kids_index'; // 潮童首页
const KEY_ACTION_LIFESTYLE_INDEX = 'key_action_lifestyle_index'; // 创意生活首页
const KEY_ACTION_INDEX_CONFIG = 'key_action_index_config'; // 频道配置
const KEY_ACTION_BOYS_BOTTOM_BANNER = 'key_action_boys_bottom_banner'; // 男生首页底部banner
const KEY_ACTION_GRILS_BOTTOM_BANNER = 'key_action_girls_bottom_banner';// 女生首页底部banner
const KEY_ACTION_PRODUCT_NEWSALE_INDEX_BOYS = 'key_action_product_newsale_index_boys'; // 男生新品到着
const KEY_ACTION_PRODUCT_NEWSALE_INDEX_GIRLS = 'key_action_product_newsale_index_girls'; // 女生新品到着
const KEY_ACTION_PRODUCT_NEWSALE_INDEX_KIDS = 'key_action_product_newsale_index_kids'; // 潮童新品到着
const KEY_ACTION_PRODUCT_NEWSALE_INDEX_LIFESTYLE = 'key_action_product_newsale_index_lifestyle'; // 创意生活新品到着
const KEY_ACTION_PRODUCT_NEWSALE_DISCOUNT_BOYS = 'key_action_product_newsale_discount_boys'; // 男生热卖排行
const KEY_ACTION_PRODUCT_NEWSALE_DISCOUNT_GIRLS = 'key_action_product_newsale_discount_girls'; // 女生热卖排行
const KEY_ACTION_PRODUCT_NEWSALE_DISCOUNT_KIDS = 'key_action_product_newsale_discount_kids'; // 潮童热卖排行
const KEY_ACTION_PRODUCT_NEWSALE_DISCOUNT_LIFESTYLE = 'key_action_product_newsale_discount_lifestyle'; // 创意生活热卖排行
const KEY_ACTION_PRODUCT_INDEX = 'key_action_product_index'; // 品类商品列表
const KEY_ACTION_PRODUCT_BRAND = 'key_action_product_brand'; // 品类商品列表
const KEY_ACTION_PRODUCT_BRAND_DOMAINS = 'key_action_product_brand_domains'; // 所有品牌域名列表
const KEY_ACTION_PRODUCT_BRAND_NAMES = 'key_action_product_brand_names'; // 所有品牌名称列表
const KEY_ACTION_PRODUCT_BRAND_LOGO = 'key_action_product_brand_logo'; // 品牌信息
const KEY_ACTION_PRODUCT_BRAND_LOGO_DOMAIN = 'key_action_product_brand_logo_domain'; // 品牌信息通过域名取得
const KEY_ACTION_SEARCH_SEARCH = 'key_action_search_search'; // 搜索的数据
const KEY_ACTION_SEARCH_FILTER = 'key_action_search_filter'; // 搜索的过滤条件
const KEY_ACTION_HOME_GRADE = 'key_action_home_grade';//会员等级
const KEY_ACTION_HOME_PREFERENTIAL = 'key_action_home_preferential';//会员特权详情
const KEY_ACTION_HOME_ORDER_ORDER = 'key_action_home_order_order';//订单列表
const KEY_ACTION_GUANG_PLUSSTAR_INDEX = 'key_action_guang_plusstar_index'; // 国际优选
const KEY_ACTION_GUANG_PLUSSTAR_LIST = 'key_action_guang_plusstar_list'; // 明星原创
const KEY_ACTION_GUANG_PLUSTAR_DATA = 'key_action_guang_plustar_data'; // 逛PLUSTAR
const KEY_ACTION_GUANG_DETAIL_DATA = 'key_action_guang_detail_data'; // 逛内容详情
const KEY_ACTION_GUANG_RSS_DATA = 'key_action_guang_detail_data'; // 逛订阅内容
const KEY_ACTION_ADDRESS_LIST_DATA = 'key_action_address_list_data'; // 地址树数据
}
<?php
namespace Configs;
/**
* 缓存配置文件
*/
class CacheConfig
{
/* 公共的数据缓存 */
const KEY_COMMON_SIDE_NAV = 'key_common_side_nav'; // 公共的侧边栏
const KEY_COMMON_CATEGORY_CLASSES = 'key_common_category_classes'; // 公共的品类数据
const KEY_COMMON_CATEGORY_CLASSES_NAMES = 'key_common_category_classes_names'; // 公共的品类名称列表
/* 控制器方法中的数据缓存 */
const KEY_ACTION_INDEX_INDEX = 'key_action_index_index'; // 频道选择
const KEY_ACTION_BOYS_INDEX = 'key_action_boys_index'; // 男生首页
const KEY_ACTION_GIRLS_INDEX = 'key_action_girls_index'; // 女生首页
const KEY_ACTION_KIDS_INDEX = 'key_action_kids_index'; // 潮童首页
const KEY_ACTION_LIFESTYLE_INDEX = 'key_action_lifestyle_index'; // 创意生活首页
const KEY_ACTION_BOYS_BOTTOM_BANNER = 'key_action_boys_bottom_banner'; // 男生首页底部banner
const KEY_ACTION_GRILS_BOTTOM_BANNER = 'key_action_girls_bottom_banner';// 女生首页底部banner
const KEY_ACTION_PRODUCT_NEWSALE_INDEX_BOYS = 'key_action_product_newsale_index_boys'; // 男生新品到着
const KEY_ACTION_PRODUCT_NEWSALE_INDEX_GIRLS = 'key_action_product_newsale_index_girls'; // 女生新品到着
const KEY_ACTION_PRODUCT_NEWSALE_INDEX_KIDS = 'key_action_product_newsale_index_kids'; // 潮童新品到着
const KEY_ACTION_PRODUCT_NEWSALE_INDEX_LIFESTYLE = 'key_action_product_newsale_index_lifestyle'; // 创意生活新品到着
const KEY_ACTION_PRODUCT_NEWSALE_DISCOUNT_BOYS = 'key_action_product_newsale_discount_boys'; // 男生热卖排行
const KEY_ACTION_PRODUCT_NEWSALE_DISCOUNT_GIRLS = 'key_action_product_newsale_discount_girls'; // 女生热卖排行
const KEY_ACTION_PRODUCT_NEWSALE_DISCOUNT_KIDS = 'key_action_product_newsale_discount_kids'; // 潮童热卖排行
const KEY_ACTION_PRODUCT_NEWSALE_DISCOUNT_LIFESTYLE = 'key_action_product_newsale_discount_lifestyle'; // 创意生活热卖排行
const KEY_ACTION_PRODUCT_INDEX = 'key_action_product_index'; // 品类商品列表
const KEY_ACTION_PRODUCT_BRAND = 'key_action_product_brand'; // 品类商品列表
const KEY_ACTION_PRODUCT_BRAND_DOMAINS = 'key_action_product_brand_domains'; // 所有品牌域名列表
const KEY_ACTION_PRODUCT_BRAND_NAMES = 'key_action_product_brand_names'; // 所有品牌名称列表
const KEY_ACTION_PRODUCT_BRAND_LOGO = 'key_action_product_brand_logo'; // 品牌信息
const KEY_ACTION_PRODUCT_BRAND_LOGO_DOMAIN = 'key_action_product_brand_logo_domain'; // 品牌信息通过域名取得
const KEY_ACTION_SEARCH_SEARCH = 'key_action_search_search'; // 搜索的数据
const KEY_ACTION_SEARCH_FILTER = 'key_action_search_filter'; // 搜索的过滤条件
const KEY_ACTION_HOME_GRADE = 'key_action_home_grade';//会员等级
const KEY_ACTION_HOME_PREFERENTIAL = 'key_action_home_preferential';//会员特权详情
const KEY_ACTION_HOME_ORDER_ORDER = 'key_action_home_order_order';//订单列表
const KEY_ACTION_GUANG_PLUSSTAR_INDEX = 'key_action_guang_plusstar_index'; // 国际优选
const KEY_ACTION_GUANG_PLUSSTAR_LIST = 'key_action_guang_plusstar_list'; // 明星原创
const KEY_ACTION_GUANG_PLUSTAR_DATA = 'key_action_guang_plustar_data'; // 逛PLUSTAR
const KEY_ACTION_GUANG_DETAIL_DATA = 'key_action_guang_detail_data'; // 逛内容详情
const KEY_ACTION_GUANG_RSS_DATA = 'key_action_guang_detail_data'; // 逛订阅内容
const KEY_ACTION_ADDRESS_LIST_DATA = 'key_action_address_list_data'; // 地址树数据
const KEY_INDEX_BRANDS_LIST_DATA = 'key_index_brands_list_data';//频道brands数据
}
... ...
... ... @@ -6,6 +6,93 @@ class ChannelConfig
* 最新上架分类
*/
public static $newArrivalSortList = array(
//首页
'boys' => array(
array(
'misort' => 16,
'viewNum' => 5
),//卫衣
array(
'misort' => 44,
'viewNum' => 5
),//休闲/运动鞋
array(
'misort' => 21,
'viewNum' => 5
),//夹克
array(
'misort' => 26,
'viewNum' => 5
),//休闲裤
array(
'misort' => 11,
'viewNum' => 5
),//T恤
array(
'misort' => 49,
'viewNum' => 5
),//双肩包
array(
'misort' => 12,
'viewNum' => 5
),//衬衫
array(
'misort' => 27,
'viewNum' => 5
),//牛仔裤
array(
'misort' => 45,
'viewNum' => 5
),//靴子
array(
'misort' => 257,
'viewNum' => 5
),//毛衣/针织
array(
'misort' => 226,
'viewNum' => 5
),//防风外套
array(
'misort' => 22,
'viewNum' => 5
),//大衣/风衣
array(
'misort' => 50,
'viewNum' => 5
),//手拎包/单肩包
array(
'misort' => 18,
'viewNum' => 5
),//羽绒服
array(
'misort' => 20,
'viewNum' => 5
),//棉衣
array(
'misort' => 345,
'viewNum' => 5
),//运动裤
array(
'misort' => 48,
'viewNum' => 5
),//时装鞋
array(
'misort' => 60,
'viewNum' => 5
),//帽子
array(
'misort' => 61,
'viewNum' => 5
),//太阳镜/眼镜
array(
'misort' => 65,
'viewNum' => 5
),//首饰
array(
'misort' => 59,
'viewNum' => 5
),//手表
),
//女首上架分类
'girls' => array(
array(
... ...
<?php
namespace LibModels\Wap\Product;
use Api\Yohobuy;
use Api\Sign;
/**
* 搜索有关数据操作类
*
* @name SearchData
* @package Library/LibModels/wap/Product
* @copyright yoho.inc
* @version 1.0 (2015-10-8)
* @author gtskk <rocky.zhang@yoho.cn>
*/
class SearchData
{
/**
* 获取搜索的服务地址
*
* 备注:此处是根据环境来确定使用阿里云内网还是外网的URL
*
* @return string
*/
private static function getSearchUrl()
{
defined('APPLICATION_ENV') || define('APPLICATION_ENV', 'developer');
switch (APPLICATION_ENV) {
case 'release':
return 'http://100.98.132.63/yohosearch/search.json';
case 'test':
case 'preview':
case 'developer':
default:
return 'http://101.200.31.165/yohosearch/search.json';
}
}
/**
* 模糊搜索提供的关键词
*
* @param string $keyword 关键词
* @return array 根据给定关键词搜索到的结果,包括数据数目count和提供的关键词keyword
*/
public static function searchFuzzyDatas($keyword)
{
// 构建必传参数
$param = Yohobuy::param();
$param['keyword'] = $keyword;
$param['method'] = 'app.search.fuzzy';
$param['client_secret'] = Sign::getSign($param);
return Yohobuy::get(Yohobuy::API_URL, $param);
}
/**
* 根据给定查询数据搜索数据列表 (老的)
*
* @param string $query 查询条件, 默认为null
* @param string $brand 品牌,默认为null
* @param string $gender 性别,默认为null,"1,3"表示男, "2,3"表示女, "1,2,3"表示全部
* @param integer $color 颜色id
* @param integer $size 尺码id
* @param integer $price 价格
* @param string $p_d 折扣,默认为null
* @param string $sort 商品所属品类,默认为null
* @param string $order 排序方式,默认为按照时间倒序排列s_t_desc,
* s_t_asc表示按时间正序排列,
* s_p_asc表示按价格正序排列,
* s_p_desc表示按价格倒序排列,
* p_d_asc表示按折扣正序排列,
* p_d_desc表示按折扣倒序排列
* @param integer $page 指定查询是多少页,默认为第一页
* @param integer $limit 指定查询多少个,默认是60个
* @param integer $channel表示哪个频道,1表示男生,2表示女生,3表示潮童,4表示创意生活
* @return array 搜索到的数据
*/
public static function searchByCondition($condition)
{
$param = Yohobuy::param();
$param['method'] = 'app.search.li';
if (!isset($condition['order'])) {
$param['order'] = 's_t_desc';
}
if (!isset($condition['page'])) {
$param['page'] = 1;
}
if (!isset($condition['limit'])) {
$param['limit'] = 60;
}
if (!empty($condition)) {
$param += $condition;
}
$param['client_secret'] = Sign::getSign($param);
return Yohobuy::get(Yohobuy::API_URL, $param);
}
/**
* 根据给定查询数据搜索数据列表 (新的)
*
* @param string $query 查询条件, 默认为null
* @param string $brand 品牌,默认为null
* @param string $gender 性别,默认为null,"1,3"表示男, "2,3"表示女, "1,2,3"表示全部
* @param integer $color 颜色id
* @param integer $size 尺码id
* @param integer $price 价格
* @param string $p_d 折扣,默认为null
* @param string $sort 商品所属品类,默认为null
* @param string $order 排序方式,默认为按照时间倒序排列s_t_desc,
* s_t_asc表示按时间正序排列,
* s_p_asc表示按价格正序排列,
* s_p_desc表示按价格倒序排列,
* p_d_asc表示按折扣正序排列,
* p_d_desc表示按折扣倒序排列
* @param integer $page 指定查询是多少页,默认为第一页
* @param integer $limit 指定查询多少个,默认是60个
* @param integer $channel表示哪个频道,1表示男生,2表示女生,3表示潮童,4表示创意生活
* @return array 搜索到的数据
*/
public static function searchElasticByCondition($condition, $cache = false)
{
// 排序数据映射表
$orderMaps = array(
's_t_desc' => 'shelve_time:desc',
's_t_asc' => 'shelve_time:asc',
's_p_asc' => 'sales_price:asc',
's_p_desc' => 'sales_price:desc',
'p_d_desc' => 'discount:desc',
'p_d_asc' => 'discount:asc',
'skn_desc' => 'product_skn:desc',
'skn_asc' => 'product_skn:asc',
'activities_desc' => 'activities.order_by:desc',
'activities_asc' => 'activities.order_by:asc',
's_n_asc' => 'sales_num:asc',
's_n_desc' => 'sales_num:desc',
'activities_id_desc' => 'activities.activity_id:desc',
'activities_id_asc' => 'activities.activity_id:asc',
);
$param = array();
$param['status'] = 1; // 是否上架,1表示在架,2表示不在
$param['sales'] = 'Y'; // 只搜索销售的产品
$param['stocknumber'] = 1; // 过滤掉已售罄的商品
// $param['needFilter'] = 1; // 是否需要返回筛选条件
if (!isset($condition['order'])) {
$param['order'] = $orderMaps['s_t_desc'];
} else {
$param['order'] = $orderMaps[$condition['order']];
}
if (!isset($condition['page'])) {
$param['page'] = 1;
}
if (!isset($condition['limit'])) {
$param['viewNum'] = 60;
} else {
$param['viewNum'] = $condition['limit'];
unset($condition['limit']);
}
if (!empty($condition)) {
$param += $condition;
}
return Yohobuy::get(self::getSearchUrl(), $param, $cache);
}
}
<?php
namespace LibModels\Wap\Product;
use Api\Yohobuy;
use Api\Sign;
/**
* 搜索有关数据操作类
*
* @name SearchData
* @package Library/LibModels/wap/Product
* @copyright yoho.inc
* @version 1.0 (2015-10-8)
* @author gtskk <rocky.zhang@yoho.cn>
*/
class SearchData
{
/**
* 获取搜索的服务地址
*
* 备注:此处是根据环境来确定使用阿里云内网还是外网的URL
*
* @return string
*/
private static function getSearchUrl()
{
defined('APPLICATION_ENV') || define('APPLICATION_ENV', 'developer');
switch (APPLICATION_ENV) {
case 'release':
return 'http://100.98.132.63/yohosearch/search.json';
case 'test':
case 'preview':
case 'developer':
default:
return 'http://101.200.31.165/yohosearch/search.json';
}
}
/**
* 模糊搜索提供的关键词
*
* @param string $keyword 关键词
* @return array 根据给定关键词搜索到的结果,包括数据数目count和提供的关键词keyword
*/
public static function searchFuzzyDatas($keyword)
{
// 构建必传参数
$param = Yohobuy::param();
$param['keyword'] = $keyword;
$param['method'] = 'app.search.fuzzy';
$param['client_secret'] = Sign::getSign($param);
return Yohobuy::get(Yohobuy::API_URL, $param);
}
/**
* 根据给定查询数据搜索数据列表 (老的)
*
* @param string $query 查询条件, 默认为null
* @param string $brand 品牌,默认为null
* @param string $gender 性别,默认为null,"1,3"表示男, "2,3"表示女, "1,2,3"表示全部
* @param integer $color 颜色id
* @param integer $size 尺码id
* @param integer $price 价格
* @param string $p_d 折扣,默认为null
* @param string $sort 商品所属品类,默认为null
* @param string $order 排序方式,默认为按照时间倒序排列s_t_desc,
* s_t_asc表示按时间正序排列,
* s_p_asc表示按价格正序排列,
* s_p_desc表示按价格倒序排列,
* p_d_asc表示按折扣正序排列,
* p_d_desc表示按折扣倒序排列
* @param integer $page 指定查询是多少页,默认为第一页
* @param integer $limit 指定查询多少个,默认是60个
* @param integer $channel表示哪个频道,1表示男生,2表示女生,3表示潮童,4表示创意生活
* @return array 搜索到的数据
*/
public static function searchByCondition($condition)
{
$param = Yohobuy::param();
$param['method'] = 'app.search.li';
if (!isset($condition['order'])) {
$param['order'] = 's_t_desc';
}
if (!isset($condition['page'])) {
$param['page'] = 1;
}
if (!isset($condition['limit'])) {
$param['limit'] = 60;
}
if (!empty($condition)) {
$param += $condition;
}
$param['client_secret'] = Sign::getSign($param);
return Yohobuy::get(Yohobuy::API_URL, $param);
}
/**
* 根据给定查询数据搜索数据列表 (新的)
*
* @param string $query 查询条件, 默认为null
* @param string $brand 品牌,默认为null
* @param string $gender 性别,默认为null,"1,3"表示男, "2,3"表示女, "1,2,3"表示全部
* @param integer $color 颜色id
* @param integer $size 尺码id
* @param integer $price 价格
* @param string $p_d 折扣,默认为null
* @param string $sort 商品所属品类,默认为null
* @param string $order 排序方式,默认为按照时间倒序排列s_t_desc,
* s_t_asc表示按时间正序排列,
* s_p_asc表示按价格正序排列,
* s_p_desc表示按价格倒序排列,
* p_d_asc表示按折扣正序排列,
* p_d_desc表示按折扣倒序排列
* @param integer $page 指定查询是多少页,默认为第一页
* @param integer $limit 指定查询多少个,默认是60个
* @param integer $channel表示哪个频道,1表示男生,2表示女生,3表示潮童,4表示创意生活
* @return array 搜索到的数据
*/
public static function searchElasticByCondition($condition, $cache = false)
{
// 排序数据映射表
$orderMaps = array(
's_t_desc' => 'shelve_time:desc',
's_t_asc' => 'shelve_time:asc',
's_p_asc' => 'sales_price:asc',
's_p_desc' => 'sales_price:desc',
'p_d_desc' => 'discount:desc',
'p_d_asc' => 'discount:asc',
'skn_desc' => 'product_skn:desc',
'skn_asc' => 'product_skn:asc',
'activities_desc' => 'activities.order_by:desc',
'activities_asc' => 'activities.order_by:asc',
's_n_asc' => 'sales_num:asc',
's_n_desc' => 'sales_num:desc',
'activities_id_desc' => 'activities.activity_id:desc',
'activities_id_asc' => 'activities.activity_id:asc',
);
$param = array();
$param['status'] = 1; // 是否上架,1表示在架,2表示不在
$param['sales'] = 'Y'; // 只搜索销售的产品
$param['stocknumber'] = 1; // 过滤掉已售罄的商品
// $param['needFilter'] = 1; // 是否需要返回筛选条件
if (!isset($condition['order'])) {
$param['order'] = $orderMaps['s_t_desc'];
} else {
$param['order'] = $orderMaps[$condition['order']];
}
if (!isset($condition['page'])) {
$param['page'] = 1;
}
if(isset($condition['viewNum'])) {
$param['viewNum'] = $condition['viewNum'];
} else if (!isset($condition['limit'])) {
$param['viewNum'] = 60;
} else {
$param['viewNum'] = $condition['limit'];
unset($condition['limit']);
}
if (!empty($condition)) {
$param += $condition;
}
return Yohobuy::get(self::getSearchUrl(), $param, $cache);
}
}
... ...
<?php
namespace Plugin;
use Hood\Cache as HoodCache;
/**
* 数据缓存的操作类
*
* @name Cache
* @package Plugin
* @copyright yoho.inc
* @version 1.0 (2015-10-10 11:23:47)
* @author fei.hong <fei.hong@yoho.cn>
*/
class Cache
{
/**
* 设置缓存
*
* 备注:
* 采用主从分布式缓存
* master 代表主缓存服务器
* slave 代表从缓存服务器
*
* 作用:
* 在后端的服务异常挂掉了并且主服务器缓存(master)的数据时间到期后,
* 能获取从服务器缓存(slave)的数据,不影响前端页面的正常访问.
*
* @param string $key 键名
* @param mixed $value 需要缓存的数据
* @param int $expire 缓存有效期(单位秒, 0表示永久)
* @return void
*/
public static function set($key, $value, $expire = 3600)
{
try {
// WINDOWS
if (DIRECTORY_SEPARATOR === '\\') {
HoodCache::Memcache('master')->set(self::makeKey($key, 'master'), $value, $expire);
HoodCache::Memcache('slave')->set(self::makeKey($key, 'slave'), $value, 86400); // 二级缓存1天
}
// LINUX
else {
HoodCache::Memcached('master')->set(self::makeKey($key, 'master'), $value, $expire);
HoodCache::Memcached('slave')->set(self::makeKey($key, 'slave'), $value, 86400); // 二级缓存1天
}
} catch (Exception $e) {
// do nothing
}
}
/**
* 获取缓存
*
* @param string $key 键名
* @param string $node master代表主服务器, slave代表从服务器
* @return mixed
*/
public static function get($key, $node = 'master')
{
$result = array();
try {
// WINDOWS
if (DIRECTORY_SEPARATOR === '\\') {
$result = HoodCache::Memcache($node)->get(self::makeKey($key, $node));
}
// LINUX
else {
$result = HoodCache::Memcached($node)->get(self::makeKey($key, $node));
}
} catch (Exception $e) {
$result = array();
}
return $result;
}
/**
* 清除缓存
*
* @param string $key 键名
* @return void
*/
public static function delete($key)
{
// WINDOWS
if (DIRECTORY_SEPARATOR === '\\') {
HoodCache::Memcache('master')->delete(self::makeKey($key, 'master'));
HoodCache::Memcache('slave')->delete(self::makeKey($key, 'slave'));
}
// LINUX
else {
HoodCache::Memcached('master')->delete(self::makeKey($key, 'master'));
HoodCache::Memcached('slave')->delete(self::makeKey($key, 'slave'));
}
}
/**
* 生成键名
*
* @param string $key 键名
* @param string $prefix 前缀标识
* @return string
*/
public static function makeKey($key, $prefix)
{
return md5($prefix . '_' . $key);
}
}
<?php
namespace Plugin;
use Hood\Cache as HoodCache;
/**
* 数据缓存的操作类
*
* @name Cache
* @package Plugin
* @copyright yoho.inc
* @version 1.0 (2015-10-10 11:23:47)
* @author fei.hong <fei.hong@yoho.cn>
*/
class Cache
{
/**
* 设置缓存
*
* 备注:
* 采用主从分布式缓存
* master 代表主缓存服务器
* slave 代表从缓存服务器
*
* 作用:
* 在后端的服务异常挂掉了并且主服务器缓存(master)的数据时间到期后,
* 能获取从服务器缓存(slave)的数据,不影响前端页面的正常访问.
*
* @param string $key 键名
* @param mixed $value 需要缓存的数据
* @param int $expire 缓存有效期(单位秒, 0表示永久)
* @return void
*/
public static function set($key, $value, $expire = 3600)
{
try {
// WINDOWS
if (DIRECTORY_SEPARATOR === '\\') {
HoodCache::Memcache('master')->set(self::makeKey($key, 'master'), $value, $expire);
HoodCache::Memcache('slave')->set(self::makeKey($key, 'slave'), $value, 86400); // 二级缓存1天
}
// LINUX
else {
HoodCache::Memcached('master')->set(self::makeKey($key, 'master'), $value, $expire);
HoodCache::Memcached('slave')->set(self::makeKey($key, 'slave'), $value, 86400); // 二级缓存1天
}
} catch (Exception $e) {
// do nothing
}
}
/**
* 获取缓存
*
* @param string $key 键名
* @param string $node master代表主服务器, slave代表从服务器
* @return mixed
*/
public static function get($key, $node = 'master')
{
$result = array();
try {
// WINDOWS
if (DIRECTORY_SEPARATOR === '\\') {
$result = HoodCache::Memcache($node)->get(self::makeKey($key, $node));
}
// LINUX
else {
$result = HoodCache::Memcached($node)->get(self::makeKey($key, $node));
}
} catch (Exception $e) {
$result = array();
}
return $result;
}
/**
* 清除缓存
*
* @param string $key 键名
* @return void
*/
public static function delete($key)
{
// WINDOWS
if (DIRECTORY_SEPARATOR === '\\') {
HoodCache::Memcache('master')->delete(self::makeKey($key, 'master'));
HoodCache::Memcache('slave')->delete(self::makeKey($key, 'slave'));
}
// LINUX
else {
HoodCache::Memcached('master')->delete(self::makeKey($key, 'master'));
HoodCache::Memcached('slave')->delete(self::makeKey($key, 'slave'));
}
}
/**
* 生成键名
*
* @param string $key 键名
* @param string $prefix 前缀标识
* @return string
*/
public static function makeKey($key, $prefix)
{
return md5($prefix . '_' . $key);
}
}
... ...
<?php
namespace Plugin\DataProcess;
use Plugin\Helpers;
/**
* 楼层数据处理类
*/
class FloorProcess
{
/**
* 频道参数
*
* @var array
*/
private static $channel = array(
'1' => '1,3',
'2' => '2,3'
);
/**
* 处理首页内容
* @param $data
* @param $type 类型 默认1:男首页 2:女首页 3:kids 4:lifestyle
*/
public static function getContent($data, $type = 1)
{
if (empty($data['list'])) {
return array();
}
$result = array();
$build = array();
foreach ($data['list'] as $v) {
if (empty($v)) {
continue;
}
$fun = $v['template_name'];
if (empty($v['data']) || !is_callable("self::$fun")) {
continue;
}
$build = self::$fun($v['data'], $type);
if (empty($build)) {
continue;
}
$result[] = $build;
}
$build = array();
return $result;
}
/**
* 焦点图数据处理方法
* @param array $data 焦点图数据
* @param $type 类型 默认1:男首页 2:女首页 3:kids 4:lifestyle
* @return array 处理之后的焦点图数据
*/
private static function focus($data, $type)
{
$result = array();
foreach ($data as $one) {
$result['bannerTop']['list'][] = Helpers::formatBanner($one, 640, 240);
}
return $result;
}
/**
* 图标
*/
private static function floor($data)
{
$result = array();
foreach ($data as &$one) {
$one['url'] = Helpers::getFilterUrl($one['url']);
$one['img'] = Helpers::getImageUrl($one['src'], 98, 98);
unset($one['src']);
}
$result['iconsEnter']['list'] = $data;
return $result;
}
/**
* 图标列表数据处理方法
*
* @param array $data 图标列表数据
* @param $type 类型 默认1:男首页 2:女首页 3:kids 4:lifestyle
* @return array 处理之后的图标列表数据
*/
private static function app_icon_list($data, $type)
{
$result = array();
foreach ($data as &$one) {
$one['url'] = Helpers::getFilterUrl($one['url']);
$one['img'] = Helpers::getImageUrl($one['src'], 98, 98);
unset($one['src']);
}
$result['iconsEnter']['list'] = $data;
return $result;
}
/**
* 单广告图片数据处理方法
*
* @param array $data 单图片数据
* @param $type 类型 默认1:男首页 2:女首页 3:kids 4:lifestyle
* @return array 处理之后的单图片数据
*/
private static function single_image($data, $type)
{
$result = array();
foreach ($data as &$one) {
$one['url'] = Helpers::getFilterUrl($one['url']);
$one['img'] = Helpers::getImageUrl($one['src'], 640, 200);
unset($one['src']);
}
$result['banner'] = $data;
return $result;
}
/**
* 女生品类
* 推荐(1T-12F)
*/
private static function recommend_content_three($data, $type)
{
$result = array();
$build = array();
if (!empty($data['big_image'][0])) {
$build['img'] = Helpers::getImageUrl($data['big_image'][0]['src'], 640, 200);
$build['url'] = Helpers::getFilterUrl($data['big_image'][0]['url']);
$result['hotCategory']['banner'] = $build;
}
$build = array();
foreach ($data['list'] as $one) {
$build['url'] = Helpers::getFilterUrl($one['url']);
$build['img'] = Helpers::getImageUrl($one['src'], 128, 140);
$build['textCn'] = $one['title'];
$result['hotCategory']['list'][] = $build;
}
if (isset($data['title']['more_url'])) {
$data['title']['more_url'] = Helpers::getFilterUrl($data['title']['more_url']);
}
return $result;
}
/**
* 带标题的12个小图推荐数据处理方法
*
* @param array $data 带标题的12个小图推荐数据
* @param $type 类型 默认1:男首页 2:女首页 3:kids 4:lifestyle
* @return array 处理之后的带标题的12个小图推荐数据
*/
private static function recommend_content_five($data, $type)
{
$result = array();
foreach ($data['list'] as &$one) {
if (isset($one['url'])) {
$one['url'] = Helpers::getFilterUrl($one['url']);
}
$one['img'] = Helpers::getImageUrl($one['src'], 140, 140);
$one['textCn'] = $one['title'];
unset($one['src']);
}
$data['is_show_name'] = $data['title']['is_show'];
$data['name'] = $data['title']['title'];
unset($data['title']);
if (isset($data['title']['more_url'])) {
$data['title']['more_url'] = Helpers::getFilterUrl($data['title']['more_url']);
}
$result['hotCategory'] = $data;
return $result;
}
/**
* 推荐品牌数据处理方法
* @param array $data 推荐品牌数据
* @param $type 类型 默认1:男首页 2:女首页 3:kids 4:lifestyle
* @return array 处理之后的推荐品牌数据
*/
private static function app_hot_brands($data, $type)
{
$result = array();
$data['is_show_name'] = (isset($data['is_show_name']) && $data['is_show_name'] === 'Y') ? 1 : 0;
$data['name'] = $data['title']['title'];
unset($data['title']);
foreach ($data['list'] as &$one) {
if (isset($one['url'])) {
$one['url'] = Helpers::getFilterUrl($one['url']);
isset(self::$channel[$type]) && $one['url'] .= '?gender=' . self::$channel[$type];
}
$one['img'] = Helpers::getImageUrl($one['src'], 158, 174);
unset($one['src']);
}
$data['more'] = '/brands?channel=' . $type;
$result['hotBrands'] = $data;
return $result;
}
/**
* 潮人·搭配数据处理方法
* @param array $data 潮人·搭配数据
* @param $type 类型 默认1:男首页 2:女首页 3:kids 4:lifestyle
* @return array 处理之后的潮人·搭配数据
*/
private static function trendsetter_collocation($data, $type)
{
$result = array();
foreach ($data as $key => &$value) {
if ($key == 'title') {
$value['more_url'] = Helpers::url('', array('id' => 3, 'gender' => self::$channel[$type]), 'guang');
}
if ($key == 'article') {
foreach ($value as &$one) {
if (isset($one['url'])) {
$one['url'] = Helpers::getFilterUrl($one['url']);
}
$one['img'] = Helpers::getImageUrl($one['src'], 400, 400);
unset($one['src']);
}
}
if ($key == 'recommend_collocation') {
foreach ($value as &$one) {
if (isset($one['url'])) {
$one['url'] = Helpers::getFilterUrl($one['url']);
}
$one['img'] = Helpers::getImageUrl($one['src'], 140, 140);
unset($one['src']);
}
}
}
$result['trendColloaction'] = $data;
return $result;
}
/**
* 潮品·话题数据处理方法
* @param array $data 潮品·话题数据
* @param $type 类型 默认1:男首页 2:女首页 3:kids 4:lifestyle
* @return array 处理之后的潮品·话题数据
*/
private static function trendgoods_topic($data, $type)
{
$result = array();
foreach ($data as $key => &$value) {
if ($key == 'title') {
$urlParam = array('id' => 4);
isset(self::$channel[$type]) && $urlParam['gender'] = self::$channel[$type];
$value['more_url'] = Helpers::url('', $urlParam, 'guang');
}
if ($key == 'list') {
foreach ($value as &$one) {
if (isset($one['url'])) {
$one['url'] = Helpers::getFilterUrl($one['url']);
}
$one['img'] = Helpers::getImageUrl($one['src'], 580, 360);
unset($one['src']);
}
}
}
$result['trendTopics'] = $data;
return $result;
}
/**
* 一张大图一个大的推荐和4个小图推荐数据处理方法
* @param array $data 一张大图一个大的推荐和4个小图推荐数据
* @param $type 类型 默认1:男首页 2:女首页 3:kids 4:lifestyle
* @return array 处理之后的一张大图一个大的推荐和4个小图推荐数据
*/
private static function recommend_content_one($data, $type)
{
$result = array();
foreach ($data['big_image'] as &$one) {
if (isset($one['url'])) {
$one['url'] = Helpers::getFilterUrl($one['url']);
}
$one['img'] = Helpers::getImageUrl($one['src'], 640, 403);
unset($one['src']);
}
// 区别多张图与一张图
if (count($data['big_image']) > 1) {
$result['big_image']['big_list'] = $data['big_image'];
unset($data['big_image']);
}
foreach ($data['list'] as $kk => &$one) {
if ($kk == 0) {
$one['img'] = Helpers::getImageUrl($one['src'], 253, 383);
} else {
$one['img'] = Helpers::getImageUrl($one['src'], 191, 191);
}
if (isset($one['url'])) {
$one['url'] = Helpers::getFilterUrl($one['url']);
}
$one['textCn'] = $one['title'];
unset($one['src']);
}
if (isset($data['title']['more_url'])) {
$data['title']['more_url'] = Helpers::getFilterUrl($data['title']['more_url']);
}
$result['goodsCategory'] = $data;
return $result;
}
/**
* 一张大图和6个小图推荐数据处理方法
* @param array $data 一张大图和6个小图推荐数据
* @param $type 类型 默认1:男首页 2:女首页 3:kids 4:lifestyle
* @return array 处理之后的一张大图和6个小图推荐数据
*/
private static function recommend_content_two($data, $type)
{
$result = array();
foreach ($data['big_image'] as &$one) {
if (isset($one['url'])) {
$one['url'] = Helpers::getFilterUrl($one['url']);
}
$one['img'] = Helpers::getImageUrl($one['src'], 640, 403);
unset($one['src']);
}
$data['banner'] = $data['big_image'];
unset($data['big_image']);
foreach ($data['list'] as &$one) {
if (isset($one['url'])) {
$one['url'] = Helpers::getFilterUrl($one['url']);
}
$one['img'] = Helpers::getImageUrl($one['src'], 191, 191);
unset($one['src']);
}
if (isset($data['title']['more_url'])) {
$data['title']['more_url'] = Helpers::getFilterUrl($data['title']['more_url']);
}
$result['creativeLife'] = $data;
return $result;
}
/**
* 小图片数据处理方法
* @param array $data 小图片数据
* @param $type 类型 默认1:男首页 2:女首页 3:kids 4:lifestyle
* @return array 处理之后的小图片数据
*/
private static function small_pic($data, $type)
{
// 按照以前业务操作,不显示
return array();
$result = array();
foreach ($data as &$one) {
if (isset($one['url'])) {
$one['url'] = Helpers::getFilterUrl($one['url']);
}
$one['img'] = Helpers::getImageUrl($one['src'], 98, 98);
unset($one['src']);
}
$result['small_pic'] = $data;
return $result;
}
/**
* plus,star
* @param array $data 单名字图片数据
* @param $type 类型 默认1:男首页 2:女首页 3:kids 4:lifestyle
* @return array 处理之后的单名字图片数据
*/
private static function single_name_image($data, $type)
{
if (empty($data)) {
return array();
}
$data['name'] = $data['title'];
$data['img'] = Helpers::getImageUrl($data['src'], 640, 198);
if (isset($data['url'])) {
$data['url'] = Helpers::getFilterUrl($data['url']);
}
return array('plusStar' => $data);
}
/**
* 自定义品牌
*
* @param unknown $data
* @param mixed $type
* @return multitype:
*/
private static function custom_brands($data, $type)
{
// URL附加参数
$param = '';
if (isset(self::$channel[$type])) {
$param = '/?gender=' . self::$channel[$type];
}
$result = array();
$result['hotBrandsScroll']['name'] = $data['title']['title'];
foreach ($data['list'] as &$one) {
if (isset($one['url'])) {
$one['url'] = Helpers::getFilterUrl($one['url']);
}
$one['img'] = Helpers::getImageUrl($one['src'], 140, 140);
$one['textCn'] = $one['name'];
if (isset($one['url'])) {
$one['url'] .= $param;
}
}
if (!empty($data['is_show_name']) && $data['is_show_name'] == 'Y') {
$result['hotBrandsScroll']['is_show_name'] = true;
}
$result['hotBrandsScroll']['list'] = $data['list'];
return $result;
}
}
<?php
namespace Plugin\DataProcess;
use Plugin\Helpers;
/**
* 楼层数据处理类
*/
class FloorProcess
{
/**
* 频道参数
*
* @var array
*/
private static $channel = array(
'1' => '1,3',
'2' => '2,3'
);
/**
* 处理首页内容
* @param $data
* @param $type 类型 默认1:男首页 2:女首页 3:kids 4:lifestyle
*/
public static function getContent($data, $type = 1)
{
if (empty($data['list'])) {
return array();
}
$result = array();
$build = array();
foreach ($data['list'] as $v) {
if (empty($v)) {
continue;
}
$fun = $v['template_name'];
if (empty($v['data']) || !is_callable("self::$fun")) {
continue;
}
$build = self::$fun($v['data'], $type);
if (empty($build)) {
continue;
}
$result[] = $build;
}
$build = array();
return $result;
}
/**
* 焦点图数据处理方法
* @param array $data 焦点图数据
* @param $type 类型 默认1:男首页 2:女首页 3:kids 4:lifestyle
* @return array 处理之后的焦点图数据
*/
private static function focus($data, $type)
{
$result = array();
foreach ($data as $one) {
$result['bannerTop']['list'][] = Helpers::formatBanner($one, 640, 240);
}
return $result;
}
/**
* 图标
*/
private static function floor($data)
{
$result = array();
foreach ($data as &$one) {
$one['url'] = Helpers::getFilterUrl($one['url']);
$one['img'] = Helpers::getImageUrl($one['src'], 98, 98);
unset($one['src']);
}
$result['iconsEnter']['list'] = $data;
return $result;
}
/**
* 图标列表数据处理方法
*
* @param array $data 图标列表数据
* @param $type 类型 默认1:男首页 2:女首页 3:kids 4:lifestyle
* @return array 处理之后的图标列表数据
*/
private static function app_icon_list($data, $type)
{
$result = array();
foreach ($data as &$one) {
$one['url'] = Helpers::getFilterUrl($one['url']);
$one['img'] = Helpers::getImageUrl($one['src'], 98, 98);
unset($one['src']);
}
$result['iconsEnter']['list'] = $data;
return $result;
}
/**
* 单广告图片数据处理方法
*
* @param array $data 单图片数据
* @param $type 类型 默认1:男首页 2:女首页 3:kids 4:lifestyle
* @return array 处理之后的单图片数据
*/
private static function single_image($data, $type)
{
$result = array();
foreach ($data as &$one) {
$one['url'] = Helpers::getFilterUrl($one['url']);
$one['img'] = Helpers::getImageUrl($one['src'], 640, 200);
unset($one['src']);
}
$result['banner'] = $data;
return $result;
}
/**
* 女生品类
* 推荐(1T-12F)
*/
private static function recommend_content_three($data, $type)
{
$result = array();
$build = array();
if (!empty($data['big_image'][0])) {
$build['img'] = Helpers::getImageUrl($data['big_image'][0]['src'], 640, 200);
$build['url'] = Helpers::getFilterUrl($data['big_image'][0]['url']);
$result['hotCategory']['banner'] = $build;
}
$build = array();
foreach ($data['list'] as $one) {
$build['url'] = Helpers::getFilterUrl($one['url']);
$build['img'] = Helpers::getImageUrl($one['src'], 128, 140);
$build['textCn'] = $one['title'];
$result['hotCategory']['list'][] = $build;
}
if (isset($data['title']['more_url'])) {
$data['title']['more_url'] = Helpers::getFilterUrl($data['title']['more_url']);
}
return $result;
}
/**
* 带标题的12个小图推荐数据处理方法
*
* @param array $data 带标题的12个小图推荐数据
* @param $type 类型 默认1:男首页 2:女首页 3:kids 4:lifestyle
* @return array 处理之后的带标题的12个小图推荐数据
*/
private static function recommend_content_five($data, $type)
{
$result = array();
foreach ($data['list'] as &$one) {
if (isset($one['url'])) {
$one['url'] = Helpers::getFilterUrl($one['url']);
}
$one['img'] = Helpers::getImageUrl($one['src'], 140, 140);
$one['textCn'] = $one['title'];
unset($one['src']);
}
$data['is_show_name'] = $data['title']['is_show'];
$data['name'] = $data['title']['title'];
unset($data['title']);
if (isset($data['title']['more_url'])) {
$data['title']['more_url'] = Helpers::getFilterUrl($data['title']['more_url']);
}
$result['hotCategory'] = $data;
return $result;
}
/**
* 推荐品牌数据处理方法
* @param array $data 推荐品牌数据
* @param $type 类型 默认1:男首页 2:女首页 3:kids 4:lifestyle
* @return array 处理之后的推荐品牌数据
*/
private static function app_hot_brands($data, $type)
{
$result = array();
$data['is_show_name'] = (isset($data['is_show_name']) && $data['is_show_name'] === 'Y') ? 1 : 0;
$data['name'] = $data['title']['title'];
unset($data['title']);
foreach ($data['list'] as &$one) {
if (isset($one['url'])) {
$one['url'] = Helpers::getFilterUrl($one['url']);
isset(self::$channel[$type]) && $one['url'] .= '?gender=' . self::$channel[$type];
}
$one['img'] = Helpers::getImageUrl($one['src'], 158, 174);
unset($one['src']);
}
$data['more'] = '/brands?channel=' . $type;
$result['hotBrands'] = $data;
return $result;
}
/**
* 潮人·搭配数据处理方法
* @param array $data 潮人·搭配数据
* @param $type 类型 默认1:男首页 2:女首页 3:kids 4:lifestyle
* @return array 处理之后的潮人·搭配数据
*/
private static function trendsetter_collocation($data, $type)
{
$result = array();
foreach ($data as $key => &$value) {
if ($key == 'title') {
$value['more_url'] = Helpers::url('', array('id' => 3, 'gender' => self::$channel[$type]), 'guang');
}
if ($key == 'article') {
foreach ($value as &$one) {
if (isset($one['url'])) {
$one['url'] = Helpers::getFilterUrl($one['url']);
}
$one['img'] = Helpers::getImageUrl($one['src'], 400, 400);
unset($one['src']);
}
}
if ($key == 'recommend_collocation') {
foreach ($value as &$one) {
if (isset($one['url'])) {
$one['url'] = Helpers::getFilterUrl($one['url']);
}
$one['img'] = Helpers::getImageUrl($one['src'], 140, 140);
unset($one['src']);
}
}
}
$result['trendColloaction'] = $data;
return $result;
}
/**
* 潮品·话题数据处理方法
* @param array $data 潮品·话题数据
* @param $type 类型 默认1:男首页 2:女首页 3:kids 4:lifestyle
* @return array 处理之后的潮品·话题数据
*/
private static function trendgoods_topic($data, $type)
{
$result = array();
foreach ($data as $key => &$value) {
if ($key == 'title') {
$urlParam = array('id' => 4);
isset(self::$channel[$type]) && $urlParam['gender'] = self::$channel[$type];
$value['more_url'] = Helpers::url('', $urlParam, 'guang');
}
if ($key == 'list') {
foreach ($value as &$one) {
if (isset($one['url'])) {
$one['url'] = Helpers::getFilterUrl($one['url']);
}
$one['img'] = Helpers::getImageUrl($one['src'], 580, 360);
unset($one['src']);
}
}
}
$result['trendTopics'] = $data;
return $result;
}
/**
* 一张大图一个大的推荐和4个小图推荐数据处理方法
* @param array $data 一张大图一个大的推荐和4个小图推荐数据
* @param $type 类型 默认1:男首页 2:女首页 3:kids 4:lifestyle
* @return array 处理之后的一张大图一个大的推荐和4个小图推荐数据
*/
private static function recommend_content_one($data, $type)
{
$result = array();
foreach ($data['big_image'] as &$one) {
if (isset($one['url'])) {
$one['url'] = Helpers::getFilterUrl($one['url']);
}
$one['img'] = Helpers::getImageUrl($one['src'], 640, 403);
unset($one['src']);
}
// 区别多张图与一张图
if (count($data['big_image']) > 1) {
$result['big_image']['big_list'] = $data['big_image'];
unset($data['big_image']);
}
foreach ($data['list'] as $kk => &$one) {
if ($kk == 0) {
$one['img'] = Helpers::getImageUrl($one['src'], 253, 383);
} else {
$one['img'] = Helpers::getImageUrl($one['src'], 191, 191);
}
if (isset($one['url'])) {
$one['url'] = Helpers::getFilterUrl($one['url']);
}
$one['textCn'] = $one['title'];
unset($one['src']);
}
if (isset($data['title']['more_url'])) {
$data['title']['more_url'] = Helpers::getFilterUrl($data['title']['more_url']);
}
$result['goodsCategory'] = $data;
return $result;
}
/**
* 一张大图和6个小图推荐数据处理方法
* @param array $data 一张大图和6个小图推荐数据
* @param $type 类型 默认1:男首页 2:女首页 3:kids 4:lifestyle
* @return array 处理之后的一张大图和6个小图推荐数据
*/
private static function recommend_content_two($data, $type)
{
$result = array();
foreach ($data['big_image'] as &$one) {
if (isset($one['url'])) {
$one['url'] = Helpers::getFilterUrl($one['url']);
}
$one['img'] = Helpers::getImageUrl($one['src'], 640, 403);
unset($one['src']);
}
$data['banner'] = $data['big_image'];
unset($data['big_image']);
foreach ($data['list'] as &$one) {
if (isset($one['url'])) {
$one['url'] = Helpers::getFilterUrl($one['url']);
}
$one['img'] = Helpers::getImageUrl($one['src'], 191, 191);
unset($one['src']);
}
if (isset($data['title']['more_url'])) {
$data['title']['more_url'] = Helpers::getFilterUrl($data['title']['more_url']);
}
$result['creativeLife'] = $data;
return $result;
}
/**
* 小图片数据处理方法
* @param array $data 小图片数据
* @param $type 类型 默认1:男首页 2:女首页 3:kids 4:lifestyle
* @return array 处理之后的小图片数据
*/
private static function small_pic($data, $type)
{
// 按照以前业务操作,不显示
return array();
$result = array();
foreach ($data as &$one) {
if (isset($one['url'])) {
$one['url'] = Helpers::getFilterUrl($one['url']);
}
$one['img'] = Helpers::getImageUrl($one['src'], 98, 98);
unset($one['src']);
}
$result['small_pic'] = $data;
return $result;
}
/**
* plus,star
* @param array $data 单名字图片数据
* @param $type 类型 默认1:男首页 2:女首页 3:kids 4:lifestyle
* @return array 处理之后的单名字图片数据
*/
private static function single_name_image($data, $type)
{
if (empty($data)) {
return array();
}
$data['name'] = $data['title'];
$data['img'] = Helpers::getImageUrl($data['src'], 640, 198);
if (isset($data['url'])) {
$data['url'] = Helpers::getFilterUrl($data['url']);
}
return array('plusStar' => $data);
}
/**
* 自定义品牌
*
* @param unknown $data
* @param mixed $type
* @return multitype:
*/
private static function custom_brands($data, $type)
{
// URL附加参数
$param = '';
if (isset(self::$channel[$type])) {
$param = '/?gender=' . self::$channel[$type];
}
$result = array();
$result['hotBrandsScroll']['name'] = $data['title']['title'];
foreach ($data['list'] as &$one) {
if (isset($one['url'])) {
$one['url'] = Helpers::getFilterUrl($one['url']);
}
$one['img'] = Helpers::getImageUrl($one['src'], 140, 140);
$one['textCn'] = $one['name'];
if (isset($one['url'])) {
$one['url'] .= $param;
}
}
if (!empty($data['is_show_name']) && $data['is_show_name'] == 'Y') {
$result['hotBrandsScroll']['is_show_name'] = true;
}
$result['hotBrandsScroll']['list'] = $data['list'];
return $result;
}
}
... ...
<?php
namespace Plugin\DataProcess;
use Plugin\Helpers;
/**
* 列表数据处理类
*/
class ListProcess
{
/**
* 返回商品和过滤数据
*
* @param $data
* @return array 处理之后的商品数据
*/
public static function getListData($data, $returnFilter = true)
{
$result = array();
if (isset($data['product_list'])) {
$result['new'] = self::getProductData($data['product_list']);
}
if ($returnFilter && isset($data['filter'])) {
$result['filter'] = self::getFilterData($data['filter']);
}
return $result;
}
/**
* 处理列表商品数据
*
* @param $data
* @return array 处理之后的商品数据
*/
public static function getProductData($data)
{
// 处理商品
$products = array();
foreach ($data as $value) {
$products[] = Helpers::formatProduct($value, true, true, true, 235, 314);
}
return $products;
}
/**
* 处理筛选数据
*
* @param $data
* @param string | integer $gender 默认选择的性别,默认1,2,3表示所有
* @param null|string $exclude 需要排除的字段
* @return array 处理之后的筛选数据
*/
public static function getFilterData($data, $gender = '1,2,3', $exclude = null)
{
// 过滤条件数据
$filters = array('classify' => array());
// 返回数据中有没有gender时要添加gender
$data['gender'] = array('2,3' => 'GIRLS', '1,3' => 'BOYS');
$num = 1;
foreach ($data as $key => $val) {
if ($key === $exclude || empty($val) || !is_callable("self::$key")) {
continue;
}
$build = self::$key($val, $gender);
if ($num === 1) {
$build['active'] = true;
}
$num++;
$filters['classify'][] = $build;
}
// 按照指定字段进行排序筛选数据
self::sortArrByField($filters['classify'], 'sort_col');
return $filters;
}
private static function brand($data)
{
$result = array(
'title' => '品牌',
'name' => '所有品牌',
'sort_col' => 1,
'dataType' => 'brand',
'subs' => array(
array(
'chosed' => true,
'dataId' => 0,
'name' => '所有品牌'
)
)
);
// 对品牌数据按照品牌字母进行排序
self::sortArrByField($data, 'brand_alif');
foreach ($data as $one) {
$brand = array();
$brand['dataId'] = $one['id'];
$brand['name'] = $one['brand_name'];
$result['subs'][] = $brand;
}
return $result;
}
private static function color($data)
{
$result = array(
'title' => '颜色',
'name' => '所有颜色',
'sort_col' => 3,
'dataType' => 'color',
'subs' => array(
array(
'chosed' => true,
'dataId' => 0,
'name' => '所有颜色'
)
)
);
foreach ($data as $one) {
$color = array();
$color['dataId'] = $one['color_id'];
$color['name'] = $one['color_name'];
$result['subs'][] = $color;
}
return $result;
}
private static function discount($data)
{
$result = array(
'title' => '折扣',
'name' => '所有商品',
'sort_col' => 6,
'dataType' => 'discount',
'subs' => array(
array(
'chosed' => true,
'dataId' => '0.1,0.9',
'name' => '所有商品'
)
)
);
foreach ($data as $key => $one) {
$discount = array();
$discount['dataId'] = $key;
$discount['name'] = $one['name'] . '折商品';
$result['subs'][] = $discount;
}
return $result;
}
private static function gender($data, $gender)
{
$result = array(
'title' => '性别',
'name' => '所有性别',
'sort_col' => 0,
'dataType' => 'gender',
'subs' => array(
array(
'dataId' => '1,2,3',
'name' => '所有性别'
),
array(
'dataId' => '1,3',
'name' => 'BOYS'
),
array(
'dataId' => '2,3',
'name' => 'GIRLS'
),
)
);
// 处理选中状态
foreach ($result['subs'] as &$val) {
if ($val['dataId'] === $gender) {
$val['chosed'] = true;
$result['name'] = $val['name'];
}
}
return $result;
}
private static function group_sort($data)
{
$result = array(
'title' => '品类',
'name' => '所有品类',
'sort_col' => 2,
'dataType' => 'sort',
'subs' => array(
array(
'chosed' => true,
'dataId' => 0,
'name' => '所有品类'
)
)
);
$category = array();
foreach ($data as $one) {
$category['dataId'] = isset($one['relation_parameter']) ? $one['relation_parameter']['sort'] : 0;
$category['name'] = $one['category_name'];
/* // 子品类(目前h5不支持二级)
if(isset($one['sub']))
{
$category['subs'] = array();
foreach ($one['sub'] as $single) {
$subitem = array();
$subitem['dataId'] = $single['category_id'];
$subitem['name'] = $single['category_name'];
$category['subs'][] = $subitem;
}
} */
$result['subs'][] = $category;
}
return $result;
}
private static function priceRange($data)
{
// 首先对价格进行排序
ksort($data, SORT_NUMERIC);
$result = array(
'title' => '价格',
'name' => '所有价格',
'sort_col' => 5,
'dataType' => 'price',
'subs' => array(
array(
'chosed' => true,
'dataId' => 0,
'name' => '所有价格'
)
)
);
$price = array();
foreach ($data as $key => $one) {
$price['dataId'] = $key;
$price['name'] = $one;
$result['subs'][] = $price;
}
return $result;
}
private static function size($data)
{
$result = array(
'title' => '尺码',
'name' => '所有尺码',
'sort_col' => 4,
'dataType' => 'size',
'subs' => array(
array(
'chosed' => true,
'dataId' => 0,
'name' => '所有尺码'
)
)
);
$size = array();
foreach ($data as $one) {
$size['dataId'] = $one['size_id'];
$size['name'] = $one['size_name'];
$result['subs'][] = $size;
}
return $result;
}
/**
* 按照数组中指定字段排序二维数组
*
* @param array &$array 需要排序的数组
* @param string $field 字段名称
* @param boolean $desc 时候降序排列,默认为false
*/
private static function sortArrByField(&$array, $field, $desc = false)
{
$fieldArr = array();
foreach ($array as $k => $v) {
$fieldArr[$k] = isset($v[$field]) ? $v[$field] : '';
}
$sort = $desc == false ? SORT_ASC : SORT_DESC;
array_multisort($fieldArr, $sort, $array);
}
}
<?php
namespace Plugin\DataProcess;
use Plugin\Helpers;
/**
* 列表数据处理类
*/
class ListProcess
{
/**
* 返回商品和过滤数据
*
* @param $data
* @return array 处理之后的商品数据
*/
public static function getListData($data, $returnFilter = true)
{
$result = array();
if (isset($data['product_list'])) {
$result['new'] = self::getProductData($data['product_list']);
}
if ($returnFilter && isset($data['filter'])) {
$result['filter'] = self::getFilterData($data['filter']);
}
return $result;
}
/**
* 处理列表商品数据
*
* @param $data
* @return array 处理之后的商品数据
*/
public static function getProductData($data)
{
// 处理商品
$products = array();
foreach ($data as $value) {
$products[] = Helpers::formatProduct($value, true, true, true, 235, 314);
}
return $products;
}
/**
* 处理筛选数据
*
* @param $data
* @param string | integer $gender 默认选择的性别,默认1,2,3表示所有
* @param null|string $exclude 需要排除的字段
* @return array 处理之后的筛选数据
*/
public static function getFilterData($data, $gender = '1,2,3', $exclude = null)
{
// 过滤条件数据
$filters = array('classify' => array());
// 返回数据中有没有gender时要添加gender
$data['gender'] = array('2,3'=>'GIRLS','1,3'=>'BOYS');
$num = 1;
foreach ($data as $key => $val) {
if ($key === $exclude || empty($val) || !is_callable("self::$key")) {
continue;
}
$build = self::$key($val, $gender);
if ($num === 1) {
$build['active'] = true;
}
$num++;
$filters['classify'][] = $build;
}
// 按照指定字段进行排序筛选数据
self::sortArrByField($filters['classify'], 'sort_col');
return $filters;
}
private static function brand($data)
{
$result = array(
'title' => '品牌',
'name' => '所有品牌',
'sort_col' => 1,
'dataType' => 'brand',
'subs' => array(
array(
'chosed' => true,
'dataId' => 0,
'name' => '所有品牌'
)
)
);
// 对品牌数据按照品牌字母进行排序
self::sortArrByField($data, 'brand_alif');
foreach ($data as $one) {
$brand = array();
$brand['dataId'] = $one['id'];
$brand['name'] = $one['brand_name'];
$result['subs'][] = $brand;
}
return $result;
}
private static function color($data)
{
$result = array(
'title' => '颜色',
'name' => '所有颜色',
'sort_col' => 3,
'dataType' => 'color',
'subs' => array(
array(
'chosed' => true,
'dataId' => 0,
'name' => '所有颜色'
)
)
);
foreach ($data as $one) {
$color = array();
$color['dataId'] = $one['color_id'];
$color['name'] = $one['color_name'];
$result['subs'][] = $color;
}
return $result;
}
private static function discount($data)
{
$result = array(
'title' => '折扣',
'name' => '所有商品',
'sort_col' => 6,
'dataType' => 'discount',
'subs' => array(
array(
'chosed' => true,
'dataId' => '0.1,0.9',
'name' => '所有商品'
)
)
);
foreach ($data as $key => $one) {
$discount = array();
$discount['dataId'] = $key;
$discount['name'] = $one['name'] . '折商品';
$result['subs'][] = $discount;
}
return $result;
}
private static function gender($data, $gender)
{
$result = array(
'title' => '性别',
'name' => '所有性别',
'sort_col' => 0,
'dataType' => 'gender',
'subs' => array(
array(
'dataId' => '1,2,3',
'name' => '所有性别'
),
array(
'dataId' => '1,3',
'name' => 'BOYS'
),
array(
'dataId' => '2,3',
'name' => 'GIRLS'
),
)
);
// 处理选中状态
foreach ($result['subs'] as &$val) {
if ($val['dataId'] === $gender) {
$val['chosed'] = true;
$result['name'] = $val['name'];
}
}
return $result;
}
private static function group_sort($data)
{
$result = array(
'title' => '品类',
'name' => '所有品类',
'sort_col' => 2,
'dataType' => 'sort',
'subs' => array(
array(
'chosed' => true,
'dataId' => 0,
'name' => '所有品类'
)
)
);
$category = array();
foreach ($data as $one) {
$category['dataId'] = isset($one['relation_parameter']) ? $one['relation_parameter']['sort'] : 0;
$category['name'] = $one['category_name'];
/* // 子品类(目前h5不支持二级)
if(isset($one['sub']))
{
$category['subs'] = array();
foreach ($one['sub'] as $single) {
$subitem = array();
$subitem['dataId'] = $single['category_id'];
$subitem['name'] = $single['category_name'];
$category['subs'][] = $subitem;
}
} */
$result['subs'][] = $category;
}
return $result;
}
private static function priceRange($data)
{
// 首先对价格进行排序
ksort($data, SORT_NUMERIC);
$result = array(
'title' => '价格',
'name' => '所有价格',
'sort_col' => 5,
'dataType' => 'price',
'subs' => array(
array(
'chosed' => true,
'dataId' => 0,
'name' => '所有价格'
)
)
);
$price = array();
foreach ($data as $key => $one) {
$price['dataId'] = $key;
$price['name'] = $one;
$result['subs'][] = $price;
}
return $result;
}
private static function size($data)
{
$result = array(
'title' => '尺码',
'name' => '所有尺码',
'sort_col' => 4,
'dataType' => 'size',
'subs' => array(
array(
'chosed' => true,
'dataId' => 0,
'name' => '所有尺码'
)
)
);
$size = array();
foreach ($data as $one) {
$size['dataId'] = $one['size_id'];
$size['name'] = $one['size_name'];
$result['subs'][] = $size;
}
return $result;
}
/**
* 按照数组中指定字段排序二维数组
*
* @param array &$array 需要排序的数组
* @param string $field 字段名称
* @param boolean $desc 时候降序排列,默认为false
*/
private static function sortArrByField(&$array, $field, $desc = false)
{
$fieldArr = array();
foreach ($array as $k => $v) {
$fieldArr[$k] = isset($v[$field]) ? $v[$field] : '';
}
$sort = $desc == false ? SORT_ASC : SORT_DESC;
array_multisort($fieldArr, $sort, $array);
}
}
... ...
<?php
namespace Plugin\DataProcess\WebChannel;
class Channel
{
public static $formatChannel = array(
'boys' => '\Plugin\DataProcess\WebChannel\Channel\Kids',
'girls' => '\Plugin\DataProcess\WebChannel\Channel\Girls',
'kids' => '\Plugin\DataProcess\WebChannel\Channel\Kids'
);
public static function getFormat($channel, $data)
{
//通用处理器
$result = Process::getContent($data, $channel);
$channelResult = array();
if(isset(self::$formatChannel[$channel]))
{
$class = self::$formatChannel[$channel];
$channelResult = $class::format($data);
}
//组合数据
$result = $result + $channelResult;
ksort($result);
return array_values($result);
}
<?php
namespace Plugin\DataProcess\WebChannel;
class Channel
{
public static $formatChannel = array(
'boys' => '\Plugin\DataProcess\WebChannel\Channel\Boys',
'girls' => '\Plugin\DataProcess\WebChannel\Channel\Girls',
'kids' => '\Plugin\DataProcess\WebChannel\Channel\Kids',
'lifestyle' => '\Plugin\DataProcess\WebChannel\Channel\Lifestyle'
);
public static function getFormat($channel, $data)
{
// 通用处理器
$result = Process::getContent($data, $channel);
$channelResult = array();
if (isset(self::$formatChannel[$channel])) {
$class = self::$formatChannel[$channel];
$channelResult = $class::format($data);
}
// 组合数据
$result = $result + $channelResult;
ksort($result);
return array_values($result);
}
}
... ...
... ... @@ -8,4 +8,38 @@ abstract class AbstractChannel
* @return array[以key方式存储数据]
*/
public static function format($resource){}
/**
* 获取格式化最新上架数据
*
* @param array &$resource
* @return array
*/
public static function getFormatNewArrivals(&$resource)
{
$result = array();
foreach($resource as $key => $val)
{
if(isset($resource[$key]) && isset($resource[$key + 2]))
{
$temp = array('newArrivls' => array('name' => '', 'navs' => array()), 'href' => '');
//newArrivals
if($resource[$key]['template_name'] == 'text' && $resource[$key + 1]['template_name'] == 'textNav')
{
//text模版
$temp['newArrivls']['name'] = $resource[$key]['data']['text'];
//textNav模版
foreach($resource[$key + 1]['data'] as $val)
{
$temp['newArrivls']['navs'][] = array('href' => $val['url'],'name' => $val['name']);
}
//link模版
$temp['href'] = $resource[$key + 2]['data'][0]['url'];
unset($resource[$key], $resource[$key + 1], $resource[$key + 2]);
$result[$key] = $temp;
}
}
}
return $result;
}
}
\ No newline at end of file
... ...
<?php
namespace Plugin\DataProcess\WebChannel\Channel;
class Boys extends AbstractChannel
{
/**
* 格式化数据
*
* @param array $resource
* @return
*/
public static function format($resource)
{
$result = self::getFormatNewArrivals($resource);
return $result;
}
}
\ No newline at end of file
... ...
<?php
namespace Plugin\DataProcess\WebChannel\Channel;
class Girls extends AbstractChannel
{
public static function format($resource)
{
$result = array();
$result += self::getFormatNewArrivals($resource);
foreach($resource as $key => $val)//老版是title模版
{
if(isset($resource[$key]) && isset($resource[$key + 2]))
{
$temp = array('newArrivls' => array('name' => '', 'navs' => array(), 'href' => ''));
if($resource[$key]['template_name'] == 'text' &&
$resource[$key + 1]['template_name'] == 'title' && //老版是title模版
$resource[$key + 2]['template_name'] == 'link'
)
{
//text模版
$temp['newArrivls']['name'] = $resource[$key]['data']['text'];
//title模版
foreach($resource[$key + 1]['data']['nav'] as $val)
{
$temp['newArrivls']['navs'][] = array('href' => $val['url'],'name' => $val['name']);
}
$link = $resource[$key + 2]['data'][0];
$temp['newArrivls']['href'] = $link['url'];
unset($resource[$key], $resource[$key + 1]);
$result[$key] = $temp;
}
}
}
return $result;
}
}
... ...
... ... @@ -3,17 +3,36 @@ namespace Plugin\DataProcess\WebChannel\Channel;
class Kids extends AbstractChannel
{
/**
* {@inheritDoc}
* @see \Plugin\DataProcess\WebChannel\Channel\AbstractChannel::format()
* 格式化数据
*
* @param array $resource
* @return
*/
public static function format($resource)
{
$data = array();
foreach($resource as $key => $val)
$result = array();
$result += self::getFormatNewArrivals($resource);
foreach($resource as $key => $val)//老版处理
{
if(isset($resource[$key]) && isset($resource[$key + 1]))
{
$temp = array('newArrivls' => array('name' => '', 'navs' => array()));
//newArrivals
if($resource[$key]['template_name'] == 'text' && $resource[$key + 1]['template_name'] == 'textNav')
{
//text模版
$temp['newArrivls']['name'] = $resource[$key]['data']['text'];
//textNav模版
foreach($resource[$key + 1]['data'] as $val)
{
$temp['newArrivls']['navs'][] = array('href' => $val['url'],'name' => $val['name']);
}
unset($resource[$key], $resource[$key + 1]);
$result[$key] = $temp;
}
}
}
return $data;
return $result;
}
... ...
<?php
namespace Plugin\DataProcess\WebChannel\Channel;
class Lifestyle extends AbstractChannel
{
/**
* 格式化数据
*
* @param array $resource
* @return
*/
public static function format($resource)
{
$result = array();
$result += self::getFormatNewArrivals($resource);
foreach($resource as $key => $val)//老版处理
{
if(isset($resource[$key]) && isset($resource[$key + 1]))
{
$temp = array('newArrivls' => array('name' => '', 'navs' => array()));
//newArrivals
if($resource[$key]['template_name'] == 'textNav' && $resource[$key + 1]['template_name'] == 'link')
{
//text模版
$temp['newArrivls']['name'] = '最新上架 NEW ARRIVALS';
//textNav模版
foreach($resource[$key]['data'] as $val)
{
$temp['newArrivls']['navs'][] = array('href' => $val['url'],'name' => $val['name']);
}
unset($resource[$key], $resource[$key + 1]);
$result[$key] = $temp;
}
}
}
return $result;
}
}
\ No newline at end of file
... ...