public static CT_Wechat_Fan GetFans(string openId) { wechatFans _fans = requestUserInfo(openId); if (_fans == null || _fans.errcode > 0) { B_W_Exception.AddExcep("wechatHandle >> GetFans", openId, "获取错误,errcode:" + _fans.errcode); return(null); } #region 赋值 CT_Wechat_Fan o = new CT_Wechat_Fan(); o.WF_OpenId = _fans.openid; o.WF_NickName = _fans.nickname; switch (_fans.sex) { case 1: o.WF_Sex = "男"; break; case 2: o.WF_Sex = "女"; break; default: o.WF_Sex = "未知"; break; } o.WF_Country = _fans.country; o.WF_Province = _fans.province; o.WF_City = _fans.city; o.WF_HeadImgurl = _fans.headimgurl; o.WF_SubscribeTime = GetLocalTime(_fans.subscribe_time); #endregion return(o); }
/// <summary> /// 发送客服消息,图文类型 /// </summary> /// <param name="data"></param> /// <returns></returns> public static int SendCustom_news(string data) { errCode _err = requestCustomSend(data); if (_err.errcode > 0) { B_W_Exception.AddExcep("wechatHandle >> SendCustom_news", "客服图文消息发送失败", _err.errcode.ToString()); } return(_err.errcode); }
/// <summary> /// 用OpenID列表 群发图文消息 /// </summary> /// <param name="StringOpenid"></param> /// <param name="media"></param> /// <returns></returns> public static int SendCustom_news_ArrayString(string mediaId, string StringOpenid) { string Jsonfile = "{\"touser\": [ " + StringOpenid + "],\"mpnews\": { \"media_id\": \"" + mediaId + "\"},\"msgtype\": \"mpnews\"}"; errCode _err = requestMessSend(Jsonfile); if (_err.errcode > 0) { B_W_Exception.AddExcep("wechatHandle >> SendCustom_news", "客服图文消息发送失败", _err.errcode.ToString()); } return(_err.errcode); }
/// <summary> /// 按照组发送图文消息 /// </summary> /// <param name="id"></param> /// <returns></returns> public static int SendCustom_news_group(string mediaId) { string json = "{\"filter\":{ \"group_id\":\"0\" },\"mpnews\":{ \"media_id\":\"" + mediaId + "\" }, \"msgtype\":\"mpnews\"}"; errCode _err = requestMesssendall(json); if (_err.errcode > 0) { B_W_Exception.AddExcep("wechatHandle >> SendCustom_news", "客服图文消息发送失败", _err.errcode.ToString()); } return(_err.errcode); }
/// <summary> /// 发送客服消息,文本类型 /// </summary> /// <param name="touser"></param> /// <param name="content"></param> /// <returns></returns> public static int SendCustom_text(string OpenId, string content) { string data = "{\"touser\":\"" + OpenId + "\",\"msgtype\":\"text\",\"text\":{\"content\":\"" + content + "\"}}"; errCode _err = requestCustomSend(data); if (_err.errcode > 0) { B_W_Exception.AddExcep("wechatHandle >> SendCustom_text", "客服消息发送失败", _err.errcode.ToString()); } return(_err.errcode); }
/// <summary> /// 文件上传 /// </summary> /// <param name="url"></param> /// <param name="fileurl"></param> /// <returns></returns> public static string UploadFile(string url, string fileurl) { try { WebClient webclient = new WebClient(); byte[] response = webclient.UploadFile(new Uri(url), fileurl); string o = Encoding.UTF8.GetString(response); return(o); } catch (Exception e) { B_W_Exception.AddExcep("wechatRequest >> UploadFile", url + "^^^" + fileurl, e.Message); return("{'errcode':10000,'errmsg':'文件长传异常'}"); } }
/// <summary> /// Http Get请求 /// </summary> /// <param name="url"></param> /// <returns></returns> public static string Get(string url) { try { WebRequest wRequest = WebRequest.Create(url); wRequest.Method = "GET"; WebResponse wResponse = wRequest.GetResponse(); Stream stream = wResponse.GetResponseStream(); StreamReader reader = new StreamReader(stream, Encoding.UTF8); string _o = reader.ReadToEnd(); reader.Close(); stream.Close(); wResponse.Close(); return(_o); } catch (Exception e) { B_W_Exception.AddExcep("wechatRequest >> Get", url, e.Message); return("{'errcode':10000,'errmsg':'Get请求异常'}"); } }
/// <summary> /// 根据code,获取OpenID /// </summary> /// <param name="codes"></param> private string _getOpenId(string code) { string OpenId = string.Empty, Key = "OpenId"; if (_http.Session != null && _http.Session[Key] != null) { OpenId = _http.Session[Key].ToString(); return(OpenId); } if (string.IsNullOrEmpty(code)) { OpenId = "ogw2MjhK4qPJbDSmtCeXWLezqAOM"; _http.Session[Key] = OpenId; B_W_Exception.AddExcep("AjaxJson >", "_getOpenId", "获取固定值:" + OpenId); } else { OpenId = wechatHandle.GetOpenId(_http, code); B_W_Exception.AddExcep("AjaxJson >", "_getOpenId", "获取动态值:" + OpenId); } return(OpenId); }
/// <summary> /// Http Post请求 /// </summary> /// <param name="url"></param> /// <param name="data"></param> /// <returns></returns> public static string Post(string url, string data) { try { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; Stream reqstream = req.GetRequestStream(); byte[] b = Encoding.UTF8.GetBytes(data); reqstream.Write(b, 0, b.Length); StreamReader responseReader = new StreamReader(req.GetResponse().GetResponseStream(), System.Text.Encoding.UTF8); string _o = responseReader.ReadToEnd(); responseReader.Close(); reqstream.Close(); return(_o); } catch (Exception e) { B_W_Exception.AddExcep("wechatRequest >> Post", url + "^^^" + data, e.Message); return("{'errcode':10000,'errmsg':'Post请求异常'}"); } }
/// <summary> /// 获取微信客户端请求时的微信OpenId /// </summary> /// <param name="_http"></param> /// <param name="code"></param> /// <returns></returns> public static string GetOpenId(HttpContext _http, string code) { string key = "OpenId", openId = string.Empty; if (_http.Session != null && _http.Session[key] != null) { openId = _http.Session[key].ToString(); B_W_Exception.AddExcep("wechatHandle >> GetOpenId", "Session获取", openId); } if (string.IsNullOrEmpty(openId)) { openId = GetAuthorization(code).openid; B_W_Exception.AddExcep("wechatHandle >> GetOpenId", "微信端交互获取", openId); } //保存OpenId到Session中 if (!string.IsNullOrEmpty(openId)) { if (_http.Session != null) { _http.Session[key] = openId; } } return(openId); }