<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> 
<% Response.Charset = "utf-8" %>
<!--#include virtual="/inc/conn.asp"-->  
<!--#include virtual="/inc/sitecon.asp"-->  
<%
	
	On error resume next 
	
	'获取 echostr 校验
	if SafeRequest("echostr")<>"" then
	response.Write SafeRequest("echostr")
	response.End()
	end if
	
	set rs2=server.CreateObject("adodb.recordset")

	'微信内置 时间格式
	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
	
	'获取来源数据
    set xml_dom = Server.CreateObject("MSXML2.DOMDocument")
    xml_dom.load request   
    ToUserName=xml_dom.getelementsbytagname("ToUserName").item(0).text    
    FromUserName=xml_dom.getelementsbytagname("FromUserName").item(0).text 	'访问者 openid
    CreateTime=xml_dom.getelementsbytagname("CreateTime").item(0).text  
	MsgType=xml_dom.getelementsbytagname("MsgType").item(0).text  			'event公众号菜单点击	image图片	text公众号直接对话
	
	'MsgType空值 返回错误
	if trim(MsgType)="" then
		response.End()
	end if
	
	'快捷回复
	Function tohuifu(fuser,touser,totime,fangshi,con)
	strresponse=""
	strresponse="<xml>"
	strresponse=strresponse&"<ToUserName><![CDATA["&fuser&"]]></ToUserName>"
	strresponse=strresponse&"<FromUserName><![CDATA["&touser&"]]></FromUserName>"
	strresponse=strresponse&"<CreateTime>"&totime&"</CreateTime>"
	strresponse=strresponse&"<MsgType><![CDATA["&fangshi&"]]></MsgType>"
	strresponse=strresponse&"<Content><![CDATA["&con&"]]></Content>"
	strresponse=strresponse&"</xml>"
	tohuifu=strresponse
	End Function
		
		
	'公众号菜单  event   点击访问 开始
	if trim(MsgType)="event" then
		EventKey=xml_dom.getelementsbytagname("EventKey").item(0).text 
	
		'EventKey空值 返回错误
		if trim(EventKey)="" then
			response.End()
		end if
			
			'########## 不需要获取商家绑定操作 #############################################################################
			
			'公众号 买家订单 EventKey = a003
			if trim(EventKey)="a003" then
			strsend=""
			set rska=server.CreateObject("adodb.recordset")
			
						For sum=0 To 5
						if sum=0 then ddku="aydingdan"
						if sum=1 then ddku="aydingdan01"
						if sum=2 then ddku="aydingdan02"
						if sum=3 then ddku="aydingdan03"
						if sum=4 then ddku="aydingdan04"
						if sum=5 then ddku="aydingdan05"
											
								sql="select top 5 aydingdanuser,openid,aydingdanzt,aydingdanid,dailikuo,ayddquantity,aydingdansum,aydingdansj,aydingdanzj from "&ddku&" where openid='"&trim(FromUserName)&"' and aydingdanzt=1 order by aydingdanid desc" 
								rs.open sql,conn,1,1
								if not(rs.bof and rs.eof) then  
								
									do while not(rs.eof)
										buykami=""
										
										if trim(rs("dailikuo"))<>"" then '代理商品
										dailiuserstr=split(dailistr(Ubound(dailistr)),",") '获取最后一个代理关系 总代 卡密库
										kamiku=dailiuserstr(5)
										else
										kamiku=getkamikuname(rs("aydingdanuser"))
										end if
	
										'kamistr=split(trim(rs("aydingdanka")),",")
										'if Ubound(kamistr)>9 then sum=10:else:sum=Ubound(kamistr):end if
											
										'For dianka=0 To sum
											rska.open "select top 10 aykasuoyoudd,aykapass,ayka from "&kamiku&" where aykasuoyoudd='"&rs("aydingdansum")&"'",conn,1,1
											if not(rska.bof and rska.eof) then  
											do while not rska.eof
												if trim(rska("aykapass"))<>"0" then
												buykami=buykami&"卡号:"&trim(rska("ayka"))&CHR(10)&"密码:"&trim(rska("aykapass"))&CHR(10)
												else
												buykami=buykami&"卡号:"&trim(rska("ayka"))&CHR(10)
												end if
											rska.movenext	
											loop
											end if
											rska.close
										'next
										if ccur(rs("ayddquantity"))>10 then buykami=buykami&"卡密数量过多,请登录官网查看 "&wapurl&"tika?ka="&trim(rs("aydingdansum"))&""&CHR(10)
										
										strsend=strsend&"订单:"&trim(rs("aydingdansum"))&CHR(10)&"时间:"&rs("aydingdansj")&CHR(10)&"金额:"&Formatnumber(rs("aydingdanzj"),2,true,-1,0)&"元"&CHR(10)&buykami&CHR(10)
			
									rs.movenext
									loop
								end if
								rs.close
						next
						
			if strsend="" then strsend="未找到订单信息"
			conn.Execute("Insert Into aywxlog(aywxf,aywxt,aywxk,ayhfcon) values('"&FromUserName&"','"&MsgType&"','"&EventKey&"','"&strsend&"')")
			response.Write tohuifu(FromUserName,ToUserName,ToUnixTime(now,+8),"text",strsend)
			response.End
			end if
			
	
			'公众号 爱卡资费 EventKey = z001
			if trim(EventKey)="z001" then
			strcon=strcon&"[资费]"&CHR(10)
			strcon=strcon&"网银:2%"&CHR(10)
			strcon=strcon&"财付通:2%"&CHR(10)
			strcon=strcon&"支付宝:2%"&CHR(10)
			strcon=strcon&"微信:2%"&CHR(10)
			strcon=strcon&"京东:2%"&CHR(10)
			strcon=strcon&"手Q:2%"&CHR(10)&CHR(10)
			strcon=strcon&"提示:购买套餐,可享受最低1.5%佣金,详情登陆爱卡官网www.xxxxx.cn查看"&CHR(10)
			conn.Execute("Insert Into aywxlog(aywxf,aywxt,aywxk,ayhfcon) values('"&FromUserName&"','"&MsgType&"','"&EventKey&"','"&strcon&"')")
			response.Write tohuifu(FromUserName,ToUserName,ToUnixTime(now,+8),"text",strcon)
			response.end
			end if
			
						
			'公众号 结算模式 EventKey = z002
			if trim(EventKey)="z002" then
			strcon=""
			strcon=strcon&"[结算模式]"&CHR(10)
			strcon=strcon&"手动提现模式"&CHR(10)
			strcon=strcon&"起结金额:100元"&CHR(10)
			strcon=strcon&"到账时间:次日10-13点到账"&CHR(10)
			strcon=strcon&"结算手续费:免费"&CHR(10)&CHR(10)
			strcon=strcon&"其他结算模式："&CHR(10)
			strcon=strcon&"自动结算，加急提现，秒结算"&CHR(10)
			strcon=strcon&"详细咨询请联系平台客服"&CHR(10)
			conn.Execute("Insert Into aywxlog(aywxf,aywxt,aywxk,ayhfcon) values('"&FromUserName&"','"&MsgType&"','"&EventKey&"','"&strcon&"')")
			response.Write tohuifu(FromUserName,ToUserName,ToUnixTime(now,+8),"text",strcon)
			response.end
			end if
						
		
			'########## 需要获取商家绑定操作 ###############################################################################
				
			'已绑定的商家 初始商家信息
			isnext="no"		'商家是否已关联
			isjihuo="no"	'商家是否已激活
			isrenzheng="no"	'商家是否已认证
			isshiming="yes"	'是否已实名认证
			sql="select top 1 * from ayname where wxopenid='"&trim(FromUserName)&"' and iszhuxiao=0 and ayusersuo=0"
			rs.open sql,conn,1,1
			if not(rs.bof and rs.eof) then
				aynameid=rs("aynameid")
				aychengjiao=rs("aychengjiao")
				ayyue=rs("ayyue")
				ayjiesuanms=rs("ayjiesuanms")
				ddku=trim(rs("ddku"))
				cwku=trim(rs("cwku"))
				rzku=trim(rs("rzku"))
				ayjibie=rs("ayjibie")
				tousutixian=rs("tousutixian")
				issprz=rs("issprz")
				ischengxin=rs("ischengxin")
				tixianjin=fix(rs("ayyue"))
				isnext="yes"
				if cint(rs("isjihuo"))=0 then isjihuo="yes"
				if cint(rs("istelrz"))=1 then isrenzheng="yes"
				if cint(rs("isqzsfz"))=1 and cint(rs("sfzisok"))<>1 then isshiming="no"
			end if
			rs.close
			
			'商家未关联
			if isnext="no" then
			strsend="请先绑定商家账号,再进行相关操作,绑定方法[点击我的账号-商家绑定]"
			response.Write tohuifu(FromUserName,ToUserName,ToUnixTime(now,+8),"text",strsend)
			response.End()
			end if
			
			'商家未激活
			if isjihuo="no" then
			strsend="商家ID未激活，请登录PC端进行激活再操作。"
			response.Write tohuifu(FromUserName,ToUserName,ToUnixTime(now,+8),"text",strsend)
			response.End()
			end if
			
			'获取财务库标识
			select case	trim(cwku)
			case "aycaiwu"
			randarrcwku="A"
			case "aycaiwu01"
			randarrcwku="B"
			case "aycaiwu02"
			randarrcwku="C"
			case "aycaiwu03"
			randarrcwku="D"
			case "aycaiwu04"
			randarrcwku="E"
			case "aycaiwu05"
			randarrcwku="F"
			case else
			randarrcwku="A"
			end select
			
			'普通提现 与 加急提现 需要的参数 和 相关校验
			if trim(EventKey)="u003" or trim(EventKey)="u005" then
				set rsjs=server.CreateObject("adodb.recordset")
				set rslist=server.CreateObject("adodb.recordset")
				
				'商家级别 高风险
				if cint(ayjibie)=1 then
				strsend="当前商家级别,请登录PC进行提现操作。"
				response.Write tohuifu(FromUserName,ToUserName,ToUnixTime(now,+8),"text",strsend)
				response.End()
				end if
						
				'商家 非手动提现模式
				if cint(ayjiesuanms)<>5 then
				strsend="仅支持手动提现商户进行提现操作。"
				response.Write tohuifu(FromUserName,ToUserName,ToUnixTime(now,+8),"text",strsend)
				response.End()
				end if
			
				'商家未认证
				if isrenzheng="no" then
				strsend="请先登录PC端，手机认证后再继续操作。"
				response.Write tohuifu(FromUserName,ToUserName,ToUnixTime(now,+8),"text",strsend)
				response.End()
				end if

				'商家需要强制实名
				if isshiming="no" then
				strsend="请先登录PC端，实名认证后再继续操作。"	
				response.Write tohuifu(FromUserName,ToUserName,ToUnixTime(now,+8),"text",strsend)
				response.End()
				end if
				
				'获取默认结算收款账户ID
				sqljs="select top 1 * from ayjiesuanid where ayjuid="&aynameid&" and ayjismoren=1 and ayjisdel=0" 
				rsjs.open sqljs,conn,1,1
				if rsjs.bof and rsjs.eof then 
				strsend="还未设置结算账号，或未设置默认结算账号，请先登陆PC端添加一个结算账号"
				response.Write tohuifu(FromUserName,ToUserName,ToUnixTime(now,+8),"text",strsend)
				response.End()
				end if
				ayjid=rsjs("ayjid")
				shoukuanfs=rsjs("ayjfs")
				rsjs.close
				
				'检测是否需要先处理投诉问题
				if cint(tousutixian)<>2 then
					if cint(tousutixian_sys)=1 or cint(tousutixian)=1 then
							rslist.open "select top 1 ayjubaouid,ayjuzt,ayjubaoid from ayjubao where ayjubaouid="&aynameid&" and ayjuzt=0",conn,1,1
							if not(rslist.bof and rslist.eof) then
							strsend="请先处理投诉，再继续操作。"	
							response.Write tohuifu(FromUserName,ToUserName,ToUnixTime(now,+8),"text",strsend)
							response.End()
							end if
							rslist.close
					end if
				end if
				
				'商家需要商品认证
				if cint(issprz)=1 then
					sql2="select * from aypicsite where aypuid="&aynameid&" and aypfang=1" 
					rs2.open sql2,conn,1,1
					if rs2.bof and rs2.eof then
					strsend="请先登录PC端，商品认证后再继续操作。"	
					response.Write tohuifu(FromUserName,ToUserName,ToUnixTime(now,+8),"text",strsend)
					response.End()
					else
						do while not rs2.eof
							if cint(rs2("aypzt"))=0 or cint(rs2("aypzt"))=2 then
							strsend="请先登录PC端，商品认证后再继续操作。"	
							response.Write tohuifu(FromUserName,ToUserName,ToUnixTime(now,+8),"text",strsend)
							response.End()
							exit do
							end if
						rs2.movenext
						loop
					end if
					rs2.close
				end if
				
				'创建随机数
				Function randarr()
				Const stringmodarr = "0123456789"
				strlength = Len(stringmodarr)
				Randomize timer
				For i = 1 To 5 
				randarr2 = randarr2&Mid(stringmodarr,Int(Rnd * strlength + 1),1)  
				Next 
				randarr=randarr2
				End Function
				crandarrlei="C"
				crandarrhost="A"
				
				'获取全局参数
				rs.open "select tousutixian,aytixianjin,iskqjiaji,jiajifei from ayquanju",conn,1,1
				aytixianjin=rs("aytixianjin")	 	'最低提现金额
				tousutixian_sys=rs("tousutixian")	'有未撤销投诉禁止提现 1是 默认0否
				iskqjiaji=rs("iskqjiaji")		    '全局是否开启加急提现
				jiajifei=trim(rs("jiajifei"))		'加急手续费
				rs.close
				
				'获取商家级别
				if cint(ayjibie)<>5 and cint(ayjibie)<>9 then
				rs2.open "select ayujbgid,ayujbtxje,ayujbjjf from ayujb where ayujbgid="&rs("ayjibie")&"",conn,1,1
				aytixianjin=rs2("ayujbtxje")	  '允许提现最小金额
				'aytxfei=trim(rs("ayujbtxfei"))	  '提现费用
				jiajifei=trim(rs("ayujbjjf"))     '加急手续费
				rs2.close
				end if 
			
				'提现余额不足
				if ccur(tixianjin)<ccur(aytixianjin) then
				strsend="账号余额低于提现金额要求。"
				response.Write tohuifu(FromUserName,ToUserName,ToUnixTime(now,+8),"text",strsend)
				response.End()
				end if
			
				'创建财务流水号
				crandarrlast=Right(("00"&Month(Now())),2)&Right(("00"&Day(Now())),2)&Right(("00"&hour(Now())),2)&Right(("00"&minute(Now())),2)&Right(("00"&second(Now())),2)&randarr()
				crandarr=crandarrlei&crandarrhost&randarrcwku&crandarrlast
				
			end if
			
			
			'公众号 余额提现 EventKey = u003
			if trim(EventKey)="u003" then
			Application.Lock
						'余额处理
						sql="select * from ayname where aynameid="&aynameid&" and iszhuxiao=0"
						rs.open sql,conn,1,3
						tixianjin=fix(rs("ayyue"))
						aytxyue=rs("ayyue")
						shouxufei="实际到账"&tixianjin&"元" 
						tixianyu=ccur(rs("ayyue"))-ccur(tixianjin)
						rs("ayyue")=ccur(rs("ayyue"))-ccur(tixianjin)
						rs.update
						rs.close
	
						conn.Execute("Insert Into "&cwku&"(aycaiwuleibie,aycailiushui,aycaiwujine,aycaiwuuser,aycaiwudate,aycaiwulastjin,aycaiwuyue,aycaiwutxcl,aycaiwujsms,aycaiwubody,aytxyue,ayjslistid,aycaiwufangshi) values('9','"&crandarr&"','"&tixianjin&"','"&clng(aynameid)&"','"&now()&"','"&tixianjin&"','"&tixianyu&"','2','5','"&trim(shouxufei)&"',"&aytxyue&","&ayjid&",'"&shoukuanfs&"')")
						conn.Execute("Insert Into "&rzku&"(ayloguserid,aylogcon,aylogip,aylogleibie,aylogerr) values("&aynameid&",'公众号-"&username&"提现"&tixianjin&"成功"&"','"&userinip&"','10','0')")
						
						strsend="提现成功"&CHR(10)
						strsend=strsend&"提现金额:"&tixianjin&"元"&CHR(10)
						strsend=strsend&"应到金额:"&tixianjin&"元"&CHR(10)
						strsend=strsend&"手续费:0元"&CHR(10)
						strsend=strsend&"账号余额:"&Formatnumber(tixianyu,2,true,-1,0)&"元"&CHR(10)
						strsend=strsend&"提现时间:"&now()&CHR(10)&CHR(10)
						strsend=strsend&"[提示:提现金额为整数,提现方式为:普通提现]"&CHR(10)
				
						conn.Execute("Insert Into aywxlog(aywxf,aywxt,aywxk,ayhfcon) values('"&FromUserName&"','"&MsgType&"','"&EventKey&"','"&strsend&"')")
						response.Write tohuifu(FromUserName,ToUserName,ToUnixTime(now,+8),"text",strsend)
			Application.Unlock
			response.end
			end if
			
			
			
			
			
			'公众号 加急提现 EventKey = u005
			if trim(EventKey)="u005" then
				isjiaji=1	'加急提现 
				
				'平台未开启加急提现
				if cint(iskqjiaji)=1 then
				strsend="平台未开启加急提现，请使用普通提现"
				response.Write tohuifu(FromUserName,ToUserName,ToUnixTime(now,+8),"text",strsend)
				response.End()
				end if
				
				'非可信商家 不支持加急提现
				if cint(ischengxin)=0 then
				strsend="只有可信商家支持加急提现，请登录PC端查看或咨询平台客服"
				response.Write tohuifu(FromUserName,ToUserName,ToUnixTime(now,+8),"text",strsend)
				response.End()
				end if
				
				'加急提现手续费分析
				aytxfeis=split(jiajifei,"%")
				if Ubound(aytxfeis)>0 then
					lei=1  '百分比
					jiajifei=replace(jiajifei,"%"," ")
					jiajifei=trim(jiajifei)
				else
					lei=0 '元
					jiajifei=trim(jiajifei)
				end if
								
				'商家余额处理
				sql="select * from ayname where aynameid="&aynameid&" and iszhuxiao=0"
				rs.open sql,conn,1,3
				tixianjin=fix(rs("ayyue"))
				aytxyue=rs("ayyue")				'提现余额
				
				'加急手续费计算	jiajif
				if lei=0 then	 				'元
				jiajif=jiajifei  				'加急手续费
				elseif lei=1 then 				'百分比
				jiajif=jiajifei/100*tixianjin   '加急手续费
				end if
				if ccur(jiajif)<1 then jiajif=1 '加急手续费下限
				
				sjjmoney=rs("jjmoney")			'账户剩余减免佣金流量
				thismianyong=0					'本次使用免佣金流量
				feiyong=jiajif					'本次应产生的手续费
				if ccur(sjjmoney)>0 then
					if ccur(sjjmoney)>=ccur(jiajif) then    		'免佣金流量大于费用
						sjjmoney=ccur(sjjmoney)-ccur(jiajif)		'应除扣后的 剩余免佣流量 sjjmoney
						feiyong=0									'加急手续费
						thismianyong=jiajif							'本次使用免佣金流量
					else      										'免佣金流量小于费用=======
						sjjmoney=0									'应除扣后的 剩余免佣流量 sjjmoney=0   
						feiyong=ccur(jiajif)-ccur(rs("jjmoney"))	'加急手续费
						thismianyong=rs("jjmoney")					'本次使用免佣金流量	
					end if
				end if
				
				'提现后账户余额
				tixianyu=ccur(rs("ayyue"))-ccur(tixianjin)
				
				rs("ayyue")=ccur(rs("ayyue"))-ccur(tixianjin)
				rs("jjmoney")=sjjmoney
				shouxufei="实际到账"&ccur(tixianjin)-ccur(feiyong)&"元" 
				rs.update
				rs.close
					
				conn.Execute("Insert Into "&cwku&"(aycaiwuleibie,aycailiushui,aycaiwujine,aycaiwuuser,aycaiwudate,aycaiwulastjin,aytxjj,jjmoney,aycaiwuyue,aycaiwutxcl,aycaiwujsms,aycaiwubody,aytxyue,ayjslistid,aycaiwufangshi) values('9','"&crandarr&"','"&tixianjin&"','"&clng(aynameid)&"','"&now()&"','"&ccur(tixianjin)-ccur(feiyong)&"','"&isjiaji&"','"&sjjmoney&"','"&tixianyu&"','2','5','"&trim(shouxufei)&"',"&aytxyue&","&ayjid&",'"&shoukuanfs&"')")
				conn.Execute("Insert Into "&rzku&"(ayloguserid,aylogcon,aylogip,aylogleibie,aylogerr) values("&aynameid&",'公众号-"&username&"加急提现"&tixianjin&"成功"&"','"&userinip&"','10','0')")
			
				strsend="加急提现成功"&CHR(10)
				strsend=strsend&"提现金额:"&tixianjin&"元"&CHR(10)
				strsend=strsend&"应到金额:"&ccur(tixianjin)-ccur(feiyong)&"元"&CHR(10)
				strsend=strsend&"手续费:"&Formatnumber(feiyong,2,true,-1,0)&"元"&CHR(10)
				strsend=strsend&"使用流量:"&Formatnumber(thismianyong,2,true,-1,0)&"元"&CHR(10)
				strsend=strsend&"账号余额:"&Formatnumber(tixianyu,2,true,-1,0)&"元"&CHR(10)
				strsend=strsend&"提现时间:"&now()&CHR(10)&CHR(10)
				strsend=strsend&"[提示:提现金额为整数,提现方式为:加急提现]"&CHR(10)
				conn.Execute("Insert Into aywxlog(aywxf,aywxt,aywxk,ayhfcon) values('"&FromUserName&"','"&MsgType&"','"&EventKey&"','"&strsend&"')")
				response.Write tohuifu(FromUserName,ToUserName,ToUnixTime(now,+8),"text",strsend)
			
			response.end
			end if
			
			
			
			
			
			
			'公众号 最近10笔订单记录 EventKey = a001
			if trim(EventKey)="a001" then
				strsend=strsend&"[最近10笔成功订单]"&CHR(10)
				sql="select top 10 * from "&ddku&" where aydingdanuser="&aynameid&" and aydingdanzt=1 order by aydingdanid asc" 
				rs.open sql,conn,1,1
					if not(rs.bof and rs.eof) then  
					do while not(rs.eof)
					strsend=strsend&"订单:"&trim(rs("aydingdansum"))&CHR(10)&"时间:"&rs("aydingdansj")&CHR(10)&"金额:"&Formatnumber(rs("aydingdanzj"),2,true,-1,0)&"元 [支付成功]"&CHR(10)&CHR(10)
					rs.movenext
					loop
					end if
				rs.close
			conn.Execute("Insert Into aywxlog(aywxf,aywxt,aywxk,ayhfcon) values('"&FromUserName&"','"&MsgType&"','"&EventKey&"','"&strsend&"')")
			conn.Execute("Insert Into "&rzku&"(ayloguserid,aylogcon,aylogip,aylogleibie,aylogerr) values("&aynameid&",'公众号-查询20笔成功订单"&"','"&userinip&"','10','0')")
			response.Write tohuifu(FromUserName,ToUserName,ToUnixTime(now,+8),"text",strsend)
			response.end
			end if
		
		
		
		
		
			'公众号最近20笔财务流水 EventKey = a002
		
		
		
			'公众号 账号余额 EventKey=u001
			if trim(EventKey)="u001" then
					sql="select * from ayname where aynameid="&aynameid&" and iszhuxiao=0" 
					rs.open sql,conn,1,1
					if not(rs.bof and rs.eof) then
						strsend=strsend&"[账号余额]"&CHR(10)
						strsend=strsend&"账号余额:"&Formatnumber(rs("ayyue"),2,true,-1,0)&"元"&CHR(10)
						strsend=strsend&"收款姓名:"&trim(rs("aytrueuser"))&CHR(10)
						strsend=strsend&"商户级别:"&getjbname(rs("ayjibie"))&CHR(10)
						strsend=strsend&"最后登录:"&rs("aylastdata")&CHR(10)&CHR(10)
					end if
					rs.close
			conn.Execute("Insert Into aywxlog(aywxf,aywxt,aywxk,ayhfcon) values('"&FromUserName&"','"&MsgType&"','"&EventKey&"','"&strsend&"')")
			conn.Execute("Insert Into "&rzku&"(ayloguserid,aylogcon,aylogip,aylogleibie,aylogerr) values("&aynameid&",'公众号-查询余额"&"','"&userinip&"','10','0')")
			response.Write tohuifu(FromUserName,ToUserName,ToUnixTime(now,+8),"text",strsend)
			response.end
			end if
		
		
		
		
			'公众号 今日销量 EventKey=u002
			if trim(EventKey)="u002" then
					jindd=0
					jinkami=0
					jinjin=0
					jlir=0
					chengben=0
					jincb=0
					strsend=strsend&"[今日销量]"&CHR(10)
					strsend=strsend&"账号余额:"&Formatnumber(ayyue,2,true,-1,0)&"元"&CHR(10)
					sql="select * from "&ddku&" where aydingdanuser="&aynameid&" and datediff(day,aydingdansj,'"&now()&"')=0 and aydingdanzt=1" 
					rs.open sql,conn,1,1
					if not(rs.bof and rs.eof) then
						do while not rs.eof 
						jinkami=ccur(jinkami)+ccur(rs("ayddquantity"))
						jinjin=ccur(jinjin)+ccur(rs("aydingdanzj"))
	
						chengben=ccur(rs("ayddquantity"))*ccur(rs("aydingdancb"))		
						jincb=ccur(jincb)+ccur(chengben)  								 '今日累计成本
						jlir=ccur(rs("aydingdanzj"))-ccur(chengben) 					 '今日利润
						jfei=ccur(jfei)+ccur(rs("aydingdanzj"))-ccur(rs("aydingdanin"))  '今日手续费
						fei=ccur(rs("aydingdanzj"))-ccur(rs("aydingdanin"))
						jlirun=ccur(jlirun)+ccur(jlir)-ccur(fei)        '今日累计利润
						
						rs.movenext
						loop
					jindd=rs.RecordCount
					end if
					rs.close
					strsend=strsend&"今日成功订单:"&jindd&"笔"&CHR(10)
					strsend=strsend&"今日卖出卡密:"&jinkami&"张"&CHR(10)
					strsend=strsend&"今日收入:"&Formatnumber(jinjin,2,true,0,0)&"元"&CHR(10)
					strsend=strsend&"今日利润:"&Formatnumber(jlirun,2,true,0,0)&"元"&CHR(10)
					strsend=strsend&"累计成交:"&aychengjiao&"笔"&CHR(10)&CHR(10)
			conn.Execute("Insert Into aywxlog(aywxf,aywxt,aywxk,ayhfcon) values('"&FromUserName&"','"&MsgType&"','"&EventKey&"','"&strsend&"')")
			conn.Execute("Insert Into "&rzku&"(ayloguserid,aylogcon,aylogip,aylogleibie,aylogerr) values("&aynameid&",'公众号-今日销量"&"','"&userinip&"','10','0')")
			response.Write tohuifu(FromUserName,ToUserName,ToUnixTime(now,+8),"text",strsend)
			response.end
			end if
		
	
	response.End()
	end if
