Coupon.php 2.7 KB
<?php

use Action\WebAction;
use WebPlugin\Helpers;
use Index\HomeModel;
use Index\CouponModel;
use LibModels\Web\Home\CouponData;

/**
 * 领券频道
 */
class CouponController extends WebAction
{

    /**
     * 判断是否登录
     */
    private function checkLogin()
    {
        $playUrl = Helpers::url('/coupon/index');

        $uid = $this->getUid();
        if (!$uid) {
            $this->go(Helpers::url('/signin.html', array('refer' => $playUrl), 'default'));
        }
    }

    /**
     * 领券页面控制器
     */
    public function indexAction()
    { 
        $channel = Helpers::getChannelNameByCookie();
        //领券频道头部
        $this->setWebNavHeader($channel);
        //获取领券频道资源
        $uid = $this->getUid();
        $code_coupon = filter_input_array(INPUT_GET, array('contentCode' => FILTER_DEFAULT ));//领券频道资源码
//        $data = CouponModel :: getCouponData(HomeModel::CODE_COUPON_CHANNEL, $uid);
        $data = CouponModel :: getCouponData( $code_coupon['contentCode'], $uid);
        $this->_view->display('index', $data);
    }

    /**
     * 领券接口
     */
    public function sendCouponAction()
    {
        $result = array('code' => 400, 'message' => '领取失败', 'data' => '');
        do {
            if (!$this->isAjax()) {
                break;
            }
            $couponId = $this->get('id', '');
            $uid = $this->getUid();
            if (!$uid) {
                $playUrl = Helpers::url('/coupon/index');
                $result = array('code' => 403, 'message' => '未登录', 'data' => '',
                    'url' => Helpers::url('/signin.html', array('refer' => $playUrl), 'default'));
                break;
            }
            //领取优惠券
            $sendcoupon = CouponData::sendCoupon($couponId, $uid);
            switch ($sendcoupon['code']) {
                case 200 :
                    $result = array('code' => 200, 'message' => '恭喜您,成功领取优惠券',
                        'data' => '', 'url' => Helpers::url('/home/coupons'));
                    break;
                case 401 :
                    $result = array('code' => 401, 'message' => '您已领取过优惠券', 'data' => '');
                    break;
                case 315;
                    $result = array('code' => 315, 'message' => '优惠券已过期', 'data' => '');
                    break;
                default :
                    $result = array('code' => 400, 'message' => '领取失败', 'data' => ''); //403已领完 400未登录
            }
        }
        while (false);
        $this->echoJson($result);
    }

}