首页控制器
public function actionIndex() {
$lottery = LotteryGoods::model()->findAll();//查询抽奖商品
$account = Yii::app()->user->id; //获取当前用户ID
$user = Users::model()->findByAttributes(array("account" => $account));//查询用户信息
$near = Yii::app()->redis->lRange('lottery_account_near', 0, 13); //得到最近记录
$this->render('index', compact("lottery", "user", "near"));
}
首页前端展示
<div class="container jy_box" style="padding:0;">
<div class="game_box gq_box">
<img src="<?php echo Yii::app()->versionUrl->changeUrl(COMMONSTATICFILE . "/WanFeng/images/wf_gq_top.png") ?>" style="margin-top: -12%;">
<img src="<?php echo Yii::app()->versionUrl->changeUrl(COMMONSTATICFILE . "/WanFeng/images/wf_gq_center.png") ?>">
<img src="<?php echo Yii::app()->versionUrl->changeUrl(COMMONSTATICFILE . "/WanFeng/images/wf_gq_kg.png") ?>">
<img src="<?php echo Yii::app()->versionUrl->changeUrl(COMMONSTATICFILE . "/WanFeng/images/wf_gq_bg.png") ?>">
<div class="Lucky_box game_v3">
//奖品展示
<ul>
<li><a href="javascript:;" class="roll-2">
<img src="<?php echo $lottery[1]->image ?>">
<div class="Lucky_box_layer" style="display: none; background-color: rgb(255, 255, 255);"></div>
</a></li>
<li><a href="javascript:;" class="Lucky_box_li_active roll-1">
<img src="<?php echo $lottery[0]->image ?>">
<div class="Lucky_box_layer " style="display: none; background-color: rgb(0, 0, 0);"></div>
</a></li>
<li><a href="javascript:;" class="roll-8">
<img src="<?php echo $lottery[7]->image ?>">
<div class="Lucky_box_layer" style="display: none; background-color: rgb(0, 0, 0);"></div>
</a></li>
<li><a href="javascript:;" class="roll-3">
<img src="<?php echo $lottery[2]->image ?>">
<div class="Lucky_box_layer" style="display: none; background-color: rgb(0, 0, 0);"></div>
</a></li>
<li><a href="javascript:;" class="lucky_box_btn" id="start">
<span class="lucky_num" style="">剩余<font id="self_point"><?php echo $user["lottery_num"] ?></font>次</span>
</a></li>
<li><a href="javascript:;" class="roll-7">
<img src="<?php echo $lottery[6]->image ?>">
<div class="Lucky_box_layer" style="display: none; background-color: rgb(0, 0, 0);"></div>
</a></li>
<li><a href="javascript:;" class="roll-4">
<img src="<?php echo $lottery[3]->image ?>">
<div class="Lucky_box_layer" style="display: none; background-color: rgb(0, 0, 0);"></div>
</a></li>
<li><a href="javascript:;" class="roll-6">
<img src="<?php echo $lottery[5]->image ?>">
<div class="Lucky_box_layer" style="display: none; background-color: rgb(0, 0, 0);"></div>
</a></li>
<li><a href="javascript:;" class="roll-5">
<img src="<?php echo $lottery[4]->image ?>">
<div class="Lucky_box_layer" style="display: none; background-color: rgb(0, 0, 0);"></div>
</a></li>
</ul>
</div>
<div class="luck_client_list" id="scrollvarea" style=" height: 400px; background: #fff;">
<div class="luck_client_list_box">
//获奖人员展示
<ul class="winer_boxs">
<?php
foreach ($near as $val) {
$win = json_decode($val, true);
$account = $win[0];
$package = $win[1];
$cur_time = $win[2];
$user = Users::model()->findByAttributes(array("account" => $account));
$name = empty($user["nickname"]) ? substr_replace($user["phone"], '****', 3, 4) : mb_substr($user["nickname"], 0, 9, "utf-8");
?>
<li>
<a href="#">
<img src="<?php echo (!empty($user["head_img"]) ? $user["head_img"] : "/images/head.png") ?>">
<div>
<span><?php echo $name ?></span>
<p>抽中了<em><?php echo $package ?></em></p>
<h4><?php echo PublicFunHelper::time_ago($cur_time) ?></h4>
</div>
</a></li><?php } ?>
</ul>
</div>
</div>
<!--中奖弹出框-->
<div class="jy_game_tips_layers" style="display: none;">
</div>
<div class="jy_game_tips" style="display: none;">
<div class="jy_game_tips_box">
<img src="<?php echo Yii::app()->versionUrl->changeUrl(COMMONSTATICFILE . "/v6/images/jy_game_tips.png") ?>" class="jy_game_tips_bg_z">
<span class="jy_game_tips_box_top">恭喜你获得</span>
<span class="jy_game_tips_box_bot" id="box_title"></span>
<img src="" id="box_img" class="jy_game_jiangpin" style="width: 40%;">
</div>
<a href="#" οnclick="$(this).hide();return false;" class="jy_game_tips_btn">关闭</a>
<div class="jy_game_close"><img src="<?php echo Yii::app()->versionUrl->changeUrl(COMMONSTATICFILE . "/v6/images/jy_game_close_.png") ?>"></div>
</div>
</div>
</div>
<script type="text/javascript" src="<?php echo Yii::app()->versionUrl->changeUrl(COMMONSTATICFILE . "/v6/js/game.js") ?>"></script>
<script type="text/javascript">
$(function() {
$('#scrollvarea').scrollVertical();
});
</script>
<script type="text/javascript">
$(document).ready(function() {
$('.jy_game_close').click(
function() {
$('.jy_game_tips_layers').hide();
$('.jy_game_tips').hide();
}
);
$('.jy_game_tips_btn').click(
function() {
$('.jy_game_tips_layers').hide();
$('.jy_game_tips').hide();
}
);
});
</script>
<script type="text/javascript">
window.$_POST = function(url, data) { //同步post,返回值封装
$.ajaxSetup({async: false});
var data = data || {};
var _rs = '';
$.post(url, data, function(rs, textStatus) {
_rs = rs;
}, 'json');
$.ajaxSetup({async: true});
return _rs;
};
$(function() {
var $run = false; //正在运行
var $site_id = 0; //到哪个位置
var $begin_site = 1; //开始位置
var $totle = 8; //总数
var $counting = 0;//计数
var slow = false; //减速
//var $sits = $('#choujiang .jp_box b');
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
$('#start').click(function(){
//alert(11111111);return false;
lottery();
return false;
});
function lottery() {
if ($run)
{
return false;
}
$rs = $_POST('/lottery/run/?' + Math.random(0, 1));
if ($rs.status !== 200) {
console.log(22222222222);
alert($rs.msg);
return false;
}
$site_id = parseInt($rs.msg.site_id);
// $sits.hide();
$run = true;
/**运行前样式改变**/
$('#self_point').text(parseInt($('#self_point').text()) - 1);
run(1, 400, $rs.msg, $rs.params);
}
function run(step, _time, ipop, json) {
$('.lucky_num').css('display', 'none');
$('.lucky_box_btn').addClass('lucky_box_btn_active');
$('.Lucky_box_layer').show();
$('.roll-' + $begin_site).children('div').css('background-color', '#FFF').end().addClass("Lucky_box_li_active");
$('.roll-' + ($begin_site - step > 0 ? $begin_site - step : $totle + $begin_site - step)).children('div').css('background-color', '#000').end().removeClass("Lucky_box_li_active");
if ($counting > 30) {
//console.log($totle+$site_id-3);
if (!slow && ($site_id - 3 > 0 && $begin_site == $site_id - 3) || ($site_id - 3 <= 0 && $begin_site == $totle + $site_id - 3)) { //开始减速
//$sits.not('#site_' + $begin_site).hide();
step = 1;
_time = 300;
slow = true;
}
if (slow)
_time = _time + 200; //进一步减速
if (slow && $begin_site == $site_id) {
$run = false;
slow = false;
$counting = 0;
/**运行后样式变化**/
//$('#self_point').text(parseInt($('#self_point').text()));
$('.lucky_num').show();
$('.lucky_box_btn').removeClass('lucky_box_btn_active');
setTimeout(function() {
$("#box_title").text($rs.msg.name);
$("#box_img").attr("src", $rs.msg.img);
$("#box_href").attr("href", $rs.msg.url);
$(".jy_game_tips_layers").show();
$(".jy_game_tips").show();
$(".jy_game_tips_btn").show();
}, 1000);
near();
return false;
}
}
if ($begin_site == $totle)
$begin_site = 0;
$begin_site++;
$counting++;
if ($counting == 5)
_time = 80; //五次后加速
window.setTimeout(function() {
run(step, _time, ipop, json)
}, _time);
}
});
</script>
//抽奖内部控制器
public function actionRun() {
$account = Yii::app()->user->id;
$user = Users::model()->findByAttributes(array("account" => $account));
$point = $user["lottery_num"];
$needPoint = 1;
if ($point < $needPoint) {
$this->_end(1, "您的抽奖次数已用完"); //积分不够
}
$rs = $this->_run(); //进行抽奖
$key = "except_coin:" . date("Ymd");
if ($rs['type'] == 3) {
$exceptCoin = Yii::app()->redis->get($key);
if (!empty($exceptCoin)) {
$this->actionRun();
return;
}
}
//用户抽奖数据目减少
$user->lottery_num = $user["lottery_num"] - 1;
$user->save();
$lotteryLog = new LotteryLog();
$lotteryLog->attributes = array(
"acount" => $account,
"create_at" => date("Y-m-d H:i:s"),
"name" => "抽奖",
"increase" => 0,
"decrease" => 1,
"amount" => $user["lottery_num"],
"note" => "您因抽将,减去1次抽奖机会,抽到:" . $rs["name"]
);
$lotteryLog->save();
Yii::app()->redis->lPush('lottery_account_near', json_encode(array($account, $rs["name"], time()))); //最近记录
if ($rs["type"] == 2) { //优惠券
for ($i = 0; $i < $rs["num"]; $i++) {
$quan10 = Coupons::model()->findByPk($rs["shop_id"])->attributes;
$id = $quan10["id"] ;
unset($quan10["id"]);
$_model = new UserCoupons();
$_model->attributes = $quan10;
$_model->coupon_id = $id;
$_model->create_at = date("Y-m-d H:i:s");
$_model->end_at = date("Y-m-d H:i:s", strtotime("+" . $quan10["days"] . " day"));
$_model->account = $account;
$_model->note = "您因抽奖,获得" . intval($quan10["price"]) . "元券一张";
$_model->save();
}
$this->_end(200, array('site_id' => $rs["site"], "name" => $rs["name"], "img" => $rs["image"], "url" => "/coupons/?type=1"));
} else if ($rs["type"] == 3) { //商品
Yii::app()->redis->set($key, 1);
$usersModel = Users::model()->findByAttributes(array("account" => $account));
$money = 100;
$totalAmount = $usersModel["total_amount"] + $money;
$orderId = SGoods::buildOrderNo();
$transaction = Yii::app()->db->beginTransaction();
try {
//用户资金增加
$usersModel->total_amount = $totalAmount;
if (!$usersModel->save()) {
throw new Exception(print_r($usersModel->getErrors(), true));
}
//支付订单条数加一条
$userPaymentsModel = new UserPayments();
$userPaymentsModel->attributes = array(
'acount' => $account,
'order_id' => $orderId,
'note' => '抽奖获得:100元美币红包,订单号' . $orderId . "完成入款",
'total_amount' => $money,
'create_at' => date("Y-m-d H:i:s"),
'used' => 1, //充值
'status' => 1, //成功
'payment_type' => 1,
'payment_type_name' => "线下汇款",
'payment_at' => date("Y-m-d H:i:s"), //支付时间
);
if (!$userPaymentsModel->save()) {
throw new Exception(print_r($userPaymentsModel->getErrors(), true));
}
//充值条数加一条
$userRecharge = new UserRecharge();
$userRecharge->attributes = array(
'acount' => $account,
'order_id' => $orderId,
'create_at' => date("Y-m-d H:i:s"),
'payment_type' => '1',
'payment_type_name' => '线下汇款',
'amount' => $money,
'note' => '抽奖获得:100元美币红包,订单号' . $orderId . "完成入款",
'status' => 1,
);
if (!$userRecharge->save()) {
throw new Exception(print_r($userRecharge->getErrors(), true));
}
//币增加一条
$userCoin = new UserCoins();
$userCoin->attributes = array(
'acount' => $account,
'coin_type_id' => 7,
'create_at' => date("Y-m-d H:i:s"),
'name' => '活动',
'increase' => $money > 0 ? $money : 0,
'decrease' => $money > 0 ? 0 : abs($money),
'amount' => $totalAmount,
'note' => '抽奖获得:100元美币红包,订单号' . $orderId . "完成入款",
'order_id' => $orderId,
);
if (!$userCoin->save()) {
throw new Exception(print_r($userCoin->getErrors(), true));
}
$transaction->commit(); //提交事务会真正的执行数据库操作
SMessages::send($usersModel["phone"], "您因" . '抽奖获得:100元美币红包,订单号' . $orderId . "完成入款" . "【今优农场】");
$this->_end(200, array('site_id' => $rs["site"], "name" => $rs["name"], "img" => $rs["image"], "url" => "/coins/"));
} catch (Exception $ex) {
$transaction->rollback(); //如果操作失败, 数据回滚
Yii::app()->end();
}
}
}
private function _run() {
$arr = array("quan" => 30, "shiwu" => 70);
$key = PublicFunHelper::randProbability($arr);
//var_dump($key);
$shopNum = Config::model()->findByPk(5)->value;
//var_dump($shopNum);
$criteria = new CDbCriteria();
$criteria->addCondition("create_at>'".date("Y-m-d 00:00:00")."'");
$criteria->addCondition("decrease=1");
$criteria->addCondition("note not like '%优惠券%'");
$count = LotteryLog::model()->count($criteria);
//var_dump($count);
if($count>=$shopNum){
$key="quan";
}
if ($key == "quan") {
$lottery = LotteryGoods::model()->findAllByAttributes(array("type" => 2));
$lotteryUniqueKey = PublicFunHelper::ArrayByUniqueKey($lottery, "id");
$arr = PublicFunHelper::ArrayKeyByUniqueKey($lotteryUniqueKey, "id", "price");
$key = PublicFunHelper::randProbability($arr);
return $lotteryUniqueKey[$key];
} else {
$criteria = new CDbCriteria();
$criteria->compare("type", 3);
$criteria->addCondition("price!=0");
$lottery = LotteryGoods::model()->findAll($criteria);
$lotteryUniqueKey = PublicFunHelper::ArrayByUniqueKey($lottery, "id");
$arr = PublicFunHelper::ArrayKeyByUniqueKey($lotteryUniqueKey, "id", "price");
$key = PublicFunHelper::randProbability($arr);
return $lotteryUniqueKey[$key];
}
}
public static function ArrayKeyByUniqueKey($model, $key, $key1) {
$_model = array();
foreach ($model as $val) {
$_model[$val[$key]] = $val[$key1];
}
return $_model;
}
public static function ArrayByUniqueKey($model, $key) {
$_model = array();
foreach ($model as $val) {
$type = is_array($val);
if ($type)
$_model[$val[$key]] = $val;
else
$_model[$val[$key]] = $val->attributes;
}
return $_model;
}
public static function randProbability($array) {
$nextSum = 0;
$sum = array_sum($array);
$rand = mt_rand(1, $sum);
foreach ($array as $key => $val) {
$nextSum += $val;
if ($rand <= $nextSum) {
return $key;
}
}
}