header('content-type: text/html;charset=utf-8');
date_default_timezone_set('prc');
//如果上面两行不存在对应问题,可以不设置
/**
* 保存转盘信息及提前设定的中奖者用户
*/
$prize_arr = array(
array('min'=>1, 'max'=>89, 'prize'=>'一等奖', 'v'=>25, 'preuser'=>array(51)),
array('min'=>90, 'max'=>179, 'prize'=>'二等奖', 'v'=>25),
array('min'=>180, 'max'=>360, 'prize'=>'三等奖', 'v'=>50)
);
/**
* 根据中奖占比获得对应的项
* @param $prizeinfo array 转盘信息
* @param $curruser int 当前玩转盘用户id
* @return int 返回中奖项
*/
function getRand($prizeinfo, $curruser = 0)
{
//根据prize_arr信息,对应key和中奖占比
foreach ($prizeinfo as $key=>$val){
$proArr[$key] = $val['v'];
}
$res = '';
$proSum = array_sum($proArr);
foreach ($prizeinfo as $key => $cur) { //预定
if(!empty($cur['preuser']) && in_array($curruser, $cur['preuser'])){
return $key;
}
}
foreach ($proArr as $key => $proCur) {//未预定
$randNum = mt_rand(1, $proSum);
if($randNum <= $proCur){ //倘若本次随机数落在占比范围内则表示已经中奖
$res = $key;
}else{
$proSum -= $proCur;
}
}
unset($proArr);
return $res;
}
$res = getRand($prize_arr); //根据转盘信息获得对应奖项
$reward = $prize_arr[$res]; //中奖的对应转盘信息
$return = array(
'angle' => mt_rand($reward['min'], $reward['max']),
'prize' => $reward['prize'],
'time' => date('Y-m-d H:i:s')
);
var_dump($return);