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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM