美文网首页
TP5实现签到

TP5实现签到

作者: 菠萝蜜朵弦 | 来源:发表于2020-01-17 11:58 被阅读0次

基于tp5 模型的一个签到功能;

由于存储所有的签到日期数据库会非常庞大,所以签到日期只存储近三个月的。

具体功能:

1、记录最近一次的签到时间

2、每次签到都会添加15积分

3、有连续签到的记录

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('user_id', $user_id)->select();
//        $user_id=22;
        //签到积分
        $commission = Commission::commission();
        $sign = $commission['sign'];
        if (count($data) == 0)  //没有该用户的签到记录
        {
            $query4 = Db::name('sign')->insert(['times' => date('Y-m-d H:i:s'), 'user_id' => $user_id, 'days' => 1, 'number' => $sign, 'one' => date('d', time())]);
            $map = [
                'user_id' => $user_id,
                'type' => 2,
                'number' => $sign,
                'is_user' => 1,
                'title' => '签到'
            ];
            Score::create_log($map);
            User::where('id', $user_id)->setInc('score', $sign);

            return '签到成功';
        } else {
            //判断今天是否签到
            $todayBegin = date('Y-m-d' . " 00:00:00");
            $todayEnd = date('Y-m-d' . " 23:59:59");
            $isexit = Db::name('sign')->field('times')->where(['user_id' => $user_id])->where('times', 'between', [$todayBegin, $todayEnd])->select();
            if (count($isexit) == 1)   //今日已签到
            {
                $this->error = '已签到';
                return false;
            } else    //今日未签到
            {
                $times = Db::name('sign')->where('user_id', $user_id)->field('times')->select();
                $time = strtotime($times[0]['times']);

                /*if ((time() - $time > 24 * 60 * 60))       //上次签到时间大于24小时,连续签到天数清零
                {
                    $query = Db::name('sign')->where('user_id', $user_id)->update(['days' => 1]);
                } else     //上次签到时间小于24小时,连续签到次数加1
                {*/
                $query = Db::name('sign')->where('user_id', $user_id)->setInc('days');
//                }
                //更新上次签到时间和签到积分
                $query1 = Db::name('sign')->where('user_id', $user_id)->update(['times' => date('Y-m-d H:i:s')]);
                $query2 = Db::name('sign')->where('user_id', $user_id)->setInc('number', $sign);
                //积分记录
                $map = [
                    'user_id' => $user_id,
                    'type' => 2,
                    'number' => 1,
                    'is_user' => 1,
                    'title' => '签到'
                ];
                Score::create_log($map);
                User::where('id', $user_id)->setInc('score', $sign);

                $sqldate = date('m', $time);    //上次签到日期的月份
                $nowdate = date('m', time());  //当前月份
                //记录本次签到日期
                if ($sqldate != $nowdate)  //上次签到日期与本次签到日期月份不一样
                {
//                    var_dump('asdddd');
                    $oldtime = $times[0]['times'];
                    $onetime = date("Y-m-d H:i:s", strtotime("-1 month"));  //获取前1个月的时间,获取格式为2016-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('user_id', $user_id)->field('one,two,three')->select();

                    if ($oldtime < $onetime && $oldtime >= $twotime)     //月份间隔 大于1个月,小于2个月
                    {
//                        var_dump(123);
                        $one = date('d', time());
                        $two = $rs[0]['one'];
                        $three = $rs[0]['two'];
                    } elseif ($oldtime < $twotime && $oldtime >= $threetime) //月份间隔 大于2个月,小于3个月
                    {
//                        var_dump(234);

                        $one = date('d', time());
                        $two = '';
                        $three = $rs[0]['one'];
                    } elseif ($oldtime < $threetime)  //月份间隔 大于3个月
                    {
//                        var_dump(345);
                        $one = date('d', time());
                        $two = '';
                        $three = '';

                    }else{
                        $one = date('d', time());
                        $two = $rs[0]['one'];
                        $three = $rs[0]['two'];
                    }
                    $res=Db::name('sign')->where('user_id', $user_id)->update(['one' => $one, 'two' => $two, 'three' => $three]);
//                    var_dump($res);
                } else {
//                    var_dump(456);
                    $one = Db::name('sign')->where('user_id', $user_id)->field('one')->select();
                    $arr[] = $one[0]['one'];
                    $arr[] = date('d', time());
                    $newones = implode(",", $arr);
                    Db::name('sign')->where('user_id', $user_id)->update(['one' => $newones]);
                }
                return '签到成功';
            }
        }
    }

相关文章

网友评论

      本文标题:TP5实现签到

      本文链接:https://www.haomeiwen.com/subject/kkalictx.html