百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>Ecshop教程> 实现ecshop点击领取红包
分享文章到:

实现ecshop点击领取红包

发布时间:12/03 来源: 浏览: 关键词:

实现ecshop点击领取红包思路:

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 = '30';    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){   // 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

复制代码
<?phpdefined('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){//zuimoban.com         $bouns_type = $this->get_bonus_type($bouns_type_id);         $bouns_type[0]['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) < 1){            $str = 'no_bouns';      // 3.没有红包(红包领取完了)        }elseif($bonus_type[0]['send_type'] !=3){  //2.红包类型不对  ,必须"线下发放红包"            $str = 'send_type_err';  //         }elseif($time < $bonus_type[0]['use_start_date']){   //4.领取红包时间还未到            $str = 'no_start_date';        }elseif($time > $bonus_type[0]['use_end_date']){   //5.领取红包时间还已经结束            $str = 'no_end_date';        }elseif($check_bonus > 0){    // 6.已经领取过红包            $str = 'pull_bonus';        }else{            $rand_key = array_rand($bouns_list,1);            $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;     }    }
复制代码
打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

相关文章

共有4人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板