Authored by hf

code review by hf: modify to format home channel codes

<?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 Index\HomeModel;
class WebAction extends AbstractAction
use Yaf\Controller_Abstract;
use Yaf\Dispatcher;
use Plugin\Cache;
use Plugin\Helpers;
use Hood\Session;
class WebAction 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 string $message
* @param mixed $data
*/
protected function helpJsonResult($code, $message = '', $data = null)
{
header('Content-Type: application/json; charset=utf-8;');
echo json_encode(array('code' => $code, 'message' => $message, 'data' => $data));
exit();
}
/**
* JSONP Callback输出, 用于远程调用
*
* @param string $callbackString
* @param int $code
* @param string $message
* @param mixed $data
*/
protected function helpJsonCallbackResult($callbackString, $code, $message = '', $data = null)
{
header('Content-Type: application/json; charset=utf-8;');
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('www_yohobuy_session', null, 'yohobuy.com')->__set($name, $value);
}
}
/**
* 获取Session
*
* @param string $name 名称
* @return mixed
*/
public function getSession($name)
{
if ($this->_useSession) {
return Session::start('www_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;
}
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 $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);
}
/**
* 设置头部
*
* @param string $channel (默认不设置)
... ... @@ -82,4 +527,5 @@ class WebAction extends AbstractAction
);
$this->_view->assign('headerdata', $header);
}
}
\ No newline at end of file
... ...
<?php
use Action\WebAction;
use Index\HomeModel;
/**
* 男首
... ... @@ -10,11 +9,12 @@ class BoysController extends WebAction
public function indexAction()
{
$this->setWebNavHeader(Index\HomeModel::COOKIE_NAME_BOYS);
$this->setWebNavHeader(\Index\HomeModel::COOKIE_NAME_BOYS);
$data = array(
'boysHomePage' => true,
'boys' => HomeModel::getChannelResource(HomeModel::COOKIE_NAME_BOYS, HomeModel::CODE_BOYS_CHANNEL)
'boys' => \Index\HomeModel::getChannelResource(HomeModel::COOKIE_NAME_BOYS, HomeModel::CODE_BOYS_CHANNEL)
);
$this->_view->display('index', $data);
}
}
\ No newline at end of file
... ...
<?php
/**
* 默认控制器
*/
use Action\WebAction;
class IndexController extends WebAction
{
/**
* 男生首页
*/
public function indexAction()
{
echo 'hello';
exit();
if (null !== $this->get('go')) {
// 先检查COOKIE是否有访问过, 有则跳转到相应的频道页
\Index\HomeModel::goSwitchChannel();
} else {
// 设置客户端浏览器1分钟内不改变
$this->setLastModified(mktime(date('H'), date('i'), 0, date('n'), date('j'), date('Y')));
// 设置浏览器缓存1分钟
$this->setExpires(60);
}
$this->setWebNavHeader(\Index\HomeModel::COOKIE_NAME_BOYS);
$data = array(
'boysHomePage' => true,
'boys' => \Index\HomeModel::getChannelResource(\Index\HomeModel::COOKIE_NAME_BOYS, \Index\HomeModel::CODE_BOYS_CHANNEL)
);
$this->_view->display('index', $data);
}
}
\ No newline at end of file
... ...
... ... @@ -113,6 +113,20 @@ class HomeModel
}
return $menu;
}
/**
* 选择频道
*
* @return void
*/
public static function goSwitchChannel()
{
// 通过COOKIE检查是否已设置过首页频道时,跳转到对应的频道页
if (!empty($_COOKIE['_Channel'])) {
headers_sent() || header('Location: /' . $_COOKIE['_Channel']);
exit();
}
}
/**
* 设置选择的频道保存到浏览器COOKIE
... ...
... ... @@ -3,8 +3,8 @@ use Yaf\Application;
define('SITE_MAIN', 'http://dev.yohobuy.com'); // 网站主域名
define('OLD_MAIN', 'http://www.yohobuy.com'); // 网站旧域名
define('COOKIE_DOMAIN', '.dev.yohobuy.com'); // COOKIE作用域
define('SUB_DOMAIN', '.dev.yohobuy.com'); // 子域名后缀
define('COOKIE_DOMAIN', '.yohobuy.com'); // COOKIE作用域
define('SUB_DOMAIN', '.yohobuy.com'); // 子域名后缀
define('USE_CACHE', false); // 缓存的开关
define('APPLICATION_PATH', dirname(__DIR__)); // 应用目录
define('ROOT_PATH', dirname(dirname(APPLICATION_PATH))); // 根目录
... ...