<!--#include file="config.asp"-->
<%	
	sub callback()
		dim states:states=eycms.enhtml(eycms.fget("state",0))
		dim code:code=eycms.enhtml(eycms.fget("code",0))
		dim t0:t0=eycms.enhtml(eycms.loadsession("qq_state"))
		if t0="" then
			eycms.go "../login.asp?t=1"
			exit sub
		end if
		if eycms.strlen(states)=0 or eycms.strlen(code)=0 then
			eycms.go "../login.asp?t=1"
			exit sub
		end if
		if states<>t0 then
			eycms.go "../login.asp?t=1"
			exit sub
		else
			dim url:url="https://graph.qq.com/oauth2.0/token?grant_type=authorization_code"
				url=url&"&client_id="&qq_appid
				url=url&"&client_secret="&qq_appkey
				url=url&"&redirect_uri="&qq_callback
				url=url&"&code="&code
			dim str:str=eycms.gethttp(url,"")
			if instr(str,"callback")>0 then
				str=replace(str,"callback( ","")
				str=replace(str," )","")
				dim json
				set json=toobject(str)
					eycms.echo "错误编号："&eycms.enhtml(json.error)&"<br>"
					eycms.echo "错误原因："&eycms.enhtml(json.error_description)&"<br>"
				set json=nothing
				exit sub
			end if
			if left(str,13)="access_token=" then
				dim access_token:access_token=mid(str,14,instr(str,"&")-14)
				eycms.setsession "access_token",eycms.enhtml(access_token)
			else
				eycms.echo "未知错误"
				exit sub
			end if
		end if
	end sub
	
	sub get_openid()
		dim url:url="https://graph.qq.com/oauth2.0/me?access_token="&eycms.loadsession("access_token")
		dim str:str=eycms.gethttp(url,"")
		dim gourl:gourl=eycms.loadsession("api_backurl")
		if instr(str,"callback")>0 then
			str=replace(str,"callback( ","")
			str=replace(str," )","")
			dim json
			set json=toobject(str)
			dim openid:openid=eycms.enhtml(json.openid)
			set json=nothing
			dim data
			'如果已登录，则绑定
			dim adminid:adminid=eycms.loadsession("adminid")
			if eycms.strlen(adminid)>0 then
				data=eycms.db.dbload(1,"openid","ey_admin","adminid="&adminid&"","")
				if ubound(data)>=0 then
					if eycms.strlen(data(0,0))=0 then
						eycms.db.dbupdate "ey_admin","adminid="&adminid&"",array(array("openid",openid,255,1))
					end if
				end if
				eycms.backurl "\u7ed1\u5b9a\u6210\u529f","../"
				exit sub
			end if
			'如果未登录，则检查是否绑定
			'查询openid，如果存在则直接获取会员信息，否则退出
			data=eycms.db.dbload(1,"adminid,adminname,adminpass,logintimes,islock,groupid","ey_admin","openid='"&openid&"'","")
			if ubound(data)<0 then
				eycms.go "../login.asp?t=1"
				exit sub
			else
				if data(4,0)=0 then
					eycms.echo "&#29992;&#25143;&#34987;&#38145;&#23450;&#65292;&#19981;&#33021;&#30331;&#24405;"
				else
					dim datanew
					datanew=array(array("logintimes",data(3,0)+1,0,0),array("lastlogindate",sqltime,0,0),array("lastloginip",eycms.getip,17,1))
					eycms.db.dbupdate "ey_admin","adminid="&data(0,0)&"",datanew
					eycms.setsession "adminid",data(0,0)
					eycms.setsession "adminname",data(1,0)
					eycms.setsession "admingroupid",data(5,0)
					eycms.setcookie "adminid",data(0,0)
					dim key:key=eycms.getkey()
					eycms.setcookie "adminid",data(0,0)
					eycms.setcookie "loginkey",key
					eycms.setcookie "loginpwd",md5(data(1,0)&data(2,0)&key)
				end if
				'清空API数据
				eycms.setsession "qq_state",""
				eycms.setsession "access_token",""
				eycms.go "../"
			end if
		else
			eycms.echo "无法获取数据，登录失败"
			exit sub
		end if
	end sub

	callback()
	get_openid()
%>
