SignController.php 9.6 KB
<?php
namespace api\portal\controller;
use cmf\controller\RestBaseController;
use Think\Db;
/**
 * @title 签到
 * @description 签到相关接口
 * @header name:key require:1 default: desc:秘钥(区别设置)
 * @param name:public type:int require:1 default:1 other: desc:公共参数(区别设置)
 */
class SignController extends RestBaseController
{
    /**
     * @title 首页接口
     * @description 麦夫子首页渲染
     * @author panhaowen
     * @url /portal/Sign/index
     * @method POST
     * @header name:XX-Token require:1 default: desc:token
     * @param
     * @return sign:1是早打卡,2是午打卡,3是晚打卡,4是睡前打卡,0是当前不在打卡时间
     * @return slogan:口号
     */
    public function index()
    {
        $re=Db::name('config')->where('id',1)->find();

        $data['morning_sign_time']=date('H:i',$re['morning_sign_time']);
        $data['morning_sign_end_time']=date('H:i',$re['morning_sign_end_time']);
        $data['noon_sign_time']=date('H:i',$re['noon_sign_time']);
        $data['noon_sign_end_time']=date('H:i',$re['noon_sign_end_time']);
        $data['night_sign_time']=date('H:i',$re['night_sign_time']);
        $data['night_sign_end_time']=date('H:i',$re['night_sign_end_time']);
        $data['sleep_sign_time']=date('H:i',$re['sleep_sign_time']);
        $data['sleep_sign_end_time']=date('H:i',$re['sleep_sign_end_time']);
        $time=date('H:i:s',time());

        $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
        $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        $user_sign=Db::name('user_sign')->where(['user_id'=>$this->getUserId(),'create_time'=>[ 'between time', [$beginToday,$endToday]]])->find();
        if ($time>$data['morning_sign_time']&&$time<$data['morning_sign_end_time']){
            if (!empty($user_sign)&&$user_sign['morning']!=null) {
                $data['sign'] = 0;
            }else{
                $data['sign'] = 1;
            }
            $data['start_time']=$data['morning_sign_time'];
            $data['end_time']=$data['morning_sign_end_time'];
        }else if ($time>$data['noon_sign_time']&&$time<$data['noon_sign_end_time']){
            if (!empty($user_sign)&&$user_sign['noon']!=null) {
                $data['sign'] = 0;
            }else{
                $data['sign'] = 2;
            }
            $data['start_time']=$data['noon_sign_time'];
            $data['end_time']=$data['noon_sign_end_time'];
        }else if ($time>$data['night_sign_time']&&$time<$data['night_sign_end_time']){
            if (!empty($user_sign)&&$user_sign['night']!=null) {
                $data['sign'] = 0;
            }else{
                $data['sign'] = 3;
            }
            $data['start_time']=$data['night_sign_time'];
            $data['end_time']=$data['night_sign_end_time'];
        }else if ($time>$data['sleep_sign_time']&&$time<$data['sleep_sign_end_time']){
            if (!empty($user_sign)&&$user_sign['sleep']!=null) {
                $data['sign'] = 0;
            }else{
                $data['sign'] = 4;
            }
            $data['start_time']=$data['sleep_sign_time'];
            $data['end_time']=$data['sleep_sign_end_time'];
        }else{
            if ($time<$data['morning_sign_time']){
                $data['start_time']=$data['morning_sign_time'];
                $data['end_time']=$data['morning_sign_end_time'];
            }elseif ($time>$data['morning_sign_end_time']&&$time<$data['noon_sign_time']){
                $data['start_time']=$data['noon_sign_time'];
                $data['end_time']=$data['noon_sign_end_time'];
            }elseif ($time>$data['noon_sign_end_time']&&$time<$data['night_sign_time']){
                $data['start_time']=$data['night_sign_time'];
                $data['end_time']=$data['night_sign_end_time'];
            }else{
                $data['start_time']=$data['sleep_sign_time'];
                $data['end_time']=$data['sleep_sign_end_time'];
            }
            $data['sign']=0;
        }
        $this->success('首页信息',array(
            'sign'=>$data['sign'],
            'slogan'=>$re['slogan'],
            'start_time'=>$data['start_time'],
            'end_time'=>$data['end_time'],
        ));
    }

