自用thinkphp签到送积分方法,不含前台展示,只判断了今日有没有签到,如需要可自行添加
1、先附上表结构,自用的]/**<br>
* tableName = 前缀_user_sing_in<br>
* id<br>
* uid<br>
* username<br>
* create_time int 10 签到时间<br>
* sign_in_year 签到,年<br>
* sign_in_month 签到,月<br>
* sign_in_day 签到,日<br>
* get_integral 签到获得积分数<br>
* sign_in_con_days 连续签到天数 default=1<br>
*/
2、签到实现方法及类<?php <br />
/**<br>
* Created by PhpStorm.<br>
* Author: 醉丶春风<br>
* Description: 本类必须登录,用户积分兑换,签到<br>
* Date: 2016/10/19<br>
* Time: 16:02<br>
*/<br>
namespace Home\Controller;<br>
//use Think\Controller;<br>
class IntegralController extends HomeController<br>
{<br>
private $uid = null;<br>
private $where = array();<br>
/**<br>
* 构造方法<br>
* 自动加载uid,初始化uid条件<br>
*/<br>
public function _initialize()<br>
{<br>
$this -> uid = $this -> check_login();<br>
$this -> where['uid'] = $this->uid;<br>
}<br>
<br>
/**<br>
* 签到获得积分方法<br>
*/<br>
public function sign_in()<br>
{<br>
$get_integral = 2; //签到赠送积分<br>
$get_added_integral = 5; //连续签到N天额外赠送积分<br>
$continuous_day = 7; //连续签到多少天可领额外积分<br>
//**************************************************************<br>
$start_time = strtotime(date('Y-m-d'));//今天凌晨的时间戳<br>
$year = date("Y"); //今天-年<br>
$month = date("m"); //今天-月<br>
$day = date("d"); //今天-日<br>
//**************************************************************<br>
$yesterday = strtotime('-1 day'); //昨天的时间戳基数<br>
$yesterday_year = date('Y',$yesterday); //昨天-年<br>
$yesterday_month = date('m',$yesterday); //昨天-月<br>
$yesterday_day = date('d',$yesterday); //昨天-日<br>
//**************************************************************<br>
//先验证今天有没有签到过<br>
$Model = M('user_sign_in');<br>
$record_count = $Model -> where(array('uid'=>$this->uid,'create_time'=>array('EGT',$start_time))) -> count();<br>
if($record_count)<br>
$this -> error('今日已签到');<br>
//**************************************************************<br>
//准备好插入数据<br>
$data = array(<br>
'uid' => $this -> uid,<br>
'sign_in_year' => $year, //年<br>
'sign_in_month' => $month,//月<br>
'sign_in_day' => $day,//日<br>
'username' => $_SESSION['userInfo']['username'],<br>
'get_integral' => $get_integral, //默认签到获得积分<br>
'create_time' => time(),<br>
);<br>
$msg = "签到成功,获得{$get_integral}积分";<br>
//**************************************************************<br>
//查询昨天的签到纪录<br>
$where = array(<br>
'uid' => $this -> uid,<br>
'sign_in_year' => $yesterday_year,<br>
'sign_in_month' => $yesterday_month,<br>
'sign_in_day' => $yesterday_day,<br>
);<br>
$yesterday_record = $Model -> where($where) -> find();<br>
//有,设置连续签到天数加1,继续判断连续签到有没有达标<br>
if($yesterday_record){<br>
//设置连续签到天数加1<br>
$data['sign_in_con_days'] = $yesterday_record['sign_in_con_days'] +1;<br>
if($yesterday_record['sign_in_con_days'] >= $continuous_day){<br>
//已 经达到连续签到要求,加送额外积分<br>
$data['get_integral'] = $get_integral + $get_added_integral;<br>
$msg = "签到成功,获得{$get_integral}积分,连续签到{$continuous_day}天额外获得{$get_added_integral}积分";<br>
}<br>
}<br>
//**************************************************************<br>
//昨日没有签到,不进行逻辑处理,执行签到送积分,默认连续签到天数为1<br>
if($Model -> add($data)){<br>
//用户积分增加<br>
M('user') -> where($this->where) -> setInc('integral',$data['get_integral']);<br>
$this -> success($msg,U('Index/index'),3);<br>
}<br>
$this -> error('签到失败');<br>
}<br>
<br>
}
自己使用的,调用的时候直接调用 sign_in()方法即可
验证今日是否已签到方法/**<br>
* 登录导航<br>
* 验证今日是否已签到<br>
*/<br>
public function login_nav(){<br>
$start_time = strtotime(date('Y-m-d'));//今天凌晨的时间戳<br>
$is_sign_in = 0;//初始化未签到<br>
//先验证今天有没有签到过<br>
$Model = M('user_sign_in');<br>
//签到纪录<br>
$sign_in_record = $Model -> where(array('uid'=>$_SESSION['userInfo']['id'],'create_time'=>array('EGT',$start_time))) -> find();<br>
if($sign_in_record){<br>
$is_sign_in = 1;<br>
//根据需要传入 $sign_in_record 或连续签到天数<br>
$this -> assign('sign_in_con_days',$sign_in_record['sign_in_con_days']);<br>
}<br>
$this -> assign('is_sign_in',$is_sign_in);<br>
//自用<br>
$this->display('Widget/login_nav');<br>
}
因为我的 error 和 success 方法中增加了 exit; 所以 上述代码中 直接 $this -> error('错误');下面可不用终止代码,大家在用的时候要考虑到自己的代码是否增加 exit 或 die;
也写在我的博客里面了
http://www.xstnet.com/article-79.html
http://www.xstnet.com/article-79.html
自己使用没问题