Coupon.php 3.24 KB
<?php

/**
 * Created by PhpStorm.
 * User: Targaryen
 * Date: 2016/4/11
 * Time: 16:31
 */
use Action\AbstractAction;
use LibModels\Wap\Coupon\CouponData;
use Plugin\DataProcess\CouponFloorProcess;
use Plugin\Helpers;


class CouponController extends AbstractAction
{
    private $contentCode = '';

    /**
     * 领券楼层数据列表
     *
     * @author Targaryen
     */
    public function floorAction()
    {
        $receiveData = filter_input_array(INPUT_GET, array(
            'contentCode' => FILTER_DEFAULT
        ));
        $this->contentCode = $receiveData['contentCode'];
        $result = array();

        // 正常接口调用
        $uid = $this->getUid();
        //tar debug
//        CouponFloorProcess::debugOut($uid);
        $resource = CouponData::getCouponRousource($receiveData['contentCode'], $uid);
        // 数据写死,调用接口,测试使用
//        $resource = CouponData::getCouponRousource('b38b9c4f1c76f89533e9214629b458e4', 8040277);
        if (isset($resource['code']) && $resource['code'] == 200) {
            $result = CouponFloorProcess::getContent($resource['data']);
        }

        // tar debug
//        CouponFloorProcess::debugOut($resource);
        $this->_view->display('index', array(
            'content' => $result,
            'floorPage' => true
        ));
    }

    /**
     * 领券操作
     *
     * @author Targaryen
     */
    public function receiveCouponAction()
    {
        // tar debug
//        CouponFloorProcess::debugOut(CouponData::receiveCoupon(8040277,2300));
        $returnData = array();
        // 获取优惠券 ID
        $receiveData = filter_input_array(INPUT_GET, array(
            'couponID' => FILTER_DEFAULT
        ));
        // 跳转 URl
        $playUrl = Helpers::url('/coupon/floor');
        // 判断用户是否登录
        if (!$this->getUid() || !is_numeric($this->getUid())) {
            // 用户未登录,跳转登录页面
            $returnData['noLogin'] = true;
            if (!$this->isApp()) {
                $returnData['url'] = Helpers::url('/signin.html', array('refer' => $playUrl), 'default');
            } else {
                $playUrlEncode = strtr($playUrl, array('/' => '\\/'));
                $returnData['url'] = $playUrl . '?openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":"' . $playUrlEncode . '","param":{"from":"app"}},"requesturl":{"url":"","param":{}},"priority":"N"}}';
            }
        } else {
            // 登录后调用领券接口
            $result = CouponData::receiveCoupon($this->getUid(), $receiveData['couponID']);
            switch ($result['code']) {
                case 200:
                    $returnData = array(
                        'msg' => '领券成功!',
                        'status' => true,
                    );
                    break;
                default:
                    $returnData = array(
                        'msg' => '领券失败!',
                        'status' => false,
                    );
                    break;
            }
        }
        echo json_encode($returnData);
    }

    /**
     * 判断是否是 APP
     *
     * @return bool
     */
    public function isApp()
    {
        return (null !== $this->get('app_version'));
    }

}