<?php
// +----------------------------------------------------------------------
// | 用户登录 [ 天方夜谭快速开发框架 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014~2019 http://www.eirds.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 天方夜谭 <17404785@qq.com>
// +----------------------------------------------------------------------

//用户登录
if (POST('action')=='login'){
    header('Content-Type:application/json; charset=utf-8');
    $username = POST('username');
    $password = POST('password');
    if (empty($username) || empty($password)){
        $arr = array('state'=>'no','msg'=>'用户名和密码都必须填写！');
    }elseif (strlen($username)<5 || strlen($username)>30){
        $arr = array('state'=>'no','msg'=>'用户名不能小于5位！');
    }elseif ((strlen($password)<5 || strlen($password)>18)){
        $arr = array('state'=>'no','msg'=>'密码不能小于5位！');
    }else{
        $password=md5Pwd($password);
        if ($info = $TFYT_DB->get_row("select * from ".PREFIX."users where (username='{$username}' or email='{$username}' or qq='{$username}') and password='{$password}'")){
            $cookie = str_rand();
            $ip = getIp();   //IP
            $city = get_region(getIp()); //城市
            $time = date('Y-m-d h:i:s'); //时间
            setcookie("tfyt_sid",$cookie,time()+3600*24*14,'/');
            if (
                $TFYT_DB->query("insert into ".PREFIX."users_lastlog (`uid`,`lastip`,`lastcity`,`lasttime`)values('{$info->uid}','{$ip}','{$city}','{$time}')")
                &&
                $TFYT_DB->query("update ".PREFIX."users set `cookie`='{$cookie}' where uid='{$info->uid}'")
            ){
                $arr = array('state'=>'ok','msg'=>'登录成功，尊敬的'.$info->name.',欢迎您回来！');
            }else{
                $arr = array('state'=>'no','msg'=>'登录成功，但是保存数据失败，系统错误，请联系管理员修复！');
            }
        }else{
            $arr = array('state'=>'no','msg'=>'用户名或密码错误！');
        }
    }
    exit(json_encode($arr));
}

//加载模板
Load_Template(Users.'login.php');