<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>

<script runat="server">
    
    string _tCode = "渠道";
    int gid = 62300;
    int sid = 3262;
    
    class Login
    {
        public string result;
        public string tid;
        public string serverNo;
        public string serverName;
        public string url;
        public string message;
    }

    private string V1tid(string uid)
    {
        var server = new com.xykj.business.View.Games.Server().GetServer(sid);
        var port = new com.xykj.business.View.Games.Port().GetPortByGameId(gid);

        string ip = XY.ClientIp;
        string md5 = string.Format("{0}&{1}&{2}&{3}&{4}", port.Unid, uid, port.GameNubmer, server.ServerNumber.Trim(), port.GameKey);
        string sign = XY.MD5(md5).ToLower();
        string url = string.Format("{0}?tCode={1}&TOID={2}&gameNo={3}&serverNo={4}&ip={5}&sign={6}",
            port.GameUrl, port.Unid, uid, port.GameNubmer, server.ServerNumber.Trim(), ip, sign);
        string login_info = XY.GetUrlByStream(url, "UTF-8");
        Login Login = XY.JToObject<Login>(login_info);
        if (Login.result == "true")
        {
            return Login.tid;
        }
        else
        {
            return Login.message;
        }
    }

    protected override void OnError(EventArgs e)
    {
        OutJson oj = new OutJson();
        oj.error = "y";
        oj.msg = "未知错误";
        oj.uid = "";
        Response.Write(XY.JToJson(oj));
    }
    protected override void OnInit(EventArgs e)
    {
        OutJson oj = new OutJson()
        {
            error = "y",
            msg = "账号不存在",
            uid = ""
        };
        string username = Request.Params["username"];
        string password = Request.Params["password"];
        string time = Request.Params["time"];
        string from = Request.Params["from"];
        string sign = Request.Params["sign"];
        if (string.IsNullOrEmpty(username))
        {
            oj.msg = "账号不能为空";
            Response.Write(XY.JToJson(oj));
            return;
        }
        if (string.IsNullOrEmpty(password))
        {
            oj.msg = "密码不能为空";
            Response.Write(XY.JToJson(oj));
            return;
        }
        var user = new com.xykj.business.View.Account.Account().GetUserByAccount(username);
        if (user == null)
        {
            Response.Write(XY.JToJson(oj));
            return;
        }
        if (!string.Equals(user.Password, XY.MD5(password), StringComparison.OrdinalIgnoreCase))
        {
            oj.msg = "密码错误";
            Response.Write(XY.JToJson(oj));
            return;
        }
        string tid = V1tid(user.ID + "");
        oj.error = "n";
        oj.tCode = tid;
        oj.msg = "success";
        oj.uid = user.ID + "";
        Response.Write(XY.JToJson(oj));
    }
    class OutJson
    {
        public string error;
        public string msg;
        public string uid;
        public string tCode;
    }
</script>