'##########  公众号菜单 event点击 结束 ###############################################################################
	
	
	
	
	
	
	
'##########  公众号 实时对话 ###############################################################################
 
    MsgId=xml_dom.getelementsbytagname("MsgId").item(0).text      
 	if trim(MsgType)="text" then content=xml_dom.getelementsbytagname("Content").item(0).text
	
	
 	'只能接受文本消息
	if trim(MsgType)="image" then
	strsend="抱歉,我们只能接收文本消息,请输入你要咨询的问题"
	response.Write tohuifu(FromUserName,ToUserName,ToUnixTime(now,+8),"text",strsend)
	response.end
	end if
	
	
	'使用商家QQ 查询店铺链接 开始 ##########################################
	if isNumeric(content) and len(content)>4 then
		
		strsend=""
		sql="select ayqq,aywebname,ayusermiaoshu,ayismd5,aygename,aymd5,iswappay,iszhuxiao from ayname where ayqq="&content&" and iswappay=1 and isgzh=1 and iszhuxiao=0"
		rs.open sql,conn,1,1
		if not(rs.bof and rs.eof) then
			do while not rs.eof
				if isnull(trim(rs("ayusermiaoshu")))=true or trim(rs("ayusermiaoshu"))="" then
				ayusermiaoshu="-"
				else
				ayusermiaoshu=trim(rs("ayusermiaoshu"))
				end if
				if rs("ayismd5")=1 then
					if trim(rs("aygename"))<>"" then
					erweima=wapurl&trim(rs("aygename"))
					else
					erweima=wapurl&trim(rs("aymd5"))
					end if
				else
					erweima=wapurl&request.Cookies("aynameid")
				end if
			strsend=strsend&"店铺名称："&trim(rs("aywebname"))&CHR(10)
			strsend=strsend&"商家QQ："&rs("ayqq")&CHR(10)
			strsend=strsend&"商家公告："&trim(ayusermiaoshu)&CHR(10)
			strsend=strsend&"店铺链接："&erweima&CHR(10)
			strsend=strsend&CHR(10)
			rs.movenext
			loop
		else
		strsend="未找到QQ为"&content&"的商家"
		end if
		rs.close
		
	conn.Execute("Insert Into aywxlog(aywxf,aywxt,aywxk,ayhfcon) values('"&FromUserName&"','"&MsgType&"','QQBUY','"&strsend&"')")
	response.Write tohuifu(FromUserName,ToUserName,ToUnixTime(now,+8),"text",strsend)
	response.End()
	end if
	'使用商家QQ 查询店铺链接 结束 ##########################################
	
	
	'自动匹配回复##########################################
	iszdhf="no"
	if instr(1,content,"解绑")>0 or instr(1,content,"解除")>0 or instr(1,content,"解除绑定")>0  then
	iszdhf="yes"
	strsend="解除绑定请点击"&wapurl&"index/jiebang"&CHR(10)
	end if
	if instr(1,content,"支付结果")>0 or instr(1,content,"卡密")>0 or instr(1,content,"DA")>0 or instr(1,content,"订单查询")>0 then
	iszdhf="yes"
	strsend="查询与提卡地址"&weburl&"chaxun"&CHR(10)
	strsend=strsend&"输入订单号或预留联系方式提取卡密"
	end if
	if instr(1,content,"找回账号")>0 then
	iszdhf="yes"
	strsend="如果忘记商家登录密码请登录"&weburl&"index/pass"&CHR(10)
	end if
	if instr(1,content,"投诉")>0 then
	iszdhf="yes"
	strsend="投诉地址"&weburl&"tousu"&CHR(10)
	end if
	if instr(1,content,"购买")>0 then
	iszdhf="yes"
	strsend="商品购买地址请联系商家索取。"
	end if
	if instr(1,content,"注册商家")>0 then
	iszdhf="yes"
	strsend="商家注册地址"&weburl&"index/reg"
	end if
	if instr(1,content,"我是商家")>0 then
	iszdhf="yes"
	strsend="商家服务请联系商家客服QQ："&allfuwuqq
	end if
	if iszdhf="yes" then
	conn.Execute("Insert Into aywxlog(aywxf,aywxt,aywxk,ayhfcon) values('"&FromUserName&"','"&MsgType&"','ZIDONG','"&strsend&"')")
	response.Write tohuifu(FromUserName,ToUserName,ToUnixTime(now,+8),"text",strsend)
	response.End()
	end if
	
	
	'图文信息说明
	'strresponse=strresponse&"<MsgType><![CDATA[news]]></MsgType>"
	'strresponse=strresponse&"<ArticleCount>1</ArticleCount>"
	'strresponse=strresponse&"<Articles><item> <Title><![CDATA[title1]]></Title><Description><![CDATA[description1]]></Description><PicUrl><![CDATA["&strsend&"]]></PicUrl><Url><![CDATA[url]]></Url></item></Articles>"
	
	
	'实时对话 写入数据库
	ismsg="no"	'是否 首次已经多次对话 
	sql="select top 1 * from ayweixin where aywxfrom='"&FromUserName&"' and aywxsid=0 order by aywxid desc" 
	rs.open sql,conn,1,3
	if not(rs.bof and rs.eof) then
	if DateDiff("n",rs("aywxtime"),now())>60 then ismsg="no"
	end if
	
	if trim(MsgType)="image" or trim(MsgType)="text" then
	rs.addnew
	if trim(MsgType)="image" then
	rs("PicUrl")=PicUrl
	rs("MediaId")=MediaId
	end if
	if trim(MsgType)="text" then
	rs("aywxcon")=Content
	end if
	rs("aywxto")=ToUserName
	rs("aywxfrom")=FromUserName
	rs("aywxdate")=CreateTime
	rs("aywxtype")=MsgType
	rs("aywxmsgid")=MsgId
	rs.update
	end if
	rs.close

	if ismsg="no" then
		if hour(now())>8 and hour(now())<18 then
		strsend="你好,正在为您转接客服人员,请稍等！"&CHR(10)&CHR(10)&"提取卡密"&CHR(10)&weburl&"chaxun"&CHR(10)&"订单投诉"&CHR(10)&weburl&"tousu"
		else
		strsend="你好,现在非工作时间,有什么问题您可以留言,请留下您的联系方式，我们会尽快回复您！"&CHR(10)&CHR(10)&"提取卡密"&CHR(10)&weburl&"chaxun"&CHR(10)&"订单投诉"&CHR(10)&weburl&"tousu"
		end if
		if trim(MsgType)<>"text" then
		strsend="抱歉,我们只能接收文本消息,请输入你要咨询的问题"
		response.Write tohuifu(FromUserName,ToUserName,ToUnixTime(now,+8),"text",strsend)
		else
		response.Write tohuifu(FromUserName,ToUserName,ToUnixTime(now,+8),"text",strsend)
		end if
	end if



%>
 