先来控制文件:
hd3.php
<?php defined('InShopNC') or exit('Access Invalid!'); class hd3 extends BaseHomeControl{ public function __construct(){ require_once(BasePath.DS.'control'.DS.'wechat'.DS.'function'.DS.'global.php'); require_once(BasePath.DS.'control'.DS.'wechat'.DS.'class'.DS.'jssdk.class.php'); $this->URL = get_url(); //获取微信配置 $cfg = config(); $this->appid = $cfg['wx_appid']; $this->appsecret = $cfg['wx_appsecret']; //获取用户信息 $this->USER = MY_chkWechat($this->URL); $this->openid = $this->USER['openid']; //$this->openid ='ou0JDt8ZJZ5vXW9SW2dcqQRvqhBE'; //获得member_id $wx_member = MY_getWechatUserByOpenId($this->openid); $this->member_id = $wx_member['member_id']; $jssdk = new JSSDK($this->appid,$this->appsecret); $this->signPackage = $jssdk->GetSignPackage(); $this->theme_id = '117'; $this->hd_id = 'hd3'; //if(!checkIsMobile()){ exit('Please use the mobile phone or Xpad to open this page!');} } public function index(){ $model = Model('weixin'); //是否报名 $cdt['where'] = " weixin='$this->openid' AND type_id='7' AND sign_up_id='$this->theme_id' "; $rs = $model->getList('sign_up_groupbuy',$cdt); //print_r($rs); $cdt2['where'] = " theme_id = $this->theme_id AND lottery_id LIKE '$this->hd_id%' AND openid = '$this->openid' "; $rs2 = $model->getList('lottery',$cdt2); //print_r($rs2); //已报名未抽奖 if(!empty($rs) && empty($rs2)){ Tpl::output('status','1'); } //已报名已抽奖 else if(!empty($rs) && !empty($rs2)){ $message = json_decode($rs2[0]['message'],true); //已领取 if($message['status']){ Tpl::output('used','1'); } Tpl::output('qr_code',$message['qr_code']); Tpl::output('result',$rs2[0]); Tpl::output('status','2'); } Tpl::output('signPackage',$this->signPackage); Tpl::output('title','2015宁波国际车展购车汇'); Tpl::output('url',$this->URL); Tpl::output('openid',$this->openid); Tpl::output('share_title','2015宁波车展:幸运转盘 抽到手软'); Tpl::output('share_desc','2015宁波国际车展购车汇:幸运大转盘 抽到你手软!'); Tpl::output('share_image','http://www.mycjj.com/upload/hd/3/01.png'); Tpl::showpage('../mobile/hd/'.$this->hd_id,'null_layout'); } public function ajax(){ $model = Model('weixin'); $type = trim($_GET['type']); switch($type){ //报名 case 'bm': $mobile = trim($_POST['mobile']); $car = trim($_POST['car']); $cdt['where'] = " weixin='$this->openid' AND type_id='7' AND sign_up_id='$this->theme_id' "; $rs = $model->getList('sign_up_groupbuy',$cdt); if(!empty($rs)){ $result = array('state'=>'1','data'=>$rs[0],'msg'=>'请勿重复报名'); }else{ $data = array( 'type_id' => '7', 'sign_up_id' => $this->theme_id, 'username' => $this->USER['nickname'], 'telephone' => $mobile, 'product_id' => $car, 'message' => '意向车型:'.$car, 'addtime' => time(), 'weixin' => $this->openid ); $id = $model->add('sign_up_groupbuy',$data); if($id){ $result = array('state'=>'SUCCESS','data'=>$data,'msg'=>'报名成功'); }else{ $result = array('state'=>'-1','data'=>array(),'msg'=>'数据传输失败,请联系攻城狮!'); } } echo json_encode($result); break; case 'get_prize': //重复抽奖 $sql="SELECT id FROM lottery WHERE theme_id = '".$this->theme_id."' AND lottery_id LIKE '".$this->hd_id."%' AND openid = '".$this->openid."'"; $u=Model()->query($sql); if(!empty($u)){ $result = array('state'=>'-1','data'=>$u,'msg'=>'一个微信号只能抽一次哦!'); echo json_encode($result);exit; } $award = array( 1=> array('name'=>'车用遮阳档','rate'=>'70','min'=>'-29','max'=>'0'), 2 => array('name'=>'万能泡沫清洗剂','rate'=>'70','min'=>'-59','max'=>'-30'), 3=> array('name'=>'安神眼罩','rate'=>'70','min'=>'-89','max'=>'-60'), 4 => array('name'=>'野餐篮','rate'=>'80','min'=>'-119','max'=>'-90'), 5 => array('name'=>'车用炭包','rate'=>'100','min'=>'-149','max'=>'-120'), 6=> array('name'=>'车用遮阳档','rate'=>'80','min'=>'-179','max'=>'-150'), 7 => array('name'=>'进口玻璃水','rate'=>'150','min'=>'-209','max'=>'-180'), 8 => array('name'=>'防雾霾口罩','rate'=>'100','min'=>'-239','max'=>'-210'), 9 => array('name'=>'万能泡沫清洗剂','rate'=>'80','min'=>'-269','max'=>'-240'), 10 => array('name'=>'安神眼罩','rate'=>'80','min'=>'-299','max'=>'-270'), 11 => array('name'=>'车用炭包','rate'=>'100','min'=>'-329','max'=>'-300'), 12 => array('name'=>'车载空气净化器','rate'=>'20','min'=>'-359','max'=>'-330') ); $r =rand(1,1000); $num = 0; $award_id = 0; foreach($award as $k=>$v){ $tmp = $num; $num += $v['rate']; if($r>$tmp && $r<=$num){ $award_id = $k; break; } } //判断奖品库存是否足够 $sum = Model()->table('lottery')->field('id')->where("theme_id='".$this->theme_id."' and lottery_id = '".$this->hd_id."_".$award_id."'")->count(); if ($sum >= $award[$award_id]['rate']){ $suiji = array(1,2,3,5,6,7,9,10,11);//除了4,8,12 $award_id = $suiji[array_rand($suiji)];//若已满额,随机派送指定 } $award[$award_id]['angle'] = rand($award[$award_id]['min'],$award[$award_id]['max']); $award[$award_id]['id'] = $award_id; $result = array('state'=>'SUCCESS','data'=>$award[$award_id],'msg'=>'抽到了'); echo json_encode($result); break; case 'play': $name = trim($_POST['name']); $id = intval($_POST['id']); $data = array( 'theme_id' => $this->theme_id,//专题 'user_id' => $this->member_id, 'lottery_id' => $this->hd_id.'_'.$id, 'lottery_time' => time(), 'lottery_code' => '', 'lottery_name' => $name, 'mobile' => '', 'username' => $this->USER['nickname'], 'openid' => $this->openid //'message' => array('status'=>'0','time'=>'') ); $id = $model->add('lottery',$data); // 生成奖品二维码 require_once(BasePath.DS.'resource'.DS.'phpqrcode'.DS.'index.php'); $PhpQRCode = new PhpQRCode(); $PhpQRCode->set('date','http://'.$_SERVER['HTTP_HOST'].DS.ncUrl(array('act'=>'hd','m'=>'verify','op'=>'index','id'=>$id), 'hd')); $PhpQRCode->set('pngTempDir',BasePath.DS.'upload'.DS.'hd'.DS.'3'.DS.'qrcode'.DS); //$PhpQRCode->init(); $qr_code = 'http://'.$_SERVER['HTTP_HOST'].'/upload/hd/3/qrcode/'.$PhpQRCode->init(); $message = array('qr_code'=>$qr_code,'status'=>'0','createtime'=>time(),'checktime'=>''); $rs = $model->update('lottery',array('message'=>json_encode($message)),$id); if($rs){ $result = array('state'=>'SUCCESS','data'=>$qr_code,'msg'=>'获取二维码成功!'); }else{ $result = array('state'=>'-1','data'=>array(),'msg'=>'duang!系统出错!请联系攻城狮!'); } echo json_encode($result); break; } } } ?>
verify.php
<?php defined('InShopNC') or exit('Access Invalid!'); class verify extends BaseHomeControl{ public function __construct(){ require_once(BasePath.DS.'control'.DS.'wechat'.DS.'function'.DS.'global.php'); if(!checkIsMobile()){ exit('Please use the mobile phone or Xpad to open this page!');} } //抽奖结果的验证取货 public function index(){ $model = Model('weixin'); $id = intval($_GET['id']); $info = $model->getRow('lottery',$id); $rs = json_decode($info['message'],true); if(!$rs['status']){ $data['message'] = array( 'qr_code' => $rs['qr_code'], 'status' => '1', 'createtime' => $rs['createtime'], 'checktime' => time() ); $param = array( 'message' => json_encode($data['message']) ); $update = $model->update('lottery',$param,$id); if($update){ exit('<!doctype html><html lang="zh_CN"><head><meta charset="UTF-8"><title>领奖成功!</title><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/></head><body><h2 style="text-align:center;">领奖成功!</h2></body></html>'); }else{ exit('<!doctype html><html lang="zh_CN"><head><meta charset="UTF-8"><title>领奖成功!</title><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/></head><body><h2 style="text-align:center;">领奖失败,请重新扫描确认!</h2></body></html>'); } }else{ exit('<!doctype html><html lang="zh_CN"><head><meta charset="UTF-8"><title>领奖成功!</title><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/></head><body><h2 style="text-align:center;">请勿重复领奖!</h2><h3 style="text-align:center;">领奖时间:'.date('Y-m-d H:i:s',$rs['checktime']).'</h3></body></html>'); } } } ?>
再来模板文件:
<!doctype html> <html lang="zh_CN"> <head> <meta charset="UTF-8"> <title><?php echo $output['title']?></title> <meta name="Description" content=""> <meta name="Keywords" content=""> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/> <meta name="apple-mobile-web-app-status-bar-style" content="black"/> <meta name="apple-mobile-web-app-title" content=""/> <meta name="apple-touch-fullscreen" content="YES" /> <meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="format-detection" content="telephone=no" /> <meta name="HandheldFriendly" content="true" /> <meta http-equiv="x-rim-auto-match" content="none" /> <meta name="format-detection" content="telephone=no" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <script type="text/javascript" src="<?php echo RESOURCE_PATH;?>/js/zepto/zepto.min.js" charset="utf-8"></script> <script type="text/javascript" src="<?php echo RESOURCE_PATH;?>/js/jquery-1.8.0.min.js"></script> <script type="text/javascript" src="<?php echo RESOURCE_PATH;?>/js/jquery.rotate/jQueryRotate.2.2.js"></script> <script type="text/javascript" src="<?php echo RESOURCE_PATH;?>/js/jquery.rotate/jquery.easing.min.js"></script> <style type="text/css"> *{margin:0;padding:0;} html{font-size:62.5%;} body{color:#333;font-size:1.6rem;font-family:'Microsoft YaHei',微软雅黑;background:#D82918;-moz-user-select:none;-webkit-user-select:none;} div,a,span,li{-webkit-tap-highlight-color:transparent;} a,a:visited{-webkit-user-modify:read-only;} .layout{position:relative;width:100%;max-width:640px;margin:0 auto;background:#D82918 url('<?php echo ATTACH_PATH.'/hd/3/01.png'?>') no-repeat;background-size:contain;} .bm{position:relative;} .form{margin:0 auto;padding-top:80%;text-align:center;position:relative;} .form p{color:#FFF153;text-decoration:underline;text-align:center;margin-top:1rem;} .btn_bm{background:url('<?php echo ATTACH_PATH.'/hd/3/03.png'?>') center no-repeat;width:60%;background-size: contain;border: 0;height: 6rem;} .ipt{width:70%;height:4rem;background:#fff;border:2px solid #ddd;border-radius:0.5rem;font-size:1.6rem;padding:0 0.5rem;margin-bottom:0.5rem;} .red{color:#D82918;} .logo{width: 100%;height: 4rem;margin:0 auto;background: url('upload/hd/3/02.png') center / contain no-repeat;z-index:1;} .lottery{margin: 0 auto;padding-top: 60%;text-align: center;position: relative;background: url('upload/hd/3/04.png') center bottom no-repeat;background-size: contain;} .lottery .in{position:relative;padding-bottom:1rem;overflow:hidden;} #disk{width:90%;background:url('<?php echo ATTACH_PATH.'/hd/3/07.png'?>') no-repeat;background-size: contain;margin: 0 auto;} #start{position:absolute;top: 36%;left: 25%;width:8rem;} #start img{width:100%;} .result,.result2{} .result .in,.result2 .in{position: absolute;top: 10%;width: 90%;left: 5%;background: #fff;box-shadow: 0 0 10px #999;z-index: 10;} .result h1,.result2 h1{font-size:2rem;text-align:center;padding:2rem 1rem 1rem 1rem;} .result .qr_code,.result2 .qr_code{margin:0 auto;width:90%;position:relative;} .result .qr_code img,.result2 .qr_code img{width:100%;} .result .tips,.result2 .tips{color:#666;text-align:center;font-size:1.2rem;padding:0 1rem;} .result .tips:last-child,.result2 .tips:last-child{padding-bottom:2rem;} .result2 .qr_code p{color:#D82918;position:absolute;font-size:4rem;font-weight:bold;text-align:center;left:0;top:0;background:#fff;} .invite{width:80%;margin:2rem auto;height:5rem;background:#F32F2F;color:#fff;font-size:2rem;border-radius:0.5rem;border:0;display: block;text-align:center;font-family:'Microsoft YaHei',微软雅黑;} #mask{position:fixed;top:0;left:0;right:0;bottom:0;width:100%;z-index:50;background:#000;opacity:0.7;} .layer{position:absolute;top:10%;width: 80%;left: 10%;margin:0 auto;z-index:60;} .layer .tit{height:4rem;color:#fff;background:#C12B20;text-align:center;font-size:1.8rem;line-height:4rem;border-top-left-radius:0.5rem;border-top-right-radius:0.5rem;position:relative;} .layer .tit em{position: absolute;top: 0.5rem;right: 0.5rem;width: 3rem;height: 3rem;text-align: center;color: #fff;background: #CE2E01;font-style: normal;font-size: 2.6rem;line-height: 2.8rem;border-radius: 0.5rem;} .layer .con{background:#fff;line-height:150%;border-bottom-left-radius:0.5rem;border-bottom-right-radius:0.5rem;padding:0 1rem;max-height: 35rem;overflow-y: auto;} .layer .con p{padding:0.7rem 0;} #share{background:url('<?php echo ATTACH_PATH.'/hd/2/06.png'?>') no-repeat;background-size:contain;height: 100%;} </style> </head> <body> <div class="layout"> <!-- 报名表格S --> <div class="bm" <?php if($output['status']==1 || $output['status']==2){?>style="display:none;"<?php }?>> <div class="form"> <input type="text" name="mobile" id="mobile" maxlength="11" class="ipt" placeholder="请输入您的手机号码(必填)"> <input type="text" name="car" id="car" class="ipt" placeholder="请输入您的意向车型(例:福特)"> <button class="btn_bm"></button> <p>活动规则</p> </div> </div> <!-- 报名表格E --> <!-- 抽奖转盘S --> <div class="lottery" <?php if($output['status']==0 || $output['status']==2){?>style="display:none;"<?php }?>> <div class="in"> <div id="disk"></div> <div id="start"><img src="<?php echo ATTACH_PATH.'/hd/3/05b.png'?>" id="startbtn"></div> </div> </div> <!-- 抽奖转盘E --> <!-- 抽奖结果S --> <div class="result" style="display:none;"> <div class="in"> <h1>恭喜您获得<p class="red"></p></h1> <div class="tips">请将二维码交于工作人员扫描并领取奖品</div> <div class="qr_code"><img src=""></div> <div class="tips">*二维码仅限扫描一次,请勿自行扫描识别。<br />*在工作人员扫描之前,请勿关闭本窗口</div> </div> </div> <!-- 报名结果E --> <!-- 抽奖结果2S --> <div class="result2" <?php if($output['status']==2){?>style="display:block;"<?php }else{?>style="display:none;"<?php }?>> <div class="in"> <h1>恭喜您获得<p class="red"><?php echo $output['result']['lottery_name']?></p></h1> <div class="tips">请将二维码交于工作人员扫描并领取奖品</div> <div class="qr_code"><img src="<?php echo $output['qr_code'];?>"><?php if($output['used']){?><p>奖品已领取</p><?php }?></div> <div class="tips">*二维码仅限扫描一次,请勿自行扫描识别。<br />*在工作人员扫描之前,请勿关闭本窗口</div> </div> </div> <!-- 报名结果2E --> <div class="logo"></div> </div> <div id="mask" style="display:none;"></div> <div class="layer" id="intro" style="display:none;"> <div class="tit"><em class="close">×</em>活动规则</div> <div class="con"><p>1、扫码关注车家佳微信免费参加游戏;</p><p>2、每个微信号仅限一次游戏机会;</p><p>3、获奖礼品仅限在宁波国际车展现场车家佳展位领取;</p><p>4、礼品抽取后将会出现领奖二维码,请将二维码交给工作人员扫描并领取奖品;</p><p>5、二维码仅限扫描一次,请勿自行扫描,如自行扫描,将失去领奖资格;</p><p>6、本活动最终解释权归车家佳所有;</p></div> </div> <div class="layer" id="share" style="display:none;"></div> </body> </html> <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> <script type="text/javascript"> var openid = '<?php echo $output['openid'];?>'; var title = '<?php echo $output['title'];?>'; var url = '<?php echo $output['url'];?>&openid='+openid; var share_title = '<?php echo $output['share_title'];?>'; var share_desc = '<?php echo $output['share_desc'];?>'; var share_image = '<?php echo $output['share_image'];?>'; $('.layout').css({'height':$(window).height()}); $('.bm,.result,.result2').css({'height':$(window).height()-60}); $('#disk').css({'width':$('.layout').width()*0.9,'height':$('#disk').width()}); $('#start').css({'top':($('#disk').height()-$('#start img').height())/2,'left':($('.layout').width()-$('#start img').width())/2}); $('.result2 .qr_code p').css({'width':$('.result2 .qr_code').width(),'height':$('.result2 .qr_code').height(),'line-height':$('.result2 .qr_code').height()+'px'}); $(function(){ //alert(openid); $('.form p').on('touchend',function(){ $('#mask').fadeIn(); $('#intro').show(); }); $('.close,#mask').live('touchend',function(){ setTimeout(function(){ $('#mask').hide(); $('#intro').fadeOut(); },320); }); $('.invite').on('touchend',function(){ $('#mask').fadeIn(); $('#share').show(); }); $('.btn_bm').on('touchend',function(){ sign(); }); $("#startbtn").bind('click',function(){ lottery(); }); }); function sign(){ if(!checkmobile($("#mobile").val())){ alert('请输入正确的手机号码'); $('#mobile').focus(); return false; } if ($("#car").val()==''){ alert('请输入您的意向车型,如:福特'); $('#car').focus(); return false; } $.post('/index.php?act=hd&m=hd3&op=ajax&type=bm',{'mobile':$('#mobile').val(),'car':$('#car').val()},function(data){ if(data.state=='SUCCESS'){ window.location.reload(); }else{ alert(data.msg); } },'json'); } function checkmobile(s){ var regu = /^[1][3|5|7|8][0-9]{9}$/; var re= new RegExp(regu); if(re.test(s)){ return true; } } function lottery(){ $.post('/index.php?act=hd&m=hd3&op=ajax&type=get_prize',{},function(data){ if(data.state=='-1'){ alert(data.msg);return false; } $("#startbtn").unbind('click').css("cursor","default"); var angle = data.data.angle; var lottery_name = data.data.name; var lottery_id = data.data.id; $("#disk").rotate({ duration:2000,angle: 0,animateTo: 1800 + angle,easing: $.easing.easeOutSine, callback: function(){ $.post('/index.php?act=hd&m=hd3&op=ajax&type=play',{'id':lottery_id,'name':lottery_name},function(e){ if(e.state=='SUCCESS'){ $('.lottery').hide(); $('.result').show(); $('.result h1 p').html(lottery_name); $('.qr_code img').attr({'src':e.data}); }else{ alert(e.msg); } },'json'); } }); },'json'); } wx.config({ debug: false, appId: '<?php echo $output['signPackage']['appId'];?>', timestamp: <?php echo $output['signPackage']['timestamp'];?>, nonceStr: '<?php echo $output['signPackage']['nonceStr'];?>', signature: '<?php echo $output['signPackage']['signature'];?>', jsApiList: [ 'onMenuShareTimeline', 'onMenuShareAppMessage', ] }); wx.ready(function () { // 分享给好友 wx.onMenuShareAppMessage({ imgUrl : share_image, link: url, desc : share_desc, title : share_title, type: 'link', dataUrl: '', success: function () { $.post('/index.php?act=wechat_mobile&m=ajax&type=share',{'openid':openid,'title':title,'url':url,'type':'1'},function(r){ if(r.state=='SUCCESS'){ alert('恭喜,成功分享给好友!'); } },'json'); } }); // 分享到朋友圈 wx.onMenuShareTimeline({ title: share_title, link: url, imgUrl: share_image, success: function () { $.post('/index.php?act=wechat_mobile&m=ajax&type=share',{'openid':openid,'title':title,'url':url,'type':'2'},function(r){ if(r.state=='SUCCESS'){ alert('恭喜,成功分享到朋友圈!'); } },'json'); } }); }); </script>
转载请注明:小码农 » PHP源码:报名+抽奖+奖品核销(二维码方式)