现在的位置: 首页 > 综合 > 正文

项目2:PHP抽奖程序 ,抽奖规则代码

2013年10月01日 ⁄ 综合 ⁄ 共 30931字 ⁄ 字号 评论关闭

项目4 抽奖程序 分时间段(按时段设置的奖品数为概率)http://blog.csdn.net/wljk506/article/details/7174151

=================================

项目3 抽奖程序 分时间段http://blog.csdn.net/wljk506/article/details/7105347

==========================================

项目2 抽奖程序

============================================

activity_lyq_lottery	奖品表
lotteryid	
name	礼品名称
sum	礼品总数
time_start	开始时间
time_end	结束时间
addtime	时间
probability	中奖概率
isdisable	是否开启
listorder	排序
time_1	时间段个数限制
time_2	
time_3	
time_4	
time_5	
time_6	
time_7	
msg	发送短信或显示信息
content	备注
angle	转盘角度
	
activity_lyq_lottery_log	抽奖日志
logid	ID
uid	用户ID
tel	手机号
addtime	抽奖时间
ip	抽奖IP
lotteryid	奖品ID
iswinning	是否抽中
result	返状态回值
hao	随机号

主程序:

<?php
require_once (APP_ROOT . "./class/class_lib.php");
require_once libfile ( 'class/StoreProcs' );
/**
 * 大转盘
 * @author Administrator
 *
 */
class lib_dzp_lottery extends app_lib {
	/**
	 * 奖品表
	 */
	public $lottery = 'activity_lyq_lottery';
	/**
	 * 奖品日志表
	 */
	public $lottery_log = 'activity_lyq_lottery_log';
	/**
	 * n等奖品
	 */
	private $level = array();
	/**
	 * 奖品信息
	 */
	private $lotteryArray = array();
	/***
	 * 随机数结束位数
	 */
	private $max = 30000;
	
