欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-05-29 22:36:15
...
  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$sql .= " and u.time_end>='$time' ";
  91. $sql .= " ORDER BY u.listorder ";
  92. $d = DB::fetch_all ( $sql );
  93. $sum = 0;
  94. foreach ( $d as $r ) {
  95. $r ['sum_over'] = intval ( $r ['sum'] ) - intval ( $r ['num'] );
  96. if ($r ['sum'] > 0) {
  97. $sum += $r ['sum'];
  98. }
  99. $r['msg']=str_replace('{name}', $r['name'],$r['msg']);
  100. $this->lotteryArray [$r ['lotteryid']] = $r;
  101. }
  102. if($sum$m = 1;
  103. $probability = 0;
  104. $newsum=0;
  105. //80%空奖+20%奖
  106. $this->max = $sum + $sum * (L_KONGJIANG/100) / ((100-L_KONGJIANG)/100);
  107. foreach ( $this->lotteryArray as $r ) {
  108. if ($r ['sum'] > 0) {
  109. $newsum=$r ['sum'];
  110. if($r['probability']>0){
  111. $probability=0;
  112. $probability=ceil($r ['sum']*100/$r['probability']);
  113. if($probability>0){
  114. $newsum=$probability;
  115. }
  116. }
  117. $this->level [$r ['lotteryid']] ['start'] = $m;
  118. $this->level [$r ['lotteryid']] ['end'] = $m + $newsum;
  119. $m = $m + $r ['sum'];
  120. }
  121. }
  122. }
  123. /**
  124. * 检测用户是否中奖 0 谢谢参与,其他 返回奖品data数据
  125. * @param array $user array(uid,tel,ip)
  126. */
  127. public function check($user) {
  128. if (intval ( $user ['uid'] ) //没有登录
  129. return 0;
  130. }
  131. if(intval(TIME)//您来早了,活动还没有开始!
  132. return -21;
  133. }
  134. if(intval(TIME)>intval(L_END)){
  135. //已经过了活动时间,非常遗憾,谢谢!
  136. return -22;
  137. }
  138. if ($this->get_user_lfb ( $user ['uid'] ) //$this->credit ( $user ['uid'], -11, $logid );
  139. return -11; //您的来福币余额不足
  140. }
  141. $data = array(
  142. 'uid' => $user ['uid'],
  143. 'tel' => $user ['tel'],
  144. 'ip' => $user ['ip'],
  145. 'addtime' => time ()
  146. );
  147. $logid = DB::insert ( $this->lottery_log, $data, true );
  148. //检查用户 每天 已中奖品数
  149. if ($this->get_lottery ( $user ['uid'], strtotime ( date ( 'Y-m-d', TIME ) ), strtotime ( "+1day", strtotime ( date ( 'Y-m-d', TIME ) ) ), true ) >= L_DAY_CISHU) {
  150. $this->credit ( $user ['uid'], -12, $logid );
  151. //很遗憾,你本次什么也没有抽到!
  152. return -12;
  153. }
  154. //检查用户 全部 已中奖品数
  155. if ($this->get_lottery ( $user ['uid'], 0, 0, true ) > L_CISHU&&L_CISHU>0) {
  156. $this->credit ( $user ['uid'], -12, $logid );
  157. //很遗憾,你本次什么也没有抽到!
  158. return -15;
  159. }
  160. //检测黑名单,设置概率
  161. if ($this->checkhei ( $user ['tel'] ) > 0) {
  162. $this->credit ( $user ['uid'], -13, $logid );
  163. //很遗憾,你本次什么也没有抽到!
  164. return -13;
  165. }
  166. $lotteryid = $this->choujiang();
  167. $this->credit ( $user ['uid'], $lotteryidhao);
  168. if ($lotteryid > 0) {
  169. return $this->lotteryArray [$lotteryid];
  170. } else {
  171. return -4;
  172. }
  173. //-1奖品已发完
  174. //-2本时段奖品已玩
  175. //-3不发奖品
  176. //-4奖品有问题
  177. //-11您的来福币余额不足
  178. //-12很遗憾,你本次什么也没有抽到!
  179. //-13黑名单里面,很遗憾,你本次什么也没有抽到!
  180. //-14中奖专业户禁止中高讲高奖品
  181. //-15已超过yizhong总奖品数
  182. //-16 掌苏传递手机 号错误
  183. //-21您来早了,活动还没有开始!
  184. //-22已经过了活动时间,非常遗憾,谢谢!
  185. //-23对不起,现在不是活动时间,活动时间为每天
  186. }
  187. /**
  188. * 显示
  189. * @param unknown_type $data
  190. */
  191. public function show($data){
  192. $ok=0;//3中奖
  193. $zz=0;//转盘度数
  194. $result='';//奖品名称
  195. $lotteryid=0;//奖品ID
  196. if(is_array($data)){
  197. $ok=3;
  198. $result=$data['msg'];
  199. $zz=$data['angle']+L_QUANSHU*360;
  200. $lotteryid=$data['lotteryid'];
  201. }else{
  202. $m='';
  203. switch($data){
  204. case 0:{
  205. $m= '请先登录!';
  206. }break;
  207. case -11:{
  208. $m= '您的来福币余额不足!';
  209. }break;
  210. case -12:{
  211. $m= '很遗憾,你本次什么也没有抽到!';
  212. }break;
  213. case -21:{
  214. $m= '您来早了,活动还没有开始!';
  215. }break;
  216. case -22:{
  217. $m= '已经过了活动时间,非常遗憾,谢谢!';
  218. }break;
  219. case -23:{
  220. $m= '对不起,现在不是活动时间,活动时间为每天'.L_DAY_START.':00-'.L_DAY_END.':00';
  221. }break;
  222. default:{
  223. $m= '很遗憾,你本次什么也没有抽到!';
  224. }break;
  225. }
  226. $result=$m;
  227. $ok=3;//3中奖
  228. $zz=360+L_QUANSHU*360;//再接再厉
  229. }
  230. header("Expires:Mon,26Jul199705:00:00GMT");
  231. header("Cache-Control:no-cache,must-revalidate");
  232. header("Pragma:no-cache");
  233. header("Content-type:text/html;charset=utf-8");
  234. if(!$this->type){
  235. echo json_encode(array('return'=>$lotteryid,
  236. 'msg'=>$result));
  237. }else{
  238. echo ''."\n";
  239. echo ''."\n";
  240. echo ''."\n";
  241. echo '
  242. '."\n";
  243. }
  244. exit;
  245. }
  246. /**
  247. * 抽奖的号
  248. */
  249. private $hao=0;
  250. /**
  251. * 抽奖开始
  252. */
  253. private function choujiang() {
  254. $j = 0;
  255. $lotteryid = 0;
  256. $j = mt_rand ( 1, $this->max );
  257. $this->hao=$j;
  258. foreach ( $this->level as $k => $r ) {
  259. if ($r ['start'] $j) {
  260. $lotteryid = $k;
  261. break;
  262. }
  263. }
  264. $d = array(
  265. 1 => array(
  266. 'time' => strtotime ( date ( 'Y-m-d 00:00:00', TIME ) ),
  267. 'time_end' => strtotime ( date ( 'Y-m-d 07:59:59', TIME ) )
  268. )
  269. );
  270. for ($i=2;$i$d[$i]=array(
  271. 'time' => strtotime ( date ( 'Y-m-d '.($i+6).':00:00', TIME ) ),
  272. 'time_end' => strtotime ( date ( 'Y-m-d '.($i+6).':59:59', TIME ) )
  273. );
  274. }
  275. if($lotteryid>0){
  276. $l=$this->lotteryArray [$lotteryid];
  277. $sql = "select count(*) m from " . DB::table ( $this->lottery_log );
  278. $sql .= " where lotteryid='$lotteryid' ";
  279. $s= intval ( DB::result_first ( $sql ) );
  280. $sum_over=$l['sum']-$s;
  281. if($sum_over>0){
  282. //本时段 奖品数
  283. $sum=0;
  284. foreach ($d as $k=>$r){
  285. if(TIME>=$r['time']&&TIMEif($l['time_'.$k]>0){//个数限制
  286. $sum=$this->get_lottery_id(0,$r['time'],$r['time_end']+1,$lotteryid);
  287. if($sum>=$l['time_'.$k]){
  288. //本时段奖品已玩
  289. $lotteryid=-2;
  290. break;
  291. }
  292. }elseif ($l['time_'.$k]==0){
  293. //不发奖品
  294. $lotteryid=-3;
  295. break;
  296. }else{
  297. //-1 不限制
  298. }
  299. }
  300. }
  301. if($this->tel_zy && $lotteryid>0&&$lotteryid$lotteryid=-14;
  302. }
  303. }else{
  304. //奖品已发完
  305. $lotteryid=-1;
  306. }
  307. }else{
  308. //奖品有问题
  309. $lotteryid=-4;
  310. }
  311. return $lotteryid;
  312. }
  313. /**
  314. * 检测黑名单和普通用户,概率
  315. * @param string $tel
  316. * @return 大于1 表示本次禁止抽
  317. */
  318. private function checkhei($tel) {
  319. $n = mt_rand ( 1, 100 );
  320. if (in_array ( $tel, $this->heimingdan ['tel'] ) && L_ZY > 0) {
  321. $this->tel_zy=true;
  322. if ($n > L_ZY) {
  323. return 1;
  324. }
  325. }else{
  326. //号码检测
  327. if (!self::check_tel($tel)) {
  328. return 1;
  329. }
  330. }
  331. return 0;
  332. }
  333. /**
  334. * 合计奖品数
  335. * @param intval $uid
  336. * @param intval $start
  337. * @param intval $end
  338. * @param bool $bool true 获得奖品数 false 不统计
  339. * @return number
  340. */
  341. private function get_lottery($uid = 0, $start = 0, $end = 0, $bool = false) {
  342. $sql = "select count(*) m from " . DB::table ( $this->lottery_log );
  343. $sql .= " where 1 ";
  344. if (intval ( $start ) > 0) {
  345. $sql .= " and addtime>='$start' ";
  346. }
  347. if (intval ( $end ) > 0) {
  348. $sql .= " and addtime}
  349. if ($bool) {
  350. $sql .= " and lotteryid>0 ";
  351. }
  352. if (intval ( $uid ) > 0) {
  353. $sql .= " and uid='$uid' ";
  354. }
  355. return intval ( DB::result_first ( $sql ) );
  356. }
  357. /**
  358. * 合计奖品数
  359. * @param intval $uid
  360. * @param intval $start
  361. * @param intval $end
  362. * @param intval $bool 奖品id
  363. * @return number
  364. */
  365. private function get_lottery_id($uid = 0, $start = 0, $end = 0, $lotteryid = 0) {
  366. $sql = "select count(*) m from " . DB::table ( $this->lottery_log );
  367. $sql .= " where 1 ";
  368. if (intval ( $start ) > 0) {
  369. $sql .= " and addtime>='$start' ";
  370. }
  371. if (intval ( $end ) > 0) {
  372. $sql .= " and addtime}
  373. if ($lotteryid>=0) {
  374. $sql .= " and lotteryid='$lotteryid' ";
  375. }
  376. if (intval ( $uid ) > 0) {
  377. $sql .= " and uid='$uid' ";
  378. }
  379. return intval ( DB::result_first ( $sql ) );
  380. }
  381. /**
  382. * 获取用户来福币
  383. * @param intval $uid
  384. * @return number
  385. */
  386. public function get_user_lfb($uid) {
  387. $sql = "select extcredits2 from sz_common_member_count ";
  388. $sql .= " where 1 ";
  389. $sql .= " and uid='$uid' ";
  390. return intval ( DB::result_first ( $sql ) );
  391. }
  392. /**
  393. * 记录日志和更新用户来福币
  394. * @param intval $uid
  395. * @param intval $r 返回值
  396. * @param intval $logid
  397. * @param intval $lotteryid
  398. */
  399. private function credit($uid, $result, $logid = 0, $lotteryid = 0,$hao=0) {
  400. $data = array();
  401. if ($lotteryid > 0) {
  402. $data ['lotteryid'] = $lotteryid;
  403. $data ['iswinning'] = 1;
  404. }
  405. $data ['result'] = $result;
  406. $data ['hao'] = $hao;
  407. $data ['HTTP_REFERER'] = $_SERVER['HTTP_REFERER'];
  408. DB::Update ( $this->lottery_log, $data, " logid='$logid' " );
  409. if (L_LFB > 0) {
  410. $sql = 'UPDATE sz_common_member_count SET extcredits2=extcredits2-' . L_LFB . ' WHERE uid=' . $uid;
  411. DB::query ( $sql );
  412. $this->credit_log ( $uid, 'reduce', L_LFB, 1, '积分清零活动' );
  413. }
  414. }
  415. /**
  416. *手机号检查
  417. */
  418. private function check_tel($tel){
  419. $different_network=table_sub::get_different_network();
  420. if (!preg_match("/$different_network/",$tel)) {
  421. return false;
  422. }
  423. return true;
  424. }
  425. /**
  426. * 存储过程链接
  427. */
  428. private $StoreProcs;
  429. /**
  430. * 来福币日志 存储过程
  431. * @param intval $uid
  432. * @param string $op add增加 reduce 消耗
  433. * @param intval $creditvalue 来福币
  434. * @param intval $credittype 1来福币 2来福点
  435. * @param string $oreason 说明
  436. */
  437. private function credit_log($uid, $op, $creditvalue, $credittype, $oreason) {
  438. if (! $this->StoreProcs) {
  439. $this->StoreProcs = new StoreProcs ();
  440. }
  441. //调用存储过程
  442. $this->StoreProcs->query ( "call app_credit_log($uid,'$op','$credittype',1,$creditvalue,'$oreason')" );
  443. }
  444. }
复制代码

三,调用:

  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. ?>
复制代码