<!--#include file="base.asp"-->
<%
''' EYCMS 素材框架模板
''' ==================================================================

	sub adddb()
		dim t0,t1,t2,t3,t4,t5
		t0=eycms.enhtml(eycms.fpost("t0",0))
		t1=eycms.fpost("t1",1)
		t2=eycms.fpost("t2",1)
		t3=eycms.getint(eycms.fpost("t3",0),0)
		if eycms.strlen(t0)=0 then eycms.ajaxjson "模板名称不能为空",0:exit sub
		t4=deal_local(t1&t2,"")
		t5=deal_pic(t4,"","")
		dim data:data=array(array("title",t0,50,1),array("header",t1,0,1),array("footer",t2,0,1),array("piclist",t4,0,1),array("uplist",t5,0,1),array("ordnum",t3,10,0))
		eycms.db.insert "wx_master_temp",data
		dim cid:cid=eycms.db.insertid("id","wx_master_temp")		
		dim imgArr:imgArr=eycms.getfilelist(t1&t2)
		delall imgArr,cid,8
		eycms.ajaxjson "保存成功",1
	end sub
	
	sub editdb()
		dim t0,t1,t2,t3,t4,t5
		t0=eycms.enhtml(eycms.fpost("t0",0))
		t1=eycms.fpost("t1",1)
		t2=eycms.fpost("t2",1)
		t3=eycms.getint(eycms.fpost("t3",0),0)

		if eycms.strlen(t0)=0 then eycms.ajaxjson "模板名称不能为空",0:exit sub

		dim id:id=eycms.getint(eycms.fget("id",0),0)
		dim data:data=eycms.db.dbload(1,"piclist,uplist","wx_master_temp","id="&id&"","")
		if ubound(data)>=0 then
			t4=deal_local(t1&t2,data(0,0))
			t5=deal_pic(t4,data(0,0),data(1,0))
			data=array(array("title",t0,50,1),array("header",t1,0,1),array("footer",t2,0,1),array("piclist",t4,0,1),array("uplist",t5,0,1),array("ordnum",t3,10,0))
			eycms.db.dbupdate "wx_master_temp","id="&id&"",data
		end if
		dim imgArr:imgArr=eycms.getfilelist(t1&t2)
		delall imgArr,id,8
		eycms.ajaxjson "保存成功",1
	end sub
	
	function deal_local(byval t0,byval t1)
		t0=replace(t0,weburl,"")
		dim t2:t2=eycms.getallpic(t0,"")
		if t1="" then
			deal_local=t2
		else
			dim i,arr
			arr=split(t2,"|")
			for i=0 to ubound(arr)
				if instr("|"&t1&"|","|"&arr(i)&"|")<=0 then
					t1=t1&"|"&arr(i)
				end if
			next
			deal_local=t1
		end if
	end function
	
	function deal_pic(byval t0,byval t1,byval t2)
		on error resume next
		if eycms.strlen(wx_token)=0 then exit function
		dim arr:arr=split(t0,"|")
		dim i,str,wx,result
		set wx=new eycms_weixin
		for i=0 to ubound(arr)
			if instr("|"&t1&"|","|"&arr(i)&"|")<=0 then
				result=""
				result=wx.upload_image(arr(i))
				set json=toobject(result)
				if instr(result,"url")>0 then
					str=str&"|"&json.url
				else
					eycms.errlog "上传图片素材失败，素材名称为："&arr(i)&"，错误原因："&json.errmsg&"",0
				end if
			end if
		next
		set json=nothing
		set wx=nothing
		if eycms.strlen(str)>0 then
			str=t2&str
		else
			str=t2
		end if
		if left(str,1)="|" then str=right(str,len(str)-1)
		deal_pic=str
	end function
	
	sub deldb()
		dim id:id=eycms.getint(eycms.fget("id",0),0)
		eycms.db.dbupdate "wx_master","tid="&id&"",array(array("cid",0,10,0))
		eycms.db.dbdel "wx_master_temp","id="&id
		eycms.echo "1"
	end sub

	sub orderdb()
		dim t0,t1,t2,t3,i
		t0=eycms.enhtml(eycms.fpost("ordnum",0))
		t1=eycms.enhtml(eycms.fpost("cid",0))
		t2=split(t0,", ")
		t3=split(t1,", ")
		if ubound(t2)-ubound(t3)<>0 then eycms.ajaxjson "0&#21442;&#25968;&#38169;&#35823;",0:exit sub
		for i=0 to ubound(t2)
			if eycms.isnum(t2(i)) then
				eycms.db.dbupdate "wx_master_temp","id="&t3(i)&"",array(array("ordnum",t2(i),10,0,0))
			end if
		next
		eycms.ajaxjson "保存成功",1
	end sub

	islogin
	checkpagelever 67
	dim act:act=lcase(eycms.fget("act",0))
	dim id:id=eycms.getint(eycms.fget("id",0),0)
	select case act
		case "add":load theme_master_temp_add
		case "adddb":adddb
		case "edit"
			dim rsshow
			set rsshow=eycms.db.exedb("select top 1 * from wx_master_temp where id="&id&"")
			if rsshow.eof then
				eycms.echo "参数错误":eycms.die
			end if
			load theme_master_temp_edit
		case "editdb":editdb
		case "del":deldb
		case "order":orderdb
		case else:load theme_master_temp
	end select
	eycms.db.dbclose
%>