PHP每日签到及连续签到奖励实现示例

数据库字段

num 记录已经连续签到次数

times 记录签到的日期 格式年月日 如 20160101

PHP代码如下

<?php
//获取今天的日期
$today = date('Ymd');
//获取签到记录
$signInfo = M('table')->field('id,num,times')->where(array('uid' => session('uid')))->find(); if($signInfo['times'] == $today){//今天已签到过了
$this->ajaxReturn(array('info' => '今天已签到过了', 'status' => 0)); }elseif($signInfo['times'] == date('Ymd', strtotime('-1 day'))){//昨天已签到,连续签到处理
if ($signInfo['num'] == 29) {//连续签到30天,每30天清零
$num = 0;//连续签到次数
$point = 30;//额外奖励积分数
}elseif($signInfo['num'] == 20){//连续签到3周
$num = $signInfo['num'] + 1; $point = 20;
}elseif($signInfo['num'] == 13){//连续签到2周
$num = $signInfo['num'] + 1; $point = 15;
}elseif($signInfo['num'] == 6){//连续签到1周
$num = $signInfo['num'] + 1; $point = 10;
}else{
$num = $signInfo['num'] + 1; $point = 0;
}
//更新签到记录
$result = M('table')->save(array('uid' => session('uid'), 'num' => $num, 'times' => $today)); }else{//断签或未签到过,重新计数
$point = 0;
if ($signInfo['id']) {//有签到记录,更新记录信息
$result = M('table')->save(array('uid' => session('uid'), 'num' => 1, 'times' => $today));
}else{//无签到记录,添加一条记录
$result = M('table')->add(array('uid' => session('uid'), 'num' => 1, 'times' => $today));
}
} if ($result){
$points = $point + 3;//本次签到获得的总积分数 = 额外奖励 + 签到一次应得的积分数 此处为3分
$result = M()->execute('update users set total_point=total_point+' . $points . ',now_point=now_point+' . $points . ' where uid=' . session('uid'));
if($result){
$msg = '签到成功,获得3积分';
$msg .= $point > 0 ? '<br />连续签到' . ($num > 0 ? $num : 30) . '天,奖励' . $point . '积分' : '';
$this->ajaxReturn(array('info' => $msg, 'status' => 1));
}
}
$this->ajaxReturn(array('info' => '签到失败', 'status' => 0));

代码是根据ThinkPHP框架开发,在项目代码的基础上进行了修改,项目代码运行正常,修改后的上述代码没有实际运行过,只作为开发思路参考

上一篇:(转)RSA算法原理


下一篇:彻底理解RSA算法原理