1 获取当日所在周的七天时间
2 前端设计好3个状态的样式 0未打卡 1打卡 2时间未到来
3 新建一个数组用来存储状态
4 后端数据写入sql的格式: $d[年][月][日,日2,日3...] ,显示3纬数组,转成json格式存储,打卡时要恢复成数组格式
通过读取数据来比对,如果>今日本周时间;一概设置状态数组为2,
5 最后输出状态代码
[0,1,1,1,0,2,2,2]
前端按照状态码显示即可,
每个用户只存一条记录,每次都复写打卡记录,在原来都基础上添加并转为json格式存储,用都时候还原即可。
如何判断今天是否已经打卡很简单,就是看打卡记录的update是否是今天,
新建一条记录的时候,created记录和update记录都是同一时间戳,
所以要判断:update!=created&&today==update (时间格式统一使用y-m-d )满足条件,则今天已经打卡,默认是未打卡。
if($signs->created_at!=$signs->updated_at&&$todadyDate==$upDate)
//新建日期与更新日期要不一样,才能使用更新日期判断是否打卡
public $state=0; //存储状态
public $r=''; //存储日期记录,用于更新
//return $record[$year][$month]; //获取数据必须将年月转为字符串
//首先统计当前年月是否存在,如不存在,需要添加年和当月的数组下标
//如果月存在则返回当月的数据
//1 查询到当日到年月,提取数组,D[2019][3][...所需要数据...]
//提取 今日的格式为天,大于今天的时间,几个时间段全部设置为2,其他的数字对比提取的数组中的日期,如果包含则1,没有则0。
//获取请求当天的日期,查找该日期属于第几周,并找到当周第七天的日期数据
//提前当前用户的打卡记录
//设定7个变量分别记录当周这7天数据的打卡情况
// 0未打卡 1 已经打卡 2 日期还没到,需判断当日,<当日 判断即可,当日直接查找,>当日一概置0
//按照顺序push Array 并返回前端 返回更新数据来判断,是否今天签到过,更新数据为今日表示已经签到
public function store()
{
$user = $this->user();
$signs= Sign::firstOrCreate(['user_id' => $user->id]);
//return $signs;
if($signs->record==null){
$D = array
(
date('Y')=>array
(
date('m')=>array(),
),
);
$signs->record=json_encode($D);
$signs->save();
$total=0; //总的打卡次数
$monthDays=date("t"); //当月的天数
$weekrecords=$this->weekState(json_decode($signs->record,true));
$result=[
'state'=>0, //打卡状态
'point'=>$user->point, //积分
'total'=>$total,
'monthDays'=>$monthDays,
'record'=>$weekrecords
];
return $result;
}else{
$this->r=json_decode($signs->record,true);
$year=(string)date("Y"); //获取今年
$month=(string)date("m"); //获取当月
//判断年或月是否存在,任意不存在,都新建年月
if(!array_key_exists($year,$this->r)||!array_key_exists($month,$this->r[$year])){
$this->r[$year][$month]=[];
$signs->record=json_encode($this->r);
$signs->save();
$this->r=[];//清空r释放内存
};
$total=count($this->r[$year][$month]); //总的打卡次数
$monthDays=date("t"); //当月的天数
$weekrecords=$this->weekState(json_decode($signs->record,true));
$todadyDate=date("Y-m-d",time());
$upDate=$signs->updated_at->format('Y-m-d');
if($signs->created_at!=$signs->updated_at&&$todadyDate==$upDate){
$this->state=1;
}
$result=[
'state'=>$this->state, //打卡状态
'point'=>$user->point,
'total'=>$total,
'monthDays'=>$monthDays,
'record'=>$weekrecords
];
return $result;
}
}
来贴一下最终的效果图