using BWP.B3WeChat; using BWP.B3WeChat.Entities; using Forks.Utils; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Web; using System.Web.Script.Serialization; using System.Web.Security; namespace BWP.B3WeChat.Utils { public static class InOutMessageUtil { static Logger logger = new Logger("InOutMessageUtil"); static string token; static DateTime? getTokenTime; static readonly TimeSpan expiredSpan = new TimeSpan(2, 0, 0); static string TOKEN { get { if (getTokenTime.HasValue && ((TimeSpan)(DateTime.Now - getTokenTime.Value)) >= expiredSpan) { token = string.Empty; } if (string.IsNullOrEmpty(token)) { token = GetToken(); getTokenTime = DateTime.Now; } return token; } } public static B3WeChatConfig config = new B3WeChatConfig(); static JavaScriptSerializer jsonHelper = new JavaScriptSerializer(); static T GetRequest(string uriStr) where T : WeChatResponseBase { var responseBody = GetRequestString(uriStr); T obj = jsonHelper.Deserialize(responseBody); if (obj.IsError) { throw new Exception(string.Format("{0}:{1}", obj.errcode, obj.errmsg)); } return obj; } private static string GetRequestString(string url) { WebClient client = new WebClient(); var data = client.DownloadData(url); var result = Encoding.UTF8.GetString(data); jsonHelper.Deserialize(result).CheckError(); return result; } static T PostRequest(string url, string json) where T : WeChatResponseBase { byte[] requestBuffer = Encoding.UTF8.GetBytes(json); HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url)); webRequest.Method = "POST"; webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.ContentLength = requestBuffer.Length; using (var requestStream = webRequest.GetRequestStream()) { requestStream.Write(requestBuffer, 0, requestBuffer.Length); } HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); var data = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd(); var obj = jsonHelper.Deserialize(data); if (obj.IsError) { throw new Exception(string.Format("{0}:{1}", obj.errcode, obj.errmsg)); } return obj; } public static string GetToken() { string token = string.Empty; string url = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", config.AppID.Value, config.AppSecret.Value); var res = GetRequest(url); return res.access_token; } public static WeiChartServerList GetIPList() { WeiChartServerList result = new WeiChartServerList(); string uriStr = string.Format("https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token={0}", TOKEN); return GetRequest(uriStr); } public static SendTemplateMessageResult SendTemplateMessage(string openID, string templateID, Dictionary dic, string url = "") { string uriStr = string.Format("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={0}", TOKEN); JavaScriptSerializer jsonHelper = new JavaScriptSerializer(); SendTemplateMessageResult result = new SendTemplateMessageResult(); MeassageBody body = new MeassageBody(); body.touser = openID; body.template_id = templateID; body.url = url; body.data = dic; string postData = jsonHelper.Serialize(body); return PostRequest(uriStr, postData); } public static void SetCustomMenu(string menuJson) { var url = string.Format("https://api.weixin.qq.com/cgi-bin/menu/create?access_token={0}", TOKEN); PostRequest(url, menuJson); } public static string GetCustomMenu() { var url = string.Format("https://api.weixin.qq.com/cgi-bin/menu/get?access_token={0}", TOKEN); return GetRequestString(url); } public static void DelteCustomMenu() { var url = string.Format("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token={0}",TOKEN); GetRequest(url); } public static List GetOpenIDList() { string openid = string.Empty; string uriStr = string.Empty; List lstOpenID = new List(); JavaScriptSerializer jsonHelper = new JavaScriptSerializer(); OpenIDObject result = new OpenIDObject(); WebClient client = new WebClient(); try { do { uriStr = string.Format("https://api.weixin.qq.com/cgi-bin/user/get?access_token={0}&next_openid={1}", TOKEN, openid); byte[] bytes = client.DownloadData(uriStr); string responseBody = Encoding.UTF8.GetString(bytes); result = jsonHelper.Deserialize(responseBody); lstOpenID.AddRange(result.data.openid); openid = result.data.openid.Last(); } while (lstOpenID.Count < result.total); } catch (Exception e) { throw e; } return lstOpenID; } public static UserBasicInfo GetUserInfo(string openid) { string uriStr = string.Empty; List lstOpenID = new List(); JavaScriptSerializer jsonHelper = new JavaScriptSerializer(); UserBasicInfo result = new UserBasicInfo(); WebClient client = new WebClient(); try { uriStr = string.Format("https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN", TOKEN, openid); byte[] bytes = client.DownloadData(uriStr); string responseBody = Encoding.UTF8.GetString(bytes); result = jsonHelper.Deserialize(responseBody); } catch (Exception e) { throw e; } return result; } /// /// 接收消息 /// /// /// public static object GetMessage(HttpRequest request) { StreamReader reader = new StreamReader(request.InputStream); String xmlData = reader.ReadToEnd(); object obj = null; MessageType type = XmlUtil.Deserialize(xmlData); if (type.MsgType == MsgType.文本) { obj = XmlUtil.Deserialize(xmlData); } else if (type.MsgType == MsgType.事件) { obj = XmlUtil.Deserialize(xmlData); } return obj; } /// /// 验证微信签名 /// /// /// * 将token、timestamp、nonce三个参数进行字典序排序 /// * 将三个参数字符串拼接成一个字符串进行sha1加密 /// * 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。 public static bool CheckSignature() { string WeChat_Token = config.Token.Value;//从配置文件获取Token string WeChat_Key = HttpContext.Current.Request.QueryString["key"]; //从微信服务器接收传递过来的数据 string signature = HttpContext.Current.Request.QueryString["signature"]; //微信加密签名 string timestamp = HttpContext.Current.Request.QueryString["timestamp"];//时间戳 string nonce = HttpContext.Current.Request.QueryString["nonce"];//随机数 string[] ArrTmp = { WeChat_Token, timestamp, nonce }; Array.Sort(ArrTmp); //字典排序 string tmpStr = string.Join("", ArrTmp);//将三个字符串组成一个字符串 tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");//进行sha1加密 tmpStr = tmpStr.ToLower(); //加过密的字符串与微信发送的signature进行比较,一样则通过微信验证,否则失败。 if (tmpStr == signature) { return true; } else { return false; } } /// /// 生成临时二维码 /// /// 二维码有效时间 /// 二维码参数 /// public static string GenerateEQCode(int days, int arg) { string ticket = string.Empty; string uriStr = string.Format("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={0}", TOKEN); JavaScriptSerializer jsonHelper = new JavaScriptSerializer(); QRCodeResult result = new QRCodeResult(); QRCodeArgs body = new QRCodeArgs() { expire_seconds = 60 * 60 * 24 * days, action_name = "QR_SCENE", action_info = new QRCodeScene() { scene = new QRCodeSceneID() { scene_id = arg } } }; string postData = jsonHelper.Serialize(body); string data = string.Empty; try { byte[] byteArray = Encoding.UTF8.GetBytes(postData); HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(uriStr)); webRequest.Method = "post"; webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.ContentLength = byteArray.Length; System.IO.Stream newStream = webRequest.GetRequestStream(); newStream.Write(byteArray, 0, byteArray.Length); newStream.Close(); HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); data = new System.IO.StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd(); result = jsonHelper.Deserialize(data); } catch (Exception e) { throw e; } if (result != null && !string.IsNullOrEmpty(result.ticket)) { ticket = result.ticket; } return ticket; } /// /// 获取二维码图片 /// /// 票据 /// 图片保存路径 /// public static void GetQRPic(string ticket, string path) { string uriStr = string.Format("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={0}", ticket); try { WebClient webClient = new WebClient(); webClient.DownloadFile(uriStr, path); } catch (Exception e) { throw e; } } } }