<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>  
<% Response.Charset = "utf-8" %>

<!--#include virtual="/inc/conn.asp"-->
<!--#include virtual="/inc/sitecon.asp"-->
<!--#include virtual="/inc/md5.asp"-->
<%
'商家绑定80服务器

Application.Lock

'On error resume next 

appid=site_appid
appsecret=site_secret

if trim(SafeRequest("act"))="no" then
response.Write "<span style=font-size:40px>请关闭页面后,尝试重新绑定</span>"
response.End()
end if

if trim(SafeRequestform("act"))="bangding" then

	ayusername=trim(SafeRequestform("ayusername"))
	aypassword=trim(SafeRequestform("aypassword"))
	openid=trim(SafeRequestform("openid"))
	
	if ayusername="" or aypassword="" then call PopErr2("数据不完整,建议关闭页面后重新尝试绑定","/weixin/namebd.asp?act=no")
	if openid="" then call PopErr2("openid错误,建议关闭页面后重新尝试绑定","/weixin/namebd.asp?act=no")
	
	sql="select * from ayname where wxopenid='"&openid&"' and iszhuxiao=0"
	rs.open sql,conn,1,1
	if not(rs.bof and rs.eof) then
		'if trim(rs("ayusername"))<>ayusername then 
		call PopErr2("该微信号已经绑定过了err1","/weixin/namebd.asp?act=no")
		response.End()
		'end if
	end if
	rs.close
	
	
	if instr(1,ayusername,"@")>0 then
	sql="select * from ayname where aymail='"&ayusername&"' and aypassword='"&md5(aypassword)&"' and iszhuxiao=0"
	else
		if isNumeric(ayusername)=true then
		sql="select * from ayname where aytel="&ayusername&" and aypassword='"&md5(aypassword)&"' and iszhuxiao=0"
		else
		call PopErr2("请正确输入手机号或邮箱！","/index/log") 
		end if
	end if
	rs.open sql,conn,1,3
		if not(rs.bof and rs.eof) then 
			if trim(rs("wxopenid"))<>"" and isnull(trim(rs("wxopenid")))=false then 
			call PopErr2("该账号已经绑定了微信号,如有其他问题请联系管理员err3","/weixin/namebd.asp?act=no")
			response.End()
			end if
			
				set rslogck=server.CreateObject("adodb.recordset")
				rslogck.open "select ayshangshenhe from ayquanju" ,conn,1,1
				if cint(rslogck("ayshangshenhe"))=1 and cint(rs("ayshenhe"))=0 then
				call PopErr2("该账号还未审核,请联系审核专员","/weixin/namebd.asp?act=no")
				end if
				rslogck.close
			
			aynameid=rs("aynameid")
			rs("wxopenid")=openid
			rs.update
			conn.Execute("Insert Into aywxlog(aywxf,aywxt,aywxk) values('"&openid&"','bangding','bangding')")
			conn.Execute("Insert Into aylog(ayloguserid,aylogcon,aylogip,aylogleibie,aylogerr) values("&aynameid&",'绑定微信号成功','"&userinip&"',10,0)")
		else
		call PopErr2("未找到该用户名,或账号密码错误,建议关闭页面后重新尝试绑定err2","/weixin/namebd.asp?act=no")
		response.End()
		end if
	rs.close
		
	%>
	
	<br><br><div align="center"><span style="font-size:40px;color:#339900">微信号绑定成功</span></div>

<%
response.End()
end if
%>

<%
if SafeRequest("state")="bd" then
%>


<script language="JScript" runat="Server">
    function toObject(json) {
        eval("var o=" + json);
        return o;
    }
</script>
<%
function getHTTPopenid(url,cset) 
dim Http 
set Http=server.createobject("MSXML2.XMLHTTP") 
Http.open "GET",url,false 
Http.send() 
if Http.readystate<>4 then exit function 
getHTTPopenid=bytesToBSTR(Http.responseBody,cset) 
set http=nothing 
if err.number<>0 then err.Clear  
end function 


code=trim(SafeRequest("code")) '获取code

if code<>"" then

openidstr=getHTTPopenid("https://api.weixin.qq.com/sns/oauth2/access_token?appid="&appid&"&secret="&appsecret&"&code="&code&"&grant_type=authorization_code","gb2312")
Set json = toObject(openidstr)
openid=json.openid
Set json = Nothing 

end if

'response.Write code&"<br>"
'response.Write openid
%>
<style type="text/css">
.newlogin{
    background-color: #02aaf1;
	color: #FFFFFF;
    font-weight: bold;
	border: medium none;
	width:250px;
	height:62px;
	font-size:40px;
}
</style>
<br><br><br>
<form action="" method="post" name="form1" id="form1">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="50%" height="35" align="right">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="60" align="right"><span style="font-size:40px;">手机号/邮箱</span>&nbsp;</td>
    <td>&nbsp;<input name="ayusername" type="text" id="ayusername" style="height:40px; width:280px;" /></td>
  </tr>
  <tr>
    <td height="60" align="right"><span style="font-size:40px;">登录密码</span>&nbsp;</td>
    <td>&nbsp;<input name="aypassword" type="password" id="aypassword" style="height:40px; width:280px;"/></td>
  </tr>
  <tr>
    <td height="80" colspan="2" align="center"> <input name="act" type="hidden" id="act" value="bangding" /><input name="openid" type="hidden" id="openid" value="<%=trim(openid)%>" />
      <input style="cursor:pointer"  class="newlogin" type="submit" name="Submit" id="tijiao" value=" 绑 定 账 号 "/></td>
    </tr>
</table>
</form>

<%end if

Application.Unlock%>


