以下是项目代码(公众号,使用积分进行抽奖活动),只可做参考:

public function Sncode()
{
$tid = I('request.tid', 0, 'intval'); // 大转盘id,
$wid = I('request.wid', 0, 'intval'); // 应用id,可去掉
$token = $this->getOpenId();       // 获取微信用户的token // 定义数据库
$big_wheel = D('Big_wheel'); // 大转盘设置表
$big_wheel_log = M('big_wheel_log'); // 抽奖记录表
$big_code = M('big_wheel_code'); // 已抽中奖品记录表
    // 查询抽奖活动数据
$where = " `id` = " . $tid;
$list = $big_wheel->relation(true)->where($where)->find(); // 查询积分,若积分不足则无法参与抽奖
$score = $this->duser['score'];
if ($score < $list['score_num']) {
echo json_encode(array('error' => 'score'));
exit;
} //已抽奖总次数
$where_log = "`big_wheel_id`=" . $tid . " and `w_id`=" . $list['w_id'] . " and `code`='" . $token . "'";
$count = $big_wheel_log->where($where_log)->count(); //减去额外获得的抽奖机会 - 分享(此操作暂时隐藏)
$map_share['wid'] = array('eq', $wid);
$map_share['token'] = array('eq', $token);
$map_share['big_wheel_id'] = array('eq', $tid);
$count_share = M('big_wheel_share')->where($map_share)->count();
$count = $count - $count_share; //当日限制次数
if ($list['most_num_times'] > 0) {
$where_log .= " and (addtime BETWEEN " . strtotime(date('Y-m-d 00:00:00')) . " and " . strtotime(date('Y-m-d 23:59:59')) . ")";
$count_today = $big_wheel_log->where($where_log)->count();
$count_today = $count_today - $count_share;// 当日抽奖次数减去额外获得的抽奖次数
    }

    //判断抽奖次数
if ($count_today >= $list['most_num_times'] && $list['most_num_times'] > 0) {
echo json_encode(array('error' => 'invalid_today', 'nums' => $list['most_num_times']));
exit;
} // 扣除积分
M('app_dishop_duser')->where(array('id' => $this->duser['id']))->setDec('score', $list['score_num']); //插入抽奖记录表
$data['big_wheel_id'] = $tid;
$data['w_id'] = $list['w_id'];
$data['code'] = $token;
$data['date'] = date('Y-m-d', time());
$data['addtime'] = time();
$big_wheel_log->add($data); /*
* 奖项数组
* 是一个二维数组,记录了所有本次抽奖的奖项信息,
* 其中id表示中奖等级,prize表示奖品,v表示中奖概率。
* 注意其中的v必须为整数,你可以将对应的 奖项的v设置成0,即意味着该奖项抽中的几率是0,
* 数组中v的总和(基数),基数越大越能体现概率的准确性。
* 本例中v的总和为100,那么中奖概率就是1%,
* 如果v的总和是10000,那中奖概率就是万分之一了。
*
*/
$prize_arr = array(
'0' => array('id' => 1, 'prize' => '1', 'v' => $list['c_probability_one']),
'1' => array('id' => 2, 'prize' => '谢谢参与', 'v' => $list['no_probability']),
'2' => array('id' => 3, 'prize' => '2', 'v' => $list['c_probability_two']),
'3' => array('id' => 4, 'prize' => '3', 'v' => $list['c_probability_three']),
); foreach ($prize_arr as $key => $val) {
$arr[$val['id']] = $val['v'];
} $rid = $this->get_rand($arr); //根据概率获取奖项id $res['yes'] = $prize_arr[$rid - 1]['prize']; //中奖项
unset($prize_arr[$rid - 1]); //将中奖项从数组中剔除,剩下未中奖项
shuffle($prize_arr); //打乱数组顺序
for ($i = 0; $i < count($prize_arr); $i++) {
$pr[] = $prize_arr[$i]['prize'];
} // 若抽中奖品,则存入库中,并返回提示
if ($res['yes'] == '1' || $res['yes'] == '2' || $res['yes'] == '3') {
//这个是保存到数据库表示你这个人抽中奖品
if ($res['yes'] == '1') {
// 查询这是第几次抽中此奖品,若抽中次数大于奖品数,则此次抽中的奖品作废
$code_count = $big_code->where(array('prizetype' => 1, 'big_wheel_id' => $tid))->count();
if ($code_count >= $list['c_num_one']) {
$data = array('prizetype' => null, 'success' => true);
echo json_encode($data);
exit;
}
$code['category'] = $list['c_name_one'];
} elseif ($res['yes'] == '2') {
// 查询这是第几次抽中此奖品,若抽中次数大于奖品数,则此次抽中的奖品作废
$code_count = $big_code->where(array('prizetype' => 2, 'big_wheel_id' => $tid))->count();
if ($code_count >= $list['c_num_two']) {
$data = array('prizetype' => null, 'success' => true);
echo json_encode($data);
exit;
}
$code['category'] = $list['c_name_two'];
} elseif ($res['yes'] == '3') {
// 查询这是第几次抽中此奖品,若抽中次数大于奖品数,则此次抽中的奖品作废
$code_count = $big_code->where(array('prizetype' => 3, 'big_wheel_id' => $tid))->count();
if ($code_count >= $list['c_num_three']) {
$data = array('prizetype' => null, 'success' => true);
echo json_encode($data);
exit;
}
$code['category'] = $list['c_name_three'];
}
$code['sn_id'] = 'sn' . time() . mt_rand(10000, 99999);
$code['w_id'] = $list['w_id'];
$code['big_wheel_id'] = $tid;
$code['prizetype'] = $res['yes'];
$code['code'] = $token;
$code['winners_time'] = time();
$code['state'] = 1;
$big_code->add($code);
$data = array('sn' => $code['sn_id'], 'prizetype' => $res['yes'], 'success' => true);
echo json_encode($data);
exit;
} else {
$data = array('prizetype' => null, 'success' => true);
echo json_encode($data);
exit;
}
}
// 抽奖概率计算
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;
}

最新文章

  1. 琴弦文字 - wpf行为
  2. C#做有模和非模式化窗体 传值例程
  3. akka实现的actor
  4. java 27 - 1 反射之 类的加载器
  5. 数往知来 ASP.NET 模拟服务器:服务端_静态页面_动态页面的响应&lt;十七&gt;
  6. TCP/IP协议原理与应用笔记23:路由选择模块 和 路由表的设计
  7. JDK1.6官方下载_JDK6官方下载_JDK1.6API(chm)下载_JDK6APICHM中文参
  8. DataReader 和 DataSet 的区别
  9. Android开源项目发现--- 工具类依赖注入DI篇(持续更新)
  10. TFT LCD控制显示总结(硬件概念、初始化相关配置)(转)
  11. synchronized和vilatile
  12. eclipse快捷键(转载)
  13. weex加入iconfont
  14. 日期时间选择器、Bootstrap日期和时间表单组件。bootstrap-datetimepicker实现年月日,时分秒的选择。
  15. C# socket通讯 send方法记录
  16. PHP 打开已有图片进行编辑
  17. jquery ajax 方法实例
  18. 欧拉函数(小于或等于n的数中与n互质的数的数目)&amp;&amp; 欧拉函数线性筛法
  19. Ubuntu下Tomcat绑定80端口(zz)
  20. String hashCode 方法为什么选择数字31作为乘子

热门文章

  1. 数据库-Oracle的使用
  2. 软件测试:1.Describe An Error
  3. Python基础之条件语句和循环
  4. 用 Python 获取 B 站播放历史记录
  5. Android 开发 8.0版本启动Service的方法
  6. OSS RFC
  7. pyqt5 -——菜单和工具栏
  8. MATLAB入门笔记
  9. spring加载配置新旧方式对比
  10. Linux网络编程学习(七) ----- 有名管道(第四章)