Favorite.php 7.46 KB
<?php

use Action\WebAction;
use WebPlugin\Helpers;
use Home\UserModel;
use Home\FavoriteModel;
use LibModels\Web\Product\FavoriteData;

/**
 * 商品收藏
 */
class FavoriteController extends WebAction
{

    /**
     * 我的收藏
     */
    public function indexAction()
    {
        //判断是否登录
        $uid = $this->auditJumpLogin(true, Helpers::url('/home/favorite'));
        $udid = $uid . $this->getUdid();
        //头部导航
        $channel = Helpers::getChannelNameByCookie();
        $this->setWebNavHeader($channel);
        $page = $this->get('page', 1);
        $type = $this->get('type', 'product'); //收藏类别
        $sort = $this->get('sort_id', 0); //商品分类
        $reduction = $this->get('is_reduction', 'N'); //降价商品
        $promotion = $this->get('is_promotion', 'N'); //参加活动的商品
        $limit = 10;
        $data = array(
            'path' => UserModel::getCenterCrumb('我的收藏'),
            'userThumb' => UserModel::getUserHeadImg($uid),
            'homeNav' => UserModel::getCenterLeftNav('我的收藏', $uid),
            'tabs' => FavoriteModel::getFavoriteTabs($type),
        );
        switch (strval($type)) {
            case 'brand':
                $data['favBrands'] = FavoriteModel::favoriteBrandList($uid, $page, $limit, $type); //品牌收藏
                break;
            case 'article':
                $data['favArticles'] = FavoriteModel::favoriteArticleList($uid, $udid, $page, $limit, $type); //文章收藏
                break;
            default:
                $data['favProducts'] = FavoriteModel::favoriteProductList($uid, $page, $limit, $type, $sort, 'N', $reduction, $promotion); //商品收藏
                break;
        }
        $this->_view->display('favorite', array('meFavoritePage' => true, 'meFavorite' => $data));
    }

    /**
     * 品牌收藏-新品到着
     */
    public function newproductAction()
    {
        $result = array('code' => 400, 'message' => '', 'data' => '');
        do {
            //获取相关参数
            $uid = $this->getUid(true);
            $page = $this->get('page', 1);
            $limit = 10;
            $id = $this->get('id', '');
            if (!$uid || !$id) {
                $result = array('code' => 400, 'message' => '缺少参数', 'data' => '');
                break;
            }
            $data = FavoriteModel::newproduct($uid, $page, $limit, $id);
            $result = array('code' => 200, 'message' => '', 'data' => $data);
        }
        while (false);

        $this->echoJson($result);
    }

    /**
     * 商品收藏,降价通知
     */
    public function reductionAction()
    {
        //判断是否登录
        $uid = $this->auditJumpLogin();
        //显示头部导航
        $channel = Helpers::getChannelNameByCookie();
        $this->setWebNavHeader($channel);
        $page = $this->get('page', 1);
        $type = $this->get('type', '');
        $limit = 10;
        $reduction = FavoriteModel::favoriteProductList($uid, $page, $limit, $type, 0, $subscribe = 'Y');
        $data = array(
            'path' => UserModel::getCenterCrumb('我的收藏'),
            'userThumb' => UserModel::getUserHeadImg($uid),
            'homeNav' => UserModel::getCenterLeftNav('我的收藏', $uid),
            'tabs' => FavoriteModel::getFavoriteTabs('product'),
            'reductionUrl' => Helpers::url('/home/favorite'),
            'goods' => $reduction['goods'],
        );
        $this->_view->display('reduction', array('meFavoritePage' => true, 'meFavorite' => $data));
    }

    /**
     * 订阅降价通知
     */
    public function noticeAction()
    {
        $result = array('code' => 400, 'message' => '', 'data' => '');
        do {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }
            //获取相关参数
            $uid = $this->getUid(true);
            $id = $this->get('id', '');
            $mobile = $this->get('mobile', '');
            $data = FavoriteData::redutionAdd($uid, $mobile, $id);
            if (!isset($data['code'])) {
                break;
            }
            //降价通知订阅失败
            if ($data['code'] === 500) {
                $result = array('code' => 500, 'message' => '', 'data' => '');
                switch (strval($data['message'])) {
                    case 'count must be  lt 5':
                        $result['message'] = '您的订阅数已经到达上限';
                        break;
                    case 'mobile must bu not null':
                        $result['message'] = '请填写手机号';
                        break;
                    default:
                        $result['message'] = '订阅失败';
                        break;
                }
            }else{
                $result = $data;
            }          
        }
        while (false); 
        $this->echoJson($result);
    }

    /**
     * 取消订阅通知
     */
    public function cancelnoticeAction()
    {
        $result = array('code' => 400, 'message' => '取消失败', 'data' => '');
        do {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }
            //获取相关参数
            $uid = $this->getUid(true);
            $id = $this->get('id', '');
            if (!$uid || !$id) {
                $result = array('code' => 400, 'message' => '取消失败', 'data' => '');
                break;
            }
            $result = FavoriteData::redutionCancel($uid, $id);
            if (!isset($result['code'])|| $result['code'] !==200) {
                break;
            }
        }
        while (false);
        $this->echoJson($result);
    }

    /**
     * 取消收藏
     */
    public function cancelAction()
    {
        $result = array('code' => 400, 'message' => '', 'data' => '');
        do {
            //判断是否ajax请求
            if (!$this->isAjax()) {
                break;
            }
            //获取相关参数
            $uid = $this->getUid(true);
            $id = $this->get('id', '');
            $type = $this->get('type', '');
            $shopid = $this->get('shopid', '');
            $brandorshoptype = $this->get('brandorshoptype', '');

            if (!$uid || !$id) {
                $result = array('code' => 400, 'message' => '缺少参数', 'data' => '');
                break;
            }
            if ($brandorshoptype && $shopid && $brandorshoptype === 'shop') {
                $result = FavoriteData::cancelProductFav($uid, $shopid, false, true); //取消店铺收藏
                break;
            }
            switch (strval($type)) {
                case 'brand':
                    $result = FavoriteData::cancelProductFav($uid, $id, true); //取消品牌收藏
                    break;
                case 'article':
                    $result = FavoriteData::changeFavoriteArticle($id, $uid); //取消文章收藏
                    break;
                default:
                    $result = FavoriteData::cancelProductFav($uid, $id); //取消商品收藏
                    break;
            }
            if (!isset($result['code'])) {
                break;
            }
        }
        while (false);

        $this->echoJson($result);
    }

}