H5微信9宫格抽奖+概率设置

2021-09-10   阅读:3752   类型:前端   分类:html    标签: html

9宫格抽奖百度找了很久也没有找到可以设置中奖概率的,后面找到一篇文章,结合后端PHP设置的中奖概率。以下是个例子,供大家参考和使用。

1、抽奖界面代码:

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" name="viewport">
<meta content="yes" name="apple-mobile-web-app-capable">
<meta content="black" name="apple-mobile-web-app-status-bar-style">
<script type="text/javascript" src="https://libs.baidu.com/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="index.css?" />

</head>

<script type="text/javascript">
  function fontSize(){
    var deviceWidth=document.documentElement.clientWidth>640?640:document.documentElement.clientWidth;
    document.documentElement.style.fontSize=(deviceWidth/25)+"px";
  }
  fontSize();
  window.onresize=fontSize;
</script>
<body>
<div class="p1">
  <div id="lottery" class="wxicke">
  <style>
    .wxicke{ position: relative; }
    .zpbg_04{ width: 100%; height: 100%; position: absolute; left: 0px; top: 0px;}
  </style>
    <table border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td class="lottery-unit lottery-unit-0"><span></span><div class="mask"></div></td>
        <td class="lottery-unit lottery-unit-1"><span></span><div class="mask"></div></td>
        <td class="lottery-unit lottery-unit-2"><span></span><div class="mask"></div></td>
      </tr>
      <tr>
        <td class="lottery-unit lottery-unit-4"><span></span><div class="mask"></div></td>
        <td colspan="2" class="btndon"><a><img src="images/sz_03.png" alt="" class="c_stsn"></a></td>
        <td class="lottery-unit lottery-unit-5"><span></span><div class="mask"></div></td>
      </tr>
      <tr>
        <td class="lottery-unit lottery-unit-6"><span></span><div class="mask"></div></td>
        <td class="lottery-unit lottery-unit-7"><span></span><div class="mask"></div></td>
        <td class="lottery-unit lottery-unit-8"><span></span><div class="mask"></div></td>
      </tr>
    </table>
  </div>
</div>

<!-- 弹窗 -->
<div class="resful">
  <div class="resfulbox">
   <img src="images/close.png" alt="" class="bigclose" onclick="bigclose()">
   <h1>获得<span>【</span><span class="pronum"></span><span>】</span>一份</h1>
    <div class="forms">
      <label for="">姓名:</label>
      <input type="text" class="txun_name" />
      <label for="">手机:</label>
      <input type="text" class="txun_tel" />
      <button type="button" class="wxapisub">登记领取</button>
    </div>
  </div>
</div>
<script>
/*
抽奖
 */
var lottery={
  index:-1,  //当前转动到哪个位置,起点位置
  count:0,  //总共有多少个位置
  timer:0,  //setTimeout的ID,用clearTimeout清除
  speed:20,  //初始转动速度
  times:0,  //转动次数
  cycle:50,  //转动基本次数:即至少需要转动多少次再进入抽奖环节
  prize:-1,  //中奖位置
  prize:0,  //中奖位置
  prizes:0,  //中奖位置自定义+
  prizesName:'', //奖项自定义+
  pass: [0,1,2], // 跳过的位置,从0开始

  init:function(id){
    if ($("#"+id).find(".lottery-unit").length>0) {
      $lottery = $("#"+id);
      $units = $lottery.find(".lottery-unit");
      this.obj = $lottery;
      this.count = $units.length;
      $lottery.find(".lottery-unit-"+this.index).addClass("active");
    };
  },
  roll:function(){
    var index = this.index;
    var count = this.count;
    var lottery = this.obj;
    $(lottery).find(".lottery-unit-"+index).removeClass("active");
    index += 1;
    if (index>count-1) {
      index = 0;
    };
    $(lottery).find(".lottery-unit-"+index).addClass("active");
    this.index=index;
    return false;
  },
  stop:function(index){
    this.prize=index;
    return false;
  }
};
// 检查中奖位置是否可用
function check(index) {
  var result = true;
  for(var i = 0, len = lottery.pass.length; i < len; i++) {
    if (index == lottery.pass[i]) {
      result = false;
      break;
    }
  }
  return result;
}
function roll(){
  lottery.times += 1;
  lottery.roll();//转动过程调用的是lottery的roll方法,这里是第一次调用初始化
  // 中奖
  lottery.prize =lottery.prizes

  if (lottery.times > lottery.cycle+5 && lottery.prize==lottery.index) {
    clearTimeout(lottery.timer);
    lottery.prize=-1;
    lottery.times=0;
    click=false;
    // console.log($('.active').text())
    console.log(lottery.prize)  
    console.log(lottery.prizesName)
    // 获得写入缓存
    localStorage.setItem('awards',lottery.prizesName)
    setTimeout('swr()',1000);
    // swr()
  }else{
    if (lottery.times<lottery.cycle) {
      lottery.speed -= 5;
    }
    // 基本转动已结束,开始计算中奖位置
    else if(lottery.times==lottery.cycle) {
      var index = Math.random()*(lottery.count)|0;
      while(!check(index)) {
        index = Math.random()*(lottery.count)|0;
      }
      lottery.prize = index;
    }else{
      // 转动次数大于基本次数+10,且(转动位置在最后一个中奖位置是第一个)或(下一个位置就是中奖位置) 开始进入抽奖环节的最后阶段,转动速度更慢
      if (lottery.times > lottery.cycle+5 && ((lottery.prize==0 && lottery.index==9) || lottery.prize==lottery.index+1)) {
        lottery.speed += 25;
      }else{
        // 刚进入抽奖环节,减慢速度
        lottery.speed += 5;
      }
    }
    if (lottery.speed<15) {
      lottery.speed=15;
    };
    //console.log(lottery.times+'^^^^^^'+lottery.speed+'^^^^^^^'+lottery.prize);
    lottery.timer = setTimeout(roll,lottery.speed);//循环调用
  }
  return false;
}
var click=false;
window.onload=function(){
  lottery.init('lottery');
  $("#lottery a").click(function(){
    if (click) {//click控制一次抽奖过程中不能重复点击抽奖按钮,后面的点击不响应
			return false;
		}else{
			$('.c_stsn').css('display','none') //点击抽奖关闭手指
			lottery.speed=25;
			roll();  //转圈过程不响应click事件,会将click置为false
			click=true; //一次抽奖完成后,设置click为true,可继续抽奖
			$.get("http://www.example.com/gl/", 
			function(data) { //获取奖品
				// console.log(data)
				lottery.prizes =data.id;
				lottery.prizesName = data.prize
			}, 
			"json") 
			return false;
		}
     

     
  });
};
 function swr(){
	var active=localStorage.getItem('awards');
	$('.resful').css('visibility','visible')
	$('.pronum').text(active);//中奖结果
 }
</script>
</body>
</html>

2、后端中奖概率设置。算法:计算总奖品概率,v即为概率,执行方法之后会返回中奖奖品名称及id

public function gl(){
	$prize_arr = [
			'0' => ['id' => 1, 'prize' => '奖品1', 'v' => 20],
			'1' => ['id' => 2, 'prize' => '奖品2', 'v' => 10],
			'2' => ['id' => 3, 'prize' => '奖品3', 'v' => 20],
			'3' => ['id' => 4, 'prize' => '奖品4', 'v' => 5],
			'4' => ['id' => 5, 'prize' => '奖品5', 'v' => 19],
			'5' => ['id' => 6, 'prize' => '奖品6', 'v' => 5],
			'6' => ['id' => 7, 'prize' => '奖品7', 'v' => 10],
			'7' => ['id' => 8, 'prize' => '奖品8', 'v' => 10],
		];
  
	foreach ($prize_arr as $key => $val) {
		$arr[$val['id']] = $val['v'];
	}

	$rid = $this->get_prize($arr); //根据概率获取奖项id
	$data = $prize_arr[$rid-1];
	return json($data);

}
public function get_prize($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;
}
腾讯云11.11上云拼团Go,2核2G3M云服务器 28元/年

‘简忆博客’微信公众号 扫码关注‘简忆博客’微信公众号,获取最新文章动态
转载:请说明文章出处“来源简忆博客”。http://tpxhm.com/fdetail/816.html

×
觉得文章有用就打赏一下文章作者
微信扫一扫打赏 微信扫一扫打赏
支付宝扫一扫打赏 支付宝扫一扫打赏

文章评论(0)

登录
简忆博客壁纸一
简忆博客壁纸二
简忆博客壁纸三
简忆博客壁纸四
简忆博客壁纸五
简忆博客壁纸六
简忆博客壁纸七
简忆博客壁纸八
头像

简忆博客
勤于学习,乐于分享

置顶推荐

打赏本站

如果你觉得本站很棒,可以通过扫码支付打赏哦!
微信扫码:你说多少就多少~
微信扫码
支付宝扫码:你说多少就多少~
支付宝扫码
×