基于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!