thinkPHP實(shí)現(xiàn)簽到功能的方法
本文實(shí)例講述了thinkPHP實(shí)現(xiàn)簽到功能的方法。分享給大家供大家參考,具體如下:
數(shù)據(jù)表:
CREATE TABLE `members_sign` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `uid` int(11) unsigned NOT NULL COMMENT '用戶id', `days` tinyint(2) unsigned NOT NULL DEFAULT '0' COMMENT '連續(xù)簽到的天數(shù)', `is_share` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否分享過', `is_sign` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否簽到過', `stime` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '簽到的時(shí)間', `atime` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '添加時(shí)間', PRIMARY KEY (`id`), KEY `index_uid` (`uid`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=162 DEFAULT CHARSET=utf8 COMMENT='簽到分享表';
Controller:
<?php
namespace Member\Controller;
use Member\Controller\MController;
class IndexController extends MController {
/**
* 用戶中心
* @param
*/
public function index(){
$pre = C('DB_PREFIX');
// 日歷列表
$monthSign = $this->getMonthSign();
$dayList = $this->showDays($monthSign);
// 今天簽到
$data = $this->todayData();
if($data['is_sign'] == 1){
$this->assign('isSign',true);
}
$this->display();
}
/**
* 執(zhí)行當(dāng)天簽到
* @return json 簽到成功返回 {status:1,info:'已簽到'}
*/
public function sign(){
$todayData = $this->todayData();
if($todayData['is_sign'] == 1){
$this->successMsg('已簽到');
}else{
$data = $this->getInsertData($this->uid);
// 無今天數(shù)據(jù)
if($todayData == NULL){
$data['uid'] = $this->uid;
$data['atime'] = time();
$id = M('members_sign')->add($data);
}else{
$save = M('members_sign')->where("id = {$todayData['id']}")->save($data);
}
if($id or $save){
$score = $this->getTodayScores($data['days']);
// 為該用戶添加積分
addScore($this->uid,$score);
$this->successMsg('已簽到',array('score' => $score,'days'=>$data['days']));
}else{
$this->errorMsg('簽到失敗,請刷新后重試!');
}
}
}
/**
* 返回每次簽到要插入的數(shù)據(jù)
*
* @param int $uid 用戶id
* @return array(
* 'days' => '天數(shù)',
* 'is_sign' => '是否簽到,用1表示已經(jīng)簽到',
* 'stime' => '簽到時(shí)間',
* );
*/
protected function getInsertData($uid){
// 昨天的連續(xù)簽到天數(shù)
$start_time = strtotime(date('Y-m-d 0:0:0',time()-86400))-1;
$end_time = strtotime(date('Y-m-d 23:59:59',time()-86400))+1;
$days = M('members_sign')->where("uid = $uid and atime > $start_time and atime < $end_time")->getField('days');
if($days){
$days++;
if($days > 30){
$days = 1;
}
}else{
$days = 1;
}
return array(
'days' => $days,
'is_sign' => 1,
'stime' => time()
);
}
/**
* 用戶當(dāng)天簽到的數(shù)據(jù)
* @return array 簽到信息 is_sign,stime 等
*/
protected function todayData(){
$time = time();
$start_stime = strtotime(date('Y-m-d 0:0:0',$time))-1;
$end_stime = strtotime(date('Y-m-d 23:59:59',$time))+1;
return M('members_sign')->field('atime',true)->where("uid = {$this->uid} and atime > $start_stime and atime < $end_stime")->find();
}
/**
* 積分規(guī)則,返回連續(xù)簽到的天數(shù)對應(yīng)的積分
*
* @param int $days 當(dāng)天應(yīng)該得的分?jǐn)?shù)
* @return int 積分
*/
protected function getTodayScores($days){
if($days == 30){
return 50;
}else if($days > 19){
return 8;
}else if($days > 9){
return 5;
}else{
return 3;
}
}
/**
* 顯示簽到列表
*
* @param array $signDays 某月簽到的日期 array(1,2,3,4,5,12,13)
* @param int $year 可選,年份
* @param int $month 可選,月份
* @return string 日期列表<li>1</li>....
*/
protected function showDays($signDays,$year,$month){
$time = time();
$year = $year ? $year : date('Y',$time);
$month = $month ? $month : date('m',$time);
$daysTotal = date('t', mktime(0, 0, 0, $month, 1, $year));
$now = date('Y-m-d',$time);
$str = '';
for ($j = 1; $j <= $daysTotal; $j++) {
$i++;
$someDay = date('Y-m-d',strtotime("$year-$month-$j"));
// 小于今天的日期樣式
if ($someDay <= $now){
// 當(dāng)天日期樣式 tdc = todayColor
if($someDay == $now){
// 當(dāng)天簽到過的
if(in_array($j,$signDays)){
$str .= '<li class="current fw tdc">'.$j.'</li>';
}else{
$str .= '<li class="today fw tdc">'.$j.'</li>';
}
}else{
// 簽到過的日期樣式 current bfc = beforeColor , fw = font-weight
if(in_array($j,$signDays)){
$str .= '<li class="current fw bfc">'.$j.'</li>';
}else{
$str .= '<li class="fw bfc">'.$j.'</li>';
}
}
}else{
$str .= '<li>'.$j.'</li>';
}
}
return $str;
}
/**
* 獲取當(dāng)月簽到的天數(shù),與 $this->showDays() 配合使用
* @return 當(dāng)月簽到日期 array(1,2,3,4,5,12,13)
*/
protected function getMonthSign(){
$time = time();
$year = date('Y',$time);
$month = date('m',$time);
$day = date("t",strtotime("$year-$month"));
$start_stime = strtotime("$year-$month-1 0:0:0")-1;
$end_stime = strtotime("$year-$month-$day 23:59:59")+1;
$list = M('members_sign')->where("uid = {$this->uid} and stime > $start_stime and stime < $end_stime")->order('stime asc')->getField('stime',true);
foreach ($list as $key => $value){
$list[$key] = date('j',$value);
}
return $list;
}
}
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》、《smarty模板入門基礎(chǔ)教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。
- tp5(thinkPHP5)框架數(shù)據(jù)庫Db增刪改查常見操作總結(jié)
- tp5(thinkPHP5)框架實(shí)現(xiàn)多數(shù)據(jù)庫查詢的方法
- thinkPHP5實(shí)現(xiàn)數(shù)據(jù)庫添加內(nèi)容的方法
- tp5(thinkPHP5)框架連接數(shù)據(jù)庫的方法示例
- thinkPHP5框架數(shù)據(jù)庫連貫操作之cache()用法分析
- thinkPHP5框架實(shí)現(xiàn)多數(shù)據(jù)庫連接,跨數(shù)據(jù)連接查詢操作示例
- Thinkphp5框架實(shí)現(xiàn)獲取數(shù)據(jù)庫數(shù)據(jù)到視圖的方法
- ThinkPHP5.1框架數(shù)據(jù)庫鏈接和增刪改查操作示例
- PHP利用pdo_odbc實(shí)現(xiàn)連接數(shù)據(jù)庫示例【基于ThinkPHP5.1搭建的項(xiàng)目】
- 基于ThinkPHP5框架使用QueryList爬取并存入mysql數(shù)據(jù)庫操作示例
- ThinkPHP5.0框架實(shí)現(xiàn)切換數(shù)據(jù)庫的方法分析
- TP5框架實(shí)現(xiàn)簽到功能的方法分析
相關(guān)文章
詳解laravel passport OAuth2.0的4種模式
這篇文章主要介紹了laravel passport OAuth2.0的4種模式,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
laravel實(shí)現(xiàn)登錄時(shí)監(jiān)聽事件,添加登錄用戶的記錄方法
今天小編就為大家分享一篇laravel實(shí)現(xiàn)登錄時(shí)監(jiān)聽事件,添加登錄用戶的記錄方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
不使用php api函數(shù)實(shí)現(xiàn)數(shù)組的交換排序示例
這篇文章主要介紹了不使用php api函數(shù)實(shí)現(xiàn)數(shù)組的交換排序示例,需要的朋友可以參考下2014-04-04
Yii2實(shí)現(xiàn)自定義獨(dú)立驗(yàn)證器的方法
這篇文章主要介紹了Yii2實(shí)現(xiàn)自定義獨(dú)立驗(yàn)證器的方法,結(jié)合實(shí)例形式分析了Yii2自定義獨(dú)立驗(yàn)證器的實(shí)現(xiàn)與使用方法,需要的朋友可以參考下2017-05-05

