/// <summary> /// 删除自定义菜单 /// </summary> /// <param name="token"></param> /// <returns></returns> public static bool Delete(string token) { string url = "https://api.weixin.qq.com/cgi-bin/menu/delete"; url += "?access_token=" + token; var res = new HttpUtil( ).Get(url); Log.Logger.Log("[wx: DeleteMenu] " + url + "#" + res); try { WxError wx_menu = JsonUtil.FromJson <WxError>(res); if (wx_menu != null && wx_menu.errcode == 0) { return(true); } else { Log.Logger.Log("[wx: DeleteMenu Failed] " + url + "#" + res); return(false); } } catch (Exception ex) { Log.Logger.Log("[wx: DeleteMenu Exception] " + url + "#" + ex.Message); return(false); } }
/// <summary> /// 发送客服消息 /// </summary> /// <param name="token">access token</param> /// <param name="msg">消息内容</param> /// <returns>是否发送成功</returns> public static bool Send(string token, string msg) { string url = "https://api.weixin.qq.com/cgi-bin/message/custom/send"; url += "?access_token=" + token; var res = new HttpUtil( ).PostJson(url, msg); Log.Logger.Log("[wx: Send发送客服消息] " + url + "#" + res); try { WxError wx_token = JsonUtil.FromJson <WxError>(res); if (wx_token != null && wx_token.errcode == 0) { return(true); } else { Log.Logger.Log("[wx: Send发送客服消息 失败] " + url + "#" + res); return(false); } } catch (Exception ex) { Log.Logger.Log("[wx: Send发送客服消息 错误] " + url + "#" + ex.Message); return(false); } }
//todo: 编辑标签 /// <summary> /// 删除标签 /// </summary> /// <param name="token"></param> /// <param name="id">标签id</param> /// <returns>是否删除成功</returns> public static bool Delete(string token, int id) { string url = "https://api.weixin.qq.com/cgi-bin/tags/delete"; url += "?access_token=" + token; var tag = new { tag = new { id = id } }; var res = new AppUtils.HttpUtil( ).PostJson(url, AppUtils.JsonUtil.ToJson(tag)); Log.Logger.Log("[wx: DeleteTag] " + url + "#" + res); try { WxError wx_tag = JsonUtil.FromJson <WxError>(res); if (wx_tag != null && wx_tag.errcode == 0) { return(true); } else { Log.Logger.Log("[wx: DeleteTag Failed] " + url + "#" + res); return(false); } } catch (Exception ex) { Log.Logger.Log("[wx: DeleteTag Exception] " + url + "#" + ex.Message); return(false); } }
/// <summary> /// 取消黑名单 /// </summary> /// <param name="token"></param> /// <param name="openids"></param> /// <returns></returns> public static bool BatchUnblockList(string token, List <string> openids) { string url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchunblacklist"; url += "?access_token=" + token; var list = new { openid_list = openids, }; var json = AppUtils.JsonUtil.ToJson(list); var res = new AppUtils.HttpUtil( ).PostJson(url, json); Log.Logger.Log("[wx: BatchUnblockList] " + url + "#" + res); try { WxError wx_status = JsonUtil.FromJson <WxError>(res); if (wx_status != null && wx_status.errcode == 0) { return(true); } else { Log.Logger.Log("[wx: BatchUnblockList Failed] " + url + "#" + res); return(false); } } catch (Exception ex) { Log.Logger.Log("[wx: BatchUnblockList Exception] " + url + "#" + ex.Message); return(false); } }
/// <summary> /// 创建自定义菜单 /// </summary> public static bool Create(string token) { string url = "https://api.weixin.qq.com/cgi-bin/menu/create"; url += "?access_token=" + token; Models.Menu menu = new Models.Menu { button = new List <Button> { new Button { name = "基本", sub_button = new List <Button> { new Button { name = "今日热搜", type = "click", key = "secret_key", }, new Button { name = "搜索", type = "view", url = "http://www.baidu.com/", }, }, }, new Button() { name = "发图", sub_button = new List <Button> { new Button { type = "pic_sysphoto", name = "系统拍照发图", key = "rselfmenu_1_0", }, new Button { type = "pic_photo_or_album", name = "拍照或者相册发图", key = "rselfmenu_1_1", }, new Button { type = "pic_weixin", name = "微信相册发图", key = "rselfmenu_1_2", }, }, }, new Button { name = "扫码", sub_button = new List <Button> { new Button { type = "scancode_waitmsg", name = "扫码带提示", key = "rselfmenu_0_0", }, new Button { type = "scancode_push", name = "扫码推事件", key = "rselfmenu_0_1", }, }, }, }, }; var json = JsonUtil.ToJson(menu); var res = new HttpUtil( ).PostJson(url, json); Log.Logger.Log("[wx: CreateMenu] " + url + "#" + res); try { WxError wx_menu = JsonUtil.FromJson <WxError>(res); if (wx_menu != null && wx_menu.errcode == 0) { return(true); } else { Log.Logger.Log("[wx: CreateMenu Failed] " + url + "#" + res); return(false); } } catch (Exception ex) { Log.Logger.Log("[wx: CreateMenu Exception] " + url + "#" + ex.Message); return(false); } }
/// <summary> /// 自拟Http请求 /// </summary> /// <param name="url">请求地址</param> /// <param name="Method">请求方式(GET/POST)</param> private string PostMethod(string Url, string Method, string PostParam, string Encode, string ContentType) { Method = Method.ToUpper(); string Param = ""; if (string.IsNullOrEmpty(PostParam)) { int ParamIndex = Url.IndexOf('?'); if (ParamIndex > -1 && Method == "POST") { Param = Url.Substring(ParamIndex + 1); Url = Url.Substring(0, ParamIndex); } } else { Param = PostParam; } HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(Url); Request.Method = Method; if (Method == "POST") { byte[] data = Encoding.GetEncoding(Encode).GetBytes(Param); Request.ContentType = ContentType; Request.ContentLength = data.Length; Stream newStream = Request.GetRequestStream(); /*发送请求*/ newStream.Write(data, 0, data.Length); newStream.Close(); } /*获取请求返回数据*/ Stream StrRes = null; StrRes = Request.GetResponse().GetResponseStream(); StreamReader StrRead = new StreamReader(StrRes); string ReturnStr = StrRead.ReadToEnd(); if (ReturnStr.Contains("errcode")) { WxError WxError = JsonConvert.DeserializeObject <WxError>(ReturnStr); if (WxError.errcode == "0") { return("ok"); } WxException _WxException = new WxException("微信接口调用异常,errorcode:" + WxError.errcode + ",errmsg:" + WxError.errmsg); _WxException.errcode = WxError.errcode; _WxException.errmsg = WxError.errmsg; _WxException.errorigin = ReturnStr; throw _WxException; } StrRead.Close(); return(ReturnStr); }