Yuanxiao.php 7.38 KB
<?php

use Action\HuodongAction;
use Cuxiao\YuanxiaoModel;
use Plugin\Helpers;

/**
 * 元宵抽签活动
 */
class YuanxiaoController extends HuodongAction
{
    // const STATIC_FILE = 'http://localhost:2222/assets/1.0.2';
    const STATIC_FILE = 'http://cdn.yoho.cn/huodong/2016yuanxiao/1.0.2';
    const ENCRYPT_KEY = 'iamgtskkwhoareyou';

    /**
     * 通过当前用户审判是否跳到登录页
     *
     */
    private function auditJumpLogin()
    {
        $uid = $this->getLoggedUid();
        if (!$uid || !is_numeric($uid)) {
            $this->go(Helpers::url('/signin.html', array('refer' => Helpers::url('/cuxiao/yuanxiao/info')), 'default'));
        }
    }

    /**
     * 元宵抽签
     */
    public function indexAction()
    {
        // 该变量用来控制和显示页面的弹出提示
        $showPopupFlag = '0';

        $this->_view->display('index', array(
            'staticTitle' => '元宵抽签',
            'staticFile' => self::STATIC_FILE,
            'staticJS' => array(
                'home.js'
            ),
            'weixinShare' => $this->_isApp ? false : true, // 是否需要微信分享
            'shareLink' => 'http://feature.yoho.cn/2016lishifeng/index.html',
            'shareTitle' => 'YOHO!BUY有货新年寻宝大作战,千元利是等你赢!',
            'shareDesc' => 'YOHO!BUY有货,全球800+潮流品牌每日上新!',
            'shareImg' => 'http://img12.static.yhbimg.com/couponImg/2015/12/30/12/02008a9724b898dee56852de9f1a3978bf.jpg',
            'showPopupFlag' => $showPopupFlag,
        ));
    }

    /**
     * 元宵抽签 信息
     */
    public function infoAction()
    {
        // 审判跳转登录页
        $this->auditJumpLogin();

        $uid = $this->getUid();
        // POST提交请求
        $posts = $this->post();
        if (!empty($posts)) {
            $nickName = $this->post('nick', '');
            $birthday = $this->post('birthday', '');
            $gender = $this->post('gender', 0);

            // 调用接口
            $result = YuanxiaoModel::getChouqianInfo($uid, $nickName, $birthday, $gender);
            if ($result['code'] === 200) { // 处理成功就跳转到等待页
                // 将返回的结果存入cookeie中15分钟
                $cookieData = array(
                    'name' => $nickName,
                    'type' => $result['data']['randomCode']
                );
                $this->setCookie('yuanxiaochouqian', json_encode($cookieData), time() + 15*60);
                $this->go(Helpers::url('/cuxiao/yuanxiao/wait'));
            } else { // 不成功就显示返回的错误信息
                $this->_view->display('info', array(
                    'staticTitle' => '元宵抽签',
                    'staticFile' => self::STATIC_FILE,
                    'staticJS' => array(
                        'info.js'
                    ),
                    'birthday' => $birthday,
                    'gender' => $gender,
                    'message' => $result['message']
                ));
            }
        } else {
            $userData = YuanxiaoModel::getUserProfileData($uid);

            $this->_view->display('info', array(
                'staticTitle' => '元宵抽签',
                'staticFile' => self::STATIC_FILE,
                'staticJS' => array(
                    'info.js'
                ),
                'birthday' => $userData['birthday'],
                'gender' => $userData['gender'],
            ));
        }
    }

    /**
     * 元宵抽签 等待
     */
    public function waitAction()
    {
        // 审判跳转登录页
        $this->auditJumpLogin();

        $this->_view->display('wait', array(
            'staticTitle' => '元宵抽签',
            'staticFile' => self::STATIC_FILE,
            'staticJS' => array(
                'wait.js'
            ),
        ));
    }

    /**
     * 元宵抽签 结果
     */
    public function resultAction()
    {
        // 审判跳转登录页
        $this->auditJumpLogin();

        $result = $this->getCookie('yuanxiaochouqian', null);
        // if (empty($result)) { // 未取到信息就重新测试
        //     $this->go(Helpers::url('/cuxiao/yuanxiao/info'));
        // }
        // cookie中存的结果
        $result = json_decode($result, true);
        $shareUrl = Helpers::url('/cuxiao/yuanxiao/share', array(
            'hash' => $this->encrypt($this->getUid())
        ));

        $this->_view->display('result', array(
            'staticTitle' => '元宵抽签',
            'weixinShare' => $this->_isApp ? false : true, // 是否需要微信分享
            'staticFile' => self::STATIC_FILE,
            'result' => $result,
            'shareUrl' => $shareUrl,
            'staticJS' => array(
                'result.js'
            )
        ));
    }

    /**
     * 元宵抽签 分享
     */
    public function shareAction()
    {
        // 用户UID
        $uid = $this->decrypt($this->get('hash', ''));
        if (empty($uid)) {
            $this->go(Helpers::url('/cuxiao/yuanxiao/index'));
        }

        // 调用接口获取type和count
        $result = YuanxiaoModel::getChoujiangShareInfo($uid);

        $this->_view->display('share', array(
            'staticTitle' => '元宵抽签',
            'staticFile' => self::STATIC_FILE,
            'result' => array(
                'name' => $result['name'],
                'type' => $result['type']
            ),
            'count' => $result['total'],
            'staticJS' => array(
                'share.js'
            )
        ));
    }

    /**
     * 检测登录状态
     *
     * @return int
     */
    private function getLoggedUid()
    {
        if ($this->_isApp) {
            $uid = $this->get('uid');
        } else {
            $uid = $this->getUid();
        }

        return $uid;
    }

    /**
     * 加密字符串
     *
     * @param string $data 需要加密的字符串
     * @return string 加密之后的字符串
     */
    private function encrypt($data)
    {
        $data = strval($data);
        $key	=	md5(self::ENCRYPT_KEY);
        $x		=	0;
        $len	=	strlen($data);
        $l		=	strlen($key);
        $char = '';
        $str = '';

        for ($i = 0; $i < $len; $i++) {
            if ($x == $l)
            {
                $x = 0;
            }
            $char .= $key{$x};
            $x++;
        }

        for ($i = 0; $i < $len; $i++) {
            $str .= chr(ord($data{$i}) + (ord($char{$i})) % 256);
        }

        return base64_encode($str);
    }

    /**
     * 解密字符串
     *
     * @param string $data 需要解密的字符串
     * @return string 解密之后的字符串
     */
    private function decrypt($data)
    {
        $key = md5(self::ENCRYPT_KEY);
        $x = 0;
        $data = base64_decode($data);
        $len = strlen($data);
        $l = strlen($key);
        $char = '';
        $str = '';

        for ($i = 0; $i < $len; $i++) {
            if ($x == $l)
            {
                $x = 0;
            }
            $char .= substr($key, $x, 1);
            $x++;
        }

        for ($i = 0; $i < $len; $i++) {
            if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))) {
                $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
            } else {
                $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
            }
        }

        return $str;
    }
}