/// <summary> /// 发送前预览 /// </summary> /// <param></param> /// <param></param> protected void LinkBtnSendPreview_Click(object sender, EventArgs e) { WeiXinServer wxs = new WeiXinServer(); ///从缓存读取accesstoken string Access_token = Cache["Access_token"] as string; if (Access_token == null) { //如果为空,重新获取 Access_token = wxs.GetAccessToken(); //设置缓存的数据7000秒后过期 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/message/mass/preview?access_token=" + Access_tokento; ///如果选择的是文本消息 if (this.RadioBtnList.SelectedValue.ToString().Equals("0")) { if (String.IsNullOrWhiteSpace(this.txtwenben.InnerText.ToString().Trim())) { ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请输入您要发送预览的文本内容!');", true); return; } if (this.txttoUserName.Value.ToString().Trim().Equals("请输入用户微信号")) { ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请输入接收消息的用户微信号!');", true); return; } //文本消息的json数据{ // "touser":"OPENID", 可改为对微信号预览,例如towxname:zhangsan // "text":{ // "content":"CONTENT" // }, // "msgtype":"text" //} string postData = "{\"towxname\":\"" + this.txttoUserName.Value.ToString() + "\",\"text\":{\"content\":\"" + this.txtwenben.InnerText.ToString() + "\"},\"msgtype\":\"text\"}"; string tuwenres = wxs.GetPage(posturl, postData); //使用前需药引用Newtonsoft.json.dll文件 JObject jsonObj = JObject.Parse(tuwenres); if (jsonObj["errcode"].ToString().Equals("0")) { ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('发送预览成功!!');", true); return; } else { ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('发送预览失败!!');", true); return; } } //如果选择的是图文消息 if (this.RadioBtnList.SelectedValue.ToString().Equals("1")) { if(String.IsNullOrWhiteSpace(this.lbtuwenmedai_id.Text.ToString().Trim())) { ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请选择要预览的图文素材!');", true); return; } if (this.txttoUserName.Value.ToString().Trim().Equals("请输入用户微信号")) { ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请输入接收消息的用户微信号!');", true); return; } //图文消息的json数据{ // "touser":"OPENID", 可改为对微信号预览,例如towxname:zhangsan // "mpnews":{ // "media_id":"123dsdajkasd231jhksad" // }, // "msgtype":"mpnews" //} string postData = "{\"towxname\":\"" + this.txttoUserName.Value.ToString() + "\",\"mpnews\":{\"media_id\":\"" + this.lbtuwenmedai_id.Text.ToString() + "\"},\"msgtype\":\"mpnews\"}"; string tuwenres = wxs.GetPage(posturl, postData); //使用前需药引用Newtonsoft.json.dll文件 JObject jsonObj = JObject.Parse(tuwenres); if (jsonObj["errcode"].ToString().Equals("0")) { Session["media_id"] = null; ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('发送预览成功!!');", true); return; } else { ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('发送预览失败!!');", true); return; } } }
关键部分,获取全部用户的openID并串联成字符串: