TP5签到功能概述与设计思路
本案例详细解析了如何在ThinkPHP 5框架中构建签到模块,供开发者参考借鉴。
该功能基于TP5的数据模型进行封装开发。
为避免全量存储签到日期造成数据库体积过大,系统策略是仅保留最近三个月内的签到记录。
主要业务逻辑包含以下要点:
首先,需准确记录用户最后一次执行签到的具体时间。
其次,用户每完成一次签到操作,账户将自动获得15点积分奖励。
同时,系统会维护并更新用户的连续签到天数记录。
CREATE TABLE `sp_sign` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
`times` datetime DEFAULT NULL COMMENT '最近一次签到时间',
`userid` int(11) DEFAULT NULL COMMENT '用户id',
`days` tinyint(6) NOT NULL DEFAULT '0' COMMENT '连续签到的天数',
`number` decimal(10,0) NOT NULL DEFAULT '0' COMMENT '当月签到给的积分',
`one` varchar(255) DEFAULT NULL COMMENT '当月签到的日期,用“,”隔开',
`two` varchar(255) DEFAULT NULL COMMENT '上个月签到的日期,用“,”隔开',
`three` varchar(255) DEFAULT NULL COMMENT '上上个月签到的日期,用“,”隔开',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
/**
* 用户签到
* @param array $userid 用户id
*/
public function add($userid)
{
$data = Db::name('sign')->where('userid',$userid)->select();
if(count($data) == 0) //没有该用户的签到记录
{
$query4 = Db::name('sign')->insert(['times'=>date('Y-m-d H:i:s'),'userid'=>$userid,'days'=>1,'number'=>'15','one'=>date('d',time())]);
return 1;
}
else
{
//判断今天是否签到
$todayBegin=date('Y-m-d'." 00:00:00");
$todayEnd= date('Y-m-d'." 23:59:59");
$isexit = Db::name('sign')->field('times')->where(['userid'=>$userid])->where('times','between',[$todayBegin,$todayEnd])->select();
if(count($isexit) == 1) //今日已签到
{
return 0;
}
else //今日未签到
{
$times = Db::name('sign')->where('userid',$userid)->field('times')->select();
$time = strtotime($times[0]['times']);
if((time()-$time > 24*60*60)) //上次签到时间大于24小时,连续签到天数清零
{
$query = Db::name('sign')->where('userid',$userid)->update(['days'=>1]);
}
else //上次签到时间小于24小时,连续签到次数加1
{
$query = Db::name('sign')->where('userid',$userid)->setInc('days');
}
//更新上次签到时间和签到积分
$query1 = Db::name('sign')->where('userid',$userid)->update(['times'=>date('Y-m-d H:i:s')]);
$query2 = Db::name('sign')->where('userid',$userid)->setInc('number', 15);
$sqldate = date('m',$time); //上次签到日期的月份
$nowdate = date('m',time()); //当前月份
//记录本次签到日期
if($sqldate != $nowdate) //上次签到日期与本次签到日期月份不一样
{
$oldtime = $times[0]['times'];
$onetime=date("Y-m-d H:i:s", strtotime("-1 month")); //获取前1个月的时间,获取格式为2026-12-30 13:26:13
$twotime=date("Y-m-d H:i:s", strtotime("-2 month")); //获取前2个月的时间
$threetime=date("Y-m-d H:i:s", strtotime("-3 month")); //获取前3个月的时间
$rs = Db::name('sign')->where('userid',$userid)->field('one,two,three')->select();
if($oldtime < $onetime && $oldtime >= $twotime) //月份间隔 大于1个月,小于2个月
{
$one = date('d',time());
$two = $rs[0]['one'];
$three = $rs[0]['two'];
}
elseif($oldtime < $twotime && $oldtime >= $threetime) //月份间隔 大于2个月,小于3个月
{
$one = date('d',time());
$two = '';
$three = $rs[0]['one'];
}
elseif($oldtime < $threetime) //月份间隔 大于3个月
{
$one = date('d',time());
$two = '';
$three = '';
}
$query3 = Db::name('sign')->where('userid',$userid)->update(['one'=>$one,'two'=>$two,'three'=>$three]);
}
else //上次签到日期与本次签到日期月份一样
{
$one = Db::name('sign')->where('userid',$userid)->field('one')->select();
$arr[] = $one[0]['one'];
$arr[] = date('d',time());
$newones = implode(",",$arr);
$query3 = Db::name('sign')->where('userid',$userid)->update(['one'=>$newones]);
}
return 1;
}
}
}
希望上述关于ThinkPHP框架下PHP程序设计的实践经验能对大家有所帮助。