Yuanxiao.php 7.04 KB
<?php

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

/**
 * 元宵抽签活动
 */
class YuanxiaoController extends HuodongAction
{
    const STATIC_FILE = 'http://cdn.yoho.cn/huodong/2016yuanxiao/1.0.0';
    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,
            '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();

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

            // 调用接口
            $result = array(
                'code' => 200,
                'data' => array(
                    'name' => '李四',
                    'type' => 'P1',
                    'count' => 8888
                )
            );
            if ($result['code'] === 200) { // 处理成功就跳转到等待页
                // 将返回的结果存入cookeie中15分钟
                $this->setCookie('yuanxiaochouqian', json_encode($result['data']), 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 {
            $uid = $this->getUid();
            $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
        ));
    }

    /**
     * 元宵抽签 结果
     */
    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(
            'name' => $this->encrypt($result['name']),
            'hash' => $this->encrypt($this->getUid())
        ));

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

    /**
     * 元宵抽签 分享
     */
    public function shareAction()
    {
        // 昵称从url中解析
        $name = $this->decrypt($this->get('name', ''));
        // 用户UID
        $uid = $this->decrypt($this->get('hash', ''));

        // 调用接口获取type和count
        $result = array('type' => 'M1', 'count' => 9887);

        $this->_view->display('share', array(
            'staticTitle' => '元宵抽签',
            'staticFile' => self::STATIC_FILE,
            'result' => array(
                'name' => $name,
                'type' => $result['type']
            ),
            'count' => $result['count'],
            '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;
    }
}