	/**
	 * 读取配置
	 */
	public function load_setting() {
		$data=getcache('lyqdzp_setting');
		define('TIME', time());									//当前时间
		define('L_START', strtotime($data['l_start']));			//报名开始时间
		define('L_END', strtotime($data['l_end']));				//报名结束时间,-1意思昨天 23:59:59
		define('L_DAY_START',$data['l_day_start']);				//每天开始时间
		define('L_DAY_END', $data['l_day_end']);				//每天结束时间
		define('L_DAY_CISHU', $data['l_day_cishu']);			//每天获得奖品次数
		define('L_MIN', 20);									//最少来福币
		define('L_LFB', $data['l_lfb']);						//减少来福币
		define('L_ZY', $data['l_zy']);							//专业户
		define('L_QUANSHU', $data['l_quanshu']);				//圈数,大转盘开始圈数
		$host='www.*****.com';								//现网
		if($_SERVER['HTTP_HOST']=='test.com'){
			$host='test.com';						//测试机
		}elseif($_SERVER['HTTP_HOST']=='125life.com'){
			$host='125life.com';								//本地
		}
		define('L_SITE', 'http://'.$host.'/');					//域名最后加/
		define('L_URL', L_SITE.'index.php');	//访问地址
	}
	/**
	 * 黑名单
	 */
	private $heimingdan = array();
	/**
	 * 读取奖品配置
	 */
	public function load() {
		$this->setlottery ();
		$this->heimingdan = getcache ( 'tel', 'blanklist' );
	}
	private function setlottery() {
		$sql = "select u.*";
		$sql .= ", (select COUNT(l.lotteryid)  from  " . DB::table ( $this->lottery_log ) . " AS l where u.lotteryid=l.lotteryid and l.iswinning='1')as num ";
		$sql .= " from " . DB::table ( $this->lottery ) . " AS u ";
		$sql .= " where 1 ";
		$sql .= " and u.isdisable='0' ";
		$sql .= " and u.time_start<='" . TIME . "' ";
		$sql .= " and u.time_end>'" . TIME . "' ";
		$sql .= " ORDER BY u.listorder ";
		$d = DB::fetch_all ( $sql );
		//print_r($d);
		$sum = 0;
		foreach ( $d as $r ) {
			$r ['sum_over'] = intval ( $r ['sum'] ) - intval ( $r ['num'] );
			if ($r ['sum_over'] > 0) {
				$sum += $r ['sum_over'];
			}
			$r['msg']=str_replace('{name}', $r['name'],$r['msg']);
			$this->lotteryArray [$r ['lotteryid']] = $r;
		}
		$m = 1;
		$probability = 0;
		$newsum=0;
		$arr = array();
		//20%空奖+80%奖
		$this->max = $sum + $sum * 0.2 / 0.8;
		foreach ( $this->lotteryArray as $r ) {
			if ($r ['sum_over'] > 0) {
				$newsum=$r ['sum_over'];
				if($r['probability']>0){
					$probability=0;
					$probability=ceil($r ['sum']*100/$r['probability']);
					if($probability>0){
						$newsum=$probability;
					}
				}
				$this->level [$r ['lotteryid']] ['start'] = $m;
				$this->level [$r ['lotteryid']] ['end'] = $m + $newsum;
				$m = $m + $r ['sum_over'];
			}
		}
		unset ( $arr );
	}
	/**
	 * 检测用户是否中奖 0 谢谢参与,其他 返回奖品data数据
	 * @param array $user array(uid,tel,ip)
	 */
	public function check($user) {
		if (intval ( $user ['uid'] ) < 1) {
			//没有登录
			return 0;
		}
		if(intval(TIME)<intval(L_START)){
			//您来早了,活动还没有开始!
			return -21;
		}
		if(intval(TIME)>intval(L_END)){
			//已经过了活动时间,非常遗憾,谢谢!
			return -22;
		}
		$hour= date('H');
		if($hour<intval(L_DAY_START)||$hour>intval(L_DAY_END)){
			//对不起,现在不是活动时间,活动时间为每天'.L_DAY_START.':00-'.L_DAY_END.':00'
			return -23;
		}
		$data = array(
				'uid' => $user ['uid'],
				'tel' => $user ['tel'],
				'ip' => $user ['ip'],
				'addtime' => time ()
		);
		$logid = DB::insert ( $this->lottery_log, $data, true );

		if ($this->get_user_lfb ( $user ['uid'] ) < L_MIN) {
			$this->credit ( $user ['uid'], -11, $logid );
			return -11; //您的来福币余额不足
		}
		//检查用户已中奖品数
		if ($this->get_lottery ( $user ['uid'], strtotime ( date ( 'Y-m-d', TIME ) ), strtotime ( "+1day", strtotime ( date ( 'Y-m-d', TIME ) ) ), true ) >= L_DAY_CISHU) {
			$this->credit ( $user ['uid'], -12, $logid );
			//很遗憾,你本次什么也没有抽到!
			return -12;
		}
		//检测黑名单,设置概率
		if ($this->checkhei ( $user ['tel'] ) > 0) {
			$this->credit ( $user ['uid'], -13, $logid );
			//很遗憾,你本次什么也没有抽到!
			return -13;
		}
		$lotteryid = $this->choujiang();
		$this->credit ( $user ['uid'], $lotteryid<1?$lotteryid:0, $logid, $lotteryid ,$this->hao);
		if ($lotteryid > 0) {
			return $this->lotteryArray [$lotteryid];
		} else {
			return -4;
		}
		//-1奖品已发完
		//-2本时段奖品已玩
		//-3不发奖品
		//-4奖品有问题
		//-11您的来福币余额不足
		//-12很遗憾,你本次什么也没有抽到!
		//-13黑名单里面,很遗憾,你本次什么也没有抽到!
		//-21您来早了,活动还没有开始!
		//-22已经过了活动时间,非常遗憾,谢谢!
		//-23对不起,现在不是活动时间,活动时间为每天
	}
	/**
	 * 显示
	 * @param unknown_type $data
	 */
	public function show($data){
		$ok=0;//3中奖
		$zz=0;//转盘度数
		$result='';//奖品名称
		if(is_array($data)){
			$ok=3;
			$result=$data['msg'];
			$zz=$data['angle'];
		}else{
			$m='';
			switch($data){
				case 0:{
					$m= '请先登录!';
				}break;
				case -11:{
					$m= '您的来福币余额不足!';
				}break;
				case -12:{
					$m= '很遗憾,你本次什么也没有抽到!';
				}break;
				case -21:{
					$m= '您来早了,活动还没有开始!';
				}break;
				case -22:{
					$m= '已经过了活动时间,非常遗憾,谢谢!';
				}break;
				case -23:{
					$m= '对不起,现在不是活动时间,活动时间为每天'.L_DAY_START.':00-'.L_DAY_END.':00';
				}break;
				default:{
					$m= '很遗憾,你本次什么也没有抽到!';
				}break;
			}
			$result=$m;
			$ok=3;//3中奖
			//再接再厉
			$suiji=array(
					0=>120,
					1=>240,
					2=>360,
					3=>120,
					4=>240,
					5=>360
			);
			$zz=$suiji[intval(mt_rand ( 0,5))];//再接再厉
		}
		header("Expires:Mon,26Jul199705:00:00GMT");
		header("Cache-Control:no-cache,must-revalidate");
		header("Pragma:no-cache");
		header("Content-type:text/html;charset=utf-8");
		echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
		echo '<gift>'."\n";
		echo '<shift ok="'.$ok.'" zz="'.$zz.'" add="9" down="0" lid="0" result="'.$result.'"/>'."\n";
		echo '</gift>'."\n";
	}
	/**
	 * 抽奖的号
	 */
	private $hao=0;
	/**
	 * 抽奖开始
	 */
	private function choujiang() {
		$j = 0;
		$lotteryid = 0;
		$j = mt_rand ( 1, $this->max );
		$this->hao=$j;
		foreach ( $this->level as $k => $r ) {
			if ($r ['start'] <= $j && $r ['end'] > $j) {
				$lotteryid = $k;
				break;
			}
		}
		$d = array(
			1 => array(
				'time' => strtotime ( date ( 'Y-m-d 00:00:00', TIME ) ), 
				'time_end' => strtotime ( date ( 'Y-m-d 01:59:59' ) ) 
			), 
			2 => array(
				'time' => strtotime ( date ( 'Y-m-d 02:00:00', TIME ) ), 
				'time_end' => strtotime ( date ( 'Y-m-d 03:59:59' ) ) 
			), 
			3 => array(
				'time' => strtotime ( date ( 'Y-m-d 04:00:00', TIME ) ), 
				'time_end' => strtotime ( date ( 'Y-m-d 08:59:59' ) ) 
			), 
			4 => array(
				'time' => strtotime ( date ( 'Y-m-d 09:00:00', TIME ) ), 
				'time_end' => strtotime ( date ( 'Y-m-d 11:59:59' ) ) 
			), 
			5 => array(
				'time' => strtotime ( date ( 'Y-m-d 12:00:00', TIME ) ), 
				'time_end' => strtotime ( date ( 'Y-m-d 17:59:59' ) ) 
			), 
			6 => array(
				'time' => strtotime ( date ( 'Y-m-d 18:00:00', TIME ) ), 
				'time_end' => strtotime ( date ( 'Y-m-d 19:59:59' ) ) 
			), 
			7 => array(
				'time' => strtotime ( date ( 'Y-m-d 20:00:00', TIME ) ), 
				'time_end' => strtotime ( date ( 'Y-m-d 23:59:59' ) ) 
			) 
		);
		if($lotteryid>0){
			$l=$this->lotteryArray [$lotteryid];
			if($l['sum_over']>0){
				//本时段 奖品数
				$sum=0;
				foreach ($d as $k=>$r){
					if(TIME>=$r['time']&&TIME<=$r['time_end']){
						//echo '<br/>'.$k;
						if($l['time_'.$k]>0){//个数限制
							$sum=$this->get_lottery_id(0,$r['time'],$r['time_end']+1,$lotteryid);
							if($sum>=$l['time_'.$k]){
								//本时段奖品已玩
								$lotteryid=-2;
								break;
							}
						}elseif ($l['time_'.$k]==0){
							//不发奖品
							$lotteryid=-3;
							break;
						}else{
							//-1 不限制
						}
						
					}
				}
			}else{
				//奖品已发完
				$lotteryid=-1;
			}
		}else{
			//奖品有问题
			$lotteryid=-4;
		}
		return $lotteryid;
	
	}
	/**
	 * 检测黑名单和普通用户,概率
	 * @param string $tel
	 * @return 大于1 表示本次禁止抽
	 */
	private function checkhei($tel) {
		$n = mt_rand ( 1, 100 );
		if (in_array ( $tel, $this->heimingdan ['tel'] ) && L_ZY > 0) {
			if ($n > L_ZY) {
				return 1;
			}
		}else{
			if ($n <= L_ZY) {
				return 1;
			}
		}
		
	}
	/**
	 * 合计奖品数
	 * @param intval $uid
	 * @param intval $start
	 * @param intval $end
	 * @param bool $bool true 获得奖品数 false 不统计
	 * @return number
	 */
	private function get_lottery($uid = 0, $start = 0, $end = 0, $bool = false) {
		
		$sql = "select count(*) m from " . DB::table ( $this->lottery_log );
		$sql .= " where 1 ";
		if (intval ( $start ) > 0) {
			$sql .= " and addtime>='$start' ";
		}
		if (intval ( $end ) > 0) {
			$sql .= " and addtime<'$end' ";
		}
		if ($bool) {
			$sql .= " and lotteryid>0 ";
		}
		if (intval ( $uid ) > 0) {
			$sql .= " and uid='$uid' ";
		}
		return intval ( DB::result_first ( $sql ) );
	}
	/**
	 * 合计奖品数
	 * @param intval $uid
	 * @param intval $start
	 * @param intval $end
	 * @param intval $bool 奖品id
	 * @return number
	 */
	private function get_lottery_id($uid = 0, $start = 0, $end = 0, $lotteryid = 0) {
	
		$sql = "select count(*) m from " . DB::table ( $this->lottery_log );
		$sql .= " where 1 ";
		if (intval ( $start ) > 0) {
			$sql .= " and addtime>='$start' ";
		}
		if (intval ( $end ) > 0) {
			$sql .= " and addtime<'$end' ";
		}
		if ($lotteryid>=0) {
			$sql .= " and lotteryid='$lotteryid' ";
		}
		if (intval ( $uid ) > 0) {
			$sql .= " and uid='$uid' ";
		}
		return intval ( DB::result_first ( $sql ) );
	}
	/**
	 * 获取用户来福币
	 * @param intval $uid
	 * @return number
	 */
	public function get_user_lfb($uid) {
		$sql = "select extcredits2 from sz_common_member_count ";
		$sql .= " where 1 ";
		$sql .= " and uid='$uid' ";
		return intval ( DB::result_first ( $sql ) );
	}
	/**
	 * 记录日志和更新用户来福币
	 * @param intval $uid
	 * @param intval $r  返回值
	 * @param intval $logid
	 * @param intval $lotteryid
	 */
	private function credit($uid, $result, $logid = 0, $lotteryid = 0,$hao=0) {
		$data = array();
		if ($lotteryid > 0) {
			$data ['lotteryid'] = $lotteryid;
			$data ['iswinning'] = 1;
		}
		$data ['result'] = $result;
		$data ['hao'] = $hao;
		DB::Update ( $this->lottery_log, $data, " logid='$logid' " );
		if (L_LFB > 0) {
			$sql = 'UPDATE sz_common_member_count SET extcredits2=extcredits2-' . L_LFB . ' WHERE uid=' . $uid;
			DB::query ( $sql );
			$this->credit_log ( $uid, 'reduce', L_LFB, 1, '积分清零活动' );
		}
	}
	/**
	 * 存储过程链接
	 */
	private $StoreProcs;
	/**
	 * 来福币日志 存储过程
	 * @param intval $uid
	 * @param string $op add增加  reduce 消耗
	 * @param intval $creditvalue 来福币
	 * @param intval $credittype 1来福币 2来福点
	 * @param string $oreason  说明
	 */
	private function credit_log($uid, $op, $creditvalue, $credittype, $oreason) {
		if (! $this->StoreProcs) {
			$this->StoreProcs = new StoreProcs ();
		}
		//调用存储过程
		$this->StoreProcs->query ( "call app_credit_log($uid,'$op','$credittype',1,$creditvalue,'$oreason')" );
	}

}

