<!--#include file="com.asp"-->
<!--#include file="class.asp"-->
<%
Dim url,data,Access_Token,userinfo,OpenID
'获取access_token
function Get_Access_Token()
	url="https://graph.qq.com/oauth2.0/token"
	data="grant_type=authorization_code&client_id="&QQ_APP_ID&"&client_secret="&QQ_APP_Key&"&code="&Request.QueryString("code")&"&state=1100&redirect_uri="&QQ_CALLBACK_URL
	Get_Access_Token = GetBody(url,data)
end function
Access_Token = Get_Access_Token()
'Response.write "Access_Token:"&Access_Token&"<br>---------<br>"

'获取openid
function Get_OpenID()
	dim t_Access_Token:t_Access_Token = MID(Trim(t_Access_Token),14,32)
	Get_OpenID = GetBody("https://graph.qq.com/oauth2.0/me",Access_Token)
end function
OpenID = Get_OpenID()
'Response.write "OpenID:"&OpenID&"<br>-----------<br>"

'验证登录
function CheckLogAccess()
	dim Re:Re = false
	if Instr(Access_Token,"error")>0 then
		Re = false
	else
		Re = true
	end if
	CheckLogAccess = Re
end function
if CheckLogAccess()=false then Response.Write "<script>alert('登录验证失败,请重新登录!');location.href='/QQConnect/';</script>":Response.End()


'获取用户信息
function GetUserInfo()
	url="https://graph.qq.com/user/get_user_info"
	data=Access_Token&"&oauth_consumer_key="&QQ_APP_ID&"&openid="&GetJson(OpenID,"openid")&"&format=json"
	GetUserInfo = GetBody(url,data)
end function
'Response.write GetUserInfo()&"<br>------<br>"

'获取全部信息
function GetInfo()
	url="https://graph.qq.com/user/get_info"
	data=Access_Token&"&oauth_consumer_key="&QQ_APP_ID&"&openid="&OpenID&"&format=json"
	GetInfo = GetBody(url,data)
end function
'Response.write GetInfo()&"<br>--------<br>"

function AddIdol()
	url="https://graph.qq.com/relation/add_idol"
	data=Access_Token&"&oauth_consumer_key="&QQ_APP_ID&"&openid="&OpenID&"&name=SPKLNET&fopenids="&"&format=json"
	AddIdol = GetBody(url,data)
end function
AddIdol()
'Response.write AddIdol()&"<br>--------<br>"
LoginAndReg

dim t_UserInfo,t_UserOpenID,t_UserClientID,t_UserName,t_UserNickName,t_UserSex,t_UserPic
Sub LoginAndReg()
	t_UserInfo = GetUserInfo()
	t_UserOpenID = GetJson(OpenID,"openid")
	t_UserClientID = GetJson(OpenID,"client_id")
	t_UserName = t_UserOpenID
	t_UserNickName = GetJson(t_UserInfo,"nickname")
	t_UserSex = GetJson(t_UserInfo,"gender")
	if t_UserSex="男" then t_UserSex = 1 else t_UserSex = 0 end if
	t_UserPic = "http://qzapp.qlogo.cn/qzapp/"&t_UserClientID&"/"&t_UserOpenID&"/100"
	
	RS_End
	'判断是否存在账户
	RS.Open "select top 1 * from [i_User_Info] a join [i_Sys_Room] b on a.[i_User_ID]=b.[i_Sys_Room_AdminUserID] where a.[i_User_OpenID]='"&t_UserOpenID&"' ", DB_CONN , 1 , 3
	if RS.recordcount>0 then'已经存在
		RS("i_User_Online")=1
		RS("i_User_LastLoginTime")=now()
		RS("i_User_LoginIP")=get_IP()
		RS.Update()
		Session(Session_UserID_Arg)=RS("i_User_ID")
		Session(Session_UserName_Arg)=RS("i_User_Name")
		Session(Session_UserNickName_Arg)=RS("i_User_NickName")
		Session(Session_UserSex_Arg)=RS("i_User_Sex")
		Session(Session_UserClass_Arg)=RS("i_User_Class")
		Session(Session_UserFace_Arg)=RS("i_User_Face")
		Session(Session_RoomID_Arg)=RS("i_Sys_Room_ID")
		Session(Session_LogIP_Arg)=get_IP()
		Session(Session_LogTime_Arg)=now()
		Response.Cookies("ishow")("iAutoLogin")="true":Response.Cookies("ishow")("iUserOpenID")=t_UserOpenID::Response.Cookies("ishow").Expires= (now()+365)
		'整合应用登录
		'i_Syn_Notifyt_Login()
		Rws "location.href='/'":Rend
	else'不存在
	
		RS_End
		RS.Open "select top 1 * from [i_User_Info]", DB_CONN , 1 , 3
		RS.addnew()
		RS("i_User_OpenID")=t_UserOpenID
		RS("i_User_Name")=t_UserName
		RS("i_User_Sex")=t_UserSex
		RS("i_User_NickName")=t_UserNickName
		RS("i_User_Face")=t_UserPic
		RS("i_User_Class")=SysConfig("i_Sys_Config_RegDefaultClass")
		RS("i_User_Score")=SysConfig("i_Sys_Config_RegDefaultScoreNum")
		RS("i_User_Money")=SysConfig("i_Sys_Config_RegDefaultMoneyNum")
		RS("i_User_Online")=1
		RS("i_User_LastLoginTime")=now()
		RS("i_User_LoginIP")=get_IP()
		RS.update()
		RS_End
		'读取注册用户ID
		Set RS=DB_CONN.EXECUTE("select top 1 * from [i_User_Info] where [i_User_Name]='"&t_UserName&"'")
		if RS.RecordCount<1 then Rws "alert('网站注册异常,请联系网站管理员');history.back();":Rend
		t_UserID = RS("i_User_ID")
		RS_End
		'注册房间
		RS.OPEN "select top 1 * from [i_Sys_Room]" , DB_CONN , 1 , 3
		RS.addnew()
		RS("i_Sys_Room_Name")=t_UserNickName
		RS("i_Sys_Room_AdminUserID")=t_UserID
		RS("i_Sys_Room_ManagerUsers")="|"&t_UserID&"|"
		RS.update()
		RS_End
		'设置登录session
		RS.Open "select top 1 * from [i_User_Info] a join [i_Sys_Room] b on a.[i_User_ID]=b.[i_Sys_Room_AdminUserID] where a.[i_User_OpenID]='"&t_UserOpenID&"' ", DB_CONN , 1 , 3
		Session("i_User_OpenID")=RS("i_User_OpenID")
		Session(Session_UserID_Arg)=RS("i_User_ID")
		Session(Session_UserName_Arg)=RS("i_User_Name")
		Session(Session_UserNickName_Arg)=RS("i_User_NickName")
		Session(Session_UserSex_Arg)=RS("i_User_Sex")
		Session(Session_UserClass_Arg)=RS("i_User_Class")
		Session(Session_UserFace_Arg)=RS("i_User_Face")
		Session(Session_RoomID_Arg)=RS("i_Sys_Room_ID")
		Session(Session_LogIP_Arg)=get_IP()
		Session(Session_LogTime_Arg)=now()
		Rws "location.href='/i/"&SysConfig("i_Sys_Config_TemplatePath")&"/Bind.html?"&vbsEscape(t_UserNickName)&"'":Rend
	end if
	RS_End
End Sub

%>