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

//QQ快捷登录
if (!empty(GET('access_token'))){
    $qqlogin = new qqlogin(GET('access_token'));
    if ($info = $TFYT_DB->get_row("select * from ".PREFIX."users where qqopenid='".$qqlogin->get_auth_info()['openid']."'")){
        $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}'")
        ){
            $msg = "<script language='javascript'>alert('登录成功，尊敬的".$info->name.",欢迎您回来！');window.location.href='/Application/Mains.php?Mould=Home';</script>";
        }else{
            $msg = "<script language='javascript'>alert('登录成功，但是保存数据失败，系统错误，请联系管理员修复！');window.location.href='/Application/Users.php?Mould=Login';</script>";
        }
    }else{
        $msg = "<script language='javascript'>alert('无法识别该QQ');window.location.href='/Application/Users.php?Mould=Login';</script>";
    }
    echo $msg;
}