TP5實現簽到功能


 基於tp5 模型的一個簽到功能;

由於存儲所有的簽到日期數據庫會非常龐大,所以簽到日期只存儲近三個月的。

具體功能:

1、記錄最近一次的簽到時間

2、每次簽到都會添加15積分

3、有連續簽到的記錄

 1 CREATE TABLE `sp_sign` (
 2   `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主鍵',
 3   `times` datetime DEFAULT NULL COMMENT '最近一次簽到時間',
 4   `userid` int(11) DEFAULT NULL COMMENT '用戶id',
 5   `days` tinyint(6) NOT NULL DEFAULT '0' COMMENT '連續簽到的天數',
 6   `number` decimal(10,0) NOT NULL DEFAULT '0' COMMENT '當月簽到給的積分',
 7   `one` varchar(255) DEFAULT NULL COMMENT '當月簽到的日期,用“,”隔開',
 8   `two` varchar(255) DEFAULT NULL COMMENT '上個月簽到的日期,用“,”隔開',
 9   `three` varchar(255) DEFAULT NULL COMMENT '上上個月簽到的日期,用“,”隔開',
10   PRIMARY KEY (`id`)
11 ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
12 /**
13      * 用戶簽到
14      * @param array $userid 用戶id
15      */
16     public function add($userid)
17     {
18             $data = Db::name('sign')->where('userid',$userid)->select();
19             if(count($data) == 0)  //沒有該用戶的簽到記錄
20             {
21                 $query4 = Db::name('sign')->insert(['times'=>date('Y-m-d H:i:s'),'userid'=>$userid,'days'=>1,'number'=>'15','one'=>date('d',time())]);
22                 return 1;
23             }
24             else
25             {
26                 //判斷今天是否簽到
27                 $todayBegin=date('Y-m-d'." 00:00:00");
28                 $todayEnd= date('Y-m-d'." 23:59:59");
29                 $isexit = Db::name('sign')->field('times')->where(['userid'=>$userid])->where('times','between',[$todayBegin,$todayEnd])->select();
30                 if(count($isexit) == 1)   //今日已簽到
31                 {
32                     return 0;
33                 }
34                 else    //今日未簽到
35                 {
36                     $times = Db::name('sign')->where('userid',$userid)->field('times')->select();
37                     $time = strtotime($times[0]['times']);
38 
39                     if((time()-$time > 24*60*60))       //上次簽到時間大於24小時,連續簽到天數清零
40                     {
41                         $query = Db::name('sign')->where('userid',$userid)->update(['days'=>1]);
42                     }
43                     else     //上次簽到時間小於24小時,連續簽到次數加1
44                     {
45                         $query = Db::name('sign')->where('userid',$userid)->setInc('days');
46                     }
47                     //更新上次簽到時間和簽到積分
48                     $query1 = Db::name('sign')->where('userid',$userid)->update(['times'=>date('Y-m-d H:i:s')]);
49                     $query2 = Db::name('sign')->where('userid',$userid)->setInc('number', 15);
50 
51                     $sqldate = date('m',$time);    //上次簽到日期的月份
52                     $nowdate = date('m',time());  //當前月份
53                     //記錄本次簽到日期
54                     if($sqldate != $nowdate)  //上次簽到日期與本次簽到日期月份不一樣
55                     {
56                         $oldtime = $times[0]['times'];
57                         $onetime=date("Y-m-d H:i:s", strtotime("-1 month"));  //獲取前1個月的時間,獲取格式為2016-12-30 13:26:13
58                         $twotime=date("Y-m-d H:i:s", strtotime("-2 month"));  //獲取前2個月的時間
59                         $threetime=date("Y-m-d H:i:s", strtotime("-3 month"));  //獲取前3個月的時間
60 
61                         $rs = Db::name('sign')->where('userid',$userid)->field('one,two,three')->select();
62 
63                         if($oldtime < $onetime && $oldtime >= $twotime)     //月份間隔 大於1個月,小於2個月
64                         {
65                             $one =  date('d',time());
66                             $two = $rs[0]['one'];
67                             $three = $rs[0]['two'];
68                         }
69                         elseif($oldtime < $twotime && $oldtime >= $threetime) //月份間隔 大於2個月,小於3個月
70                         {
71                             $one =  date('d',time());
72                             $two = '';
73                             $three = $rs[0]['one'];
74                         }
75                         elseif($oldtime < $threetime)  //月份間隔 大於3個月
76                         {
77                             $one =  date('d',time());
78                             $two = '';
79                             $three = '';
80                         }
81                         $query3 = Db::name('sign')->where('userid',$userid)->update(['one'=>$one,'two'=>$two,'three'=>$three]);
82                     }
83                     else  //上次簽到日期與本次簽到日期月份一樣
84                     {
85                         $one = Db::name('sign')->where('userid',$userid)->field('one')->select();
86                         $arr[] = $one[0]['one'];
87                         $arr[] = date('d',time());
88                         $newones = implode(",",$arr);
89                         $query3 = Db::name('sign')->where('userid',$userid)->update(['one'=>$newones]);
90                     }
91                 return 1;
92                 }
93             }
94     }

 

 

over!over!over!

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM