<!--#include file="../../MainClass.asp"-->
<!--#include file="../inc.asp"-->
<!--#include file="sha1.asp"-->
<%
dim rArr,Fun:rArr=requestArrey("",0)
dim xmlobj,appid,callback,appkey,SqlNowString
SET xmlobj=server.CreateObject("inc_inc_MainClass.xml")
xmlobj.load "../api.config","xmlfile"
appid=xmlobj.getAttributes("rs:data/z:row","api_qqappid",0)
appkey=xmlobj.getAttributes("rs:data/z:row","api_qqappkey",0)
callback=xmlobj.getAttributes("rs:data/z:row","api_qqcallback",0)
Set xmlobj=Nothing
Set Fun=server.CreateObject("inc_inc_MainClass.CommonFun")
SqlNowString=fun.ifthen(databaseType=0,"now()","getdate()")
Select case rArr(0)
  case "qqbind":qqbind
  case "doreg":doreg
  case "check":check
  case else : redirect_to_login
End Select
Function requestArrey(Byval url,Byval ArrI)'返回URl以"-"组成的数组.(1)声名数组的大小
  dim paras,i,num,parasArr,urlnum
  if url="" then url=request.QueryString
  urlnum=InstrRev(url,fileSuffix)
  if urlnum<>0 then paras=left(url,urlnum-1) else paras=url
  parasArr=split(paras,"/")
  num=ubound(parasArr)
  redim parasArray(ArrI)
  for i=0 to ArrI
	if i<=num then
	  parasArray(i)=parasArr(i)
	End if
  next
  requestArrey=parasArray
End Function
Function ToUnixTime(strTime,intTimeZone)'生成时间戳
  If IsEmpty(strTime) or Not IsDate(strTime) Then strTime=Now
  If IsEmpty(intTimeZone) or Not isNumeric(intTimeZone) Then intTimeZone=0
  ToUnixTime=DateAdd("h",-intTimeZone,strTime)
  ToUnixTime=DateDiff("s","1970-1-1 0:0:0",ToUnixTime)
End Function
function redirect_to_login()
  dim url
  Session("state") = year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&fun.MakeRandom(10)
  url="https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id="&appid&"&redirect_uri="&server.urlencode(callback)&"&state="&Session("state") &"&scope=get_user_info,add_share,check_page_fans,add_t,del_t,add_pic_t,get_repost_list,get_info,get_other_info,get_fanslist,get_idolist,add_idol,del_idol"
   response.write "<div style='margin-top:90px;color:#666;font-size:16px;text-align:center;'>正在转向qq登录授权页面，请稍候！！！如果长时间没有反应请<a href=""javascript:;"" onclick=""top.location.href='" & url & "';"" style='color:red'>点此跳转</a>。</div>"
  response.redirect url
end function
Sub check
  dim openid,username,password,rs
  openid=session("openid"):if isNul(openid) then die "请不要非法绑定!"
  username="admin"
  password=md5.md5(conn.getForm("password","post"),32)
  set rs=conn.db("select top 1 * from {pre}user where {pre}userName='"&username&"' and {pre}pwd='"&password&"'","records1")
  if rs.eof and rs.bof then
	rs.close:set rs=nothing
	die "<script>alert('对不起，您输入的账号不存在或是密码不正确，请重输!');history.back(-1);</"&"script>"
  else
	'绑定到已有账号
	if session("api")="QQ" then
	  conn.db "update {pre}user set qqopenid='"&openid&"' where u_userName='"&username&"'","execute" 
	elseif session("api")="支付宝" then
	  conn.db "update {pre}user set alipayID='"&openid&"' where u_userName='"&username&"'","execute" 
	end if
	'调用登录
	user.Login username,password,""
	response.Redirect("../../user/?info.html")
  end if
