先说下整体思路,代码已附下方。

1、递归产生一个非中奖数(即非连续数字:'111','222','333','444','555','666','777','888')

2、点击摇奖,把奖项通过设置的中奖概率顺序排序(如代码:maopao)

3、放入中奖产生(如代码:get_rand)

4、若中奖则重新从(即连续数字:'111','222','333','444','555','666','777','888')  中随机一个,若未中奖则返回(1)随机数

/*中奖产生*/
public function get_rand($proArr) {
  $result = '';
  //概率数组的总概率精度
  $proSum = array_sum($proArr);
  //概率数组循环
  foreach ($proArr as $key => $proCur) {
$randNum = mt_rand(1, $proSum);
if ($randNum <= $proCur) {
$result = $key;
break;
} else {
$proSum -= $proCur;
}
}
unset ($proArr);
return $result;
} /*
*随机数
* */
function randNum(){   $randNum=mt_rand(1,8).mt_rand(1,8).mt_rand(1,8);   $award_arr=array('111','222','333','444','555','666','777','888');   if(in_array($randNum,$award_arr)){     return $this->randNum();
  } return $randNum;
} public function maopao($arr){
  for($i=0;$i<count($arr)-1;$i++){   for($j=$i+1;$j<count($arr);$j++){   if($arr[$j]['nums']<$arr[$i]['nums']){   $temp=$arr[$i];
  $arr[$i]=$arr[$j];
  $arr[$j]=$temp;
      }     }   }
return $arr;
} /*摇奖*/
public function luckDraw($userId){ ini_set('date.timezone','Asia/Shanghai'); $tigerrock_user_obj=new IModel('tigerrock_user');
//用户摇奖信息:已摇奖次数等
$tiger_data=$tigerrock_user_obj->getObj('user_id='.$userId,'id,award_type,rock_num,award_num,rock_time'); if(!$tiger_data){ //如果没有摇奖,则新建用户记录 $add_data=array(
'user_id'=>$userId,
'rock_num'=>1,
'award_num'=>0,
'award_type'=>1,
'rock_time'=>date('Y-m-d',time())
);
$tigerrock_user_obj=new IModel('tigerrock_user');
$tigerrock_user_obj->setData($add_data);
$tiger_data['id']=$tigerrock_user_obj->add(); $proId=0;
$rock_num=0;
$award_num=0; }else{ $award_type=$tiger_data['award_type'];
$rock_num=$tiger_data['rock_num'];
$award_num=$tiger_data['award_num'];
//判断是否为当天摇奖,不是则清空,是则累加
if(empty($tiger_data['rock_time'])||strtotime($tiger_data['rock_time'])-strtotime(date('Y-m-d'))!=0){
$a_data=array(
'rock_time'=>"'".date('Y-m-d')."'",
'rock_num'=>0,
);
$tigerrock_user1_obj=new IModel('tigerrock_user');
$tigerrock_user1_obj->update('id='.$tiger_data['id'],$a_data);
$rock_num=0;
} if($award_num>0){
$award_num=$award_num-1;
}else{
$award_num=0;
}
}
$total_amount=$this->tiger_proValue(); //奖金池--总金额
$randNum=$this->randNum(); //生成3位不同的随机数
dump_str($randNum,'randNum');
$randNum_arr=str_split($randNum); //把随机数切割成数组 $rock_num1=$randNum_arr[0];
$rock_num2=$randNum_arr[1];
$rock_num3=$randNum_arr[2]; //摇奖统计
$TC_OBJ=new IModel('tigerrock_count');
$countData=$TC_OBJ->getObj('id=1','today_num,total_num,today_date'); //新一天的第一次抽奖
if(empty($countData['today_date'])||strtotime($countData['today_date'])-strtotime(date('Y-m-d',time()))!=0){ $use_updata=array(
'usenums'=>0,
'usetime'=>"'".date('Y-m-d',time())."'",
); $tconfig_obj=new IModel('tigerrock_config');
$tconfig_obj->update('1=1',$use_updata); //更新清空所有历史一天中奖数以及时间 $conf1_updata=array(
'today_date'=>"'".date('Y-m-d',time())."'",
'today_num'=>0,
);
$countData['today_num']=0;
$tount_obj=new IModel('tigerrock_count');
$tount_obj->update('id=1',$conf1_updata); //更新清空历史一天抽奖总次数 } $member_obj=new IModel('member');
//查询用户积分
$point=$member_obj->getObj('member_id='.$userId,'member_points');
//是否超出免费抽奖次数
if($rock_num>2){ if($point<5){
return array(
'code'=>'200',
'msg'=>'可使用经验值不足',
'data'=>array('point'=>$point,'rock_num'=>$rock_num+1,'rock_num1'=>$rock_num1,'rock_num2'=>$rock_num2,'rock_num3'=>$rock_num3,'is_award'=>0,'total_amount'=>$total_amount)
);
} $point=$point-5; $member_obj=new IModel('member'); $member_obj->update('member_id='.$userId,array('member_points'=>$point)); } $updata_data=array(
'award_num'=>$award_num,
'rock_time'=>"'".date('Y-m-d',time())."'",
'rock_num'=>$rock_num+1, ); //更新个人摇奖次数
$tigerrock_user_obj=new IModel('tigerrock_user');
$tigerrock_user_obj->update('id='.$tiger_data['id'],$updata_data); /*加入摇奖日志*/
$rock_log=new IModel('tigerrock_log'); $data=array(
'user_id'=>$userId,
'nums'=>$randNum,
'time'=>"'".date('Y-m-d H:i:s',time())."'",
); $rock_log->setData($data); $rock_log->add(); $today_num=$countData['today_num']; $TC_OBJ->update('id=1',array('total_num'=>$countData['total_num']+1,'today_num'=>$today_num+1 )); //修改总数量 $tclass_obj=new IModel('tigerrock_class');
$classData=$tclass_obj->getObj('start_num<'.$today_num.' and end_num>='.$today_num,'end_num,id'); $rock=$classData['end_num'];
$classId=$classData['id']; if(!$classData){
$rock=1000;
$classId=1;
} $tc_obj=new IModel('tigerrock_config'); //获取摇奖配置数据
$proId_arr=$tc_obj->query('classId='.$classId.' and is_del=0 and status=1','id,nums,usenums,usetime,proId'); $proId_arr=$this->maopao($proId_arr); //通过奖品数量排序
$prize_arr=array();
$goodNum=0;
$prize_arr[0]=array();
$tp_obj=new IModel('tigerrock_products');
foreach($proId_arr as $v){ //统计总奖品数量
$tp_data=$tp_obj->getObj('id='.$v['proId'],'stock,winnings');
if($tp_data['stock']-$tp_data['winnings']<=0){//如果库存不足
$prize_arr[]=array('id'=>(int)$v['id'],'prize'=>$v['id'],'v'=>0);
}else{
$prize_arr[]=array('id'=>(int)$v['id'],'prize'=>$v['id'],'v'=>(int)$v['nums']-$v['usenums']);
}
$goodNum+=(int)$v['nums']-$v['usenums'];
}
$prize_arr[]=array('id'=>1,'prize'=>0,'v'=>$rock-$today_num);
unset($prize_arr[0]);
$arr=array();
foreach ($prize_arr as $key => $val) {
$arr[$val['id']] = $val['v'];
}
$rid =$this->get_rand($arr); //根据概率获取奖项id // $tl_obj=new IModel('tigerrock_log');
// $tl_obj->setData(array('user_id'=>$userId,'nums'=>$randNum,'time'=>date('Y-m-d H:i:s',time())));
// $tl_obj->add(); if($rid!=1){ $TC_OBJ=new IQuery('tigerrock_config as tc');
$TC_OBJ->fields='tp.id,tp.name as good_name,tp.img,tp.discription,tc.nums,tp.stock,tp.winnings,tp.id as tpid,tc.usenums,tc.usetime';
$TC_OBJ->where='tc.is_del=0 and tc.status=1 and tc.id='.$rid;
$TC_OBJ->join='left join tigerrock_products as tp on tp.id=tc.proId';
$pro_data1=$TC_OBJ->find();
$pro_data=array();
if($pro_data1){ $pro_data=$pro_data1[0];
} /*未绑定手机中将过滤*/
$u_obj=new IModel('member');
$phone_status=$u_obj->getObj('member_id='.$userId,'member_mobile_bind');
if(!$phone_status){
$pro_data='';
} //获取用户是否已中奖
// $ta_obj=new IModel('tigerrock_award');
// $award_id=$ta_obj->getObj('user_id='.$userId.' and status=1','id'); // if($award_id){
// $pro_data='';
// } //中奖次数达到设定次数
if(isset($pro_data['nums'])&&isset($pro_data['usenums'])&&$pro_data['nums']<=(int)$pro_data['usenums']){ $pro_data='';
} //验证实际商品库存,如果库存不足,则不中奖且更新中奖设置
if($pro_data['stock']<$pro_data['winnings']){
$pro_data='';
$tc_obj->update('id='.$rid,array('status'=>0));
} if(!$pro_data||empty($pro_data)){
return array(
'code'=>'200',
'msg'=>'差一点点,再试一把!',
'data'=>array('point'=>$point,'rock_num'=>$rock_num+1,'rock_num1'=>$rock_num1,'rock_num2'=>$rock_num2,'rock_num3'=>$rock_num3,'is_award'=>0,'total_amount'=>$total_amount)
); }else{ //更新库存以及可用奖品数量 $tc_obj=new IModel('tigerrock_config'); //更新已使用数
$tc_obj->update(' status=1 and id='.$rid,array('usenums'=>$pro_data['usenums']+1)); /*更新库存*/ /*$tco_obj=new IQuery('tigerrock_config as tc');
$tco_obj->fields='tp.stock,tp.id';
$tco_obj->join=' left join '.C('DB.DB_PREFIX').'tigerrock_products tp on tp.id=tc.proId';
$tco_obj->where='tc.id='.$rid;
$tco_obj->limit='1';
$product_data=$tco_obj->find();
$tp_where='id='.$product_data[0]['id'];
$tp_obj=new IModel('tigerrock_products');
$tp_obj->update($tp_where,array('stock'=>$product_data[0]['stock']-1));*/ //更新奖品表中的已中奖数
$tp_obj=new IModel('tigerrock_products');
$tp_obj->update('id='.$pro_data['tpid'],array('winnings'=>$pro_data['winnings']+1)); $award_data=array(
'user_id'=>$userId,
'proId'=>$pro_data['id'],
'time'=>date('Y-m-d')
); $tigerrock_award_oj=new IModel('tigerrock_award'); $tigerrock_award_oj->setData($award_data); $award_id=$tigerrock_award_oj->add(); $pro_data['img']=imgChangePath($pro_data['img']); $aNum=rand(0,7);
$award_arr=array('111','222','333','444','555','666','777','888'); //中奖号码池 $tl_obj=new IModel('tigerrock_log'); //添加中奖记录
$tl_obj->setData(array('user_id'=>$userId,'nums'=>$award_arr[$aNum],'time'=>date('Y-m-d H:i:s',time())));
$tl_obj->add(); $randNum_arr=str_split($award_arr[$aNum]); //切割三位数中奖码 $pro_data['id']=$award_id;
$pro_data['rock_num1']=$randNum_arr[0];
$pro_data['rock_num2']=$randNum_arr[1];
$pro_data['rock_num3']=$randNum_arr[2];
$pro_data['point']=$point;
$pro_data['rock_num']=$rock_num+1;
$pro_data['is_award']=1;
$pro_data['total_amount']=$total_amount; return array(
'code'=>'200',
'msg'=>'恭喜你中奖了',
'data'=>$pro_data
); } }else{ return array(
'code'=>'200',
'msg'=>'差一点点就中了,再试一把!',
'data'=>array('point'=>$point,'rock_num'=>$rock_num+1,'rock_num1'=>$rock_num1,'rock_num2'=>$rock_num2,'rock_num3'=>$rock_num3,'is_award'=>0,'total_amount'=>$total_amount)
);
} }
04-27 02:56