<!--#include file="config.asp"-->
<%
''' EYCMS 新浪微博登录
''' ==================================================================

	sub callback()
		dim code:code=eycms.enhtml(eycms.fget("code",0))

		if eycms.strlen(code)=0 then
			eycms.echo "非法参数传递，无法登录"
			exit sub
		end if
		
		dim url
		url="https://api.weibo.com/oauth2/access_token?"
		url=url&"client_id="&weibo_appid
		url=url&"&client_secret="&weibo_appkey
		url=url&"&grant_type=authorization_code"
		url=url&"&code="&code
		url=url&"&redirect_uri="&callbackurl

		dim str:str=eycms.gethttp(url,"post")
		dim json
		set json=toobject(str)
		if instr(str,"error_code")>0 then
			eycms.echo "错误编号："&eycms.enhtml(json.error_code)&"<br>"
			eycms.echo "错误原因："&eycms.enhtml(json.error_description)&"<br>"
			exit sub
		else
			dim token:token=json.access_token
			openid=json.uid
			eycms.setsession "token",token
			eycms.setsession "openid",openid
		end if
		set json=nothing
	end sub
	
	sub get_openid()
		dim gourl:gourl=eycms.loadsession("api_backurl")
			dim data:data=eycms.db.dbload(1,"n.userid,u.logintimes","ey_user_bind n left join ey_user u on n.userid=u.id","n.openid='"&openid&"' and n.opentype='weibo'","")
			if ubound(data)<0 then
				eycms.setsession "api_backurl",""
				eycms.go "bind.asp?api_backurl="&gourl
				exit sub
			else
				eycms.db.dbupdate "ey_user","id="&data(0,0)&"",array(array("logintimes",data(1,0)+1,0,0))
				eycms.resetuserinfo("u.id="&data(0,0)&"")
				eycms.setsession "openid",""
				eycms.setsession "api_backurl",""
				eycms.setsession "token",""
				if eycms.strlen(gourl)=0 then gourl=webroot
				eycms.go gourl
			end if
	end sub
	dim openid
	callback()
	get_openid()
%>
