Common.php 7.46 KB
<?php
use Action\WebAction;
use Index\HomeModel;
use LibModels\Web\Home\IndexData;
use WebPlugin\Cache;
use Configs\CacheConfig;
use Api\Yohobuy;
use WebPlugin\Images;
use WebPlugin\Helpers;

class CommonController extends WebAction
{

    /**
     * 获取首页资源品牌
     */
    public function getIndexResourceBrandAction()
    {
        // 首页资源品牌,采用内存存储
        $type = $this->get('type');
        $data = array();
        if (! empty($type)) {
            $key = CacheConfig::KEY_INDEX_BRANDS_LIST_DATA . '_' . $type;
            // array('logoBrand'=>'','moreBrand'=>'')
            $data = Cache::get($key);
        }
        echo $this->echoJson($data);
    }

    /**
     * 新品上架 接口数据
     *
     * @param string channel 当前频道
     * @param int pageIndex 当前页数
     * @param int pageCount 一页显示个数
     */
    public function getNewArrivalAction()
    {
        $result = $data = array();
        do {
            /* 判断是不是AJAX请求 */
            if (! $this->isAjax()) {
                break;
            }
            $channels = array(
                HomeModel::COOKIE_NAME_BOYS,
                HomeModel::COOKIE_NAME_GIRLS,
                HomeModel::COOKIE_NAME_KIDS,
                HomeModel::COOKIE_NAME_LIFESTYLE
            );
            $channel = $this->post('type', '');
            $pageIndex = intval($this->post('pageIndex', 0));
            $pageCount = intval($this->post('pageCount', 8));
            if (! in_array($channel, $channels)) {
                break;
            } else {
                $data = HomeModel::getNewArrival($channel);
            }
            if($pageIndex < 0) {
                $pageIndex = 0;
            }
            if($pageCount < 0 || $pageCount > 50) {
                $pageCount = 20;
            }
            $result = array_slice($data, $pageIndex, $pageCount);
            if (empty($result)) {
                break;
            }
            $result = array(
                'code' => 200,
                'goods' => $result
            );
        } while (false);
        
        $this->echoJson($result);
    }
    
   /**
     * 获取资源位banner
     * 
     * @return jsonp
     */
    public function getbannerAction()
    {
        $url = 'http://service.api.yohobuy.com/operations/api/v4/resource/get?';
        $content_code = $this->get('content_code', '');
        $client_type = $this->get('client_type', 'web');
        $callback = $this->get('callback', '');
        $width = $this->get('width', '');
        $height = $this->get('height', '');
        $params = array(
                'content_code' => $content_code, 
                'client_type' => $client_type
        );
        $data = IndexData::getResourceData($content_code);//Yohobuy::get($url.http_build_query($params));
        if(empty($data['data']))
        {
            return $this->helpJsonCallbackResult($callback, 200, '没有数据', '');
        }
        else 
        {
            $banner = '';
            if(isset($data['data'][0]['data']))
            {
                if($data['data'][0]['template_name'] == 'single_image') {
                    $banner = current($data['data'][0]['data']);
                } else if($data['data'][0]['template_name'] == 'single_name_image') {
                    $banner = $data['data'][0]['data'];
                }
                if(!empty($banner)) {
                    if(empty($width) || empty($height) ) {
                          $width = 2600;//通栏广告
                          $height = 60;
                    }
                    $banner['src'] = Images::getImageUrl($banner['src'], $width, $height ,2);
                   //str_replace('?imageView/{mode}/w/{width}/h/{height}', '', $banner['src']);
                }
            }
        }
        return $this->helpJsonCallbackResult($callback, $data['code'], $data['message'], $banner);
    }
    
    /**
     * 获取邮件订阅
     * 
     * @return jsonp
     */
    public function emailsubscriberAction()
    {
        $callback = $this->get('callback', '');
        $email = $this->get('email', '');
        $uid = intval($this->get('uid', '0'));
        $data = array();
        //验证邮件
        if(Helpers::verifyEmail($email)) {
            $data = IndexData::emailSubscriber($email, $uid);
            return $this->helpJsonCallbackResult($callback, $data['code'], $data['message'], $data['data']);
        } else {
            return $this->helpJsonCallbackResult($callback, 403, '订阅失败', '');
        }
    }
    
    /**
     * 意见反馈
     * 
     * @return jsonp
     */
    public function suggestfeedbackAction()
    {
        $callback = $this->get('callback', '');
        $feedback_id = intval($this->get('feedback_id', 0));
        $question_id = intval($this->get('question_id', 0));
        $answer = trim($this->get('answer'));
        $solution = intval($this->get('solution', 0));
        if(!empty($feedback_id) || !empty($question_id) || !empty($answer) || !empty($solution)) {
            $data = IndexData::suggestFeedback($feedback_id, $question_id, $answer, $solution);
            return $this->helpJsonCallbackResult($callback, $data['code'], $data['message'], $data['data']);
        } else {
            return $this->helpJsonCallbackResult($callback, 403, '意见反馈失败', '');
        }
    }

    /*
     * 简单头部
     */

    public function getSimpleHeaderAction()
    {
        $result = array();
        do {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }

            //获取用户
            $uid = $this->getUid(true);
            if (!$uid) {
                $isLogin = false;
                $username = '';
            }
            else {
                $isLogin = true;
                $username = $this->_uname;
            }
            //拼接简单头部
            $tool = array(
                'favorite' => Helpers::url('/home/favorite?t=' . time()), //我的收藏链接
                'coupon' => Helpers::url('/home/coupons?t=' . time()), //我的优惠券链接
                'order' => Helpers::url('/home/orders?t=' . time()), //订单中心连接
                'help' => Helpers::url('/help'),
            );
            if ($isLogin) {
                $tool+=array(
                    'user' => $username,
                    'userCenter' => Helpers::url('/home?t=' . time()), //用户中心链接
                    'logout' => Helpers::url('/logout.html?t=' . time()), //退出
                );
            }
            else {
                $tool+=array(
                    'login' => Helpers::url('/signin.html'), //登录链接,已登录不传
                    'register' => Helpers::url('/reg.html'), //注册链接,已登录不传
                );
            }

            $simpleHeader = array(
                'user' => $username,
                'href' => $tool,
                'logo' => array(
                    'img' => 'http://static.yohobuy.com/newheader/img/logo_e.png',
                    'url' => SITE_MAIN
                ),
            );

            $result = array(
                'code' => 200,
                'data' => $simpleHeader
            );
        }
        while (false);

        $this->echoJson($result);
    }

}