====================================================================

以下部分有点错误,项目1抽奖程序

===================================================================================================================

做了一个抽奖的小项目,是DX的模块,发出来做个备份

数据库先.

app_qiuxie_log	访问日志	
logid	ID	
uid	用户ID	
tel	手机号	
host	网址名称	
url	来源页地址	
addtime	时间	
ip	IP	
type	类型	0默认1.www.****.com/a/autumncrab/统计来源
		
app_qiuxie_lottery_log	抽奖日志	
logid	ID	
uid	用户ID	
tel	手机号	
addtime	抽奖时间	
ip	抽奖IP	
lotteryid	奖品ID	
iswinning	是否抽中	1抽中0无
isdisable	是否禁用	1禁用0允许
disablecontent	禁用理由	
isedit	是否已修改过	0无1已修改
orderid	订单ID号	用于充值订单号
money	金额	充值金额
recharge_time	充值时间	
isout	超时	0无1超时
ismenpiao	门票	0无1是
ispay	是否支付	0无1已经支付成功
		
		
app_qiuxie_lottery	奖项设置	
lotteryid	id	
name	名称	
minute	分钟	默认60分钟放出
num	数量	放出多少个
sum	总数	奖品总数
start_time	开始时间	
end_time	结束时间	
togetherlast	上次一起	上次如果没中,是否和本次一起放出,1一起,0否
probability	中奖概率	默认1%
isopen	开启	1开启0关闭
addtime	添加时间	
listorder	排序	正序
isrepeat	是否重复中奖	0是1否
ismenpiao	门票	0无1是
		
app_qiuxie_pay_log	支付日志表	
logid	ID	
notify_id	支付宝订单ID	
out_trade_no	支付订单ID	
trade_no		
total_fee	金额	
trade_status	成功标志	
sign	密钥	
query	查询字符串	
uid	用户ID	
tel	手机号	
ip	IP	
addtime	时间	

ctl_qiuxie_index.php 


<?php
/*
* by 夜色紫宸風 2011-10-25
*/
include_once DISCUZ_ROOT.'source/app/qiuxie/cache.class.php';
class qiuxie_index extends app_mod{
        function init(){
            $this->lib('lib_qiuxie');
            $this->lib('lib_qiuxie_lottery');
            $c=qgetcache('qiuxie_setting');
            define('QIUXIE_TIME', time());							//当前时间
            define('QIUXIE_BM_STAR', strtotime($c['bm_start']));	//报名开始时间
            define('QIUXIE_BM_END', strtotime($c['bm_end']));		//报名结束时间,-1意思昨天 23:59:59
            define('QIUXIE_CJ_STAR', strtotime($c['cj_start']));	//抽奖开始时间
            define('QIUXIE_CJ_END', strtotime($c['cj_end']));		//抽奖结束时间,-1意思昨天 23:59:59
            $host='www.*****.com';								//现网
            if($_SERVER['HTTP_HOST']=='http://test.com'){
            	$host='http://test.com';						//测试机
            }elseif($_SERVER['HTTP_HOST']=='125life.com'){
            	$host='125life.com';								//本地
            }
            define('QIUXIE_URL', 'http://'.$host.'/');				//域名最后加 /
            define('QIUXIE_LFB',$c['lfb']);							//赚取来福币数
            define('QIUXIE_CJ_CISHU', $c['cishu']);					//抽奖次数
            define('QIUXIE_CJ_Probability', 3);						//抽奖概率=报名人数*3倍数
            define('QIUXIE_RECHARGE', $c['recharge']);				//中奖后3小时内充值
            define('QIUXIE_MONEY', $c['money']);					//充值金额
            
        }
        /**
         * 主页
         * @see app_mod::on_index()
         */
        public function on_index(){
        	global $_G;
            $data=array();
            //第几栏
            $data['style']['1'] = 1;//2背景白色1背景咖啡
            $data['style']['2'] = 2;
            $data['style']['3'] = 2;
            $data['style']['4'] = 2;
            $data['style']['5'] = 2;
            //检测用户是否已登录
            if(intval($_G['uid'])>0){
            	//检测报名
            	$data['user']=$this->qiuxie->user($_G['uid']);
            }
            
            //获取用户来福币
            $data['user']['sum']=$this->qiuxie->user_lfb($_G['uid'],QIUXIE_BM_STAR,QIUXIE_BM_END);
            //时间规则判断设置进度
            if(QIUXIE_TIME<QIUXIE_BM_STAR){									//step1报名
            	$data['style']['1'] = 1;
            	$data['style']['2'] = 2;
            }elseif(QIUXIE_TIME>QIUXIE_BM_STAR&&QIUXIE_TIME<QIUXIE_BM_END){	//step2赚取来福币
            	$data['style']['1'] = 2;
            	$data['style']['2'] = 1;
            	//检测用户设置进度
	        	if(intval($data['user']['uid'])<1){
	            	$data['style']['1'] = 1;
	            	$data['style']['2'] = 2;
	            }
	        }elseif(QIUXIE_TIME>QIUXIE_BM_END&&QIUXIE_TIME<QIUXIE_CJ_STAR){	//step3抽奖
            	$data['style']['1'] = 2;
            	$data['style']['2'] = 2;
            	$data['style']['3'] = 1;
            	//检测是否抽中												//step4支付押金
            	$data['winning']=$this->qiuxie_lottery->getuserwinningqiuxie($_G ['uid'],true);
            	
            }elseif(QIUXIE_TIME>QIUXIE_CJ_STAR&&QIUXIE_TIME<QIUXIE_CJ_END){	//step3抽奖
            	$data['style']['1'] = 2;
            	$data['style']['2'] = 2;
            	$data['style']['3'] = 1;
            	//检测是否抽中												//step4支付押金
            	$data['winning']=$this->qiuxie_lottery->getuserwinningqiuxie($_G ['uid'],true);
            	
            }else{															//step5参加蟹宴
            	$data['style']['1'] = 2;
            	$data['style']['2'] = 2;
            	$data['style']['5'] = 1;
            }
            //用户已抽奖次数
            $data['user']['lottery']=$this->qiuxie_lottery->getusercount(intval($_G['uid']));

            //用户剩余次数
            $data['user']['lottery_over']=QIUXIE_CJ_CISHU-$data['user']['lottery'];
            if($data['user']['lottery_over']<0) {$data['user']['lottery_over']=0;}
           
            
            //检测是否抽中												//step4支付押金
            $data['winning']=$this->qiuxie_lottery->getuserwinningqiuxie($_G ['uid'],true,2);
            //截止小时
            foreach ($data['winning'] as $k=>$r){
            	if($r['isout']==1){
            		continue;
            	}
            	if($r['ispay']<1){
	            	$r['over_time']=date('H小时i分s秒',strtotime(date('Y-m-d',QIUXIE_TIME))+QIUXIE_RECHARGE*3600-(QIUXIE_TIME-$r['addtime']));
	            	
	            	if((QIUXIE_TIME-$r['addtime'])<=(QIUXIE_RECHARGE*3600)){
	            		//订单ha
	            		$r['url']=$this->qiuxie_lottery->alipay_url(date('YmdHis',QIUXIE_TIME).'-'.$r['logid'],
	            		$r['name'].'1张押金'.QIUXIE_MONEY.'元',
	            		$r['logid']);
	            	}
            	}
            	$data['winning'][$k]=$r;
            	break;
            }
			
			
            
            //访问日志
            $this->qiuxie->log(array('uid'=>$_G['uid'],'tel'=>$_G['tel'],'ip'=>$_G['clientip']));
            
            $this->view('qiuxie/index',$data);
        }
        public function on_chouzhong(){
        	global $_G;
        	print_r($_G);
        	
        	if ($_G ['uid']) {
        		$data=$this->qiuxie_lottery->getuserwinningqiuxie($_G ['uid'],true);
        		print_r($data);
        	}else{
        		$param=array ();
	        	$extra=array (
	        	'showmsg' => true ,
	        	'closetime'=>3,
	        	);
        		showmessage ( '请先登录!',$forward, $param,$extra);
        	}
        }
        /**
         * 报名
         */
        public function on_baoming(){
        	global $_G;
        	
        	$forward=QIUXIE_URL.'index.php?dir=qiuxie';

        	if ($_G ['uid']) {
        		
        		if(intval(QIUXIE_TIME)<intval(QIUXIE_BM_STAR)){
        			$msg= ' 您来早了,报名还没有开始!';
        			$param=array ();
		        	$extra=array ('msgtype' =>2, 
		        	'showmsg' => true ,
		        	'timeout'=>3,
		        	'refreshtime'=>3,
		        	);
					showmessage ( $msg,'', $param, $extra);
        		}
        		if(intval(QIUXIE_TIME)>intval(QIUXIE_BM_END)){
        			$msg= ' 已经过了报名时间,非常遗憾,谢谢!';
        			$param=array ();
		        	$extra=array ('msgtype' =>2, 
		        	'showmsg' => true ,
		        	'timeout'=>true,
		        	'refreshtime'=>3,
		        	);
					showmessage ( $msg,'', $param, $extra);
        		}
        		$user=$this->qiuxie->user($_G['uid']);
        		if(intval($user['uid'])>0){
        			$msg= '您已经报名过了!<br/>';
        			$msg.='手机号为 '.$user['tel'];
        			$msg.=' 的用户<br/>在 '.date('Y-m-d H:i:s',$user['addtime']);
        			$mag.=' 报名';
        			$param=array ('locationtime'=>3);
		        	$extra=array ('msgtype' =>2, 
		        	'showmsg' => true ,
		        	'timeout'=>3,
		        	'refreshtime'=>3,
		        	'locationtime'=>3,
		        	);
					showmessage ( $msg,'', $param, $extra);
        		}else{
					$this->qiuxie->user_add(array('uid'=>$_G['uid'],'tel'=>$_G['tel'],'ip'=>$_G['clientip']));
					$msg='提示信息:<br/><h2 style="color:red;font-size:16px;">报名成功!</h2>';
					$msg.='<script type="text/javascript">';
		        	$msg.='setTimeout("window.location.href=\''.$forward.'\'", 3000);';
		        	//$msg.='setTimeout("hideWindow(\'baoming\')", 4000);';
		        	$msg.='</script>';
        			showmessage ($msg,$forward, '','',1);
        		}
        	}else{
        		$param=array ();
	        	$extra=array (
	        	'showmsg' => true ,
	        	'closetime'=>3,
	        	);
        		showmessage ( '请先登录!',$forward, $param,$extra);
        	}
        	exit;
        }
        /**
         * 
         */
        public function on_choujiang(){
        	global $_G;
        	
        	$forward=QIUXIE_URL.'index.php?dir=qiuxie';
        	if ($_G ['uid']) {
        		
        		if(intval(QIUXIE_TIME)<intval(QIUXIE_CJ_STAR)){
        			$msg= ' 您来早了,抽奖还没有开始!';
        			$param=array ();
		        	$extra=array ('msgtype' =>2, 
		        	'showmsg' => true ,
		        	'timeout'=>3,
		        	'refreshtime'=>3,
		        	);
					showmessage ( $msg,'', $param, $extra);
        		}
        		if(intval(QIUXIE_TIME)>intval(QIUXIE_CJ_END)){
        			$msg= ' 已经过了抽奖时间,非常遗憾,谢谢!';
        			$param=array ();
		        	$extra=array ('msgtype' =>2, 
		        	'showmsg' => true ,
		        	'timeout'=>true,
		        	'refreshtime'=>3,
		        	);
					showmessage ( $msg,'', $param, $extra);
        		}
        		$lfb=$this->qiuxie->user_lfb($_G['uid'],QIUXIE_BM_STAR,QIUXIE_BM_END);
        		if($lfb<intval(QIUXIE_LFB)){
        			$msg= ' 你在活动时间内已获得来福币'.$lfb.'个,再多赚'.(intval(QIUXIE_LFB)-$lfb).'个,就可以获得'.QIUXIE_CJ_CISHU.'次抽奖机会!';
        			$param=array ();
		        	$extra=array ('msgtype' =>2, 
		        	'showmsg' => true ,
		        	'timeout'=>true,
		        	'refreshtime'=>3,
		        	);
					showmessage ( $msg,'', $param, $extra);
        		}
        		//用户已抽奖次数
            	$sum=$this->qiuxie_lottery->getusercount(intval($_G['uid']));
				if(QIUXIE_CJ_CISHU-$sum<=0){
					$msg= ' 对不起,您的抽奖次数已经用完了!';
        			$param=array ();
		        	$extra=array ('msgtype' =>2, 
		        	'showmsg' => true ,
		        	'timeout'=>true,
		        	'refreshtime'=>3,
		        	);
					showmessage ( $msg,'', $param, $extra);
				}

        		$user=$this->qiuxie->user($_G['uid']);
        		if(intval($user['uid'])>0){
        			$this->qiuxie_lottery->setting(QIUXIE_TIME,QIUXIE_CJ_STAR,QIUXIE_CJ_END);
        			$data=$this->qiuxie_lottery->checklevel(array('uid'=>$_G['uid'],'tel'=>$_G['tel'],'ip'=>$_G['clientip']));
        			
        			$sum=QIUXIE_CJ_CISHU-$sum-1;
        			if($sum<0) $sum=0;
					$msg='提示信息:<br/><h2 style="color:red;font-size:20px;">';
					if(is_array($data)){
						$msg.='恭喜您,中奖了,';
						$msg.=$data['name'].'1张 ,<br/>';
						if(strpos($data['name'],'门票')){
							$msg.='不要忘记支付押金哦!';
						}
						$msg.='<br/>您还有 '.$sum.'次抽奖机会!';
					}else{
						$msg.='您没有中奖,再接再厉!';
					}
					$msg.='</h2>';
					$msg.='<script type="text/javascript">';
		        	$msg.='setTimeout("window.location.href=\''.$forward.'\'", 3000);';
		        	//$msg.='setTimeout("hideWindow(\'baoming\')", 4000);';
		        	$msg.='</script>';
        			showmessage ($msg,$forward, '','',1);
        			
        		}else{
					$msg= '您还没报名!';
        			$param=array ('locationtime'=>3);
		        	$extra=array ('msgtype' =>2, 
		        	'showmsg' => true ,
		        	'timeout'=>3,
		        	'refreshtime'=>3,
		        	'locationtime'=>3,
		        	);
					showmessage ( $msg,'', $param, $extra);
        		}
        	}else{
        		$param=array ();
	        	$extra=array (
	        	'showmsg' => true ,
	        	'closetime'=>3,
	        	);
        		showmessage ( '请先登录!',$forward, $param,$extra);
        	}
        	exit;
        }
    	/**
         * 书签
         * 发送短信到书签
         */
        public function on_shuqian(){
        	global $_G;
        	
        	$forward=QIUXIE_URL.'index.php?dir=qiuxie';
        	$param=array ();
	        $extra=array (
	        	'showmsg' => true ,
	        	'closetime'=>3,
	        );
	        $msg='请先登录!';
        	if ($_G ['uid']) {
        		if($_G ['tel']){
        			
        			$content='G3手机免费送啦!即刻下载快捷书签访问****,天天免费送彩票,G3手机、肯德基现金券、话费等你拿!';
        			$content.='地址:http://wap.*****.cn/index?lanmu=00260009';
        			
        			app_ctl::fun('sms');
					send_sms_first(trim($_G ['tel']),$content,2);
					
        			$msg='信息已经发送,你将在1分钟内收到短信息!';
        		}else{
        			$msg='手机号错误,请联系管理员!';
        		}
        		
        	}else{
        		
        	}
        	showmessage ( $msg,$forward, $param,$extra);
        }
		/**
         * 规则
         * @see app_mod::on_guize()
         */
        public function on_guize(){
            $this->view('qiuxie/guize');
        }
        /**
         * 支付返回数据
         */
        public function on_zhifu(){
        	global $_G;

        	$forward=QIUXIE_URL.'index.php?dir=qiuxie';
        	//记录日志
        	$this->qiuxie_lottery->paylog(array('uid'=>intval($_G['uid']),'tel'=>trim($_G['tel']),'ip'=>$_G['clientip']));
        	//使用团购的支付类
        	include_once DISCUZ_ROOT.'./buy/include/library/AlipayNotify.class.php';
        	list($ec_contract, $ec_securitycode, $ec_partner, $ec_creditdirectpay) =
        	 explode("\t", authcode($_G['setting']['ec_contract'], 'DECODE', $_G['config']['security']['authkey']));
        	
			unset($_GET['dir'],$_GET['do']);
			$alipay = new AlipayNotify($ec_partner,$ec_securitycode,'MD5','utf-8');
			$verify_result = $alipay->return_verify();
			if ( $verify_result ) {
				if($_GET['trade_status'] == 'TRADE_FINISHED' || $_GET['trade_status'] == 'TRADE_SUCCESS') {
					$out_trade_no = trim($_GET['out_trade_no']);   //获取订单号
					$total_fee  = intval($_GET['total_fee']);      //获取总价格  
					$this->qiuxie_lottery->updatelotterylogpay($out_trade_no,$total_fee);
					if($_G['uid']>0){
						$this->qiuxie->updateusermoney($_G['uid'],$total_fee);
					}
					$msg='提示信息:<br/><h2 style="color:red;font-size:20px;">';
					$msg.='支付成功!';
					$msg.='</h2>';
					$msg.='<br/>3秒后跳转';
					$msg.='<script type="text/javascript">';
		        	$msg.='setTimeout("window.location.href=\''.$forward.'\'", 3000);';
		        	//$msg.='setTimeout("hideWindow(\'baoming\')", 4000);';
		        	$msg.='</script>';
        			showmessage ($msg,$forward, '','',1);
				}
			}
			
			$param=array ();
	        	$extra=array (
	        	'showmsg' => true ,
	        	'closetime'=>3,
	        	);
        	showmessage ( '错误!',$forward, $param,$extra);
        }
    }
	?>

