Index.php 11 KB
<?php

use Action\AbstractAction;
use Guang\IndexModel;
use LibModels\Wap\Guang\ListData;
use Plugin\Helpers;
use Plugin\Cache;
use Configs\CacheConfig;

/**
 * 逛首页、列表页、编辑页
 */
class IndexController extends AbstractAction
{

    /**
     * 首页
     * 
     * @param int id 分类ID 0:最新,1:话题,2:搭配,3:潮人,4:潮品,5:小贴士
     * @param string gender '1,3'表示男,'2,3'表示女, 默认为所有
     */
    public function indexAction()
    {
        $this->setTitle('逛');
        $this->setNavHeader('逛', true, SITE_MAIN);

        $uid = $this->getUid();
        $udid = $this->getUdid();

        $type = $this->get('id', 0);
        $gender = $this->get('gender');
        if (is_string($gender)) {
            $gender = rawurldecode($gender);
        } else {
            $gender = Helpers::getGenderByCookie();
        }

        //首次进来,保存app_version参数,当前会话,其它页面会用到
        $this->setSession('app_version', $this->get('app_version', false));
        
//        // 设置侧边栏逛的默认选中状态
//        if ($gender === '1,3') {
//            $this->setNavSide('boys');
//        } elseif ($gender === '2,3') {
//            $this->setNavSide('girls');
//        } else {
//            $this->setNavSide();
//        }

        $this->_view->display('index', IndexModel::getArticle($gender, $type, $uid, $udid));
    }

    /**
     * 列表页
     * 
     * @param string tag 标签名称
     * @param int yh_channel APP客户端标识 1表示男,2:表示女,3:潮童,4:创意生活
     */
    public function tagAction()
    {
        $tag = $this->get('query');
        $channel = $this->get('yh_channel');

        // 标识是不是APP客户端
        $isApp = is_numeric($channel);
        // APP访问时通过频道参数判断性别
        if ($isApp) {
            switch (intval($channel)) {
                case 1: // 男
                    $gender = '1,3';
                    break;
                case 2: // 女
                    $gender = '2,3';
                    break;
                default: // 所有
                    $gender = '1,2,3';
            }
            $uid = $this->get('uid');
            $udid = $this->get('udid');
            if ($uid) {
                $this->setSession('uid', $uid);
            }
            if ($udid) {
                $this->setSession('udid', $udid);
            }
        }
        // 从Cookie获取
        else {
            $gender = Helpers::getGenderByCookie();

            $uid = $this->getUid();
            $udid = $this->getUdid();

            $this->setNavHeader($tag, true, SITE_MAIN);
        }

        // 标签聚合内容列表
        //$article = ListData::article($gender, 0, $uid, $udid, 1, $tag);
        $article = IndexModel::getArticleByTagOrEditor($gender, 0, $uid, $udid, 1, $tag);
        // 标签聚合内容不存在, 跳到错误页面
        if (empty($article['data']['list']['artList'])) {
            $this->error();
        }

        $this->setTitle($tag);

        $data = array();
        // 模板中使用JS的标识
        $data['guangList'] = true;

        // 构建资讯文章内容
        $build = array();
        foreach ($article['data']['list']['artList'] as $article) {
            $build[] = Helpers::formatArticle($article, true, $isApp, true, $uid);
        }
        $data['guang']['infos'] = $build;

        // 分页需要的参数
        $data['guang']['tag'] = $tag;
        $data['guang']['gender'] = $gender;
        $data['guang']['isApp'] = $isApp ? 1 : 0;

        $this->_view->display('list', $data);
    }

    /**
     * 编辑页
     * 
     * @param int id 作者ID
     * @param int yh_channel APP客户端标识 1表示男,2:表示女,3:潮童,4:创意生活
     */
    public function editorAction()
    {
        $id = $this->get('id');
        $channel = $this->get('yh_channel');

        // 标识是不是APP客户端
        $isApp = $this->isApp();
        // APP访问时通过频道参数判断性别
        if ($isApp) {
            switch (intval($channel)) {
                case 1: // 男
                    $gender = '1,3';
                    break;
                case 2: // 女
                    $gender = '2,3';
                    break;
                default: // 所有
                    $gender = '1,2,3';
            }
            $uid = $this->get('uid');
            $udid = $this->get('udid');
            if ($uid) {
                $this->setSession('uid', $uid);
            }
            if ($udid) {
                $this->setSession('udid', $udid);
            }
        }
        // 从Cookie获取
        else {
            $gender = Helpers::getGenderByCookie();

            $uid = $this->getUid();
            $udid = $this->getUdid();

            $this->setNavHeader('编辑简介', true, SITE_MAIN);
        }

        // 获取作者信息
        $author = ListData::author($id);
        // 作者信息不存在,则跳到错误页面
        if (!isset($author['name'])) {
            $this->error();
        }

        $this->setTitle('编辑简介', false, '');

        $data = array();
        // 模板中使用JS的标识
        $data['guangList'] = true;

        // 作者信息
        $data['authorInfo'] = array();
        $data['authorInfo']['avatar'] = Helpers::getImageUrl($author['avatar'], 100, 100);
        $data['authorInfo']['name'] = $author['name'];
        $data['authorInfo']['info'] = $author['author_desc'];
        $data['authorInfo']['id'] = $id;

        // 标签聚合内容列表
        //$article = ListData::article($gender, 0, $uid, $udid, 1, null, $id);
        $article = IndexModel::getArticleByTagOrEditor($gender, 0, $uid, $udid, 1, null, $id);
        // 构建资讯文章内容
        if (!empty($article['data']['list']['artList'])) {
            $build = array();
//            foreach ($article['data']['list']['artList'] as $article) {
//                $build[] = Helpers::formatArticle($article, true, $isApp, false, $uid);
//            }
            foreach ($article['data']['list']['artList'] as $articlekey =>$article) {
                $build[$articlekey] = Helpers::formatArticle($article, true, $isApp, false, $uid);
                if($this->isApp()){
                    $build[$articlekey]['collect']['url'] = Helpers::url('/author/index', array('id' => $id), 'guang');
                }
            }
            $data['guang']['infos'] = $build;
        }

        $data['guang']['gender'] = $gender;
        $data['guang']['isApp'] = $isApp ? 1 : 0;

        $this->_view->display('list', $data);
    }