End Sub
Sub doreg
  Dim UserName,urs,u_niceName,sex
  u_niceName=conn.getForm("nickname","post")
  sex=conn.getForm("sex","post")
  if not isNul(session("openid")) then
	IF InStr(UserName, "-") > 0 Or InStr(UserName, "=") > 0 Or InStr(UserName, ".") > 0 Or InStr(UserName, "%") > 0 Or InStr(UserName, Chr(32)) > 0 Or InStr(UserName, "?") > 0 Or InStr(UserName, "&") > 0 Or InStr(UserName, ";") > 0 Or InStr(UserName, ",") > 0 Or InStr(UserName, "'") > 0 Or InStr(UserName, ",") > 0 Or InStr(UserName, Chr(34)) > 0 Or InStr(UserName, Chr(9)) > 0 Or InStr(UserName, "") > 0 Or InStr(UserName, "$") > 0 Or InStr(UserName, "*") Or InStr(UserName, "|") Or InStr(UserName, """") > 0 Then
	  Die("<script>alert('用户名中含有非法字符');history.back();</script>")
	End If
	UserName=GetUsernameNum
	set urs=conn.db("select {pre}username,{pre}nicename,{pre}regtime,{pre}pwd,{pre}qq,{pre}mail,{pre}age,{pre}face,{pre}score,{pre}logTime,{pre}level,{pre}ThisArea,{pre}birthday,{pre}RealArea,{pre}QQopenid,{pre}sex,{pre}alipayID from {pre}user","records3")
	urs.addnew
	urs("u_username")=UserName
	urs("u_niceName")=u_niceName
	urs("u_regTime")=now
	urs("u_logTime")=now
	urs("u_score")=loadRules("regScore")
	urs("u_pwd")=session("openid")
	urs("u_level")=0
	urs("sex")=sex
	urs("birthday")="0,0,0,0"
	urs("RealArea")=",,,"
	urs("ThisArea")=",,,"
	if session("api")="QQ" then
	  urs("QQopenid")=session("openid")
	elseif session("api")="支付宝" then
	  urs("alipayID")=session("openid")
	end if
	urs.update
	urs.close
	set urs=nothing
	user.Login UserName,session("openid"),""
	response.Redirect("../../user/?info.html")
  end if
End Sub
Sub qqbind'QQ绑定处理
  '===================绑定处理=================================
  dim ret,msg,nickname,figureurl,sex,rs
  dim resultxml:resultxml=get_user_info(1,conn.rCookie("access_token"),conn.rCookie("openid"))
  dim obj:set obj = getjson(resultxml)
  if instr(resultxml,"access token check failed")<>0 then
    if isobject(obj) Then
	  conn.die obj.msg
    else
	  conn.die "error!"
   end if
  Else
	if isobject(obj) Then
	  nickname=obj.nickname
	  figureurl=obj.figureurl
	  sex=obj.gender
	End If
	set obj=nothing
  end if
  if fun.chkclng(ret)<0 then    '获取失败
    fun.die "登录失败:" & msg
  end if
  set rs=conn.db("select top 1 {pre}name,{pre}password from {pre}user where {pre}qqopenid='" & conn.rCookie("openid") & "'","execute")
  if rs.eof and rs.bof then
	if conn.rCookie("pre_username")<>"" then'如果当前会员是登录状态的，直接绑定
	   conn.db"update {pre}User Set {pre}qqtoken='" & conn.rCookie("access_token") & "',{pre}qqopenid='" & conn.rCookie("openid") & "' where {pre}name='"&conn.rCookie("pre_username")&"'","execute"
       conn.echo"已绑定"
	   'Response.Redirect("../../user/user_bind.asp")
	else
	  Call DoBind("用QQ登录成功",nickname,figureurl,sex,"qq",conn.rCookie("openid"),conn.rCookie("access_token"))'快速自动注册登录
	end if
  Else
	conn.db"Update {pre}User Set {pre}qqToken='" & conn.rCookie("access_token") &"' WHERE {pre}Name='" & rs(0) &"'","execute"
	'登录成功后调用登录函数进行登录
	Call DoLogin(rs(0),rs(1))
  end if
  set rs=nothing
  '=============================================================
End Sub
%>
