<!--#include virtual="/inc/conn.asp"-->
<!--#include virtual="/inc/sitecon.asp"-->
<!--#include virtual="/inc/md52.asp"-->
<!--#include virtual="/inc/getip/index.asp"-->
<%
On error resume next 

function getHTTP(url,cset) 
dim Http 
set Http=server.createobject("MSXML2.XMLHTTP") 
Http.open "GET",url,false 
Http.send() 
if Http.readystate<>4 then exit function 
getHTTP=bytesToBSTR(Http.responseBody,cset) 
set http=nothing 
if err.number<>0 then err.Clear  
end function 

appid=wxkfappid
secret=wxkfsecret

code=SafeRequest("code")
body=SafeRequest("state")

if code="" then
response.Write "登录失败"
response.end
end if

response.Cookies("kf_access_token")=""

'获取access_token
if request.Cookies("kf_access_token")="" then
	getaccess_token=getHTTP("https://api.weixin.qq.com/sns/oauth2/access_token?appid="&appid&"&secret="&secret&"&code="&code&"&grant_type=authorization_code","gb2312")
	Set json = toObject(getaccess_token)
	response.Cookies("kf_access_token")=json.access_token
	response.Cookies("kf_openid")=json.openid
	'response.Write access_token&"<br>"
	'response.Write openid
	'response.end
end if

'get_user_info 获取登录用户的昵称、头像、性别
getget_user_info_token=getHTTP("https://api.weixin.qq.com/sns/userinfo?access_token="&request.Cookies("kf_access_token")&"&openid="&request.Cookies("kf_openid")&"","utf-8")
Set json = toObject(getget_user_info_token)
nickname=json.nickname
'response.Write nickname&"<br>"

if err.number<>0 then
response.Write "未成功获取授权信息，请尝试刷新"
response.end
end if


if body="bdmai" then  		'买家后台关联qq
act="bdmai"
tourl=weburl&"u/kuaijielog"

elseif body="bdshang" then  '商家后台关联qq
act="bdshang"
tourl=weburl&"user/kuaijielog"

elseif body="log" then  	'首页登录
act="log"

else						'购买页快速登录
act="buylog"
tourl=replace(body,"%3A%2F%2F","://")
tourl=replace(tourl,"%2F","/")
tourl=replace(tourl,"%3F","?")
tourl=replace(tourl,"%3D","=")
tourl=replace(tourl,"%26","&")
end if
		 
			
if act="bdshang" then  '商家后台关联wx 已测试成功

			rs.open "select kfwxopenid from ayname where kfwxopenid='"&request.Cookies("kf_openid")&"'",conn,1,1
			if not(rs.bof and rs.eof) then
			response.Cookies("kf_access_token")=""
			response.Cookies("kf_openid")=""
			response.Write "该微信已经被关联过了！不能重复关联！"
			response.End()
			end if
			rs.close
			
			sql="select * from ayname where aynameid="&clng(request.Cookies("aynameid"))&""
			rs.open sql,conn,1,3
			if not(rs.bof and rs.eof) then


				if cint(rs("ayjibie"))=9 then 
				response.Cookies("kf_access_token")=""
				response.Cookies("kf_openid")=""
				response.Write rs("ayjibie")&"-商家信息获取失败"
				response.End()
				end if	
				
				if cint(rs("ayjibie"))=0 then 
				response.Cookies("kf_access_token")=""
				response.Cookies("kf_openid")=""
				response.Write "需要使用商家身份进行关联"
				response.End()
				end if

				if trim(rs("kfwxopenid"))<>"" and isnull(trim(rs("kfwxopenid")))=false then
				response.Cookies("kf_access_token")=""
				response.Cookies("kf_openid")=""
				response.Write "该ID已经关联过微信了！不能再次关联！"
				response.End()
				else
				rs("kfwxopenid")=request.Cookies("kf_openid")
				rs("wxkfname")=nickname
				rs.update
				response.Cookies("kf_access_token")=""
				response.Cookies("kf_openid")=""
				call logadd(request.Cookies("aynameid"),"商家后台微信关联成功",0,0)
				end if
				response.Redirect tourl
				response.End()
			end if
			rs.close
end if

