<!--#include file="base.asp"-->
<%
''' EYCMS 群发消息
''' ==================================================================

	sub adddb()
		dim t0,t1,t2,t3,t4
		t0=eycms.enhtml(eycms.fpost("t0",-1))
		t1=eycms.getint(eycms.fpost("t1",0),0)
		t2=eycms.enhtml(eycms.fpost("t2",0))
		t3=eycms.getint(eycms.fpost("t3",0),0)
		if t1=2 then
			if t3=0 then
				eycms.ajaxjson "请选择素材",0	
				exit sub
			end if
			dim data:data=eycms.db.dbload(1,"id","wx_master_class","id="&t3&"","")
			if ubound(data)<0 then
				eycms.ajaxjson "素材选择错误",0	
				exit sub
			end if
		end if
		data=array(array("title","群发消息",255),array("groupid",t0,10),array("types",t1,10),array("reply",t2,0),array("replyid",t3,10),array("lastdate",now(),50),array("isok",0,10),array("totalnum",0,10),array("oknum",0,10),array("nonum",0,10))
		eycms.db.dbnew "wx_mass",data,"1<>1"
		dim nid:nid=eycms.db.insertid("id","wx_mass")
		if t1=1 then
			t4=t2
		else
			on error resume next
			dim idarr
			data=eycms.db.dbload(10,"id,title,author,content,pic,oldurl,media_id,media_date,tid","wx_master","classid="&t3&" and islock=1","ordnum,id")
			if ubound(data)<0 then
				eycms.ajaxjson "素材中没有内容，请先添加",0	
				exit sub
			end if
			dim i,js
			js="{""articles"":["
			for i=0 to ubound(data,2)
				if i>0 then
					js=js&","
					idarr=idarr&","
				end if
				idarr=idarr&data(0,i)
				dim u0,u1,u2,u3,u4,u5,u6,u7,u8,u9
				u0=data(0,i)
				u1=data(1,i)
				u2=data(2,i)
				u3=deal_pic(data(3,i))
				u4=eycms.iif(i=0,1,0)
				u4=0
				u5=data(4,i) 
				u6=data(5,i)
				if eycms.strlen(u6)=0 then
					u6=weburl&webroot&showrule&u0
				end if
				u7=data(6,i)
				u8=data(7,i)
				u9=data(8,i)
				if eycms.strlen(u7)=0 then
					dim wx,result,json
					set wx=new eycms_weixin
					result=wx.upload_media_image(u5)
					set json=toobject(result)
					if instr(result,"media_id")>0 then
						u7=json.media_id
						eycms.db.dbupdate "wx_master","id="&u0&"",array(array("media_id",u7,0,1),array("media_date",date(),50,1))
					else
						eycms.errlog "上传图片素材失败，素材ID为："&u0&"，错误原因："&json.errmsg&"",0
						eycms.ajaxjson "上传图片素材失败，错误原因请查阅错误日志",0
						exit sub
					end if
					set json=nothing
					set wx=nothing
				else
					if datediff("d",u8,now())>=3 then
						set wx=new eycms_weixin
						result=wx.upload_media_image(u5)
						set json=toobject(result)
						if instr(result,"media_id")>0 then
							u7=json.media_id
							eycms.db.dbupdate "wx_master","id="&u0&"",array(array("media_id",u7,0,1),array("media_date",date(),50,1))
						else
							eycms.errlog "上传图片素材失败，素材ID为："&u0&"，错误原因："&json.errmsg&"",0
							eycms.ajaxjson "上传图片素材失败，错误原因请查阅错误日志",0
							exit sub
						end if
						set json=nothing
						set wx=nothing
					end if
				end if
				dim intro:intro=eycms.cutstr(eycms.dishtml(u3),80,0)
				u3=gzip(deal_temp(u9,u3))
				js=js&"{""thumb_media_id"":"""&u7&""",""author"":"""&u2&""",""title"":"""&u1&""",""content_source_url"":"""&u6&""",""content"":"""&u3&""",""digest"":"""&intro&""",""show_cover_pic"":"""&u4&"""}"
			next
			js=js&"]}"
			set wx=new eycms_weixin
				result=wx.upload_master(js)
				set json=toobject(result)
				if instr(result,"media_id")>0 then
					t4=json.media_id
					eycms.db.dbupdate "wx_master_class","id="&t3&"",array(array("media_id",t4,255,1))
				else
					eycms.errlog "上传图文消息失败，错误原因："&json.errmsg&"",0
					eycms.ajaxjson "上传图文消息失败，错误原因请查阅错误日志",0
					exit sub
				end if
				set json=nothing
			set wx=nothing
		end if
		set wx=new eycms_weixin
			result=wx.mass_message(t0,t1,t4)
			set json=toobject(result)
			if json.errcode="0" then
				eycms.db.dbupdate "wx_mass","id="&nid&"",array(array("msg_id",json.msg_id,255,1))
			else
				eycms.ajaxjson "发送失败："&json.errmsg&"",0
				exit sub
			end if
			set json=nothing
		set wx=nothing
		eycms.ajaxjson "群发内容已提交到微信服务器",1	
	end sub
	
	function deal_temp(byval t0,byval t1)
		if t0=0 then
			deal_temp=t1
			exit function
		end if
		dim data:data=eycms.db.dbload(1,"header,footer,piclist,uplist","wx_master_temp","id="&t0&"","")
		if ubound(data)<0 then
			deal_temp=t1
			exit function
		else
			dim arr1,arr2,i,str
			arr1=split(data(2,0),"|")
			arr2=split(data(3,0),"|")
			str=data(0,0)&t1&data(1,0)
			str=replace(str,weburl,"")
			for i=0 to ubound(arr1)
				str=replace(str,arr1(i),arr2(i))
			next
			deal_temp=str
		end if
	end function
	
	function deal_pic(byval t0)
		t0=replace(t0,weburl,"")
		dim t1:t1=eycms.getallpic(t0,"")
		dim arr:arr=split(t1,"|")
		dim i,str
		for i=0 to ubound(arr)
			dim wx,result,json
			set wx=new eycms_weixin
			result=wx.upload_image(arr(i))
			set json=toobject(result)
			if instr(result,"url")>0 then
				t0=replace(t0,arr(i),json.url)
			end if
			set json=nothing
			set wx=nothing
		next
		deal_pic=t0
	end function
 
	sub deldb()
		dim id:id=eycms.getint(eycms.fget("id",0),0)
		eycms.db.dbdel "wx_mass","id="&id
		eycms.echo "1"
	end sub
	
	sub doset()
		dim id:id=eycms.enhtml(eycms.fget("id",0))
		dim go:go=eycms.enhtml(eycms.fget("go",0))
		dim idarr:idarr=split(id,",")
		if ubound(idarr)<0 then
			eycms.echo "0&#33267;&#23569;&#36873;&#25321;&#19968;&#26465;&#20449;&#24687;"
		else
			dim i
			for i=0 to ubound(idarr)
				if not isnumeric(idarr(i)) then
					eycms.echo "0&#21442;&#25968;&#65306;"&idarr(i)&"&#19981;&#27491;&#30830;&#65292;&#35831;&#30830;&#35748;&#21518;&#20877;&#25805;&#20316;"
					exit sub
				end if
			next
			dim filed,result
			select case go
				case "islock":filed="islock":result=0
				case "nolock":filed="islock":result=1
				case else:eycms.echo "0&#21442;&#25968;&#38169;&#35823;":exit sub
			end select
			eycms.db.dbupdate "wx_mass","id in("&id&")",array(array(filed,result,2,0))
			eycms.echo "1"
		end if
	end sub
	
	sub delsome()
		dim id:id=eycms.enhtml(eycms.fget("id",0))
		dim idarr:idarr=split(id,",")
		if ubound(idarr)<0 then
			eycms.echo "0&#33267;&#23569;&#36873;&#25321;&#19968;&#26465;&#20449;&#24687;"
		else
			dim i
			for i=0 to ubound(idarr)
				if not isnumeric(idarr(i)) then
					eycms.echo "0&#21442;&#25968;&#65306;"&idarr(i)&"&#19981;&#27491;&#30830;&#65292;&#35831;&#30830;&#35748;&#21518;&#20877;&#25805;&#20316;"
					exit sub
				else
					eycms.db.dbdel "wx_mass","id="&idarr(i)&""
				end if
			next
			eycms.echo "1"
		end if
	end sub
	
	function gzip(byval t0)
		dim reg
		set reg=new regexp
			reg.ignorecase=true
			reg.global=true
			reg.pattern=" +":t0=reg.replace(t0,chr(32))
			reg.pattern="\t+|\r+|\n+":t0=reg.replace(t0,"")
			reg.pattern="\r+":t0=reg.replace(t0,"")
			reg.pattern="\n+":t0=reg.replace(t0,"")
		set reg=nothing
		t0=replace(t0,"""","\""")
		gzip=t0
	end function
	 
	islogin
	checkpagelever 70
	dim act:act=lcase(eycms.fget("act",0))
	dim classid:classid=eycms.getint(eycms.fget("classid",0),0)
	select case act
		case "add":load theme_mass_add
		case "adddb":adddb
		case "del":deldb
		case "doset":doset
		case "delsome":delsome
		case else:load theme_mass
	end select
	eycms.db.dbclose
%>