资讯专栏INFORMATION COLUMN

php抽奖实现

fsmStudy / 2586人阅读

摘要:抽奖地址主要代码其他引用可以在上面地址页面根据引用地址下载页面点我注册立即签到抽奖次数前名九宫格抽奖

抽奖地址:https://www.huijindaicn.com/i...
主要代码:(其他引用可以在上面地址页面根据引用地址下载)
1、html页面

</>复制代码

  1. 剩余积分:

  2. 可抽奖次数:

  3. 已抽奖次数:

2、js 主要代码

</>复制代码

  1. lottery.js
  2. var lottery = {
  3. index: 0, //当前转动到哪个位置,起点位置
  4. count: 16, //总共有多少个位置
  5. timer: 0, //setTimeout的ID,用clearTimeout清除
  6. speed: 20, //初始转动速度
  7. times: 0, //转动次数
  8. cycle: 50, //转动基本次数:即至少需要转动多少次再进入抽奖环节
  9. prize: -1, //中奖位置
  10. init: function(id) {
  11. if ($("#" + id).find(".lottery-unit").length > 0) {
  12. $lottery = $("#" + id);
  13. $units = $lottery.find(".lottery-unit");
  14. this.obj = $lottery;
  15. this.count = $units.length;
  16. $lottery.find(".lottery-unit-" + this.index).addClass("active");
  17. }
  18. },
  19. roll: function() {
  20. var index = this.index;
  21. var count = this.count;
  22. var lottery = this.obj;
  23. $(lottery).find(".lottery-unit-" + index).removeClass("active");
  24. index += 1;
  25. if (index > count - 1) {
  26. index = 0;
  27. }
  28. $(lottery).find(".lottery-unit-" + index).addClass("active");
  29. this.index = index;
  30. return false;
  31. },
  32. stop: function(index) {
  33. this.prize = index;
  34. return false;
  35. }
  36. };
  37. function roll() {
  38. lottery.times += 1;
  39. lottery.roll();
  40. var prize_site = $("#lottery").attr("prize_site");
  41. if (lottery.times > lottery.cycle + 10 && lottery.index == prize_site) {
  42. // var prize_id = $("#lottery").attr("prize_id");
  43. var prize_name = $("#lottery").attr("prize_name");
  44. // alert("中奖名称:"+prize_name+"
  45. 中奖id"+prize_id);
  46. $.showSuccess("恭喜你中得 "+ prize_name,function(){
  47. get_win_num();
  48. get_win_list();
  49. get_tot_num();
  50. });
  51. clearTimeout(lottery.timer);
  52. lottery.prize = -1;
  53. lottery.times = 0;
  54. click = false;
  55. } else {
  56. if (lottery.times < lottery.cycle) {
  57. lottery.speed -= 10;
  58. } else if (lottery.times == lottery.cycle) {
  59. var index = Math.random() * (lottery.count) | 0;
  60. lottery.prize = index;
  61. } else {
  62. if (lottery.times > lottery.cycle + 10 && ((lottery.prize == 0 && lottery.index == 7) || lottery.prize == lottery.index + 1)) {
  63. lottery.speed += 110;
  64. } else {
  65. lottery.speed += 20;
  66. }
  67. }
  68. if (lottery.speed < 40) {
  69. lottery.speed = 40;
  70. }
  71. lottery.timer = setTimeout(roll, lottery.speed);
  72. }
  73. return false;
  74. }
  75. /* 获取中奖列表 */
  76. function get_win_list(){
  77. $.get("/index.php?ctl=lottery&pc_ct=get_new_win&num=100", function(a){
  78. // console.log(a);
  79. if(typeof a != "object"){
  80. return;
  81. }
  82. for(k in a){
  83. html = "
      ";
    • for(i = 0, l = a[k].length; i < l; i++){
    • html += "
    • "+ a[k][i].user_name +""+ a[k][i].goods_name +"
    • ";
    • }
    • html += "
    ";
  84. $(".lottery-list").html(html);
  85. }
  86. /* 中奖列表滚动插件 */
  87. $(".lottery-list ul").totemticker({
  88. row_height: "21px",
  89. next: "#ticker-next",
  90. previous: "#ticker-previous",
  91. stop: "#stop",
  92. start: "#start",
  93. mousestop: true
  94. });
  95. }, "json");
  96. }
  97. /* 获取剩余抽奖次数 */
  98. function get_win_num(){
  99. $.get("/index.php?ctl=lottery&pc_ct=get_win_num&type=t_box", function(a){
  100. if(typeof a != "object"){
  101. return;
  102. }
  103. $(".score").html(a.score);
  104. $(".num").html(a.num);
  105. $(".count").html(a.count);
  106. }, "json");
  107. }
  108. /* 获取抽奖排行 */
  109. function get_tot_num(){
  110. $.get("/index.php?ctl=lottery&pc_ct=get_tot_num", function(a){
  111. if(typeof a != "object"){
  112. return;
  113. }
  114. var html = "";
  115. $.each(a, function(i, n){
  116. html = html+""+(i+1)+":"+n.user_name+"("+n.num+"次)";
  117. });
  118. $(".c_count").html(html);
  119. }, "json");
  120. }
  121. var click = false;
  122. $(function() {
  123. var ajax_url = "/index.php?ctl=lottery&pc_ct=get_win";
  124. lottery.init("lottery");
  125. /* 开始抽奖 */
  126. $("#lottery a").click(function() {
  127. console.log(click);
  128. if (click) {
  129. return false;
  130. } else {
  131. $.post(ajax_url, {uid: 1}, function(a) { // 获取奖品,也可以在这里判断是否登陆状态
  132. if(!a.code){
  133. var _url = a.status == 2 ? "/index.php?ctl=user&act=login" : "/index.php?ctl=deals";
  134. return $.showErr(a.msg, function(){window.location = _url;});
  135. }else if(a.win){
  136. $("#lottery").attr("prize_site", a.win.id);
  137. $("#lottery").attr("prize_name", a.win.name);
  138. lottery.speed = 100;
  139. roll();
  140. click = true;
  141. return false;
  142. }else{
  143. $.showSuccess("通讯错误 稍后再试");
  144. }
  145. }, "json")
  146. }
  147. });
  148. /*获取积分和用户抽奖次数*/
  149. get_win_num();
  150. /*获取中奖信息*/
  151. get_win_list();
  152. /* 获取抽奖前十名 */
  153. get_tot_num();
  154. })

