<strong> PagedDataSource pds =
new
PagedDataSource();
protected
void Page_Load(object sender, EventArgs e)
{
if
(!Page.IsPostBack)
{
BindGroupList();
BindGetAllUserOpenIdList();
this.DataBind();
this.CheckAll.AutoPostBack = true;
this.DDlAddgroups.AutoPostBack = true;
}
}
private
void BindGetAllUserOpenIdList()
{
WeiXinServer wxs =
new
WeiXinServer();
string Access_token = Cache[
"Access_token"
]
as
string;
if
(Access_token == null)
{
Access_token = wxs.GetAccessToken();
Cache.Insert(
"Access_token"
, Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
}
string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
string jsonres =
""
;
string content = Cache[
"AllUserOpenList_content"
]
as
string;
if
(content == null)
{
jsonres =
"https://api.weixin.qq.com/cgi-bin/user/get?access_token="
+ Access_tokento;
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(jsonres);
myRequest.Method =
"GET"
;
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader =
new
StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
content = reader.ReadToEnd();
reader.Close();
Cache.Insert(
"AllUserOpenList_content"
, content, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
}
JObject jsonObj = JObject.Parse(content);
int totalnum = int.Parse(jsonObj[
"count"
].ToString());
List<WxOpenIdInfo> openidlist =
new
List<WxOpenIdInfo>();
for
(int i = 0; i < totalnum;i++ )
{
WxOpenIdInfo wxopeninfo =
new
WxOpenIdInfo();
wxopeninfo.WxopenId = jsonObj[
"data"
][
"openid"
][i].ToString();
openidlist.Add(wxopeninfo);
}
pds.DataSource = openidlist;
pds.AllowPaging = true;
pds.PageSize = 20;
int CurrentPage;
if
(!String.IsNullOrWhiteSpace(this.txtPageIndex.Text.ToString().Trim()))
{
CurrentPage = Convert.ToInt32(this.txtPageIndex.Text.ToString().Trim());
}
else
if
(Request.QueryString[
"Page"
] != null)
{
CurrentPage = Convert.ToInt32(Request.QueryString[
"Page"
]);
}
else
{
CurrentPage = 1;
}
pds.CurrentPageIndex = CurrentPage - 1;
if
(!pds.IsFirstPage)
{
this.lnkTop.NavigateUrl = Request.CurrentExecutionFilePath +
"?Page="
+ Convert.ToString(CurrentPage - 1);
this.lnkFist.Enabled = this.lnkTop.Enabled = true;
this.lnkNext.Enabled = this.lnkLast.Enabled = true;
}
else
{
this.lnkFist.Enabled = this.lnkTop.Enabled = false;
this.lnkNext.Enabled = this.lnkLast.Enabled = true;
this.lnkFist.Attributes.Add(
"style"
,
"color:#ced9df;"
);
this.lnkTop.Attributes.Add(
"style"
,
"color:#ced9df;"
);
this.lnkNext.Attributes.Remove(
"style"
);
this.lnkLast.Attributes.Remove(
"style"
);
}
if
(!pds.IsLastPage)
{
this.lnkNext.NavigateUrl = Request.CurrentExecutionFilePath +
"?Page="
+ Convert.ToString(CurrentPage + 1);
this.lnkFist.Enabled = this.lnkTop.Enabled = true;
this.lnkNext.Enabled = this.lnkLast.Enabled = true;
}
else
{
this.lnkNext.Enabled = this.lnkLast.Enabled = false;
this.lnkFist.Enabled = this.lnkTop.Enabled = true;
this.lnkNext.Attributes.Add(
"style"
,
"color:#ced9df;"
);
this.lnkLast.Attributes.Add(
"style"
,
"color:#ced9df;"
);
this.lnkFist.Attributes.Remove(
"style"
);
this.lnkTop.Attributes.Remove(
"style"
);
}
this.lnkFist.NavigateUrl = Request.CurrentExecutionFilePath +
"?Page="
+ Convert.ToString(1);
this.lnkLast.NavigateUrl = Request.CurrentExecutionFilePath +
"?Page="
+ Convert.ToString(pds.PageCount);
this.RepeaterWxUserList.DataSource = pds;
this.RepeaterWxUserList.DataBind();
this.lbCountData.Text = openidlist.
Count
.ToString();
this.lbPageIndex.Text = (pds.CurrentPageIndex + 1).ToString();
this.lbPageSize.Text =
"每页"
+ pds.PageSize.ToString() +
"条记录"
;
this.lbCountPage.Text = pds.PageCount.ToString();
this.txtPageIndex.Text = (pds.CurrentPageIndex + 1).ToString();
if
(int.Parse(openidlist.
Count
.ToString()) <= int.Parse(pds.PageSize.ToString()))
{
this.lnkFist.Visible = this.lnkTop.Visible = this.lnkNext.Visible = this.lnkLast.Visible = this.txtPageIndex.Visible = this.LinkBtnToPage.Visible = false;
}
else
{
this.lnkFist.Visible = this.lnkTop.Visible = this.lnkNext.Visible = this.lnkLast.Visible = this.txtPageIndex.Visible = this.LinkBtnToPage.Visible = true;
}
this.lbsubscribeCount.Text = openidlist.
Count
.ToString();
}
private
void BindGroupList()
{
WeiXinServer wxs =
new
WeiXinServer();
string Access_token = Cache[
"Access_token"
]
as
string;
if
(Access_token == null)
{
Access_token = wxs.GetAccessToken();
Cache.Insert(
"Access_token"
, Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
}
string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
string jsonres =
""
;
string content = Cache[
"AllGroups_content"
]
as
string;
if
(content == null)
{
jsonres =
"https://api.weixin.qq.com/cgi-bin/groups/get?access_token="
+ Access_tokento;
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(jsonres);
myRequest.Method =
"GET"
;
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader =
new
StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
content = reader.ReadToEnd();
reader.Close();
Cache.Insert(
"AllGroups_content"
, content, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
}
JObject jsonObj = JObject.Parse(content);
int groupsnum = jsonObj[
"groups"
].
Count
();
this.DDLgroups.Items.Clear();
this.DDlAddgroups.Items.Clear();
this.DDLgroups.Items.Insert(0,
new
ListItem(
"分组统计"
,
"0"
));
this.DDlAddgroups.Items.Insert(0,
new
ListItem(
"移动用户到分组"
,
"0"
));
for
(int i = 0; i < groupsnum; i++)
{
this.DDLgroups.Items.Add(
new
ListItem(jsonObj[
"groups"
][i][
"name"
].ToString() +
"("
+ jsonObj[
"groups"
][i][
"count"
].ToString() +
")"
, jsonObj[
"groups"
][i][
"id"
].ToString()));
this.DDlAddgroups.Items.Add(
new
ListItem(jsonObj[
"groups"
][i][
"name"
].ToString(), jsonObj[
"groups"
][i][
"id"
].ToString()));
}
}
protected
void LinkBtnToPage_Click(object sender, EventArgs e)
{
if
(String.IsNullOrWhiteSpace(this.txtPageIndex.Text.ToString().Trim()))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.
GetType
(),
""
,
"alert('页码不能为空!')"
, true);
this.txtPageIndex.Focus();
return
;
}
if
(IsNum(this.txtPageIndex.Text.ToString().Trim()))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.
GetType
(),
""
,
"alert('页码数只能输入数字!')"
, true);
this.txtPageIndex.Focus();
this.txtPageIndex.Text = this.lbPageIndex.Text.ToString();
return
;
}
if
(int.Parse(this.txtPageIndex.Text.ToString().Trim()) > int.Parse(this.lbCountPage.Text.ToString().Trim()))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.
GetType
(),
""
,
"alert('所输页数不能大于总页数!')"
, true);
this.txtPageIndex.Focus();
this.txtPageIndex.Text = this.lbPageIndex.Text.ToString();
return
;
}
BindGetAllUserOpenIdList();
}
public
static
bool IsNum(string text)
{
for
(int i = 0; i < text.Length; i++)
{
if
(!Char.IsNumber(text, i))
{
return
true;
}
}
return
false;
}
protected
void RepeaterWxUserList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if
(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
WxOpenIdInfo wxopen = e.Item.DataItem
as
WxOpenIdInfo;
Label lbwxopenID = e.Item.FindControl(
"lbwxopenID"
)
as
Label;
lbwxopenID.Text = wxopen.WxopenId.ToString();
WeiXinServer wxs =
new
WeiXinServer();
string Access_token = Cache[
"Access_token"
]
as
string;
if
(Access_token == null)
{
Access_token = wxs.GetAccessToken();
Cache.Insert(
"Access_token"
, Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
}
string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
string jsonres =
"https://api.weixin.qq.com/cgi-bin/user/info?access_token="
+ Access_tokento +
"&openid="
+ lbwxopenID.Text.ToString();
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(jsonres);
myRequest.Method =
"GET"
;
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader =
new
StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string content = reader.ReadToEnd();
reader.Close();
JObject jsonObj = JObject.Parse(content);
Image ImgHeadUrl = e.Item.FindControl(
"ImgHeadUrl"
)
as
Image;
Label lbNickName = e.Item.FindControl(
"lbNickName"
)
as
Label;
Label lbRemark = e.Item.FindControl(
"lbRemark"
)
as
Label;
Label lbSubscrine_time = e.Item.FindControl(
"lbSubscrine_time"
)
as
Label;
Label lbgroupId = e.Item.FindControl(
"lbgroupId"
)
as
Label;
DropDownList DDlAddgroupss = e.Item.FindControl(
"DDlAddgroupss"
)
as
DropDownList;
lbNickName.Text = jsonObj[
"nickname"
].ToString();
if
(!String.IsNullOrWhiteSpace(jsonObj[
"remark"
].ToString()))
{
lbRemark.Text =
"("
+ jsonObj[
"remark"
].ToString() +
")"
;
}
ImgHeadUrl.ImageUrl = jsonObj[
"headimgurl"
].ToString();
lbgroupId.Text = jsonObj[
"groupid"
].ToString();
int totaltiem = int.Parse(jsonObj[
"subscribe_time"
].ToString());
DateTime t =
new
DateTime(1970, 1, 1).AddSeconds(totaltiem);
lbSubscrine_time.Text = t.AddHours(8).ToString();
string jjjjjjjjjddd = Cache[
"AllGroups_content"
]
as
string;
if
(jjjjjjjjjddd == null)
{
jsonres =
"https://api.weixin.qq.com/cgi-bin/groups/get?access_token="
+ Access_tokento;
HttpWebRequest myRequestss = (HttpWebRequest)WebRequest.Create(jsonres);
myRequest.Method =
"GET"
;
HttpWebResponse myResponsess = (HttpWebResponse)myRequest.GetResponse();
StreamReader readerss =
new
StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
jjjjjjjjjddd = reader.ReadToEnd();
reader.Close();
Cache.Insert(
"AllGroups_content"
, jjjjjjjjjddd, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
}
JObject jsonObjss = JObject.Parse(jjjjjjjjjddd);
int groupsnumss = jsonObjss[
"groups"
].
Count
();
for
(int i = 0; i < groupsnumss;i++ )
{
if
(jsonObjss[
"groups"
][i][
"id"
].ToString().Equals(lbgroupId.Text.ToString()))
{
DDlAddgroupss.SelectedItem.Text = jsonObjss[
"groups"
][i][
"name"
].ToString();
}
}
}
}
protected
void LinkBtnCreateGroup_Click(object sender, EventArgs e)
{
if
(this.txtgroupsName.Value.ToString().Equals(
"分组名称"
))
{
ScriptManager.RegisterClientScriptBlock(this.Page,this.
GetType
(),
""
,
"alert('不能为空!')"
,true);
this.txtgroupsName.Focus();
return
;
}
WeiXinServer wxs =
new
WeiXinServer();
string res =
""
;
string Access_token = Cache[
"Access_token"
]
as
string;
if
(Access_token == null)
{
Access_token = wxs.GetAccessToken();
Cache.Insert(
"Access_token"
, Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
}
string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
string posturl =
"https://api.weixin.qq.com/cgi-bin/groups/create?access_token="
+ Access_tokento;
string postData =
"{\"group\":{\"name\":\""
+this.txtgroupsName.Value.ToString().Trim()+
"\"}}"
;
res = wxs.GetPage(posturl, postData);
ScriptManager.RegisterClientScriptBlock(this.Page, this.
GetType
(),
""
,
"alert('创建成功!如未显示,请退出重新登录即可!');location='WeiXinUserList.aspx';"
, true);
}
protected
void CheckAll_CheckedChanged(object sender, EventArgs e)
{
CheckBox checkAll = (CheckBox)sender;
foreach
(RepeaterItem item in this.RepeaterWxUserList.Items)
{
CheckBox checkIn = (CheckBox)item.FindControl(
"CheckIn"
);
checkIn.Checked = checkAll.Checked;
}
}
protected
void DDlAddgroups_SelectedIndexChanged(object sender, EventArgs e)
{
string groupId = this.DDlAddgroups.SelectedValue.ToString();
Boolean bools = false;
foreach
(RepeaterItem item in this.RepeaterWxUserList.Items)
{
CheckBox checkIn = (CheckBox)item.FindControl(
"CheckIn"
);
if
(checkIn.Checked)
{
bools = true;
Label lbwxopenID = item.FindControl(
"lbwxopenID"
)
as
Label;
WeiXinServer wxs =
new
WeiXinServer();
string res =
""
;
string Access_token = Cache[
"Access_token"
]
as
string;
if
(Access_token == null)
{
Access_token = wxs.GetAccessToken();
Cache.Insert(
"Access_token"
, Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
}
string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
string posturl =
"https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token="
+ Access_tokento;
string postData =
"{\"openid\":\""
+ lbwxopenID.Text.ToString() +
"\",\"to_groupid\":\""
+ groupId.ToString() +
"\"}"
;
res = wxs.GetPage(posturl, postData);
JObject jsonObj = JObject.Parse(res);
string isright = jsonObj[
"errcode"
].ToString();
string istrueorfalse = jsonObj[
"errmsg"
].ToString();
if
(isright.Equals(
"0"
) && istrueorfalse.Equals(
"ok"
))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.
GetType
(),
""
,
"alert('移动用户成功!');location='WeiXinUserList.aspx';"
, true);
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.
GetType
(),
""
,
"alert('移动用户失败!');"
, true);
return
;
}
}
}
if
(!bools)
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.
GetType
(),
""
,
"alert('未选中项!');location='WeiXinUserList.aspx';"
, true);
return
;
}
}</strong>