1 <?php 2 3 // 权限判断 4 unset ( $map ); //释放变量 5 $map ['token'] = get_token (); //设置数组token值 6 $map ['openid'] = get_openid (); //设置数组openid值 7 $follow = M ( 'follow' )->where ( $map )->find (); //查表 8 $is_admin = is_login (); //使用判断是否登陆方法 9 $error = ''; 10 if ($data ['end_time'] <= time ()) { 11 $error = '活动已结束'; 12 } else if ($data ['max_num'] > 0 && $data ['max_num'] <= $my_count) { //控制抽奖次数 13 $error = '您的刮卡机会已用完啦'; 14 } else if ($data ['follower_condtion'] > intval ( $follow ['status'] ) && ! $is_admin) { //判断领取人权限 15 switch ($data ['follower_condtion']) { 16 case 1 : 17 $error = '关注后才能参与'; 18 break; 19 case 2 : 20 $error = '用户绑定后才能参与'; 21 break; 22 case 3 : 23 $error = '领取会员卡后才能参与'; 24 break; 25 } 26 } else if ($data ['credit_conditon'] > intval ( $follow ['score'] ) && ! $is_admin) { //判断领取人财富值 27 $error = '您的财富值不足'; 28 } else if ($data ['credit_bug'] > intval ( $follow ['score'] ) && ! $is_admin) { 29 $error = '您的财富值不够扣除'; 30 } else if (! empty ( $data ['addon_condition'] )) { 31 addon_condition_check ( $data ['addon_condition'] ) || $error = '您没权限参与'; 32 } 33 $this->assign ( 'error', $error ); 34 // 抽奖算法 35 empty ( $error ) && $this->_lottery ( $data, $prizes, $new_prizes, $my_count, $has, $no_count ); 36 37 $this->display ( 'show' ); 38 } 39 40 41 // 抽奖算法 中奖概率 = 奖品总数/(预估活动人数*每人抽奖次数) 42 function _lottery($data, $prizes, $new_prizes, $my_count = 0, $has = array(), $no_count = 0) { 43 $max_num = empty ( $data ['max_num'] ) ? 1 : $data ['max_num']; 44 $count = $data ['predict_num'] * $max_num; // 总基数 45 46 foreach ( $prizes as $p ) { // 获取已经中过的奖 47 $prizesArr [$p ['id']] = $p; 48 49 $prize_num = $p ['num'] - $has [$p ['id']]; //剩余中奖数 50 for($i = 0; $i < $prize_num; $i ++) { 51 $rand [] = $p ['id']; // 中奖的记录,同时通过ID可以知道中的是哪个奖 52 } 53 } 54 // dump ( $rand ); 55 // dump ( $prizesArr ); 56 57 if ($data ['predict_num'] != 1) { 58 $remain = $count - count ( $rand ) - $no_count; 59 $remain > 5000 && $remain = 5000; // 防止数组过大导致内存溢出 60 for($i = 0; $i < $remain; $i ++) { 61 $rand [] = 0; // 不中奖的记录 62 } 63 } 64 if (empty ( $rand )) { 65 $rand [] = - 1; 66 } 67 68 shuffle ( $rand ); // 所有记录随机排序 69 $prize_id = $rand [0]; // 第一个记录作为当前用户的中奖记录 70 $prize = array (); 71 72 if ($prize_id > 0) { //判断中奖情况 73 $prize = $prizesArr [$prize_id]; 74 } elseif ($prize_id == - 1) { 75 $prize ['id'] = 0; 76 $prize ['title'] = '奖品已抽完'; 77 } else { 78 $prize ['id'] = 0; 79 $prize ['title'] = '谢谢参与'; 80 } 81 82 // 获取我的抽奖机会 83 if (empty ( $data ['max_num'] )) { 84 $prize ['count'] = 1; 85 } else { 86 $prize ['count'] = $max_num - $my_count - 1; 87 $prize ['count'] < 0 && $prize ['count'] = 0; 88 } 89 90 // dump ( $prize ); 91 $this->assign ( 'prize', $prize ); 92 } 93 94 // 记录中奖数据到数据库 95 function set_sn_code() { 96 //设置数据数组 97 $data ['sn'] = uniqid (); 98 $data ['uid'] = $this->mid; 99 $data ['cTime'] = time (); 100 $data ['addon'] = 'Scratch'; 101 $data ['target_id'] = I ( 'id' ); 102 103 $data ['prize_id'] = $map ['id'] = I ( 'prize_id' ); 104 105 $title = ''; 106 if (! empty ( $map ['id'] )) { //确定奖品标题 107 $title = M ( 'prize' )->where ( $map )->getField ( 'title' ); 108 $title || $title = ''; 109 } 110 $data ['prize_title'] = $title; 111 // dump ( $data ); 112 113 $res = M ( 'sn_code' )->add ( $data ); //添加sn码 114 if ($res) { 115 // 更新获取数 116 M ( "scratch" )->where ( 'id=' . $data ['target_id'] )->setInc ( "collect_count" ); 117 118 // 扣除积分 119 $data = M ( 'scratch' )->find ( $data ['target_id'] ); 120 if (! empty ( $data ['credit_bug'] )) { 121 $credit ['score'] = $data ['credit_bug']; 122 $credit ['experience'] = 0; 123 add_credit ( 'scratch_credit_bug', 5, $credit ); 124 } 125 } 126 echo $res; 127 } 128 }