3、php代码(奖品获取代码)

</>复制代码

  1. // 摇奖操作 获取中奖结果 并返回 信息
  2. private function get_win()
  3. {
  4. $user_id = intval($GLOBALS["user_info"]["id"]);
  5. $type = isset($_REQUEST["type"]) && preg_match("/^w+$/", $_REQUEST["type"]) ? $_REQUEST["type"] : "t_box";
  6. $json = array("code" => 0, "msg" => "", "uid" => $user_id);
  7. if(!$user_id){
  8. $json["msg"] = "请登录后再试!";
  9. $json["status"] = 2;
  10. es_session::set("gopreview", "/index.php?ctl=user&act=login");
  11. return $json;
  12. }
  13. $base_score = 300;
  14. $user_num = $GLOBALS["db"]->getOne("SELECT num from ".DB_PREFIX."turntable_user_num where user_id = {$user_id} AND type = "t_box"");
  15. $user_num = $user_num ? $user_num : 0;
  16. $user_score = $GLOBALS["db"]->getOne("SELECT score from ".DB_PREFIX."user where id = {$user_id}");
  17. $tot_num = intval($user_score/$base_score) + intval($user_num);
  18. // return $user_num;
  19. if(!$tot_num){ // 积分不足且没有赠送的抽奖次数
  20. $json["msg"] = "您的积分不足,通过投资或签到可获取相应积分!";
  21. $json["status"] = 3;
  22. $json["data"] = $_data;
  23. return $json;
  24. }
  25. // 可抽中奖品列表
  26. $arr = array(
  27. "0" => array("id"=>0,"good_id"=>50,"name"=>"平台10积分","v"=>15),
  28. "1" => array("id"=>1,"good_id"=>70,"name"=>"智能折叠代步车","v"=>1),
  29. "2" => array("id"=>2,"good_id"=>54,"name"=>"理财红包5元","v"=>30),
  30. "3" => array("id"=>3,"good_id"=>66,"name"=>"蓝牙运动耳机","v"=>0),
  31. "4" => array("id"=>4,"good_id"=>51,"name"=>"平台50积分","v"=>15),
  32. "5" => array("id"=>5,"good_id"=>68,"name"=>"遥控四轴飞行器","v"=>1),
  33. "6" => array("id"=>6,"good_id"=>53,"name"=>"理财红包18元","v"=>20),
  34. "7" => array("id"=>7,"good_id"=>60,"name"=>"LED充电台灯","v"=>5),
  35. "8" => array("id"=>8,"good_id"=>52,"name"=>"平台300积分","v"=>10),
  36. "9" => array("id"=>9,"good_id"=>59,"name"=>"20元话费","v"=>5),
  37. "10" => array("id"=>10,"good_id"=>67,"name"=>"太阳能充电器","v"=>3),
  38. "11" => array("id"=>11,"good_id"=>58,"name"=>"10元话费","v"=>10),
  39. "12" => array("id"=>12,"good_id"=>56,"name"=>"理财红包58元","v"=>10),
  40. "13" => array("id"=>13,"good_id"=>61,"name"=>"24小时保温杯","v"=>10),
  41. "14" => array("id"=>14,"good_id"=>57,"name"=>"理财红包88元","v"=>1),
  42. "15" => array("id"=>15,"good_id"=>69,"name"=>"智能运动手环","v"=>5),
  43. );
  44. $user_ids = array(
  45. "13" => array(20861,3661,20631,7433,2447,7239,12471,3338,13031,12070), // 24小时保温杯
  46. "15" => array(2086,8824,6699,747,2291,1127,2955,2408,5127,12070), // 智能运动手环
  47. );
  48. /* 正常抽奖 */
  49. $key = $this->get_rand($arr);
  50. $win = $arr[$key];
  51. /* 特殊客户 抽实物 */
  52. $g_count = $GLOBALS["db"]->getOne("SELECT count(*) FROM ".DB_PREFIX."turntable_user_goods WHERE user_id = ".$GLOBALS["user_info"]["id"]." AND type="t_box" AND goods_id IN(61,66,67,68,69,70)");
  53. foreach ($user_ids as $k => $v) {
  54. if(in_array($user_id, $v) && !$g_count)
  55. {
  56. $win = $arr[$k];
  57. break;
  58. }
  59. }
  60. $good = $GLOBALS["db"]->getRow("SELECT * FROM ".DB_PREFIX."turntable_goods WHERE id = ".$win["good_id"]);
  61. // 抽奖成功
  62. if(!empty($good))
  63. {
  64. // 扣除抽奖积分,或赠送机会
  65. $this->upadte_user_num_score($user_num,$user_score);
  66. // 添加奖品给用户
  67. $this->update_user_goods($good);
  68. return array(
  69. "code" => 1,
  70. "status" => 1,
  71. "win" => array(
  72. "id" => $win["id"],
  73. "name" => $good["name"],
  74. ),
  75. );
  76. }else{
  77. return array("code" => 0, "status" => 4, "msg" => "该活动 处于关闭状态,详情咨询客服!");
  78. }
  79. }
  80. // 抽奖概率算法
  81. private function get_rand($proArr)
  82. {
  83. $result = "";
  84. foreach ($proArr as $key => $val) {
  85. $arr[$key] = $val["v"];
  86. }
  87. // 概率数组的总概率
  88. $proSum = array_sum($arr);
  89. // 概率数组循环
  90. foreach ($arr as $k => $v) {
  91. $randNum = mt_rand(1, $proSum);
  92. if ($randNum <= $v) {
  93. $result = $k;
  94. break;
  95. } else {
  96. $proSum -= $v;
  97. }
  98. }
  99. return $result;
  100. }
  101. // 获取最新中奖列表
  102. private function get_new_win()
  103. {
  104. $num = isset($_REQUEST["num"]) ? intval($_REQUEST["num"]) : 10;
  105. $type = isset($_REQUEST["type"]) ? $_REQUEST["type"] : "t_box";
  106. $list = array();
  107. $sql = "SELECT g.id, g.user_id, u.user_name, g.goods_id, g.goods_name, g.time
  108. FROM ".DB_PREFIX."turntable_user_goods g
  109. LEFT JOIN ".DB_PREFIX."user u ON u.id = g.user_id
  110. WHERE g.is_effect = 1 AND g.type = "{$type}"
  111. ORDER BY g.id DESC limit {$num}";
  112. $list = $GLOBALS["db"]->getAll($sql);
  113. if(is_array($list)){
  114. foreach ($list as $key => $v) {
  115. $list[$key]["user_name"] = utf_substr($v["user_name"]);
  116. }
  117. }
  118. return array("list" => $list);
  119. }
  120. /* 获取中奖前五名 */
  121. private function get_tot_num()
  122. {
  123. $sql = "SELECT count(*) as num,u.user_name FROM fanwe_turntable_user_goods t
  124. LEFT JOIN fanwe_user u ON t.user_id = u.id
  125. WHERE t.type = "t_box" GROUP BY t.user_id ORDER BY num desc LIMIT 5";
  126. $list = $GLOBALS["db"]->getAll($sql);
  127. if(is_array($list)){
  128. foreach ($list as $key => $v) {
  129. $list[$key]["user_name"] = utf_substr($v["user_name"]);
  130. }
  131. }
  132. return $list;
  133. }
  134. // 获取自己所剩抽奖次数 和 积分
  135. private function get_win_num()
  136. {
  137. $types = isset($_REQUEST["types"]) ? $_REQUEST["types"] : "t_box";
  138. $arr = array();
  139. $user_num = $GLOBALS["db"]->getOne("SELECT num from ".DB_PREFIX."turntable_user_num where user_id = {$GLOBALS["user_info"]["id"]} AND type = "t_box"");
  140. $user_score = $GLOBALS["db"]->getOne("SELECT score from ".DB_PREFIX."user where id = {$GLOBALS["user_info"]["id"]}");
  141. $user_num = $user_num ? $user_num : 0;
  142. $arr["num"] = intval($user_score/300) + intval($user_num);
  143. $arr["score"] = $user_score;
  144. $arr["count"] = $GLOBALS["db"]->getOne("SELECT count(*) FROM ".DB_PREFIX."turntable_user_goods WHERE user_id = ".$GLOBALS["user_info"]["id"]." AND type="t_box"");
  145. return $arr;
  146. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/22098.html

相关文章

  • php抽奖实现

    摘要:抽奖地址主要代码其他引用可以在上面地址页面根据引用地址下载页面点我注册立即签到抽奖次数前名九宫格抽奖 抽奖地址:https://www.huijindaicn.com/i...主要代码:(其他引用可以在上面地址页面根据引用地址下载)1、html页面 ...

    IamDLY 评论0 收藏0
  • php抽奖实现

    摘要:抽奖地址主要代码其他引用可以在上面地址页面根据引用地址下载页面点我注册立即签到抽奖次数前名九宫格抽奖 抽奖地址:https://www.huijindaicn.com/i...主要代码:(其他引用可以在上面地址页面根据引用地址下载)1、html页面 ...

    hlcfan 评论0 收藏0
  • php抽奖实现

    摘要:抽奖地址主要代码其他引用可以在上面地址页面根据引用地址下载页面点我注册立即签到抽奖次数前名九宫格抽奖 抽奖地址:https://www.huijindaicn.com/i...主要代码:(其他引用可以在上面地址页面根据引用地址下载)1、html页面 ...

    MangoGoing 评论0 收藏0
  • 按照奖品概率分布抽奖实现

    摘要:京东券电影票淘宝券代码如下按照概率抽取一个奖品返回奖品所有奖品的概率总和应该为总概率基数方式二该方式如果直接看代码比较难理解。 首发于 樊浩柏科学院 需求:首先用户通过以一定方式(好友点赞等)开启抽奖资格,然后按照用户 100% 中奖概率进行抽奖,且系统的发放奖品需要按照各个奖品整体的期望中奖比例来进行分布,最后用户抽中奖品调用第三方发放接口发放奖品并记录保存,另有些奖品存在发放数量...

    Tamic 评论0 收藏0
  • jQuery+PHP实现砸金蛋抽奖

    摘要:演示下载地址效果图三个金蛋一把锤子及中奖结果代码如下锤子当鼠标滑向金蛋时,锤子会仅靠金蛋右上方,通过来控制位置。当挥动锤子砸向金蛋前,我们先把金蛋中的数字编号隐藏起来。最后,我们向后台发送一个请求,后台程序会处理奖项分配并把中奖结果返回。 演示下载地址:http://www.erdangjiade.com/js...效果图:showImg(https://segmentfault.co...

    Invoker 评论0 收藏0

发表评论

0条评论

fsmStudy

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<