Coupon.php 5.12 KB
<?php

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


class CouponController extends AbstractAction
{
    /**
     * 领券楼层数据列表
     *
     * @author chengyao.guo
     */
    public function floorAction()
    {
        $receiveData = filter_input_array(INPUT_GET, array(
            'code' => FILTER_DEFAULT,
            'title' => FILTER_DEFAULT,
        ));
        $result = array();
        $uid = '';
        if ($this->isApp()) {
            $uid = $this->get('uid');
        } else {
            $uid = $this->getUid();
        }
        $resource = CouponData::getCouponRousource($receiveData['code'], $uid);
        if (isset($resource['code']) && $resource['code'] === 200) {
            $result = CouponFloorProcess::getContent($resource['data'],$this->isApp());
        } else {
            $result['noData'] = true;
        }
        // 分享
        $shareData = CouponFloorProcess::getShare($receiveData['code'], '领券中心');
        $result[count($result)] = $shareData;
        if (!$this->isApp()) {
            $this->setNavHeader($receiveData['title'], true, SITE_MAIN);
        } else {
            $this->setTitle($receiveData['title'], false, null);
        }
        $this->_view->display('index', array(
            'content' => $result,
            'floorPage' => true
        ));
    }

    /**
     * 领券操作
     *
     * @author chengyao.guo
     */
    public function receiveCouponAction()
    {
        $returnData = array();
        // 获取优惠券 ID
        $receiveData = filter_input_array(INPUT_GET, array(
            'couponID' => FILTER_DEFAULT,
            'code' => FILTER_DEFAULT,
            'app_version' => FILTER_DEFAULT
        ));
        if ($receiveData['couponID']) {
            $receiveData['couponID'] = Encryption::decrypt($receiveData['couponID']);
        }
        // 跳转 URl
        $playUrl = Helpers::url('/coupon/floor');
        // 判断用户是否登录
        $isLogin = false;
        if ($this->isApp()) {
            if ($this->get('uid')) {
                $isLogin = true;
            }
        } else {
            if ($this->getUid()) {
                $isLogin = true;
            }
        }
        if (!$isLogin) {
            // 用户未登录,跳转登录页面
            $returnData['noLogin'] = true;
            if (!$this->isApp()) {
                $returnData['url'] = Helpers::url('/signin.html', array('refer' => $playUrl), 'default');
            } else {
                $returnData['isApp'] = true;
                $playUrlEncode = strtr($playUrl . '?code=' . $receiveData['code'] .'&app_version=' . $receiveData['app_version'], array('/' => '\\/'));
                $returnData['url'] = $playUrl . '?openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":"' . $playUrlEncode . '","param":{"from":"app"}},"requesturl":{"url":"","param":{}},"priority":"Y"}}';
//                $playUrlEncode = strtr($playUrl, array('/' => '\\/'));
//                $returnData['url'] = $playUrl . '?openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":"' . $playUrlEncode . '","param":{"from":"app","code":"' . $receiveData['code'] . '"}},"requesturl":{"url":"","param":{}},"priority":"N"}}';
            }
        } else {
            // 登录后调用领券接口
            $result = array();
            if($this->isApp()){
                $result = CouponData::receiveCoupon($this->get('uid'), $receiveData['couponID']);
            }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()
    {
        $isMobile = $this->get('app_version');
        return !empty($isMobile);
    }

    /**
     * 获取用户User-Agent
     * @return bool
     */
    public function getUserAgent()
    {
        if (!isset($_SERVER['HTTP_USER_AGENT']) || empty($_SERVER['HTTP_USER_AGENT'])) {
            return '';
        }
        return $_SERVER['HTTP_USER_AGENT'];
    }


    /**
     * 获取优惠券
     */
    public function couponSendAction()
    {
        $callback = $this->get('callback');
        $token = $this->get('token', '');
        $uid = $this->getUid(true);
        $app = $this->get('app', array());

        $result = CouponModel::couponSend($uid, $token, $app);

        $this->helpJsonCallbackResult($callback, $result['code'], $result['message'], $result['data']);
    }
}