<!--#include file="base.asp"-->
<%
''' EYCMS 粉丝
''' ==================================================================

	sub getlistdb(byval t0)
		dim wx,datalist,json
		set wx=new eycms_weixin
			datalist=wx.get_user_list(t0)
			set json=toobject(datalist)
				if instr(datalist,"errcode") then
					eycms.echo json.errmsg
				else
					if eycms.strlen(json.data.openid)>0 then
						dim arr:arr=split(json.data.openid,",")
						dim i
						for i=0 to ubound(arr)
							eycms.db.dbnew "wx_user",array(array("openid",arr(i),255),array("isget",0,10),array("isuser",1,10)),"openid='"&arr(i)&"'"
						next
					end if
					if (json.total-json.count)>0 and json.count<>0 then
						getlistdb json.next_openid
					end if
					eycms.echo "1"
				end if
			set json=nothing
		set wx=nothing
	end sub
	
	sub getdatadb()
		dim openid,result,wx
		openid=eycms.enhtml(eycms.fget("openid",0))
		set wx=new eycms_weixin
		result=wx.get_user_base(openid)
		set wx=nothing
		if result="true" then
			eycms.echo "1"
		else
			eycms.echo result
		end if
	end sub

	sub getalldb()
		dim data,i,wx
		set wx=new eycms_weixin
		data=eycms.db.dbload("","openid","wx_user","","")
		if ubound(data)<0 then
			eycms.echo "没有需要获取的粉丝数据"
			exit sub
		end if
		for i=0 to ubound(data,2)
			wx.get_user_base data(0,i)
		next
		set wx=nothing
		eycms.echo "1"
	end sub
	
	sub remarkdb()
		dim t0:t0=eycms.enhtml(eycms.fget("t0",0))
		dim t1:t1=eycms.enhtml(eycms.fget("t1",0))
		dim wx,json,result
		set wx=new eycms_weixin
			result=wx.set_user_remark(t0,t1)
			set json=toobject(result)
			if json.errcode<>"0" then
				eycms.echo json.errmsg
			else
				eycms.db.dbupdate "wx_user","openid='"&t0&"'",array(array("remark",t1,30,1))
				eycms.echo "1"
			end if
			set json=nothing
		set wx=nothing
	end sub
	
	sub tagsdb()
		dim t0:t0=eycms.enhtml(eycms.fget("t0",0))
		dim t1:t1=eycms.enhtml(eycms.fget("t1",0))
		dim t2:t2=eycms.enhtml(eycms.fget("t2",0))
		dim arr1:arr1=split(t1,",")
		dim arr2:arr2=split(t2,",")
		if ubound(arr1)>2 then
			eycms.echo "不能超过3个标签":exit sub
		end if
		dim wx,json,result,i
		set wx=new eycms_weixin
		for i=0 to ubound(arr2)
			wx.set_user_del_tags t0,arr2(i)
		next
		for i=0 to ubound(arr1)
			wx.set_user_add_tags t0,arr1(i)
		next
		set wx=nothing
		eycms.db.dbupdate "wx_user","openid='"&t0&"'",array(array("taglist",t1,255,1))
		eycms.echo "1"
	end sub
	
	sub loaddb()
		dim data:data=eycms.db.dbload("","groupid,groupname","wx_user_group","","")
		if ubound(data)<0 then
			exit sub
		end if
		dim i
		for i=0 to ubound(data,2)
			get_tree=get_tree&""&eycms.iif(i=0,"",",")&"{"
			get_tree=get_tree&"id:"&data(0,i)&""
			get_tree=get_tree&",pId:-1"
			get_tree=get_tree&",name:"""&data(1,i)&""""
			get_tree=get_tree&"}"
		next
		load theme_user_tree
	end sub
	
	sub movedb()
		dim id:id=eycms.enhtml(eycms.fget("id",0))
		dim go:go=eycms.getint(eycms.fget("go",0),0)
		dim idarr:idarr=split(id,",")
		if ubound(idarr)<0 then
			eycms.echo "0&#33267;&#23569;&#36873;&#25321;&#19968;&#26465;&#20449;&#24687;"
			exit sub
		end if
		dim data,countnum
		data=eycms.db.dbload(1,"countnum","wx_user_group","groupid="&go&"","")
		if ubound(data)<0 then
			eycms.echo "0参数错误"
			exit sub
		end if
		countnum=data(0,0)
		dim i,dt,oldnum,openid,str
		for i=0 to ubound(idarr)
			if isnumeric(idarr(i)) then
				data=eycms.db.dbload(1,"groupid,openid","wx_user","id="&idarr(i)&" and isuser=1","")
				if ubound(data)>=0 then
					if data(0,0)<>go then
						dt=eycms.db.dbload(1,"countnum","wx_user_group","groupid="&data(0,0)&"","")
						if ubound(dt)>=0 then
							openid=data(1,0)
							if str="" then
								str=openid
							else
								str=str&","&openid
							end if
							oldnum=dt(0,0)-1
							eycms.db.dbupdate "wx_user","id="&idarr(i)&"",array(array("groupid",go,10,0))
							eycms.db.dbupdate "wx_user_group","groupid="&data(0,0)&"",array(array("countnum",oldnum,10,0))
							countnum=countnum+1
						end if
					end if
				end if
			end if
		next
		eycms.db.dbupdate "wx_user_group","groupid="&go&"",array(array("countnum",countnum,10,0))
		if str<>"" then
			str=""""&replace(str,",",""",""")&""""
			dim wx,result,json
			set wx=new eycms_weixin
				result=wx.batch_user_group(str,go)
				set json=toobject(result)
				if json.errcode<>"0" then
					eycms.echo "0"&json.errmsg
					exit sub
				end if
			set wx=nothing
		end if
		eycms.echo "1"
	end sub
	
	function get_tags(byval t0)
		if eycms.strlen(t0)=0 then
			get_tags=""
			exit function
		end if
		dim i,str
		if not isarray(eycms.loadsession("wx_user_tags")) then
			dim data:data=eycms.db.dbload("","tagid,tagname","wx_user_tags","","id")
			if ubound(data)<0 then
				eycms.setsession "wx_user_tags",""
			else
				eycms.setsession "wx_user_tags",data
			end if
		else
			data=eycms.loadsession("wx_user_tags")
		end if
		if isarray(data) then
			for i=0 to ubound(data,2)
				if instr(","&t0&",",","&data(0,i)&",")>0 then
					str=str&","&data(1,i)
				end if
			next
			if left(str,1)="," then
				str=right(str,len(str)-1)
			end if
		end if
		if eycms.strlen(str)>0 then str=replace(str,",",", ")
		get_tags=str
	end function

	islogin
	checkpagelever 71
	dim act:act=lcase(eycms.fget("act",0))
	dim keyword:keyword=eycms.enhtml(eycms.fget("keyword",1))
	select case act
		case "getlist":getlistdb("")
		case "getdata":getdatadb
		case "getall":getalldb
		case "remark":remarkdb
		case "load":dim get_tree:loaddb
		case "move":movedb
		case "tags":tagsdb
		case else
			dim wherekey:wherekey="isuser=1"
			if eycms.strlen(keyword)>0 then
				if datatype then
					wherekey=wherekey&" and InStr(1,LCase(nickname),LCase('"&keyword&"'),0)<>0 "
				else
					wherekey=wherekey&" and nickname like '%"&keyword&"%'"
				end if
			end if
		load theme_user
	end select
	eycms.db.dbclose
%>