if act="bdmai" then  '买家后台关联wx 已测试成功

			rs.open "select kfwxopenid from ayname where kfwxopenid='"&request.Cookies("kf_openid")&"'",conn,1,1
			if not(rs.bof and rs.eof) then
			response.Cookies("kf_access_token")=""
			response.Cookies("kf_openid")=""
			response.Write "该微信已经被关联过了！不能重复关联！"
			response.End()
			end if
			rs.close
			
			sql="select * from ayname where aynameid="&clng(request.Cookies("aynameid"))&""
			rs.open sql,conn,1,3
			if not(rs.bof and rs.eof) then

				if cint(rs("ayjibie"))=9 then 
				response.Cookies("kf_access_token")=""
				response.Cookies("kf_openid")=""
				response.Write rs("ayjibie")&"-商家信息获取失败"
				response.End()
				end if	
				
				if cint(rs("ayjibie"))<>0 then 
				response.Cookies("kf_access_token")=""
				response.Cookies("kf_openid")=""
				response.Write "需要使用买家身份进行关联"
				response.End()
				end if

				if trim(rs("kfwxopenid"))<>"" and isnull(trim(rs("kfwxopenid")))=false then
				response.Cookies("kf_access_token")=""
				response.Cookies("kf_openid")=""
				response.Write "该ID已经关联过微信了！不能再次关联！"
				response.End()
				else
				rs("kfwxopenid")=request.Cookies("kf_openid")
				rs("wxkfname")=nickname
				rs.update
				response.Cookies("kf_access_token")=""
				response.Cookies("kf_openid")=""
				call logadd(request.Cookies("aynameid"),"商家后台微信关联成功",0,0)
				end if
				response.Redirect tourl
				response.End()
			end if
			rs.close
end if


if act="log" then  '首页快捷登录 包括 买家  商家    买家首页已测试成功     商家首页已测试成功 

			sql="select * from ayname where kfwxopenid='"&request.Cookies("kf_openid")&"'"
			rs.open sql,conn,1,1
			if not(rs.bof and rs.eof) then
			
						if cint(rs("ayjibie"))=9 then
						response.Cookies("kf_access_token")=""
						response.Cookies("kf_openid")=""
						response.Write "管理员不支持该登录方式"
						response.end
						end if
			
						if cint(rs("iszhuxiao"))=1 then
						call PopErr2("系统未查询到该用户名,或已被删除","/index/log")
						response.end
						end if
						
						if cint(rs("ayusersuo"))=1 then 
						call PopErr2("该账户已被锁定","/") 
						response.end
						end if
						
						if cint(rs("isjihuo"))=1 then 
						response.Redirect("/index/jihuo")
						response.end
						end if
						
						set rslogck=server.CreateObject("adodb.recordset")
						rslogck.open "select ayshangshenhe,aylog from ayquanju" ,conn,1,1
						if cint(rslogck("ayshangshenhe"))=1 and cint(rs("ayshenhe"))=0 and cint(rs("ayjibie"))<>9 then
						call PopErr2("请联系客服专员,激活您的账号","/index/jh") 
						response.end
						end if
						if cint(rs("ayjibie"))<>9 and cint(rslogck("aylog"))=0 then
						call PopErr2("目前关闭了用户登录 请联系管理员","/") 
						response.end
						end if
						rslogck.close
						
						dtime=now()
						
						signstr=""
						response.Cookies("aymd5")=trim(rs("aymd5"))
						response.Cookies("useripd")=Look_Ip(userinip)
						response.Cookies("ayjibie")=rs("ayjibie")
						response.Cookies("ayusername")=trim(rs("ayusername"))
						response.Cookies("aynameid")=rs("aynameid") 
						response.Cookies("aytrueuser")=rs("aytrueuser") 
						response.Cookies("aymasterid")=rs("aymasterid")
						response.Cookies("kamiku")=trim(rs("kamiku"))
						response.Cookies("flku")=trim(rs("flku"))
						response.Cookies("spku")=trim(rs("spku"))
						response.Cookies("ddku")=trim(rs("ddku"))
						response.Cookies("cwku")=trim(rs("cwku"))
						response.Cookies("rzku")=trim(rs("rzku"))
						response.Cookies("logdate")=dtime
						
						signstr="ikuserdl"&rs("aynameid")&trim(rs("ayusername"))&rs("ayjibie")&trim(rs("ddku"))&trim(rs("cwku"))&trim(rs("rzku"))&dtime&ptjiami
						signstr=md5(signstr)
						response.Cookies("aysignmd5")=trim(signstr)
						
						response.Cookies("kf_access_token")=""
						response.Cookies("kf_openid")=""
						
						if cint(rs("ayjibie"))=0 then
						response.Cookies("aymd5").expires=DateAdd("m",3,now())
						response.Cookies("useripd").expires=DateAdd("m",3,now())
						response.Cookies("ayjibie").expires=DateAdd("m",3,now())
						response.Cookies("ayusername").expires=DateAdd("m",3,now())
						response.Cookies("aynameid").expires=DateAdd("m",3,now())
						response.Cookies("aytrueuser").expires=DateAdd("m",3,now())
						response.Cookies("aymasterid").expires=DateAdd("m",3,now())
						response.Cookies("kamiku").expires=DateAdd("m",3,now())
						response.Cookies("flku").expires=DateAdd("m",3,now())
						response.Cookies("spku").expires=DateAdd("m",3,now())
						response.Cookies("ddku").expires=DateAdd("m",3,now())
						response.Cookies("cwku").expires=DateAdd("m",3,now())
						response.Cookies("rzku").expires=DateAdd("m",3,now())
						response.Cookies("logdate").expires=DateAdd("m",3,now())
						response.Cookies("aysignmd5").expires=DateAdd("m",3,now())
						else
						response.Cookies("aymd5").expires=DateAdd("n",60,now())
						response.Cookies("useripd").expires=DateAdd("n",60,now())
						response.Cookies("ayjibie").expires=DateAdd("n",60,now())
						response.Cookies("ayusername").expires=DateAdd("n",60,now())
						response.Cookies("aynameid").expires=DateAdd("n",60,now())
						response.Cookies("aytrueuser").expires=DateAdd("n",60,now())
						response.Cookies("aymasterid").expires=DateAdd("n",60,now())
						response.Cookies("kamiku").expires=DateAdd("n",60,now())
						response.Cookies("flku").expires=DateAdd("n",60,now())
						response.Cookies("spku").expires=DateAdd("n",60,now())
						response.Cookies("ddku").expires=DateAdd("n",60,now())
						response.Cookies("cwku").expires=DateAdd("n",60,now())
						response.Cookies("rzku").expires=DateAdd("n",60,now())
						response.Cookies("logdate").expires=DateAdd("n",60,now())
						response.Cookies("aysignmd5").expires=DateAdd("n",60,now())
						end if
						
						
