ecshop 点击领取红包

时间:2022-12-22 21:18:01

思路:

1.领取红包:也就是传递一个红包ID,和user_id然后update更新过来

2.获取未领取过的红包列表:然后随机函数array_rand($arr,1),随机返回一个数组,从而获得红包列表的红包ID

以上两步基本满足,领取红包功能

3.判断该用户是否已经领取过红包

基本代码

html代码1:

<input type="button" value="领取红包" onClick="bouns(30)">
<script type="text/javascript" src="__PUBLIC__/js/jquery.min.js" ></script>
<script>
function bouns($type_id){
$.ajax({
url:"{:url('bonus/bonus_ajax')}"+"&type_id="+$type_id,
success: function(html){

if(html == 'ok'){
alert('成功领取红包');
}if(html == 'no_bouns'){
alert('已经没有红包了哦');
}if(html == 'no_start_date'){
alert('领取红包的时间还没到哦');
}if(html == 'no_end_date'){
alert('你来晚了哦,红包已经结束了哦');
}if(html == 'pull_bonus'){
alert('你咋又来了呢,每个ID只能领取一次哦');
}

        }
});
}
</script>

完整版html代码:

<script>
$(document).ready(function(){
$type_id = '';
check_user_bonus();
//alert($f);
});
$('#DivBg').click(function(){
$(this).css('display','none');
});
// 领取红包
function bouns(){
$.ajax({
url:"{:url('bonus/bonus_ajax')}"+"&type_id="+$type_id,
success: function(html){
if(html == 'ok'){ // 可以根据返回的值,多做几种选择
alert('成功领取红包');
}else{
alert(html);
}
}
});
}
//检测用户是否领取过红包;领取过,则不显示;未领取就显示红包
function check_user_bonus(){
$.ajax({
url:"{:url('bonus/check_user_bonus_ajax')}"+"&type_id="+$type_id,
success: function (html){
if(html !=){ // 0,显示红包;否则就隐藏红包
$("#DivBg").css('display','none');
}else{
$("#DivBg").css('display','block');
}
}
});
}
</script>

BonusController.php

public function bonus_ajax(){
$type_id = $_GET['type_id'];
$m = model('Bonus')->get_bonus($type_id);
echo $m;
}

BonusModel.class.php

<?php

defined('IN_ECTOUCH') or die('Deny Access');

class BonusModel extends BaseModel {

    /**
* 红包
*
* @access private
* @param integer $brand_id
* @return array
*/
// 获取ecs_bouns_type 红包类型
function get_bonus_type($type_id=''){
if(!empty($type_id)){ // 有值获取单个;无值则获取全部
$where = ' where type_id = '.$type_id;
}else{
$where = ' ';
}
$sql = 'select * from '. $this->pre . 'bonus_type '.$where;
$res = $this->query($sql);
return $res;
}
// 获取ecs_user_bouns 红包列表(未被领取的红包)
function get_user_bonus($bouns_type_id=''){
if(!empty($bouns_type_id)){ // 有值获取单个;无值则获取全部
$where = ' where user_id =0 and bonus_type_id = '.$bouns_type_id; //必须是“线下发放红包”
}else{
$where = ' ';
}
$sql = 'select bonus_id from '. $this->pre . 'user_bonus '.$where;
$res = $this->query($sql);
return $res;
}
// 合并红包类型 与红包列表
function bouns_info($bouns_type_id){
$bouns_type = $this->get_bonus_type($bouns_type_id);
$bouns_type[]['user_bouns'] = $this->get_user_bonus($bouns_type_id);
return $bouns_type;
}
//检测是否重复领取; 已经领取( >1 )
function check_user_bonus($bonus_type_id){
$sql = 'select * from '. $this->pre .'user_bonus where bonus_type_id = '.$bonus_type_id.' and user_id='.$_SESSION['user_id'];
$result = $this->query($sql);
return count($result);
}
// 领取红包
function get_bonus($type_id){
$user_id = $_SESSION['user_id'];
$time = gmtime(); //红包类型
$bonus_type = $this->get_bonus_type($type_id);
// 红包列表
$bouns_list = $this->get_user_bonus($type_id);
// 是否领取过红包
$check_bonus = $this->check_user_bonus($type_id); // 判断条件
if(empty($user_id)){
$str = 'no_user'; // 1.未登录
}elseif(count($bouns_list) < ){
$str = 'no_bouns'; // 3.没有红包(红包领取完了)
}elseif($bonus_type[]['send_type'] !=){ //2.红包类型不对 ,必须"线下发放红包"
$str = 'send_type_err'; //
}elseif($time < $bonus_type[]['use_start_date']){ //4.领取红包时间还未到
$str = 'no_start_date';
}elseif($time > $bonus_type[]['use_end_date']){ //5.领取红包时间还已经结束
$str = 'no_end_date';
}elseif($check_bonus > ){ // 6.已经领取过红包
$str = 'pull_bonus';
}else{
$rand_key = array_rand($bouns_list,);
$rand_id = $bouns_list[$rand_key]['bonus_id'];
$sql = "UPDATE ". $this->pre ."user_bonus SET user_id = ".$user_id.",used_time = ".$time." where bonus_id=".$rand_id;
if($this->query($sql)){
$str = 'ok';
}else{
$str = 'err';
}
}
echo $str;
} }