    /**
     * @title 日打卡页面
     * @description 麦夫子日打卡页面渲染
     * @author panhaowen
     * @url /portal/Sign/daySign
     * @method POST
     * @header name:XX-Token require:1 default: desc:token
     * @param
     */
    public function daySign(){
        //用户信息
        $user=Db::name('user')->where('id',$this->getUserId())->find();
        //当天签到记录
        $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
        $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        $daySign=Db::name('user_sign')->where(['user_id'=>$user['id'],'create_time'=>[ 'between time', [$beginToday,$endToday]]])->find();
        $daySign['morning']=empty($daySign['morning'])?null:date('Y/m/d H:i:s',$daySign['morning']);
        $daySign['noon']=empty($daySign['noon'])?null:date('Y/m/d H:i:s',$daySign['noon']);
        $daySign['night']=empty($daySign['night'])?null:date('Y/m/d H:i:s',$daySign['night']);
        $daySign['sleep']=empty($daySign['sleep'])?null:date('Y/m/d H:i:s',$daySign['sleep']);
        //返回
        $this->success('日打卡信息',array(
            'all'=>$user['all_sign_day'],
            'last'=>$user['stay_sign_day'],
            'longest'=>$user['longest_sign_day'],
            'morning'=>$daySign['morning'],
            'noon'=>$daySign['noon'],
            'night'=>$daySign['night'],
            'sleep'=>$daySign['sleep'],
        ));
    }

    /**
     * @title 日打卡提交
     * @description 麦夫子日打卡提交接口
     * @author panhaowen
     * @url /portal/Sign/daySignPost
     * @method POST
     * @header name:XX-Token require:1 default: desc:token
     * @param name:sign type:int require:1 other: desc:日打卡种类
     * @return slogan:口号
     */
    public function daySignPost(){
        $param=$this->request->param();
        //根据星期判断music,image
        $date=date("w",time()); //这个可以获取数字星期比如123,注意0是星期日
        $config=Db::name('sign_image')->where('date',$date)->find();
        $music=json_decode($config['music'],true);
        $music=$music[rand(0,count($music,0)-1)];
        //根据签到种类判断增加积分数
        $score=Db::name('config')->where('id',1)->find();
        if ($param['status']==1){
            $score=$score['morning_sign_score'];
            $type='morning';
        }elseif ($param['status']==2){
            $score=$score['noon_sign_score'];
            $type='noon';
        }elseif ($param['status']==3){
            $score=$score['night_sign_score'];
            $type='night';
        }elseif ($param['status']==4){
            $score=$score['sleep_sign_score'];
            $type='sleep';
        }
        //将签到写入记录
        $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
        $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        $yesterday = strtotime(date("Y-m-d",strtotime("-1 day")));
        //已有记录
        $re=Db::name('user_sign')->where(['user_id'=>$this->getUserId(),'create_time'=>[ 'between time', [$beginToday,$endToday]]])->find();
        if ($re){
            //更新打卡
            Db::name('user_sign')->where('id',$re['id'])->update(["$type"=>time()]);
            //判断是否打满了4次
            if (!empty($re['morning'])&&!empty($re['noon'])&&!empty($re['night'])&&!empty($re['sleep'])){
                Db::name('user_sign')->where('id',$re['id'])->update(['status'=>1]);
                Db::name('user')->where('id',$this->getUserId())->setInc('all_sign_day',1);
                //判断是否为连续打卡
                $last=Db::name('user_sign')->where(['user_id'=>$this->getUserId(),'status'=>1,'create_time'=>[ 'between time', [$yesterday,$beginToday]]])->find();
                $user=Db::name('user')->where('id',$this->getUserId())->find();
                if ($user['stay_sign_day']==0||$last)
                {
                    Db::name('user')->where('id',$this->getUserId())->setInc('all_sign_day',1);
                    //判断最大连续打卡天数
                    if ($user['stay_sign_day']>$user['longest_sign_day']){
                        Db::name('user')->where('id',$this->getUserId())->update(['longest_sign_day'=>$user['stay_sign_day']]);
                    }
                }
            }
        }
        //无记录
        else{
            Db::name('user_sign')->insert(['user_id'=>$this->getUserId(),'create_time'=>time(),"$type"=>time()]);
        }
        //记录打卡积分
        Db::name('user')->where('id',$this->getUserId())->setInc('score',$score);
        Db::name('user_score_log')->insert(['score'=>$score,'create_time'=>time(),'action'=>'打卡','user_id'=>$this->getUserId(),'now_score'=>Db::name('user')->where('id',$this->getUserId())->find()['score']]);

        //判断是否需要进行周打卡
        if (!Db::name('user_week_sign')->where('user_id',$this->getUserId())->find()){
            $is_week=1;
        }elseif (Db::name('user_week_sign')->where(['user_id'=>$this->getUserId(),'create_time'=>[ '<', $beginToday-($score['week_sign']*24*60*60)]])->find()){
            $is_week=1;
        }else{
            $is_week=0;
        }

        //返回
        $this->success('日打卡提交结果',array(
            'background'=>cmf_get_image_preview_url($config['image']),
            'music'=>cmf_get_file_download_url($music['url']),
            'music_name'=>$music['name'],
            'score'=>$score,
            'week'=>$is_week
        ));
    }

}