lib_qiuxie_lottery.php

<?php
/*
* by 夜色紫宸風 2011-10-25
*/
require_once(APP_ROOT."./class/class_lib.php");
class lib_qiuxie_lottery extends app_lib{
	/**
	 * 抽奖次数
	 */
	private $num=0;
	/**
	 * n等奖品
	 */
	private $level=array();
	/**
	 * 奖品信息
	 */
	private $lottery=array();
	/**
	 * 抽奖开始时间
	 */
	private $lottery_star='2011-10-25';
	/**
	 * 抽奖结束时间
	 */
	private $lottery_end ='2011-10-27';
	/**
	 * 现在时间
	 */
	private $time= 0;
	/**
	 * 奖项
	 */
	private $qx_lottery="app_qiuxie_lottery";
	/**
	 * 获奖日志
	 */
	private $qx_lottery_log="app_qiuxie_lottery_log";
	/**
    	 * 秋蟹报名表
    	 */
	public $qx_user='app_qiuxie_user';
	/**随机数开始数
	 */
	private $min=1;
	/***
	 * 随机数结束位数
	 */
	private $max=100;
	/**
	 * 本次发放奖品总数
	 */
	private $sum=0;

	function __construct(){
		$this->setout();
    }
	public function setting($time,$star,$end){
		$this->lottery_star =$star;
		$this->lottery_end =$end;
		$this->time=$time;
		$this->setout();
		$this->setlottery();
		$this->setlevel();
	}
	/**
	 * 读取奖品信息
	 */
	private function setlottery(){
		$sql="SELECT * ";
		$sql.=" ,(select ifnull(lotteryid,0) from ".DB::table($this->qx_lottery)." where isrepeat='0' and isopen='1' ORDER BY listorder limit 0,1 ) as repeatid";
		$sql.=" ,(select l.sum-ifnull(count(*),0) from ".DB::table($this->qx_lottery_log)." where 1 and addtime<'".$this->time."' and isdisable='0' and lotteryid=l.lotteryid and isout='0'  ) as sum_over";
		$sql.=" FROM ".DB::table($this->qx_lottery)." as l ";
		$sql.=" WHERE isopen='1' ORDER BY listorder ";
		$data = DB::fetch_all($sql);
		foreach ($data as $r){
			$k=$r['lotteryid'];
			//总剩余
			//$r['sum_over']=$r['sum']-$this->getlotterycount(0,$this->time, $r['lotteryid']);
			if($r['sum_over']<1){
				continue;
			}
			//检测是否开始时间和结束时间区间内
			if($this->time<$r['start_time']||$this->time>$r['end_time']){
				continue;
			}

			//存在自定义时间
			if($r['minute']>0){
				//算出间隔时间
				$r['interval_time']=$r['minute']*60;
				//几个 时间短内发放
				$r['interval_time_num']=floor(($r['end_time']-$r['start_time'])/$r['interval_time']);
			}else{
				//算出间隔时间
				$r['interval_time']=ceil(($r['end_time']-$r['start_time'])/$r['sum']);
				//几个 时间短内发放
				$r['interval_time_num']=floor(($r['end_time']-$r['start_time'])/$r['interval_time']);
			}
			//存在 自定义 数量
			if($r['num']>0){
				//每次间隔,发放几个
				$r['interval_num']=$r['num'];
				//发放几次
				$r['interval_count']=floor($r['sum']/$r['interval_num']);
			}else{
				//每次间隔,发放几个
				$r['interval_num']=floor($r['sum']/$r['interval_time_num']);
				//发放几次
				$r['interval_count']=$r['interval_time_num'];
			}
			
			//本次属于第几次
			$r['this_num']=1;
			for($i=0;$i<$r['interval_time_num'];$i++){
				if(($r['start_time']+$i*$r['interval_time'])>$this->time){
					$r['this_num']=$i;
					break;
				}
			}
			
			//本次开始时间
			$r['this_time_star']=$r['start_time']+$r['this_num']*$r['interval_time']+1;
			//本次结束时间
			$r['this_time_end']=$r['start_time']+($r['this_num']+1)*$r['interval_time'];
			if($r['this_num']>1){
				//本次开始时间以前已经 获奖数 合计
				$r['this_time_star_lastcount']=$this->getlotterycount(0,$r['this_time_star'], $r['lotteryid']);
				//以前是否还有没有中奖的数
				$r['this_time_star_lastcount_over']=($r['this_num']-1)*$r['interval_num']-$r['this_time_star_lastcount'];
			}else{
				$r['this_time_star_lastcount']=0;
				$r['this_time_star_lastcount_over']=0;
			}

			//本次时间内已经获奖数
			$r['this_time_count']=$this->getlotterycount($r['this_time_star'],$r['this_time_end'], $r['lotteryid']);
			//本次时间内还有多少个
			$r['this_time_count_over']=$r['this_num']*$r['interval_num']-$r['this_time_count'];
		
			$this->lottery[$k]=$r;
			
			//本次奖品总数
			$this->sum +=$r['interval_num']>0?$r['interval_num']:0;
//			echo '间隔时间:'.ceil(($r['end_time']-$r['start_time'])/$r['sum']);
//			echo " 间隔时间次数:";
//			echo floor(($r['end_time']-$r['start_time'])/$r['interval_time']);
//			echo " 每次间隔时间发放个数:";
//			echo floor($r['sum']/$r['interval_time_num']);
//			echo "\n";
			
		}
		//计算出奖品概率,如果不存在,默认20%
		if(defined('QIUXIE_CJ_Probability')){
			$sum=$this->getbaoming();
			if($sum<1) $sum=1;
			$sum2=$this->getlottery();
			if($sum2<1) $sum2=1;
			$sum3=$sum2/$sum/QIUXIE_CJ_Probability;
			if($sum3>1) $sum3=1;
			$this->max=$this->sum/$sum3;
			
		}else{
			$this->max=$this->sum/0.2;
		}
	}
	/**
	 * 设置奖项标志位
	 */
	private function setlevel(){
		$max=$this->max;
		//上次如果没中,是否和本次一起放出,1一起,0否
		foreach ($this->lottery as $k=>$r){
			if($r['togetherlast']==1&&$r['this_time_star_lastcount_over']>0){
				$max+=$r['this_time_star_lastcount_over'];
			}
		}
		$min=$this->min;
		$this->max=$max;
		foreach ($this->lottery as $k=>$r){
			if($r['sum_over']<1){
				continue;
			}
			//增加获奖概率
			if($r['probability']<1){
				$r['probability']=1;
			}
			for ($i=1;$i<=$r['probability'];$i++){
				$j=0;
				do{
					$j=mt_rand($min, $max);
				}while (in_array($j, $this->level));
				$this->level[$k][]=$j;
			}
			//上次如果没中,是否和本次一起放出,1一起,0否
			if($r['togetherlast']==1&&$r['this_time_star_lastcount_over']>0){
				for($i=1;$i<=$r['this_time_star_lastcount_over'];$i++){
					$j=0;
					do{
						$j=mt_rand($min, $max);
					}while (in_array($j, $this->level));
					$this->level[$k][]=$j;
				}
			}
			
		}
	}
	/**
	 * 检测用户是否中奖 0 谢谢参与,其他 返回奖品data数据
	 * @param array $user array(uid,tel,ip)
	 */
	public  function checklevel($user){
		if(intval($user['uid'])<1){
			//没中奖
			return 0;
		}
		$winning=array();
		$tmp=$this->getuserwinningqiuxie($user['uid'],true);
		foreach ($tmp as $r){
			$winning[]=$r['lotteryid'];
		}
		
    	$data=array(
    		'uid'=>$user['uid'],
    		'tel'=>$user['tel'],
    		'ip'=>$user['ip'],
    		'addtime'=>time(),
    	);
    	$j=0;
    	for($i=1;$i<=10;$i++){
    		$j=mt_rand($thi->min, $this->max);
    	}
    	
    	$lotteryid=0;
    	foreach ($this->level as $k=>$r){
    		if(in_array($j, $r)){
    			if($this->lottery[$k]['isrepeat']==1&&in_array($k, $winning)){
    				$k=$this->lottery[$k]['repeatid'];
    			}
    			$lotteryid=$k;
    			break;
    		}
    	}
    	if($this->lottery[$lotteryid]['ismenpiao']==1){
    		$data['ismenpiao']=1;
    	}
    	$data['lotteryid']=$lotteryid;
    	$data['iswinning']=1;
    	DB::insert($this->qx_lottery_log,$data,true);
    	if($lotteryid>0){
    		return $this->lottery[$lotteryid];
    	}else{
    		return 0;
    	}
	}
	/**
	 * 已经获奖数
	 */
	public  function getlotterycount($start=0,$end=0,$lotteryid=0){
		
		$sql="select count(*) m from ".DB::table($this->qx_lottery_log);
		$sql .=" where 1 ";
		if(intval($start)>0){
			$sql .=" and addtime>='$start' ";
		}
		if(intval($end)>0){
			$sql .=" and addtime<'$end' ";
		}
		$sql .=" and isdisable='0' and lotteryid='$lotteryid'";
        return intval(DB::result_first($sql));
	}
	/**
	 * 用户抽奖次数
	 */
	public function getusercount($uid){
		$sql="select count(*) m from ".DB::table($this->qx_lottery_log);
		$sql .=" where 1 ";
		$start=QIUXIE_CJ_STAR;
		if(intval($start)>0){
			$sql .=" and addtime>='$start' ";
		}
		$end=QIUXIE_CJ_END;
		if(intval($end)>0){
			$sql .=" and addtime<'$end' ";
		}
		$sql .=" and isdisable='0' and uid='$uid'";
        return intval(DB::result_first($sql));
	}
	/**
	 * 查询用户中奖的奖品ID
	 */
	public function getuserwinning($uid){
		$sql="select lotteryid from ".DB::table($this->qx_lottery_log);
		$sql .=" where 1 ";
		$sql .=" and isdisable='0' and iswinning='1' and uid='$uid'";
		$data = DB::fetch_all($sql);
		$return=array();
		foreach ($data as $r){
			$return[]=$r['lotteryid'];
		}
        return $return;
	}
	/**
	 * 报名用户总数
	 */
	public function getbaoming(){
		$sql="select count(*) m from ".DB::table($this->qx_user);
		$sql .=" where 1 ";
		if(defined('QIUXIE_BM_STAR')){
			$sql .=" and addtime>='".QIUXIE_BM_STAR."' ";
		}
		if(defined('QIUXIE_BM_END')){
			$sql .=" and addtime<'".QIUXIE_BM_END."' ";
		}
        return intval(DB::result_first($sql));
	}
	/**
	 * 奖品总数
	 */
	public function getlottery(){
		$sql="select SUM(sum) m from ".DB::table($this->qx_lottery);
		$sql .=" where isopen='1' ";
        return intval(DB::result_first($sql));
	}
	/**
	 * 输出用户抽中门票
	 * @param intval $uid
	 * @param bool $bool 是否是门票
	 * @param int $isout 0没有过期1过期2全部
	 */
	public function getuserwinningqiuxie($uid,$bool=false,$isout=0){
		$sql="select l.*,y.name ";
		$sql .=" from ".DB::table($this->qx_lottery)." AS y";
		$sql .=" LEFT JOIN ".DB::table($this->qx_lottery_log)." as l";
		$sql .=" ON l.lotteryid=y.lotteryid ";
		$sql .=" where 1 and l.iswinning='1' and l.isdisable='0' ";
		$sql .=" and l.uid='$uid'";
		if($isout==2){
		}elseif($isout==1){
			$sql .=" and l.isout='1' ";
		}else{
			$sql .=" and l.isout='0' ";
		}
		if($bool){
			$sql .=" and l.ismenpiao='1' ";
		}
        return DB::fetch_all($sql);
	}
	public function paylog($uer){
		$g=$_GET;
		$data=array('uid'=>$user['uid'],
    		'tel'=>$user['tel'],
    		'ip'=>$user['ip'],
    		'addtime'=>time(),
		'notify_id'=>$g['notify_id'],
		'out_trade_no'=>$g['out_trade_no'],
		'trade_no'=>$g['trade_no'],
		'total_fee'=>$g['total_fee'],
		'trade_status'=>$g['trade_status'],
		'sign'=>$g['sign'],
		'query'=>$_SERVER['QUERY_STRING']
		);
		DB::insert('app_qiuxie_pay_log',$data,true);
	}
	/**
	 * 设置超时
	 */
	private function setout(){
		$data=array('isout'=>1);
		$time=$this->time>0?$this->time:time();
		$time=$time-3*3600;
		DB::update($this->qx_lottery_log,$data," iswinning='1' and ismenpiao='1' and recharge_time<'1' and addtime<'$time' ");
	}
	/**
	 * 设置支付
	 */
	public  function updatelotterylog($orderid,$money,$logid){
		$data=array('orderid'=>$orderid,
		'money'=>$money);
		$logid=intval($logid);
		DB::update($this->qx_lottery_log,$data," logid='$logid' ");
	}
	/**
	 * 设置支付成功
	 */
	public  function updatelotterylogpay($orderid,$money){
		$data=array('money'=>$money,'ispay'=>1,'recharge_time'=>time());
		DB::update($this->qx_lottery_log,$data," orderid='$orderid' ");
	}
	public function alipay_url($orderid,$subject,$logid){
		global $_G;
		$this->qiuxie_lottery->updatelotterylog($orderid,QIUXIE_MONEY,$logid);
		list($ec_contract, $ec_securitycode, $ec_partner, $ec_creditdirectpay) =
        	 explode("\t", authcode($_G['setting']['ec_contract'], 'DECODE', $_G['config']['security']['authkey']));
		$parameter = array(
			"service"         => 'create_direct_pay_by_user',
			"partner"         => $ec_partner,  
			"return_url"      => QIUXIE_URL. 'index.php?dir=qiuxie&do=zhifu',  
			"notify_url"      => QIUXIE_URL. 'index.php?dir=qiuxie&do=zhifu', 
			"_input_charset"  => 'utf-8',
			"subject"         => $subject,
			"body"            => QIUXIE_URL . "index.php?dir=qiuxie",
			"out_trade_no"    => $orderid,
			"price"       => QIUXIE_MONEY,  
			'quantity' 		=> 1,
			"payment_type"    => 1,
			"show_url"        => QIUXIE_URL . "index.php?dir=qiuxie",
			"seller_email"    => '111111111@163.com',
		);
		
		return self::trade_returnurl($parameter,$ec_securitycode);
	}
	private function trade_returnurl($args,$code) {
		ksort($args);
		$urlstr = $sign = '';
		foreach($args as $key => $val) {
			$sign .= '&'.$key.'='.$val;
			$urlstr .= $key.'='.rawurlencode($val).'&';
		}
		$sign = substr($sign, 1);
		$sign = md5($sign.$code);
		return 'https://www.alipay.com/cooperate/gateway.do?'.$urlstr.'sign='.$sign.'&sign_type=MD5';
	}
}?>

