Opt.php 3.31 KB
<?php

use Action\AbstractAction;
use LibModels\Wap\Product\BrandData;
use Plugin\Helpers;

/**
 * 商品操作相关的控制器
 * 
 * @name OptController
 * @package product
 * @copyright yoho.inc
 * @version 1.0 (2015-10-27 19:13:28)
 * @author fei.hong <fei.hong@yoho.cn>
 */
class OptController extends AbstractAction
{

    /**
     * 品牌[店铺]收藏/取消收藏
     * 
     * @param int id 品牌ID
     * @param string opt 操作标识("ok":表示收藏,"cancel":表示取消收藏)
     * @return json
     */
    public function favoriteBrandAction()
    {
        header('Access-Control-Allow-Origin:' . $_SERVER['HTTP_ORIGIN']);
        header('Access-Control-Allow-Credentials:true');
        $result = array('code' => 401, 'message' => '参数不正确', 'data' => false);

        do {
            /* 判断品牌ID是否有效 */
            $id = $this->post('id');
            if (!is_numeric($id)) {
                break;
            }

            /* 判断用户是否登录 */
            $uid = $this->getUid();
            if (!$uid) {
                $referer = $this->server('HTTP_REFERER', SITE_MAIN);
                $result = array('code' => 400, 'message' => '未登录', 'data' => Helpers::url('/signin.html', array('refer' => $referer)));
                break;
            }

            /* 取消收藏 */
            $opt = $this->post('opt', 'ok');
            if ($opt !== 'ok') {
                $result = BrandData::favoriteCancel($id, $uid);
                break;
            }

            /* 收藏 */
            $result = BrandData::favorite($id, $uid);
            if (!isset($result['code'])) {
                $result = array('code' => 401, 'message' => '参数不正确', 'data' => false);
                break;
            }
        } while (false);

        $this->echoJson($result);
    }

    /**
     * 商品收藏/取消收藏
     * 
     * @param int id 商品ID
     * @param string opt 操作标识("ok":表示收藏,"cancel":表示取消收藏)
     * @return json
     */
    public function favoriteProductAction()
    {
        header('Access-Control-Allow-Origin:' . $_SERVER['HTTP_ORIGIN']);
        header('Access-Control-Allow-Credentials:true');
        $result = array('code' => 401, 'message' => '参数不正确', 'data' => false);

        do {
            /* 判断品牌ID是否有效 */
            $id = $this->post('id');
            if (!is_numeric($id)) {
                break;
            }

            /* 判断用户是否登录 */
            $uid = $this->getUid();
            if (!$uid) {
                $referer = $this->server('HTTP_REFERER', SITE_MAIN);
                $result = array('code' => 400, 'message' => '未登录', 'data' => Helpers::url('/signin.html', array('refer' => $referer)));
                break;
            }

            /* 取消收藏 */
            $opt = $this->post('opt', 'ok');
            if ($opt !== 'ok') {
                $result = BrandData::favoriteCancel($id, $uid, false);
                break;
            }

            /* 收藏 */
            $result = BrandData::favorite($id, $uid, false);
            if (!isset($result['code'])) {
                $result = array('code' => 401, 'message' => '参数不正确', 'data' => false);
                break;
            }
        } while (false);

        $this->echoJson($result);
    }

}