<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {

    /**
     * 登陆
     */
    public function login()
    {
        //接受数据
        $username = I('username');
        $password = md5(I('password'));

        $where['username'] = $username;
        $userInfo = M('users')->where($where)->find();



        //判断用户是否存在
        if($userInfo)
        {
            if($password == $userInfo['password'])
            {

                $playerdata = M('Playerdata')->where("uid = '".$userInfo['id']."'")->find();

                if(!$playerdata)
                {
                   $add['uid'] = $userInfo['id'];
                   M('Playerdata') -> add($add);
                }

               //连续登陆添加300金币

                $today = mktime(0,0,0,date('m'),date('d'),date('Y'));   //今天0点
                $yesterday  = mktime(0,0,0,date('m'),date('d')-1,date('Y')); //昨天0点

                if ($userInfo['endlogintime'] >= $today) {

                }
                if($userInfo['endlogintime'] >= $yesterday  and  $userInfo['endlogintime'] <= $today)
                {
                    M('Playerdata')->where("uid = '".$userInfo['id']."'")->setInc('gold',300);
                    $res = 2;
                }
                else
                {
                $res = 1;
                }
                $save['endlogintime']    = time();
                M('Users')->where("id = '".$userInfo['id']."'")->save($save);

            }
            else
            {
                $res = "0";   //输入的密码有误
            }
        }
        else
        {
            $res = "0";   //该用户不存在
        }

        //结果输出
        print_r($res);
    }

    /**
     * 用户注册
     */
    public function register()
    {
        //接受数据
        $username = I('username');
        $password = md5(I('password'));
        $SecurityCode = md5(I('SecurityCode'));

        //根据用户名查询信息
        $existence['username'] = $username;
        $userInfo = M('Users')->where($existence)->find();

        //判断用户名是否存在
        if($userInfo)
        {
            $res = "0";  //该用户名已注册
        }
        else
        {
            $add['username'] = $username;
            $add['password'] = $password;
            $add['SecurityCode'] = $SecurityCode;
            $add['registertime'] = time();
            echo date('Y-m-d H:i:s',mktime(15,15,20,3,18,2019));
            $judge = M('Users') -> add($add);
            echo "  judge  ".$judge."  ";
            //判断是否注册成功
            if($judge)
            {
                $res = "1";
                $userID = M('Users') ->where("username = '".$username."'")->field('id')->find();
                $playerdataadd['UID']=$userID['id'];
                $playerdataadd['EXPAdditionDataTime']=time();
                M('Playerdata') -> add($playerdataadd);
            }
            else
            {
                $res = "0";  //注册失败
            }
        }

        //结果输出
        print_r($res);
    }


}