cache.class.php

<?php
/**
 * 写入缓存,默认为文件缓存,不加载缓存配置。
 * @param $name 缓存名称
 * @param $data 缓存数据
 * @param $filepath 数据路径   DISCUZ_ROOT.$filepath/
 */
function qsetcache($name, $data,$filepath='') {
	$cache= new cache_file('', $filepath);
	return $cache->set($name, $data);
}

/**
 * 读取缓存,默认为文件缓存,不加载缓存配置。
 * @param string $name 缓存名称
 * @param $filepath 数据路径   DISCUZ_ROOT.$filepath/
 */
function qgetcache($name, $filepath='') {
	$cache= new cache_file('',$filepath);
	return $cache->get($name);
}

/**
 * 删除缓存,默认为文件缓存,不加载缓存配置。
 * @param $name 缓存名称
 * @param $filepath 数据路径   DISCUZ_ROOT.$filepath/
 */
function qdelcache($name, $filepath='') {
	$cache= new cache_file('',$filepath);
	return $cache->delete($name);
}
class cache_file {
	
	/*缓存默认配置*/
	protected $_setting = array(
								'suf' => '.cache.php',	/*缓存文件后缀*/
								'type' => 'array',		/*缓存格式:array数组,serialize序列化,null字符串*/
							);
	