conn.Execute("Insert Into logsession(aymd5,ip,useripd,ayjibie,ayusername,aynameid,aytrueuser,aymasterid,kamiku,flku,spku,ddku,cwku,rzku,aysignmd5,logdate) values('"&request.Cookies("aymd5")&"','"&userinip&"','"&request.Cookies("useripd")&"',"&request.Cookies("ayjibie")&",'"&request.Cookies("ayusername")&"',"&request.Cookies("aynameid")&",'"&request.Cookies("aytrueuser")&"',"&request.Cookies("aymasterid")&",'"&request.Cookies("kamiku")&"','"&request.Cookies("flku")&"','"&request.Cookies("spku")&"','"&request.Cookies("ddku")&"','"&request.Cookies("cwku")&"','"&request.Cookies("rzku")&"','"&request.Cookies("aysignmd5")&"','"&dtime&"')")
						
						if cint(rs("ayjibie"))=0 then
						call logadd(rs("aynameid"),"买家微信首页快捷登录成功",0,0)
						response.Redirect weburl&"u/index"
						else
						call logadd(rs("aynameid"),"商家微信首页快捷登录成功",0,0)
						conn.execute "update ayname set aylastdata='"&now()&"',aylogcishu="&clng(rs("aylogcishu"))+1&" where aynameid="&rs("aynameid")&""
						response.Redirect weburl&"user/index"
						end if

						
			else
			response.Write "未找到该微信的关联账号，请登录后台进行关联"
			response.end
			end if
			rs.close

end if




