/// <summary> /// 发送模板消息 /// </summary> /// <param name="message">消息内容</param> /// <param name="access_token">公众号的access_token</param> /// <returns></returns> public static string sendTemplateMessage(WXTemplateMessageBase message, string access_token) { try { if (access_token.isNull()) { return("access_token 有误"); } string body = message.getContent(); if (body.isNull()) { return("模板消息内容有误"); } string url = WXUrls.TEMPLATE_MESSAGE + "?access_token=" + access_token; string response = HttpUtil.postJson(url, null, body); var result = new WXResponseBase(response); if (result.isSuccess()) { return(""); } return(result.errorMessage()); } catch (Exception ex) { return(ex.Message); } }
/// <summary> /// 创建自定义菜单(指定内容) /// </summary> /// <param name="menu_content">自定义菜单内容</param> /// <param name="access_token">公众号的access_token</param> /// <returns></returns> public static string refreshCustomMenu(string menu_content, string access_token) { try { Dictionary <string, object> test = JsonConvert.DeserializeObject <Dictionary <string, object> >(menu_content); if (test == null) { return("自定义菜单内容格式有误"); } string msg = deleteCustomMenu(access_token); if (msg != "") { return("删除原菜单失败, 错误信息:" + msg); } var url = WXUrls.CUSTOM_MENU_CREATE + "?access_token=" + access_token; var response = HttpUtil.postJson(url, null, menu_content); var result = new WXResponseBase(response); if (result.isSuccess()) { return(""); } return(result.errorMessage()); } catch (Exception ex) { return(ex.Message); } }
/// <summary> /// 根据微信授权code获取微信信息 /// </summary> /// <param name="code">微信授权code</param> /// <param name="app_id">公众号appid</param> /// <param name="app_secret">公众号app_secret</param> /// <returns></returns> public static WXUserInfo userInfoByCode(string code, string app_id, string app_secret) { WXGetBase get = new WXGetBase(); get.set("appid", app_id); get.set("secret", app_secret); get.set("code", code); get.set("grant_type", "authorization_code"); var response = HttpUtil.get(WXUrls.WEB_AUTH_ACCESS_TOKEN, null, get.parameters); var result = new WXResponseBase(response); if (!result.isSuccess()) { return(null); } string access_token = result.getValueString("access_token"); string open_id = result.getValueString("openid"); if (open_id.isNull() || access_token.isNull()) { return(null); } get.clear(); get.set("access_token", access_token); get.set("openid", open_id); get.set("lang", "zh_CN"); response = HttpUtil.get(WXUrls.GET_USERINFO, null, get.parameters); result = new WXResponseBase(response); if (result.isSuccess()) { return(new WXUserInfo(result)); } return(null); }
public WXUserInfo(WXResponseBase response) { var fields = this.GetType().GetFields(); foreach (FieldInfo fi in fields) { this.setValue(fi, response.getValue(fi.Name)); } }
/// <summary> /// 删除自定义菜单 /// </summary> /// <param name="access_token">公众号的access_token</param> /// <returns></returns> public static string deleteCustomMenu(string access_token) { string url = WXUrls.CUSTOM_MENU_DELETE + "?access_token=" + access_token; string response = HttpUtil.get(url, null, null); var result = new WXResponseBase(response); if (result.isSuccess()) { return(""); } return(result.errorMessage()); }
/// <summary> /// 根据用户openid获取微信信息 /// </summary> /// <param name="open_id">用户openid</param> /// <param name="access_token">公众号的access_token</param> /// <returns></returns> public static WXUserInfo userInfoByOpenId(string open_id, string access_token) { if (open_id.isNull()) { return(null); } WXGetBase get = new WXGetBase(); get.clear(); get.set("access_token", access_token); get.set("openid", open_id); get.set("lang", "zh_CN"); var response = HttpUtil.get(WXUrls.GET_USERINFO, null, get.parameters); var result = new WXResponseBase(response); if (result.isSuccess()) { return(new WXUserInfo(result)); } return(null); }