<!--#include file="config.asp"-->
<%
''' EYCMS 支付宝登录绑定
''' ==================================================================

	dim alipay_user_id:alipay_user_id=eycms.loadsession("alipay_user_id")
	dim api_backurl:api_backurl=eycms.fget("api_backurl",0)
	dim nickname:nickname=eycms.enhtml(eycms.fget("nickname",0))
	
	function check_user_info()
		dim t0:t0=false
		if eycms.strlen(alipay_user_id)=0 then
			t0=true
		end if
		check_user_info=t0
	end function
	
	sub get_user_info()
		if check_user_info then
			eycms.echo "&#20250;&#35805;&#36807;&#26399;&#65292;&#26080;&#27861;&#30331;&#24405;"
			eycms.die
			exit sub
		end if
	end sub
	
	sub checkbind()
		if check_user_info then
			eycms.echo "0&#20250;&#35805;&#36807;&#26399;&#65292;&#26080;&#27861;&#32465;&#23450;"
			exit sub
		end if
		
		dim t0,t1,t2,data,arr
		t0=eycms.enhtml(eycms.fpost("username",0))
		t1=eycms.enhtml(eycms.fpost("password",0))
		if eycms.strlen(t0)=0 then
			eycms.ajaxjson "&#36134;&#25143;&#19981;&#33021;&#20026;&#31354;",0
			exit sub
		end if
		if not(eycms.checkstr(t0,"username")) then
			eycms.ajaxjson "&#36134;&#25143;&#19981;&#31526;&#21512;&#35201;&#27714;",0
			exit sub
		end if
		if eycms.strlen(t1)=0 then
			eycms.ajaxjson "&#23494;&#30721;&#19981;&#33021;&#20026;&#31354;",0
			exit sub
		end if
		if not(eycms.checkstr(t1,"password")) then
			eycms.ajaxjson "&#23494;&#30721;&#19981;&#31526;&#21512;&#35201;&#27714;",0
			exit sub
		end if
		data=eycms.db.dbload(1,"id,islock,logintimes","ey_user","username='"&t0&"' and password='"&md5(t1)&"'","")
		if ubound(data)<0 then
			eycms.ajaxjson "&#36134;&#25143;&#25110;&#23494;&#30721;&#38169;&#35823;&#65292;&#26080;&#27861;&#32465;&#23450;",0
		else
			select case data(1,0)
				case "-1":eycms.ajaxjson "&#36134;&#25143;&#34987;&#38145;&#23450;&#65292;&#26080;&#27861;&#32465;&#23450;",0:exit sub
				case "0":eycms.ajaxjson "&#36134;&#25143;&#26410;&#36890;&#36807;&#37038;&#31665;&#39564;&#35777;&#65292;&#26080;&#27861;&#32465;&#23450;",0:exit sub
			end select
			eycms.db.dbupdate "ey_user","id="&data(0,0)&"",array(array("logintimes",data(2,0)+1,0,0),array("lastlogindate",now(),50,1),array("lastloginip",eycms.getip,50,1))
			eycms.db.insert "ey_user_bind",array(array("openid",alipay_user_id,255,1),array("opentype","alipay",50,1),array("userid",data(0,0),0,0))
			eycms.resetuserinfo "u.id="&data(0,0)&""
			eycms.setsession "alipay_user_id",""
			eycms.setsession "api_backurl",""
			eycms.ajaxjson "&#32465;&#23450;&#25104;&#21151;",1
		end if
	end sub
	
	sub checkdb()
		if check_user_info then
			eycms.echo "0&#20250;&#35805;&#36807;&#26399;&#65292;&#26080;&#27861;&#32465;&#23450;"
			exit sub
		end if
		if not(eycms.getsys("isreg")) then
			eycms.echo "0&#31995;&#32479;&#26410;&#24320;&#21551;&#20250;&#21592;&#27880;&#20876;&#21151;&#33021;"
			exit sub
		end if
		dim t0,t1,t1_1,t2,t3,t4,data
		t0="alipay"&left(eycms.getrndfilename,16)
		t1=eycms.enhtml(eycms.fpost("password",0))
		t1_1=eycms.enhtml(eycms.fpost("repass",0))
		t2=eycms.enhtml(eycms.fpost("email",0))
		t3=eycms.enhtml(eycms.fpost("imgcode",0))
		t4=eycms.enhtml(eycms.fpost("nickname",0))
	
		if not(eycms.checkpost) then
			eycms.ajaxjson "禁止外部提交数据",0
			exit sub
		end if

		if eycms.strlen(t1)=0 then
			eycms.ajaxjson "密码不能为空",0
			exit sub
		end if
		if not(eycms.checkstr(t1,"password")) then
			eycms.ajaxjson "密码由6-16位数字、字母组成",0
			exit sub
		end if
		if eycms.strlen(t1_1)=0 then
			eycms.ajaxjson "确认密码不能为空",0
			exit sub
		end if
		if t1<>t1_1 then
			eycms.ajaxjson "确认密码不正确",0
			exit sub
		end if
		if eycms.strlen(t4)=0 then
			eycms.ajaxjson "昵称不能为空",0
			exit sub
		end if
		if eycms.strlen(t2)=0 then
			eycms.ajaxjson "电子邮箱不能为空",0
			exit sub
		end if
		if not(eycms.checkstr(t2,"email")) then
			eycms.ajaxjson "邮箱格式不正确",0
			exit sub
		end if
		data=eycms.db.dbcount("ey_user","email='"&t2&"'")
		if data>0 then
			eycms.ajaxjson "邮箱已存在，请换个试试",0
			exit sub
		end if
		if eycms.strlen(t3)=0 then
			eycms.ajaxjson "验证码不能为空",0
			exit sub
		end if
		if eycms.loadsession("imgcode")<>t3 then
			eycms.ajaxjson "验证码错误",0
			exit sub
		end if
		dim groupid:groupid=eycms.getsys("groupid")
		dim useract:useract=eycms.getsys("ispass")
		dim somecode:somecode=eycms.getrnd(10)
		
		data=array(array("username",t0,20,1),array("password",md5(t1),16,1),array("nickname",t4,10,1),array("email",t2,50,1),array("groupid",groupid,0,0),array("islock",useract,2,0),array("createdate",sqltime,0,0),array("createip",eycms.getip,50,1),array("logintimes",1,1,0),array("somecode",somecode,50,1),array("percent",23,2,0),array("amount",0,2,0),array("point",0,2,0),array("isvip",0,2,0),array("avatar",0,0,0))
		eycms.db.insert "ey_user",data
		
		dim userid:userid=eycms.db.insertid("id","ey_user")
		eycms.db.insert "ey_user_bind",array(array("openid",alipay_user_id,255,1),array("opentype","alipay",50,1),array("userid",userid,0,0))
		select case useract
			case "0"
				dim subject,body
				subject=eycms.getsys("regtitle")
				body=eycms.dehtml(eycms.getsys("regcontent"))
				body=replace(body,"{webname}",eycms.getsys("webname"))
				body=replace(body,"{url}","{weburl}/user/reg.asp?act=reg&uid="&left(t0,20)&"&code="&somecode&"")
				body=replace(body,"{weburl}",weburl)
				eycms.sendmail t2,subject,body
				eycms.ajaxjson "1&#36134;&#25143;&#27880;&#20876;&#25104;&#21151;&#65292;&#35831;&#26597;&#25910;&#37038;&#31665;&#40;"&t2&"&#41;&#65292;&#24182;&#28608;&#27963;&#24744;&#30340;&#36134;&#25143;&#65281;",1
			case "-1"
				eycms.ajaxjson "2&#36134;&#25143;&#27880;&#20876;&#25104;&#21151;&#65292;&#38656;&#35201;&#31649;&#29702;&#21592;&#23457;&#26680;&#21518;&#25165;&#33021;&#30331;&#24405;&#65281;",1
			case "1"
				eycms.resetuserinfo("u.username='"&t0&"'")
				eycms.userreg_auto userid
				eycms.ajaxjson "3&#36134;&#25143;&#27880;&#20876;&#25104;&#21151;&#65281;",1
		end select
		if eycms.resetuserinfo("u.id="&userid&"") then
			eycms.setsession "alipay_user_id",""
			eycms.setsession "api_backurl",""
		else
			eycms.ajaxjson "&#36134;&#25143;&#32465;&#23450;&#22833;&#36133;",0
		end if
	end sub
	dim act:act=lcase(eycms.fget("act",0))
	select case act
		case "bind":checkbind()
		case "db":checkdb()
		case else
			if eycms.is_login then
			dim userdata:userdata=eycms.userinfo
				dim userid:userid=userdata(0)
				if eycms.db.dbcount("ey_user_bind","userid="&userid&" and opentype='alipay'")=0 then
					eycms.db.insert "ey_user_bind",array(array("openid",alipay_user_id,255,1),array("opentype","alipay",50,1),array("userid",userid,0,0))
				end if
				eycms.setsession "alipay_user_id",""
				eycms.setsession "api_backurl",""
				eycms.go api_backurl
			else
				eycms.show theme_userapibind,""
			end if
	end select
%>