if act="buylog" then 

			sql="select * from ayname where kfwxopenid='"&request.Cookies("kf_openid")&"'"
			
			rs.open sql,conn,1,1
			if not(rs.bof and rs.eof) then
						
						if cint(rs("iszhuxiao"))=1 then
						call PopErr2("系统未查询到该用户名,或已被删除","/index/log")
						response.end
						end if
						
						if cint(rs("ayjibie"))<>0 then
						call PopErr2("请使用买家身份登录购买，目前不支持商家身份购买","/") 
						response.end
						end if
						
						if cint(rs("ayusersuo"))=1 then 
						call PopErr2("该账户已被锁定","/") 
						response.end
						end if
						
						if cint(rs("isjihuo"))=1 then 
						response.Redirect("/index/jihuo")
						response.end
						end if
						
						set rslogck=server.CreateObject("adodb.recordset")
						rslogck.open "select ayshangshenhe,aylog from ayquanju" ,conn,1,1
						if cint(rslogck("ayshangshenhe"))=1 and cint(rs("ayshenhe"))=0 and cint(rs("ayjibie"))<>9 then
						call PopErr2("请联系客服专员,激活您的账号","/index/jh") 
						response.end
						end if
						if cint(rs("ayjibie"))<>9 and cint(rslogck("aylog"))=0 then
						call PopErr2("目前关闭了用户登录 请联系管理员","/") 
						response.end
						end if
						rslogck.close
						
						dtime=now()
			
						signstr=""
						response.Cookies("aymd5")=trim(rs("aymd5"))
						response.Cookies("useripd")=Look_Ip(userinip)
						response.Cookies("ayjibie")=rs("ayjibie")
						response.Cookies("ayusername")=trim(rs("ayusername"))
						response.Cookies("aynameid")=rs("aynameid") 
						response.Cookies("aytrueuser")=rs("aytrueuser") 
						response.Cookies("aymasterid")=rs("aymasterid")
						response.Cookies("kamiku")=trim(rs("kamiku"))
						response.Cookies("flku")=trim(rs("flku"))
						response.Cookies("spku")=trim(rs("spku"))
						response.Cookies("ddku")=trim(rs("ddku"))
						response.Cookies("cwku")=trim(rs("cwku"))
						response.Cookies("rzku")=trim(rs("rzku"))
						response.Cookies("logdate")=dtime
						
						signstr="ikuserdl"&rs("aynameid")&trim(rs("ayusername"))&rs("ayjibie")&trim(rs("ddku"))&trim(rs("cwku"))&trim(rs("rzku"))&dtime&ptjiami
						signstr=md5(signstr)
						response.Cookies("aysignmd5")=trim(signstr)
						
						'response.Cookies("kfopenid")=request.Cookies("kf_openid")
						'response.Cookies("kfopenname")=nickname

						response.Cookies("aymd5").expires=DateAdd("m",3,now())
						response.Cookies("useripd").expires=DateAdd("m",3,now())
						response.Cookies("ayjibie").expires=DateAdd("m",3,now())
						response.Cookies("ayusername").expires=DateAdd("m",3,now())
						response.Cookies("aynameid").expires=DateAdd("m",3,now())
						response.Cookies("aytrueuser").expires=DateAdd("m",3,now())
						response.Cookies("aymasterid").expires=DateAdd("m",3,now())
						response.Cookies("kamiku").expires=DateAdd("m",3,now())
						response.Cookies("flku").expires=DateAdd("m",3,now())
						response.Cookies("spku").expires=DateAdd("m",3,now())
						response.Cookies("ddku").expires=DateAdd("m",3,now())
						response.Cookies("cwku").expires=DateAdd("m",3,now())
						response.Cookies("rzku").expires=DateAdd("m",3,now())
						response.Cookies("aysignmd5").expires=DateAdd("m",3,now())
						response.Cookies("logdate").expires=DateAdd("m",3,now())
						
conn.Execute("Insert Into logsession(aymd5,ip,useripd,ayjibie,ayusername,aynameid,aytrueuser,aymasterid,kamiku,flku,spku,ddku,cwku,rzku,aysignmd5,logdate) values('"&request.Cookies("aymd5")&"','"&userinip&"','"&request.Cookies("useripd")&"',"&request.Cookies("ayjibie")&",'"&request.Cookies("ayusername")&"',"&request.Cookies("aynameid")&",'"&request.Cookies("aytrueuser")&"',"&request.Cookies("aymasterid")&",'"&request.Cookies("kamiku")&"','"&request.Cookies("flku")&"','"&request.Cookies("spku")&"','"&request.Cookies("ddku")&"','"&request.Cookies("cwku")&"','"&request.Cookies("rzku")&"','"&request.Cookies("aysignmd5")&"','"&dtime&"')")

						call logadd(request.Cookies("aynameid"),"买家微信购买页快捷登录成功",0,0)
						response.Redirect tourl
						
			else
			
			
			response.Cookies("kfopenid")=request.Cookies("kf_openid")
			response.Cookies("kfopenname")=nickname
			response.Redirect "/u/addbuyuser?l=w&url="&tourl
			
			end if
			rs.close
			
			
			
			
			
end if
%>

