一。总体流程
1):管理员在后台修改积分规则
2):数据被写入数据表creditrule 中,并将数据写入缓存文件data/data_creditrule.php 中
3):用户发表文章或者进行其他操作的时候,通过getreward函数来获取奖罚积分
4):将数据写入用户数据库表space中,从而增加或减少用户积分
二。具体文件
1):UCHOME积分变动提示是通过footer.htm的showreward()来监控
2):showreward()在source/script_common.js中
function showreward() { if(Cookie.get('reward_notice_disable')) { return false; } var x = new Ajax(); x.get('do.php?ac=ajax&op=getreward', function(s){ if(s) { msgwin(s, 2000); } }); }
3):AJAX处理页面source/do_ajax.php中的getreward部分
elseif($op == 'getreward') { $reward = ''; if($_SCOOKIE['reward_log']) { $log = explode(',', $_SCOOKIE['reward_log']); if(count($log) == 2 && $log[1]) { @include_once(S_ROOT.'./data/data_creditrule.php'); $query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('creditlog')." WHERE clid='$log[1]'"); $creditlog = $_SGLOBAL['db']->fetch_array($query); $rule = $_SGLOBAL['creditrule'][$log[0]]; $rule['cyclenum'] = $rule['rewardnum']? $rule['rewardnum'] - $creditlog['cyclenum'] : 0; } ssetcookie('reward_log', ''); } }
三。关键函数
//获取指定动作能获得多少积分 function getreward($action, $update=1, $uid=0, $needle='', $setcookie = 1) { global $_SGLOBAL, $_SCOOKIE; $credit = 0; $reward = array( 'credit' => 0, 'experience' => 0 ); $creditlog = array(); @include_once(S_ROOT.'./data/data_creditrule.php'); $rule = $_SGLOBAL['creditrule'][$action]; if($rule['credit'] || $rule['experience']) { $uid = $uid ? intval($uid) : $_SGLOBAL['supe_uid']; if($rule['rewardtype']) { //增加积分 $query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('creditlog')." WHERE uid='$uid' AND rid='$rule[rid]'"); $creditlog = $_SGLOBAL['db']->fetch_array($query); if(empty($creditlog)) { $reward['credit'] = $rule['credit']; $reward['experience'] = $rule['experience']; $setarr = array( 'uid' => $uid, 'rid' => $rule['rid'], 'total' => 1, 'cyclenum' => 1, 'credit' => $rule['credit'], 'experience' => $rule['experience'], 'dateline' => $_SGLOBAL['timestamp'] ); //判断是否需要去重 if($rule['norepeat']) { if($rule['norepeat'] == 1) { $setarr['info'] = $needle; } elseif($rule['norepeat'] == 2) { $setarr['user'] = $needle; } elseif($rule['norepeat'] == 3) { $setarr['app'] = $needle; } } if(in_array($rule['cycletype'], array(2,3))) { $setarr['starttime'] = $_SGLOBAL['timestamp']; } $clid = inserttable('creditlog', $setarr, 1); } else { $newcycle = false; $setarr = array(); $clid = $creditlog['clid']; switch($rule['cycletype']) { case 0: //一次性奖励 break; case 1: //每天限次数 case 4: //不限周期 $sql = 'cyclenum+1'; if($rule['cycletype'] == 1) { $today = sstrtotime(sgmdate('Y-m-d')); //判断是否为昨天 if($creditlog['dateline'] < $today && $rule['rewardnum']) { $creditlog['cyclenum'] = 0; $sql = 1; $newcycle = true; } } if(empty($rule['rewardnum']) || $creditlog['cyclenum'] < $rule['rewardnum']) { //验证是否为需要去重操作 if($rule['norepeat']) { $repeat = checkcheating($creditlog, $needle, $rule['norepeat']); if($repeat && !$newcycle) { return $reward; } } $reward['credit'] = $rule['credit']; $reward['experience'] = $rule['experience']; //更新次数 $setarr = array( 'cyclenum' => "cyclenum=$sql", 'total' => 'total=total+1', 'dateline' => "dateline='$_SGLOBAL[timestamp]'", 'credit' => "credit='$reward[credit]'", 'experience' => "experience='$reward[experience]'", ); } break; case 2: //整点 case 3: //间隔分钟 $nextcycle = 0; if($creditlog['starttime']) { if($rule['cycletype'] == 2) { //上一次执行时间 $start = sstrtotime(sgmdate('Y-m-d H:00:00', $creditlog['starttime'])); $nextcycle = $start+$rule['cycletime']*3600; } else { $nextcycle = $creditlog['starttime']+$rule['cycletime']*60; } } if($_SGLOBAL['timestamp'] <= $nextcycle && $creditlog['cyclenum'] < $rule['rewardnum']) { //验证是否为需要去重操作 if($rule['norepeat']) { $repeat = checkcheating($creditlog, $needle, $rule['norepeat']); if($repeat && !$newcycle) { return $reward; } } $reward['experience'] = $rule['experience']; $reward['credit'] = $rule['credit']; $setarr = array( 'cyclenum' => "cyclenum=cyclenum+1", 'total' => 'total=total+1', 'dateline' => "dateline='$_SGLOBAL[timestamp]'", 'credit' => "credit='$reward[credit]'", 'experience' => "experience='$reward[experience]'", ); } elseif($_SGLOBAL['timestamp'] >= $nextcycle) { $newcycle = true; $reward['experience'] = $rule['experience']; $reward['credit'] = $rule['credit']; $setarr = array( 'cyclenum' => "cyclenum=1", 'total' => 'total=total+1', 'dateline' => "dateline='$_SGLOBAL[timestamp]'", 'credit' => "credit='$reward[credit]'", 'starttime' => "starttime='$_SGLOBAL[timestamp]'", 'experience' => "experience='$reward[experience]'", ); } break; } //记录操作历史 if($rule['norepeat'] && $needle) { switch($rule['norepeat']) { case 0: break; case 1: //信息去重 $info = empty($creditlog['info'])||$newcycle ? $needle : $creditlog['info'].','.$needle; $setarr['info'] = "`info`='$info'"; break; case 2: //用户去重 $user = empty($creditlog['user'])||$newcycle ? $needle : $creditlog['user'].','.$needle; $setarr['user'] = "`user`='$user'"; break; case 3: //应用去重 $app = empty($creditlog['app'])||$newcycle ? $needle : $creditlog['app'].','.$needle; $setarr['app'] = "`app`='$app'"; break; } } if($setarr) { $_SGLOBAL['db']->query("UPDATE ".tname('creditlog')." SET ".implode(',', $setarr)." WHERE clid='$creditlog[clid]'"); } } if($setcookie && $uid = $_SGLOBAL['supe_uid']) { //其中有新值时才重写cookie值 if($reward['credit'] || $reward['experience']) { $logstr = $action.','.$clid; ssetcookie('reward_log', $logstr); $_SCOOKIE['reward_log'] = $logstr; } } } else { //扣除积分 $reward['credit'] = "-$rule[credit]"; $reward['experience'] = "-$rule[experience]"; } if($update && ($reward['credit'] || $reward['experience'])) { $setarr = array(); if($reward['credit']) { $setarr['credit'] = "credit=credit+$reward[credit]"; } if($reward['experience']) { $setarr['experience'] = "experience=experience+$reward[experience]"; } $_SGLOBAL['db']->query("UPDATE ".tname('space')." SET ".implode(',', $setarr)." WHERE uid='$uid'"); } } return array('credit'=>abs($reward['credit']), 'experience' => abs($reward['experience'])); }