首页 » 二次开发 » 阅读文章
为ECSHOP增加积分计划,主程序
最近网站要增加积分计划,让用户有更多地方可以赚取积分,首先放出主程序。如果有BUG,就请大家留言给我。
<?php
/*
*用户积分计划
*/
if (!defined(‘IN_ECS’)){
die(‘Hacking attempt’);
}
//增加用户积分
function add_integral($type_id=1,$user_id=0){
$user_id=($user_id==0)?$_SESSION[‘user_id’]:$user_id;
if($user_id>0){
$t=cat_integral($type_id);
$sql=”SELECT COUNT(*) FROM “. $GLOBALS[‘ecs’]->table(‘integral’) .” WHERE user_id='”.$user_id.”‘ AND type_id=’$type_id'”;
$num=$GLOBALS[‘db’]->getOne($sql);
if($num>0){
$sql=”SELECT * FROM “. $GLOBALS[‘ecs’]->table(‘integral’) .” WHERE user_id='”.$user_id.”‘ AND type_id=’$type_id'”;
$u=$GLOBALS[‘db’]->getRow($sql);
$time=0;
//计算时间
if($t[‘type_time’]==1){//每年
$time=(int)local_date(‘Y’)-(int)local_date(‘Y’,$u[‘times’]);
}elseif($t[‘type_time’]==2){//每月
$time=(int)local_date(‘Ym’)-(int)local_date(‘Ym’,$u[‘times’]);
}elseif($t[‘type_time’]==3){//每天
$time=(int)local_date(‘Ymd’)-(int)local_date(‘Ymd’,$u[‘times’]);
}elseif($t[‘type_time’]==4){//半天,即5点为分界线
$time=(int)local_date(‘Ymd’)-(int)local_date(‘Ymd’,$u[‘times’]);
$u[‘num’]=(local_date(‘H’)>16)?(local_date(‘H’,$u[‘times’])>16?$t[‘type_num’]:$u[‘num’]):$t[‘type_num’];
}
$u[‘num’]=($time>0)?0:$u[‘num’];
if($u[‘num’]<$t[‘type_num’]){
$sql=”UPDATE “. $GLOBALS[‘ecs’]->table(‘integral’) .” SET num='”.($u[‘num’]+1).”‘,times='”.gmtime().”‘ WHERE user_id='”.$user_id.”‘ AND type_id=’$type_id'”;
$GLOBALS[‘db’]->query($sql);
update_integral($t[‘type_score’],$user_id);
jilu_integral($user_id,$t[‘type_score’],$t[‘type_name’]);
}
}else{
$sql=”INSERT INTO “. $GLOBALS[‘ecs’]->table(‘integral’) .” (`user_id`, `type_id`, `num`, `times`) VALUES (‘”.$user_id.”‘, ‘$type_id’, ‘1’, ‘”.gmtime().”‘);”;
$GLOBALS[‘db’]->query($sql);
update_integral($t[‘type_score’],$user_id);
jilu_integral($user_id,$t[‘type_score’],$t[‘type_name’]);
}
}
}
//记录积分日志
function jilu_integral($user_id,$pay_points,$change_desc){
$sql=”INSERT INTO “. $GLOBALS[‘ecs’]->table(‘account_log’) .” (`user_id`, `pay_points`, `change_desc`, `change_type`, `change_time`) VALUES (‘$user_id’, ‘$pay_points’, ‘$change_desc’,’99’, ‘”.gmtime().”‘);”;
$GLOBALS[‘db’]->query($sql);
}
//积分分类
function cat_integral($type_id=1){
$sql=”SELECT * FROM “. $GLOBALS[‘ecs’]->table(‘integral_type’) .” WHERE type_id=’$type_id'”;
return $GLOBALS[‘db’]->getRow($sql);
}
//积分更新到用户表
function update_integral($num=0,$user_id=0){
$sql=”UPDATE “. $GLOBALS[‘ecs’]->table(‘users’) .” SET pay_points=pay_points+$num WHERE user_id=’$user_id'”;
$GLOBALS[‘db’]->query($sql);
}
//登录送积分
function login_zf(){
add_integral(2);
if(local_date(‘m’)==18){
add_integral(4);
}
}
//删除评论扣积分
function del_integral($id=0){
$sql=”SELECT user_id FROM “. $GLOBALS[‘ecs’]->table(‘comment’) .” WHERE comment_id=’$id'”;
$user_id=$GLOBALS[‘db’]->getOne($sql);
if($user_id>0){
$t=cat_integral();
$num=$t[‘type_score’]*(-2);
update_integral($num,$user_id);
jilu_integral($user_id,$num,’删除评论扣积分’);
}
}
//会员生日自动送积分
function birthday_integral(){
$day=local_date(‘d’);
$month=local_date(‘m’);
$sql=”SELECT user_id FROM “.$GLOBALS[‘ecs’]->table(‘users’).” WHERE day(birthday)=’$day’ AND month(birthday)=’$month’ “;
$users=$GLOBALS[‘db’]->getAll($sql);
foreach($users as $value){
add_integral(3,$value[‘user_id’]);
}
}
//会员等级自动降级
function auto_syrating(){
//找出最近90天内买过产品的用户ID
$time=gmtime()-90*24*3600;
$sql=’SELECT user_id FROM ‘.$GLOBALS[‘ecs’]->table(‘order_info’).” WHERE order_status IN (‘1′,’5′,’6′) AND user_id>’0′ AND add_time>’$time’ GROUP BY user_id”;
$userid=$GLOBALS[‘db’]->getAll($sql);
$in=”;
foreach($userid as $value){
$in.=$in ? “,’$value[user_id]'” : “‘$value[user_id]'”;
}
$sql=’UPDATE ‘. $GLOBALS[‘ecs’]->table(‘users’) .” SET rank_points=FLOOR(rank_points*0.99) WHERE rank_points>0 AND user_id NOT IN ($in)”;
$GLOBALS[‘db’]->query($sql);
}
//重新计算用户购物送消费积分
function buy_syzf($userid=0,$pay_points=0){
$sql=”SELECT user_rank,rank_points FROM “.$GLOBALS[‘ecs’]->table(‘users’).” WHERE user_id=’$userid'”;
$scale=1;
if ($row = $GLOBALS[‘db’]->getRow($sql)){
if($row[‘user_rank’]==0){
$sql = ‘SELECT rank_id, integral_scale FROM ‘ . $GLOBALS[‘ecs’]->table(‘user_rank’) . ” WHERE special_rank = ‘0’ AND min_points <= ” . intval($row[‘rank_points’]) . ‘ AND max_points > ‘ . intval($row[‘rank_points’]);
if($row = $GLOBALS[‘db’]->getRow($sql)){
$scale=$row[‘integral_scale’];
}
}else{
$sql = ‘SELECT rank_id, integral_scale FROM ‘ . $GLOBALS[‘ecs’]->table(‘user_rank’) . ” WHERE rank_id = ‘$row[user_rank]'”;
if($row = $GLOBALS[‘db’]->getRow($sql)){
$scale=$row[‘integral_scale’];
}
}
}
return $pay_points*$scale;
}
//最近10条积分详细
function list_integral(){
$sql=”SELECT pay_points,change_time,change_desc FROM “.$GLOBALS[‘ecs’]->table(‘account_log’).” WHERE user_id='”.$_SESSION[‘user_id’].”‘ ORDER BY log_id DESC LIMIT 10″;
$res=$GLOBALS[‘db’]->getAll($sql);
foreach($res as $key=>$row){
$rest[$key]=$row;
$rest[$key][‘time’].=local_date(‘Y-m-d H:m:s’,$row[‘change_time’]);
}
return $rest;
}
?>
评论 共0条 (RSS 2.0) 发表评论