完善签到逻辑
This commit is contained in:
parent
25b1f34e5e
commit
edb6be900d
|
@ -39,13 +39,14 @@ class SignController
|
|||
$user_id=$request->data['id'];
|
||||
// $res=Signlog::all();
|
||||
// 获取当前月份的天数
|
||||
$days_in_month = date('t');
|
||||
for ($day = 1; $day <= $days_in_month; $day++) {
|
||||
$res['sign_info'][$day]=0;
|
||||
}
|
||||
// $days_in_month = date('t');
|
||||
// for ($day = 1; $day <= $days_in_month; $day++) {
|
||||
// $res['sign_info'][$day]=0;
|
||||
// }
|
||||
$res['sign_info']=SignDao::search_Sign_tomon($user_id);
|
||||
$res['today_sign']=SignDao::search_Sign_today($user_id);
|
||||
$res['sign_days']=0;
|
||||
$res['next_days']=7;
|
||||
$res['sign_days']=SignDao::search_Sign_tolong($user_id);
|
||||
$res['next_days']=7-$res['sign_days'];
|
||||
$res['next_score']=500;
|
||||
$res['day_score']=30;
|
||||
return ApiResponseApp::success($res);
|
||||
|
|
|
@ -6,59 +6,99 @@ use App\model\Signlog;
|
|||
|
||||
|
||||
|
||||
class SignDao {
|
||||
class SignDao
|
||||
{
|
||||
/**
|
||||
* 签到函数
|
||||
*
|
||||
*/
|
||||
public static function Sign($user_id) {
|
||||
$Signlog=new Signlog();
|
||||
$Signlog->userid=$user_id;
|
||||
public static function Sign($user_id)
|
||||
{
|
||||
if (self::search_Sign_today($user_id) == 1) {
|
||||
return 0;
|
||||
}
|
||||
$Signlog = new Signlog();
|
||||
$Signlog->userid = $user_id;
|
||||
$Signlog->save();
|
||||
UserRewardDao::sing($user_id,30);
|
||||
UserRewardDao::sing($user_id, 30);
|
||||
}
|
||||
/**
|
||||
* 查询今天是否签到
|
||||
* 0没签到1签到
|
||||
*/
|
||||
public static function search_Sign_today($user_id){
|
||||
public static function search_Sign_today($user_id)
|
||||
{
|
||||
$today = date('Y-m-d');
|
||||
$is_sign=Signlog::where('userid',$user_id)->where('created_at', '>=', $today." 00:00:00")->count();
|
||||
return $is_sign==0?0:1;
|
||||
$is_sign = Signlog::where('userid', $user_id)->where('created_at', '>=', $today . " 00:00:00")->count();
|
||||
return $is_sign == 0 ? 0 : 1;
|
||||
}
|
||||
/**
|
||||
* 查询当月签到数据
|
||||
*
|
||||
*/
|
||||
public static function search_Sign_tomon($user_id){
|
||||
$startTime = date('Y-m-01',time());//获取该月份的第一天
|
||||
$endTime = date('Y-m-t',time());//获取该月份的最后一天
|
||||
$mon_data=Signlog::whereBetween('created_at', [$startTime." 00:00:00", $endTime." 23:59:59"])->orderBy('created_at','ASC')->get();
|
||||
$signDates = [];
|
||||
public static function search_Sign_tomon($user_id)
|
||||
{
|
||||
$startTime = date('Y-m-01', time()); //获取该月份的第一天
|
||||
$endTime = date('Y-m-t', time()); //获取该月份的最后一天
|
||||
$mon_data = Signlog::where('userid',$user_id)->whereBetween('created_at', [$startTime . " 00:00:00", $endTime . " 23:59:59"])->orderBy('created_at', 'ASC')->get();
|
||||
$days = [];
|
||||
foreach ($mon_data as $sign) {
|
||||
// 将签到日期提取出来
|
||||
$signDates[] = $sign->created_at->format('Y-m-d'); // 格式化为 Y-m-d 格式
|
||||
$days[] = $sign->created_at->format('d'); // 格式化为 Y-m-d 格式
|
||||
}
|
||||
|
||||
// 判断是否连续签到
|
||||
$consecutiveSign = true;
|
||||
$missingDates = []; // 用于记录未签到的日期
|
||||
|
||||
for ($i = 1; $i < count($signDates); $i++) {
|
||||
// 计算相邻两天的差距
|
||||
$currentDate = strtotime($signDates[$i]);
|
||||
$previousDate = strtotime($signDates[$i - 1]);
|
||||
$diff = ($currentDate - $previousDate) / (60 * 60 * 24); // 日期差值(天数)
|
||||
|
||||
if ($diff != 1) {
|
||||
$consecutiveSign = false; // 如果差距不为 1 天,说明不是连续签到
|
||||
$missingDates[] = date('Y-m-d', strtotime('+1 day', $previousDate)); // 记录缺失的日期
|
||||
// 获取当前月份的天数
|
||||
$days_in_month = date('t');
|
||||
for ($day = 1; $day <= $days_in_month; $day++) {
|
||||
// 判断该天是否有签到
|
||||
if (in_array($day, $days)) {
|
||||
$res[$day] = 1; // 已签到
|
||||
} else {
|
||||
$res[$day] = 0; // 未签到
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'consecutive' => $consecutiveSign,
|
||||
'missing_dates' => $missingDates
|
||||
];
|
||||
return $res;
|
||||
}
|
||||
/**
|
||||
* 查询连续签到天数
|
||||
*
|
||||
*/
|
||||
public static function search_Sign_tolong($user_id)
|
||||
{
|
||||
$startTime = date('Y-m-01', time()); // 获取该月份的第一天
|
||||
$endTime = date('Y-m-t', time()); // 获取该月份的最后一天
|
||||
$mon_data = Signlog::where('userid',$user_id)->whereBetween('created_at', [$startTime . " 00:00:00", $endTime . " 23:59:59"])
|
||||
->orderBy('created_at', 'ASC')
|
||||
->get();
|
||||
|
||||
// 签到的日期数组
|
||||
$signed_days = [];
|
||||
$today = date('d');
|
||||
foreach ($mon_data as $sign) {
|
||||
// 获取签到的日期并保存
|
||||
// 只有没有兑换奖励的才算签到有效
|
||||
if ($sign->isexchange == 0) {
|
||||
$signed_days[] = $sign->created_at->format('d');
|
||||
}
|
||||
}
|
||||
|
||||
// 获取当前月份的天数
|
||||
$days_in_month = date('t');
|
||||
$sign_days = 0; // 连续签到天数
|
||||
|
||||
// 检查每从今天开始往后是否连续签到
|
||||
for ($day = $today; $day >= 0; $day--) {
|
||||
// 检查当前日期是否有签到
|
||||
if (in_array($day, $signed_days)) {
|
||||
$sign_days++; // 连续签到天数加 1
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if($sign_days==7){
|
||||
UserRewardDao::sing($user_id,500);
|
||||
}
|
||||
|
||||
// 返回连续签到天数
|
||||
return $sign_days;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue