自用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 HomeController;<br>
//use ThinkController;<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(time'=>array('EGT',$start_time))) -> count();
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
自己用沒問題