当前位置:Gxlcms > PHP教程 > php分时间段的抽奖程序代码

php分时间段的抽奖程序代码

时间:2021-07-01 10:21:17 帮助过:30人阅读

  1. require_once (APP_ROOT . "./class/class_lib.php");
  2. require_once libfile ( 'class/StoreProcs' );
  3. pc_base::load_app_class('table_sub','',0);
  4. /**
  5. * 大转盘
  6. * @author Administrator
  7. *
  8. */
  9. class lib_dzp_lottery extends app_lib {
  10. /**
  11. * 奖品表
  12. */
  13. public $lottery = 'activity_lyq_lottery';
  14. /**
  15. * 奖品日志表
  16. */
  17. public $lottery_log = 'activity_lyq_lottery_log';
  18. /**
  19. * n等奖品
  20. */
  21. private $level = array();
  22. /**
  23. * 奖品信息
  24. */
  25. private $lotteryArray = array();
  26. /***
  27. * 随机数结束位数
  28. */
  29. private $max = 30000;
  30. /**
  31. * 类别 true:默认false:掌苏
  32. */
  33. private $type=true;
  34. /**
  35. * 中奖专业户
  36. */
  37. private $tel_zy=false;
  38. /**
  39. * 读取配置
  40. */
  41. public function load_setting() {
  42. $data=getcache('lyqdzp_setting');
  43. define('TIME', time()); //当前时间
  44. define('L_START', strtotime($data['l_start'])); //报名开始时间
  45. define('L_END', strtotime($data['l_end'])); //报名结束时间,-1意思昨天 23:59:59
  46. define('L_DAY_START',$data['l_day_start']); //每天开始时间
  47. define('L_DAY_END', $data['l_day_end']);//每天结束时间
  48. define('L_DAY_CISHU', $data['l_day_cishu']);//每天获得奖品次数
  49. define('L_CISHU', $data['l_cishu']);//共计获得奖品次数
  50. define('L_MIN', 20);//最少来福币
  51. define('L_LFB', $data['l_lfb']);//减少来福币
  52. define('L_ZY', $data['l_zy']); //专业户
  53. define('L_QUANSHU', $data['l_quanshu']);//圈数,大转盘开始圈数
  54. define('L_KONGJIANG', intval($data['l_kongjiang']));//空奖概率
  55. $host='www.****.com'; //现网
  56. if($_SERVER['HTTP_HOST']=='221.****0'){
  57. $host='221.*****'; //测试机
  58. }elseif($_SERVER['HTTP_HOST']=='125.com'){
  59. $host='125life.com';//本地
  60. }elseif(strpos($_SERVER['HTTP_HOST'],'192.168.')==0){
  61. $host=$_SERVER['HTTP_HOST'].'/125e.com';//本地
  62. }
  63. define('L_SITE', 'http://'.$host.'/'); //域名最后加/
  64. define('L_URL', L_SITE.'index.php');//访问地址
  65. //只能通过flash地址访问
  66. $s=L_SITE.'flash/dzp/i.swf';
  67. if(preg_replace('/swf.*$/','swf',$_SERVER['HTTP_REFERER'])!=$s&&$this->type){
  68. exit('老兄你已经得到得够多了,请通过正规途径摇奖!');
  69. }
  70. $this->load();
  71. }
  72. /**
  73. * 黑名单
  74. */
  75. private $heimingdan = array();
  76. /**
  77. * 读取奖品配置
  78. */
  79. public function load() {
  80. $this->setlottery ();
  81. $this->heimingdan = getcache ( 'tel', 'blanklist' );
  82. }
  83. private function setlottery() {
  84. $time=strtotime(date('Y-m-d',TIME));
  85. $sql = "select u.*";
  86. $sql .= ", (select COUNT(l.lotteryid) from " . DB::table ( $this->lottery_log ) . " AS l where u.lotteryid=l.lotteryid and l.iswinning='1')as num ";
  87. $sql .= " from " . DB::table ( $this->lottery ) . " AS u ";
  88. $sql .= " where 1 ";
  89. $sql .= " and u.isdisable='0' ";
  90. $sql .= " and u.time_start<='$time' ";
  91. $sql .= " and u.time_end>='$time' ";
  92. $sql .= " ORDER BY u.listorder ";
  93. $d = DB::fetch_all ( $sql );
  94. $sum = 0;
  95. foreach ( $d as $r ) {
  96. $r ['sum_over'] = intval ( $r ['sum'] ) - intval ( $r ['num'] );
  97. if ($r ['sum'] > 0) {
  98. $sum += $r ['sum'];
  99. }
  100. $r['msg']=str_replace('{name}', $r['name'],$r['msg']);
  101. $this->lotteryArray [$r ['lotteryid']] = $r;
  102. }
  103. if($sum<100) $sum=1000;
  104. $m = 1;
  105. $probability = 0;
  106. $newsum=0;
  107. //80%空奖+20%奖
  108. $this->max = $sum + $sum * (L_KONGJIANG/100) / ((100-L_KONGJIANG)/100);
  109. foreach ( $this->lotteryArray as $r ) {
  110. if ($r ['sum'] > 0) {
  111. $newsum=$r ['sum'];
  112. if($r['probability']>0){
  113. $probability=0;
  114. $probability=ceil($r ['sum']*100/$r['probability']);
  115. if($probability>0){
  116. $newsum=$probability;
  117. }
  118. }
  119. $this->level [$r ['lotteryid']] ['start'] = $m;
  120. $this->level [$r ['lotteryid']] ['end'] = $m + $newsum;
  121. $m = $m + $r ['sum'];
  122. }
  123. }
  124. }
  125. /**
  126. * 检测用户是否中奖 0 谢谢参与,其他 返回奖品data数据
  127. * @param array $user array(uid,tel,ip)
  128. */
  129. public function check($user) {
  130. if (intval ( $user ['uid'] ) < 1) {
  131. //没有登录
  132. return 0;
  133. }
  134. if(intval(TIME)//您来早了,活动还没有开始!
  135. return -21;
  136. }
  137. if(intval(TIME)>intval(L_END)){
  138. //已经过了活动时间,非常遗憾,谢谢!
  139. return -22;
  140. }
  141. if ($this->get_user_lfb ( $user ['uid'] ) < L_MIN) {
  142. //$this->credit ( $user ['uid'], -11, $logid );
  143. return -11; //您的来福币余额不足
  144. }
  145. $data = array(
  146. 'uid' => $user ['uid'],
  147. 'tel' => $user ['tel'],
  148. 'ip' => $user ['ip'],
  149. 'addtime' => time ()
  150. );
  151. $logid = DB::insert ( $this->lottery_log, $data, true );
  152. //检查用户 每天 已中奖品数
  153. if ($this->get_lottery ( $user ['uid'], strtotime ( date ( 'Y-m-d', TIME ) ), strtotime ( "+1day", strtotime ( date ( 'Y-m-d', TIME ) ) ), true ) >= L_DAY_CISHU) {
  154. $this->credit ( $user ['uid'], -12, $logid );
  155. //很遗憾,你本次什么也没有抽到!
  156. return -12;
  157. }
  158. //检查用户 全部 已中奖品数
  159. if ($this->get_lottery ( $user ['uid'], 0, 0, true ) > L_CISHU&&L_CISHU>0) {
  160. $this->credit ( $user ['uid'], -12, $logid );
  161. //很遗憾,你本次什么也没有抽到!
  162. return -15;
  163. }
  164. //检测黑名单,设置概率
  165. if ($this->checkhei ( $user ['tel'] ) > 0) {
  166. $this->credit ( $user ['uid'], -13, $logid );
  167. //很遗憾,你本次什么也没有抽到!
  168. return -13;
  169. }
  170. $lotteryid = $this->choujiang();
  171. $this->credit ( $user ['uid'], $lotteryid<1?$lotteryid:0, $logid, $lotteryid ,$this->hao);
  172. if ($lotteryid > 0) {
  173. return $this->lotteryArray [$lotteryid];
  174. } else {
  175. return -4;
  176. }
  177. //-1奖品已发完
  178. //-2本时段奖品已玩
  179. //-3不发奖品
  180. //-4奖品有问题
  181. //-11您的来福币余额不足
  182. //-12很遗憾,你本次什么也没有抽到!
  183. //-13黑名单里面,很遗憾,你本次什么也没有抽到!
  184. //-14中奖专业户禁止中高讲高奖品
  185. //-15已超过yizhong总奖品数
  186. //-16 掌苏传递手机 号错误
  187. //-21您来早了,活动还没有开始!
  188. //-22已经过了活动时间,非常遗憾,谢谢!
  189. //-23对不起,现在不是活动时间,活动时间为每天
  190. }
  191. /**
  192. * 显示
  193. * @param unknown_type $data
  194. */
  195. public function show($data){
  196. $ok=0;//3中奖
  197. $zz=0;//转盘度数
  198. $result='';//奖品名称
  199. $lotteryid=0;//奖品ID
  200. if(is_array($data)){
  201. $ok=3;
  202. $result=$data['msg'];
  203. $zz=$data['angle']+L_QUANSHU*360;
  204. $lotteryid=$data['lotteryid'];
  205. }else{
  206. $m='';
  207. switch($data){
  208. case 0:{
  209. $m= '请先登录!';
  210. }break;
  211. case -11:{
  212. $m= '您的来福币余额不足!';
  213. }break;
  214. case -12:{
  215. $m= '很遗憾,你本次什么也没有抽到!';
  216. }break;
  217. case -21:{
  218. $m= '您来早了,活动还没有开始!';
  219. }break;
  220. case -22:{
  221. $m= '已经过了活动时间,非常遗憾,谢谢!';
  222. }break;
  223. case -23:{
  224. $m= '对不起,现在不是活动时间,活动时间为每天'.L_DAY_START.':00-'.L_DAY_END.':00';
  225. }break;
  226. default:{
  227. $m= '很遗憾,你本次什么也没有抽到!';
  228. }break;
  229. }
  230. $result=$m;
  231. $ok=3;//3中奖
  232. $zz=360+L_QUANSHU*360;//再接再厉
  233. }
  234. header("Expires:Mon,26Jul199705:00:00GMT");
  235. header("Cache-Control:no-cache,must-revalidate");
  236. header("Pragma:no-cache");
  237. header("Content-type:text/html;charset=utf-8");
  238. if(!$this->type){
  239. echo json_encode(array('return'=>$lotteryid,
  240. 'msg'=>$result));
  241. }else{
  242. echo ''."\n";
  243. echo ''."\n";
  244. echo ''."\n";
  245. echo ''."\n";
  246. }
  247. exit;
  248. }
  249. /**
  250. * 抽奖的号
  251. */
  252. private $hao=0;
  253. /**
  254. * 抽奖开始
  255. */
  256. private function choujiang() {
  257. $j = 0;
  258. $lotteryid = 0;
  259. $j = mt_rand ( 1, $this->max );
  260. $this->hao=$j;
  261. foreach ( $this->level as $k => $r ) {
  262. if ($r ['start'] <= $j && $r ['end'] > $j) {
  263. $lotteryid = $k;
  264. break;
  265. }
  266. }
  267. $d = array(
  268. 1 => array(
  269. 'time' => strtotime ( date ( 'Y-m-d 00:00:00', TIME ) ),
  270. 'time_end' => strtotime ( date ( 'Y-m-d 07:59:59', TIME ) )
  271. )
  272. );
  273. for ($i=2;$i<=17;$i++){
  274. $d[$i]=array(
  275. 'time' => strtotime ( date ( 'Y-m-d '.($i+6).':00:00', TIME ) ),
  276. 'time_end' => strtotime ( date ( 'Y-m-d '.($i+6).':59:59', TIME ) )
  277. );
  278. }
  279. if($lotteryid>0){
  280. $l=$this->lotteryArray [$lotteryid];
  281. $sql = "select count(*) m from " . DB::table ( $this->lottery_log );
  282. $sql .= " where lotteryid='$lotteryid' ";
  283. $s= intval ( DB::result_first ( $sql ) );
  284. $sum_over=$l['sum']-$s;
  285. if($sum_over>0){
  286. //本时段 奖品数
  287. $sum=0;
  288. foreach ($d as $k=>$r){
  289. if(TIME>=$r['time']&&TIME<=$r['time_end']){
  290. if($l['time_'.$k]>0){//个数限制
  291. $sum=$this->get_lottery_id(0,$r['time'],$r['time_end']+1,$lotteryid);
  292. if($sum>=$l['time_'.$k]){
  293. //本时段奖品已玩
  294. $lotteryid=-2;
  295. break;
  296. }
  297. }elseif ($l['time_'.$k]==0){
  298. //不发奖品
  299. $lotteryid=-3;
  300. break;
  301. }else{
  302. //-1 不限制
  303. }
  304. }
  305. }
  306. if($this->tel_zy && $lotteryid>0&&$lotteryid<4){
  307. $lotteryid=-14;
  308. }
  309. }else{
  310. //奖品已发完
  311. $lotteryid=-1;
  312. }
  313. }else{
  314. //奖品有问题
  315. $lotteryid=-4;
  316. }
  317. return $lotteryid;
  318. }
  319. /**
  320. * 检测黑名单和普通用户,概率
  321. * @param string $tel
  322. * @return 大于1 表示本次禁止抽
  323. */
  324. private function checkhei($tel) {
  325. $n = mt_rand ( 1, 100 );
  326. if (in_array ( $tel, $this->heimingdan ['tel'] ) && L_ZY > 0) {
  327. $this->tel_zy=true;
  328. if ($n > L_ZY) {
  329. return 1;
  330. }
  331. }else{
  332. //号码检测
  333. if (!self::check_tel($tel)) {
  334. return 1;
  335. }
  336. }
  337. return 0;
  338. }
  339. /**
  340. * 合计奖品数
  341. * @param intval $uid
  342. * @param intval $start
  343. * @param intval $end
  344. * @param bool $bool true 获得奖品数 false 不统计
  345. * @return number
  346. */
  347. private function get_lottery($uid = 0, $start = 0, $end = 0, $bool = false) {
  348. $sql = "select count(*) m from " . DB::table ( $this->lottery_log );
  349. $sql .= " where 1 ";
  350. if (intval ( $start ) > 0) {
  351. $sql .= " and addtime>='$start' ";
  352. }
  353. if (intval ( $end ) > 0) {
  354. $sql .= " and addtime<'$end' ";
  355. }
  356. if ($bool) {
  357. $sql .= " and lotteryid>0 ";
  358. }
  359. if (intval ( $uid ) > 0) {
  360. $sql .= " and uid='$uid' ";
  361. }
  362. return intval ( DB::result_first ( $sql ) );
  363. }
  364. /**
  365. * 合计奖品数
  366. * @param intval $uid
  367. * @param intval $start
  368. * @param intval $end
  369. * @param intval $bool 奖品id
  370. * @return number
  371. */
  372. private function get_lottery_id($uid = 0, $start = 0, $end = 0, $lotteryid = 0) {
  373. $sql = "select count(*) m from " . DB::table ( $this->lottery_log );
  374. $sql .= " where 1 ";
  375. if (intval ( $start ) > 0) {
  376. $sql .= " and addtime>='$start' ";
  377. }
  378. if (intval ( $end ) > 0) {
  379. $sql .= " and addtime<'$end' ";
  380. }
  381. if ($lotteryid>=0) {
  382. $sql .= " and lotteryid='$lotteryid' ";
  383. }
  384. if (intval ( $uid ) > 0) {
  385. $sql .= " and uid='$uid' ";
  386. }
  387. return intval ( DB::result_first ( $sql ) );
  388. }
  389. /**
  390. * 获取用户来福币
  391. * @param intval $uid
  392. * @return number
  393. */
  394. public function get_user_lfb($uid) {
  395. $sql = "select extcredits2 from sz_common_member_count ";
  396. $sql .= " where 1 ";
  397. $sql .= " and uid='$uid' ";
  398. return intval ( DB::result_first ( $sql ) );
  399. }
  400. /**
  401. * 记录日志和更新用户来福币
  402. * @param intval $uid
  403. * @param intval $r 返回值
  404. * @param intval $logid
  405. * @param intval $lotteryid
  406. */
  407. private function credit($uid, $result, $logid = 0, $lotteryid = 0,$hao=0) {
  408. $data = array();
  409. if ($lotteryid > 0) {
  410. $data ['lotteryid'] = $lotteryid;
  411. $data ['iswinning'] = 1;
  412. }
  413. $data ['result'] = $result;
  414. $data ['hao'] = $hao;
  415. $data ['HTTP_REFERER'] = $_SERVER['HTTP_REFERER'];
  416. DB::Update ( $this->lottery_log, $data, " logid='$logid' " );
  417. if (L_LFB > 0) {
  418. $sql = 'UPDATE sz_common_member_count SET extcredits2=extcredits2-' . L_LFB . ' WHERE uid=' . $uid;
  419. DB::query ( $sql );
  420. $this->credit_log ( $uid, 'reduce', L_LFB, 1, '积分清零活动' );
  421. }
  422. }
  423. /**
  424. *手机号检查
  425. */
  426. private function check_tel($tel){
  427. $different_network=table_sub::get_different_network();
  428. if (!preg_match("/$different_network/",$tel)) {
  429. return false;
  430. }
  431. return true;
  432. }
  433. /**
  434. * 存储过程链接
  435. */
  436. private $StoreProcs;
  437. /**
  438. * 来福币日志 存储过程
  439. * @param intval $uid
  440. * @param string $op add增加 reduce 消耗
  441. * @param intval $creditvalue 来福币
  442. * @param intval $credittype 1来福币 2来福点
  443. * @param string $oreason 说明
  444. */
  445. private function credit_log($uid, $op, $creditvalue, $credittype, $oreason) {
  446. if (! $this->StoreProcs) {
  447. $this->StoreProcs = new StoreProcs ();
  448. }
  449. //调用存储过程
  450. $this->StoreProcs->query ( "call app_credit_log($uid,'$op','$credittype',1,$creditvalue,'$oreason')" );
  451. }
  452. }

三,调用:

  1. $dzp=new lib_dzp_lottery();
  2. $u=array('uid'=>$_G ['uid'],'tel'=>$_G ['tel'],'ip'=> $_G ['clientip']);
  3. $dzp->load_setting();
  4. $dzp->show($dzp->check($u));
  5. ?>

人气教程排行