PHP转盘抽奖接口实例

这篇文章主要介绍了PHP转盘抽奖接口的实现方法,实例分析了随机抽奖接口的实现原理与对应数据库操作的技巧,需要的朋友可以参考下

本文实例讲述了PHP转盘抽奖接口的实现方法。分享给大家供大家参考。具体如下:

这里的转盘抽奖随机返回一个转盘角度,概率可自己定义

lottery_get.php接口文件如下:

  1. <?php
  2. /*session_start();
  3. if(!isset($_SESSION['zaszh_user_id'])){
  4. echo json_encode(array('status'=>'error','msg'=>'连接超时,请重新打开页面。'));
  5. exit;
  6. }
  7. $user_id = $_SESSION['zaszh_user_id'];*/
  8. $user_id = 1; // 测试用
  9. // 转盘区域
  10. $arr_area = array(
  11. '0' => array('min_angle'=>1,'max_angle'=>45,'prize'=>'小米'),
  12. '1' => array('min_angle'=>46,'max_angle'=>90,'prize'=>'拍立得'),
  13. '2' => array('min_angle'=>91,'max_angle'=>135,'prize'=>'10元话费'),
  14. '3' => array('min_angle'=>136,'max_angle'=>180,'prize'=>'5元话费'),
  15. '4' => array('min_angle'=>181,'max_angle'=>225,'prize'=>'谢谢参与'),
  16. '5' => array('min_angle'=>226,'max_angle'=>270,'prize'=>'谢谢参与'),
  17. '6' => array('min_angle'=>271,'max_angle'=>315,'prize'=>'谢谢参与'),
  18. '7' => array('min_angle'=>316,'max_angle'=>360,'prize'=>'谢谢参与')
  19. );
  20. // 选定区域
  21. $area_selected = array();
  22. // 随机抽取
  23. $num_rand = mt_rand(1,10);
  24. switch($num_rand){
  25. // 小米
  26. case 1: $area_selected = $arr_area[0]; break;
  27. // 拍立得
  28. case 2: $area_selected = $arr_area[1]; break;
  29. // 10元话费
  30. case 3: $area_selected = $arr_area[2]; break;
  31. // 5元话费
  32. case 4: $area_selected = $arr_area[3]; break;
  33. // 谢谢参与
  34. default:
  35. switch(mt_rand(1,4)){
  36. case 1: $area_selected = $arr_area[4]; break;
  37. case 2: $area_selected = $arr_area[5]; break;
  38. case 3: $area_selected = $arr_area[6]; break;
  39. case 4: $area_selected = $arr_area[7]; break;
  40. }
  41. break;
  42. }
  43. echo $area_selected['prize'];
  44. require('connect_database.php');
  45. // 扣除答题积分
  46. $mysqli->query("update zaszh_user set answer_points=answer_points-5 where and answer_points>=5");
  47. if($mysqli->affected_rows){
  48. // 有积分
  49. // 记录积分消耗
  50. $mysqli->query("insert into zaszh_answer_points_consume(user_id,points_consume,consume_for,create_date) values('{$user_id}',5,'lottery',unix_timestamp(now()))");
  51. switch($area_selected['prize']){
  52. case '小米':
  53. $mysqli->query("update zaszh_prize_surplus set surplus=surplus-1 where prize='小米' and surplus>0");
  54. if($mysqli->affected_rows){
  55. // 有剩余
  56. $mysqli->query("insert into zaszh_award(user_id,prize,create_date) values('{$user_id}','小米',unix_timestamp(now()))");
  57. if($mysqli->affected_rows){
  58. echo json_encode(array('status'=>'success','msg'=>'小米'));
  59. }else{
  60. // 获奖失败
  61. }
  62. }else{
  63. // 无剩余
  64. }
  65. break;
  66. case '拍立得':
  67. $mysqli->query("update zaszh_prize_surplus set surplus=surplus-1 where prize='拍立得' and surplus>0");
  68. if($mysqli->affected_rows){
  69. // 有剩余
  70. $mysqli->query("insert into zaszh_award(user_id,prize,create_date) values('{$user_id}','拍立得',unix_timestamp(now()))");
  71. if($mysqli->affected_rows){
  72. echo json_encode(array('status'=>'success','msg'=>'拍立得'));
  73. }else{
  74. // 获奖失败
  75. }
  76. }else{
  77. // 无剩余
  78. }
  79. break;
  80. case '10元话费':
  81. $mysqli->query("update zaszh_telephone_charge_surplus set charge_surplus=charge_surplus-10 where date=substring(now(),1,10) and charge_surplus>=10");
  82. if($mysqli->affected_rows){
  83. // 有剩余
  84. $mysqli->query("insert into zaszh_award(user_id,prize,create_date) values('{$user_id}','10元话费',unix_timestamp(now()))");
  85. if($mysqli->affected_rows){
  86. echo json_encode(array('status'=>'success','msg'=>'10元话费'));
  87. }else{
  88. // 获奖失败
  89. }
  90. }else{
  91. // 无剩余
  92. }
  93. break;
  94. case '5元话费':
  95. $mysqli->query("update zaszh_telephone_charge_surplus set charge_surplus=charge_surplus-5 where date=substring(now(),1,10) and charge_surplus>=5");
  96. if($mysqli->affected_rows){
  97. // 有剩余
  98. $mysqli->query("insert into zaszh_award(user_id,prize,create_date) values('{$user_id}','5元话费',unix_timestamp(now()))");
  99. if($mysqli->affected_rows){
  100. echo json_encode(array('status'=>'success','msg'=>'5元话费'));
  101. }else{
  102. // 获奖失败
  103. }
  104. }else{
  105. // 无剩余
  106. }
  107. break;
  108. default:
  109. echo json_encode(array('status'=>'success','msg'=>'谢谢参与'));
  110. }
  111. }else{
  112. // 无积分
  113. echo json_encode(array('status'=>'error','msg'=>'您的积分不足。'));
  114. }
  115. $mysqli->close();