<?php
class Controller_Activity_Christ extends Controller_Base
{
private $_openId = null;
private $_wechatUnionId = null;
public function init()
{
parent::init();
if(!Dg_Helper_Common::isWeixin() || Dg_Helper_Common::isWeixin()){
//微信跳转到登录页面
// 获取openid , 本地数据库中的用户数据
$this->_openId = Hnb_Session::getInstance()->getSessionData('weixin_open_id');
$this->_wechatUnionId = Hnb_Session::getInstance()->getSessionData('weixin_union_id');
$nowUrl = "http://m.hinabian.com/activity_christ/index.html";
if (!$this->_openId && !$this->_wechatUnionId) {
$jumpUrl = 'http://www.hinabian.com/weixin/snsBaseForMActivity1000?login_referrer=' . urlencode($nowUrl);
header("Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . WEIXIN_WEB_APP_ID . "&redirect_uri=" . urlencode($jumpUrl). "&response_type=code&scope=snsapi_base&state=1#wechat_redirec");
exit();
}
} /*else {
header("Content-type: text/html; charset=utf-8");
echo "微信登录失败!<a href='http://m.hinabian.com/activity_christ/index.html'>点击重试</a>";
}*/
$this->assign('title',"摇滚圣诞节,high到海那边");
$this->assign("share_title" , "我在海那边摇滚圣诞,一大波圣诞礼物来袭,一起来摇滚吧!");
$this->assign("share_friend_title" , "我在海那边摇滚圣诞,一大波圣诞礼物来袭,一起来摇滚吧!");
$this->assign("share_friend_desc" , "我在海那边摇滚圣诞,一大波圣诞礼物来袭,一起来摇滚吧!");
$this->assign("share_url" , "http://m.hinabian.com/activity_christ/index.html?ss=1212");
}
private function _top()
{
$arr_max = Hnb_Model_Activity_Christ::getInstance()->selectMax();
$arr_return = Dg_Weixin::getInstance()->getUserInfo(WEIXIN_WEB_APP_ID , WEIXIN_WEB_APP_SECRET , $arr_max['f_wechat_open_id']);
if(!isset($arr_return['data']['nickname']))
{
$arr_return['data'] = $arr_max;
} else {
$arr_return['data']['f_headimgurl'] = $arr_return['data']['headimgurl'];
}
return $arr_return;
}
// 初始化网页授权
private function _initSns()
{
$nowUrl = "http://m.hinabian.com/activity_christ/index.html";
$jumpUrl = 'http://www.hinabian.com/weixin/snsInfo?login_referrer=' . urlencode($nowUrl);
header("Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . WEIXIN_WEB_APP_ID . "&redirect_uri=" . urlencode($jumpUrl). "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirec");
}
public function indexAction()
{
$arr_visitor = Dg_Weixin::getInstance()->getUserInfo(WEIXIN_WEB_APP_ID , WEIXIN_WEB_APP_SECRET , $this->_openId);
$arr_sns_visitor = unserialize(Hnb_Session::getInstance()->getSessionData('weixin_info'));
// 通过公众号关注接口获取的数据错误时,那么就进行用户授权模式
if(!isset($arr_visitor['data']['nickname']) && !$arr_sns_visitor && Dg_Helper_Common::isWeixin())
{
$this->_initSns();
}
$arr_wxUserInfo = $this->_top();
$this->assign("share_img" , $arr_wxUserInfo['data']['f_headimgurl']);
$this->assign('arr_wxUserInfo' , $arr_wxUserInfo);
$this->assign("mod" , 'christ-index');
$this->display("activity");
}
// 获取微信用户资料
private function _getWxUserInfo()
{
$arr_visitor = Dg_Weixin::getInstance()->getUserInfo(WEIXIN_WEB_APP_ID , WEIXIN_WEB_APP_SECRET , $this->_openId);
$arr_sns_visitor['data'] = unserialize(Hnb_Session::getInstance()->getSessionData('weixin_info'));
return isset($arr_visitor['data']['nickname']) ? $arr_visitor : $arr_sns_visitor;
}
// 记录用户
public function recAction()
{
$count = Dg_Helper_UrlParam::request('count' , '');
$circle = Dg_Helper_UrlParam::request('circle' , '');
$sessionKey = Hnb_Session::getInstance()->getSid();
$arr_wxUserInfo = $this->_getWxUserInfo();
$arr_row = array(
'f_no' => $count,
//'f_circle' => $circle,
'f_name' => $arr_wxUserInfo['data']['nickname'],
'f_session_key' => $sessionKey,
'f_wechat_open_id' => $this->_openId,
'f_headimgurl' => $arr_wxUserInfo['data']['headimgurl'],
'f_ctime' => date('Y-m-d H:i:s')
);
$id = Hnb_Model_Activity_Christ::getInstance()->rec($arr_row);
$arr_row = Hnb_Model_Activity_Christ::getInstance()->getRow(array('f_id' => $id));
// On tree
$arr_top = $this->_top();
$return = $this->_logic($this->_openId , $id);
$before = Hnb_Model_Activity_Christ::getInstance()->getGtNum($arr_row['f_no']);
$arr_return = Dg_Helper_Common::initReturn($id ? NO_ERROR : ERROR_COMMON , $id ? NO_ERROR : ERROR_COMMON , $id && $return['data'] ? $return['data'] : array('f_id' => false , 'headimgurl' => $arr_top['data']['f_headimgurl'] , 'had_win' => $return['had_win'] , 'no_win' => $return['no_win'] , 'num' => $arr_row['f_no'] , 'before' => $before));
$this->end($arr_return);
}
private function _logic($wechat_open_id , $id)
{
$arr_row = Hnb_Model_Activity_Christ::getInstance()->getRow(array('f_wechat_open_id' => $wechat_open_id));
//$arr_return = false;
$arr_return = array('had_win' => false , 'no_win' => false , 'data' => false);
if(!$arr_row)
{
// 计算当前的用户获奖的概率
$arr_row = Hnb_Model_Activity_Christ::getInstance()->getRow(array('f_id' => $id));
// 随机计算
//$award_id = $this->_award();
$award_id = $this->_award100();
//$award_id = rand(2 , 4); // 测试使用
// 中奖的
if($award_id)
{
// 查询奖品的信息
$arr_return['data'] = Hnb_Model_Activity_Christ::getInstance()->getOneGift($award_id)[0];
// 将该奖品放入中奖池子
$a = Hnb_Model_Activity_Christ::getInstance()->updateRow(array('f_wechat_open_id' => $this->_openId));
$arr_row['award_id'] = $award_id;
$logPath = DOC_ROOT . '/log/activity/' . date('Y-m-d') . '-awards.log';
Dg_Helper_Log::getInstance()->init($logPath);
Dg_Helper_Log::getInstance()->info($arr_row);
} else {
$arr_return['no_win'] = true;
}
} else {
$arr_return['had_win'] = true;
}
return $arr_return;
}
public function _award100()
{
return false;
$ret = $this->_limitNum(rand(1 , 100));
if($ret)
{
$arr_row = array(
'f_wechat_open_id' => $this->_openId,
'f_session_key' => Hnb_Session::getInstance()->getSid(),
'f_award_id' => $ret,
'f_ctime' => date('Y-m-d H:i:s')
);
Hnb_Model_Activity_Christ::getInstance()->insertWin($arr_row);
}
return $ret;
}
private function _award()
{
$percent = mt_rand() / mt_getrandmax();
// 8 , 9 , 10
// Hnb
if($percent < 0.01212)
{
$no = array(11);
}
if($percent < 0.01212)
{
$no = array(11);
}
// lvpeng
if($percent < 0.38787)
{
$no = array(8 , 9 , 10);
}
// lvpeng
if($percent < 0.377)
{
$no = array(8 , 9 , 10);
}
// lvpeng
if($percent < 0.34444)
{
$no = array(8 , 9 , 10);
}
// lvpeng
if($percent < 0.25)
{
$no = array(8 , 9 , 10);
}
// dada
if($percent < 0.48485)
{
$no = array(1);
}
// qiubao
if($percent < 0.00485)
{
$no = array(2);
}
// qiubao
if($percent < 0.90)
{
$no = array(2);
}
// jumi
if($percent < 0.11271)
{
$no = array(5 , 6 , 7);
}
if($percent < 0.18)
{
$no = array(5 , 6 , 7);
}
if($percent < 0.38)
{
$no = array(5 , 6 , 7);
}
if($percent < 0.74)
{
$no = array(5 , 6 , 7);
}
$r = false;
if($no)
{
if(count($no) > 1)
{
$rand = rand(1 ,11);
if(in_array($rand , $no))
{
$r = $rand;
} else {
return false;
}
} else {
$r = $no[0];
}
// 奖品数量比较大的brand
$arr_id = array(9 , 10 , 5 , 6 , 7);
// 如果奖品消耗完了,立即返回 false
$this->_limitNum($r);
if(in_array($r , $arr_id))
{
$arr_row = array(
'f_wechat_open_id' => $this->_openId,
'f_session_key' => Hnb_Session::getInstance()->getSid(),
'f_award_id' => $r,
'f_ctime' => date('Y-m-d H:i:s')
);
Hnb_Model_Activity_Christ::getInstance()->insertWin($arr_row);
} else {
return 0;
}
}
return $r;
}
// 限制数量
private function _limitNum($id)
{
$arr_nums = array(
1 => 2000,
2 => 20,
3 => 5,
4 => 30,
5 => 30,
6 => 200,
7 => 200,
8 => 100,
9 => 500,
10 => 1000,
11 => 50
);
$arr_awNums = Hnb_Model_Activity_Christ::getInstance()->getNumOfAw();
$arr_awNums = array_column($arr_awNums , 'tt' , 'id');
if($arr_awNums[$id] >= $arr_nums[$id])
{
return false;
}
}
// 规则
public function ruleAction()
{
$this->assign("share_img" , "http://cache.hinabian.com/mobile/images/activity/christ/meman.png");
$this->assign("mod" , "christ-rule");
$this->display("activity");
}
public function focusAction()
{
$arr_allRows = Hnb_Model_Activity_Christ::getInstance()->getAll(array('f_name' => true , 'f_headimgurl' => true , 'f_no' => true));
// 每个wechat_open_id取最大值的f_no
//$arr_focus = array_slice($arr_allRows , 0 , 10);
$arr_focus = array();
$wechat_open_id = null;
foreach($arr_allRows as $k => $u)
{
// 分组
$arr_focus[$u['f_wechat_open_id']][] = $u;
}
$arr_return = [];
foreach($arr_focus as $rows)
{
usort($rows , array($this , '_cmp'));
$arr_return[] = $rows[0];
}
if(count($arr_return) > 1)
{
usort($arr_return , array($this , '_cmp'));
}
$this->assign("share_img" , "http://cache.hinabian.com/mobile/images/activity/christ/meman.png");
$this->assign('arr_focus' , $arr_return);
$this->assign("mod" , "christ-focus");
$this->display("activity");
}
public function ssAction()
{
$name = Dg_Helper_UrlParam::request('name' , '');
$mobile = Dg_Helper_UrlParam::request('mobile' , '');
$addr = Dg_Helper_UrlParam::request('addr' , '');
$id = Dg_Helper_UrlParam::request('id' , '');
$validate = new Dg_Helper_Validate();
$validate->addRule('name' , array('required' => true));
$validate->addRule('mobile' , array('required' => true));
if($id != 4)
{
$validate->addRule('addr' , array('required' => true));
}
$this->runCheck($validate);
$arr_row = array(
'f_name' => $name,
'f_mobile' => $mobile,
'f_addr' => $addr,
'f_award_id' => $id,
'f_wechat_open_id' => $this->_openId,
'f_ctime' => date('Y-m-d H:i:s')
);
$id = Hnb_Model_Activity_Christ::getInstance()->insertAward($arr_row);
$this->end(Dg_Helper_Common::initReturn($id ? NO_ERROR : ERROR_COMMON , $id ? NO_ERROR : ERROR_COMMON , $id ? $id : '您的网络有点慢,请稍后再试。'));
}
private function _cmp($a , $b)
{
if($a['f_no'] == $b['f_no'])
{
return 0;
}
return ($a['f_no'] > $b['f_no']) ? -1 : 1;
}
public function testAction()
{
$arr_allGifts = Hnb_Model_Activity_Christ::getInstance()->getAllGifts();
$this->assign('arr_allGifts' , $arr_allGifts);
$this->assign('mod' , 'christ-test');
$this->display('activity');
}
public function ttAction()
{
$arr_wechatOpenIds= array(
'oBOvWtyThe9Mc7-hAWSfT48BGbU4',
'oBOvWt-AHcqbdd84YVV-SXUQzbFU',
'oBOvWt4asEQla9H5Yld2ynTMMOzU',
'oBOvWtwZI7FJUvTdjpo7833su-sA',
'oBOvWt2Tu1m8Lq5opkzwONl0I90s',
'oBOvWtxLzYSbfYA6IjsrDbkAtpBM'
);
$arr_return = [];
foreach($arr_wechatOpenIds as $id)
{
$arr_return[] = Dg_Weixin::getInstance()->getUserInfo(WEIXIN_WEB_APP_ID , WEIXIN_WEB_APP_SECRET , $id);
}
print_r($arr_return);
}
public function getaumAction()
{
$results = Hnb_Model_Activity_Christ::getInstance()->selectAwIdNum(3);
var_dump($results);
}
}