using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using System.Net;
using xabaraCOM;

public partial class member_api_qqReturnUrl : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        admin a = new admin();
        sqlServer sql = new sqlServer();
        webPublic wp = new webPublic();
        user u = new user();
        javasrcipt js = new javasrcipt();
        encryptDecrypt ed=new encryptDecrypt();

        string appID = xml.read("xabara.config", "qqAppID", 180);
        string appKEY = xml.read("xabara.config", "qqAppKey", 180);
        string code = Request.QueryString["code"];
        string state = Request.QueryString["state"];
        string callbackUrl = "http://" + Request.Url.Host + "/member/api/qqReturnUrl.aspx";

        if (state.Equals(Session["state"].ToString()) && !string.IsNullOrEmpty(code as string))
        {
            string url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=" + appID + "&client_secret=" + appKEY + "&state=" + state + "&redirect_uri=" + callbackUrl + "&code=" + code;
            string access_token = xabara.remoteHttp(url, "utf-8", "", "");
            string[] token = access_token.Split(new char[] { '&' });
            string[] accessToken = token[0].Split(new char[] { '=' });
            //Response.Write(access_token);
            //Response.End();

            string result = xabara.remoteHttp("https://graph.qq.com/oauth2.0/me?access_token=" + accessToken[1], "utf-8", "", "");
            if (result.IndexOf("callback") != -1)
            {
                int lpos = result.IndexOf("(");
                int rpos = result.IndexOf(")");
                result = result.Substring(lpos + 1, rpos - lpos - 1);

                jsonclass qq = webPublic.JsonToObject<jsonclass>(result);
                //Response.Write(qq.openid.ToString());
                string userInfo = xabara.remoteHttp("https://graph.qq.com/user/get_user_info?format=json&access_token=" + accessToken[1] + "&openid=" + qq.openid.ToString() + "&oauth_consumer_key=" + appID, "utf-8", "", "");
                //Response.Write(qq.openid.ToString());

                jsonclass user = webPublic.JsonToObject<jsonclass>(userInfo);
                //Response.Write(user.nickname.ToString());

                if (null != user)
                {
                    string qqID = "qq_" + qq.openid;
                    DataTable dt;
                    string strSql = "select * from XabaraUser where qqID='" + qqID + "'";
                    dt = sql.getDataTable(strSql);
                    if (dt.Rows.Count > 0)
                    {
                        string email = dt.Rows[0]["userEmail"].ToString().Trim();
                        if (string.IsNullOrEmpty(email as string))  //检查有效邮箱
                        {
                            js.div(true, "电子邮箱", "text", "请填写您的电子邮箱！", 0, 0, "/member/user/edit.aspx", "top");
                        }
                        else
                        {
                            u.login(dt.Rows[0]["userCardID"].ToString().Trim(), email, "", "", true, false);
                        }
                    }
                    else
                    {
                        wp.regUser("qq^" + qqID, qq.openid + "@qq.com", "", xabara.rnd("abc123", 8), "QQ快捷登录" + qq.nickname);

                        //QQ一键登录不允许捆绑会员
                        /*Session.Add("qq", qqID);
                        Response.Redirect("login.aspx?token=" + ed.encryptStr(appID + appKEY), true);*/
                    }
                    dt.Dispose();

                }
                else//验证失败
                {
                    js.div(false, "", "text", "验证失败，请重新登录！", 400, 150, "/member/", "top");
                }
            }
            else
            {
                js.div(false, "", "text", "QQ官网验证有误，请再尝试！", 350, 150, "/member/", "top");
            }
        }
        else
        {
            js.div(false, "", "text", "验证失败，请重新登录！", 350, 150, "/member/", "top");
        }
    }
}