	/*缓存路径*/
	protected $filepath = '';

	/**
	 * 构造函数
	 * @param	array	$setting	缓存配置
	 * @return  void
	 */
	public function __construct($setting = '',$filepath='') {
		if(!defined('CACHE_PATH')){
			define('CACHE_PATH', DISCUZ_ROOT.'data/');
		}
		if(!empty($filepath)){
			$this->filepath=DISCUZ_ROOT.$filepath.'/';
		}
		$this->get_setting($setting);
	}
	
	/**
	 * 写入缓存
	 * @param	string	$name		缓存名称
	 * @param	mixed	$data		缓存数据
	 * @param	array	$setting	缓存配置
	 * @param	string	$module		所属模型
	 * @return  mixed				缓存路径/false
	 */

	public function set($name, $data, $setting = '', $module = 'qiuxie') {
		$this->get_setting($setting);
		$filepath = CACHE_PATH.'caches_'.$module.'/';
		if(!empty($this->filepath)){
			$filepath=$this->filepath;
		}
		$filename = $name.$this->_setting['suf'];
	    if(!is_dir($filepath)) {
			mkdir($filepath, 0777, true);
	    }
	    
	    if($this->_setting['type'] == 'array') {
	    	$data = "<?php\nreturn ".var_export($data, true).";\n?>";
	    } elseif($this->_setting['type'] == 'serialize') {
	    	$data = serialize($data);
	    }
	    
	    $file_size = file_put_contents($filepath.$filename, $data);
	    return $file_size ? $file_size : 'false';
	}
	
