php 積分抽獎活動(大轉盤)

如下是項目代碼(公衆號,使用積分進行抽獎活動),只可作參考:數據庫

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;}
相關文章
相關標籤/搜索