    /**
     * 逛列表页面的资讯分页
     * 
     * 逛首页、标签页、编辑页资讯列表
     * 
     * @param string tag 标签名称, 没有传空或不传
     * @param int type 逛首页的分类ID
     * @param int page 分页的页码
     * @param string gender "1,2,3"表示所有, "1,3"表示男, "2,3"表示女
     * @param int authorId 作者ID
     * @return html
     */
    public function pageAction()
    {
        do {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }

            /* 判断参数是否有效 */
            $tag = $this->get('tag');
            $sortId = $this->get('type', 0);
            $page = $this->get('page');
            $gender = $this->get('gender');
            $authorId = $this->get('authorId');
            $isApp = $this->get('isApp', false);
            $isTab = $this->get('isTab', false); // 是否为tab切换操作
            $showAuthor = true;
            if (!empty($sortId) && !is_numeric($sortId)) {
                break;
            }
            if (!empty($page) && !is_numeric($page)) {
                break;
            }
            if (!empty($authorId) && is_numeric($authorId)) {
                $showAuthor = false;
            }

            /* 获取资讯文章列表 */
            if ($isApp) {
                $uid = $this->getSession('uid');
                $udid = $this->getSession('udid');
            } else {
                $uid = $this->getUid();
                $udid = $this->getUdid();
            }
            //$page = intval($page) + 1;
            $data = IndexModel::getPageData($gender, $sortId, $uid, $udid, $page, $tag, $authorId, $isApp, $showAuthor, $isTab);
        } while (false);

        if (isset($data) && !empty($data)) {
            $this->_view->display('page', $data);
        } else {
            echo ' ';
        }
    }

    /**
     * 内部清缓存
     */
    public function clearAction()
    {
        $key = $this->get('key');
        if ($key !== '9646abcdef') {
            exit();
        }

        $type = $this->get('type');
        switch (strval($type)) {
            case 'boys': // 男首
                Cache::delete(CacheConfig::KEY_ACTION_BOYS_INDEX);
                break;
            case 'girls': // 女首
                Cache::delete(CacheConfig::KEY_ACTION_GIRLS_INDEX);
                break;
            case 'kids': // 潮童
                Cache::delete(CacheConfig::KEY_ACTION_KIDS_INDEX);
                break;
            case 'lifestyle': // 创意生活
                Cache::delete(CacheConfig::KEY_ACTION_LIFESTYLE_INDEX);
                break;
            case 'bgimg': // 频道选择背景图
                Cache::delete(CacheConfig::KEY_ACTION_INDEX_INDEX);
                break;
            case 'brands': // 品牌一览
                Cache::delete('3206a153b0cebbd6e47be90c6ac41ee8');
                Cache::delete('1c6bb62c480aec413b247d6f21e9c23a');
                Cache::delete('60bb7899f49b3549dde438ac53526dea');
                Cache::delete('9085ba21ac3d4f849c977da37a290a23');
                break;
            case 'new': // 新品
                Cache::delete(CacheConfig::KEY_ACTION_PRODUCT_NEWSALE_INDEX . '3cf2c1be5217fbab6009ce83959e1e12');
                Cache::delete(CacheConfig::KEY_ACTION_PRODUCT_NEWSALE_INDEX . '1cf7f9f10e2a2670e73d05c568793ad9');
                Cache::delete(CacheConfig::KEY_ACTION_PRODUCT_NEWSALE_INDEX . '57457adececa6c748b29c90cad0ae940');
                Cache::delete(CacheConfig::KEY_ACTION_PRODUCT_NEWSALE_INDEX . '04953a61cbf1db426a681e55d496d2fe');
                break;
            case 'sale': // SALE
                Cache::delete(CacheConfig::KEY_ACTION_PRODUCT_NEWSALE_DISCOUNT . '153180b9a88c0b565848850c523bb637');
                Cache::delete(CacheConfig::KEY_ACTION_PRODUCT_NEWSALE_DISCOUNT . '0b2d133419a0f7c381306fd3522365e1');
                Cache::delete(CacheConfig::KEY_ACTION_PRODUCT_NEWSALE_DISCOUNT . 'de23648d28ee1e8a3f087a9dbac506f8');
                Cache::delete(CacheConfig::KEY_ACTION_PRODUCT_NEWSALE_DISCOUNT . '01269e498ff5b07756e0733ec0e88c75');
                break;
            case 'nav': // 首页左侧边栏
                Cache::delete(CacheConfig::KEY_COMMON_SIDE_NAV);
                break;
            case 'index': // 频道选择
                Cache::delete(CacheConfig::KEY_ACTION_INDEX_INDEX);
                Cache::delete(CacheConfig::KEY_ACTION_INDEX_CONFIG);
                break;
        }
    }

}