	/**
	 * 获取缓存
	 * @param	string	$name		缓存名称
	 * @param	array	$setting	缓存配置
	 * @param	string	$type		缓存类型
	 * @param	string	$module		所属模型
	 * @return  mixed	$data		缓存数据
	 */
	public function get($name, $setting = '', $module = 'qiuxie') {
		$this->get_setting($setting);
		$filepath = CACHE_PATH.'caches_'.$module.'/';
	if(!empty($this->filepath)){
			$filepath=$this->filepath;
		}
		$filename = $name.$this->_setting['suf'];
		if (!file_exists($filepath.$filename)) {
			return false;
		} else {
		    if($this->_setting['type'] == 'array') {
		    	$data = @require($filepath.$filename);
		    } elseif($this->_setting['type'] == 'serialize') {
		    	$data = unserialize(file_get_contents($filepath.$filename));
		    }
		    
		    return $data;
		}
	}
	
	/**
	 * 删除缓存
	 * @param	string	$name		缓存名称
	 * @param	array	$setting	缓存配置
	 * @param	string	$module		所属模型
	 * @return  bool
	 */
	public function delete($name, $setting = '', $module = 'qiuxie') {
		$this->get_setting($setting);
		$filepath = CACHE_PATH.'caches_'.$module.'/';
		if(!empty($this->filepath)){
			$filepath=$this->filepath;
		}
		$filename = $name.$this->_setting['suf'];
		if(file_exists($filepath.$filename)) {
			return @unlink($filepath.$filename) ? true : false;
		} else {
			return false;
		}
	}
	
	/**
	 * 和系统缓存配置对比获取自定义缓存配置
	 * @param	array	$setting	自定义缓存配置
	 * @return  array	$setting	缓存配置
	 */
	public function get_setting($setting = '') {
		if($setting) {
			$this->_setting = array_merge($this->_setting, $setting);
		}
	}

	public function cacheinfo($name, $setting = '', $module = 'qiuxie') {
		$this->get_setting($setting);
		$filepath = CACHE_PATH.'caches_'.$module.'/';
		if(!empty($this->filepath)){
			$filepath=$this->filepath;
		}
		$filename = $filepath.$name.$this->_setting['suf'];
		
		if(file_exists($filename)) {
			$res['filename'] = $name.$this->_setting['suf'];
			$res['filepath'] = $filepath;
			$res['filectime'] = filectime($filename);
			$res['filemtime'] = filemtime($filename);
			$res['filesize'] = filesize($filename);
			return $res;
		} else {
			return false;
		}
	}

}

?>

主程序都在这了。

抱歉!评论已关闭.