/// <summary> /// 根据code获取openid(一个code只能查询一次) /// </summary> /// <param name="code"></param> /// <returns></returns> public String GetOpenIdByCode(String code) { //// 微信API地址 string wxAPIURL = WebConfigeOpert.GetWXAPIURL(); //// 微信平台的APPID string appid = WebConfigeOpert.GetWXappid(); //// 微信平台的密码 string secret = WebConfigeOpert.GetWXAppSecret(); secret = string.IsNullOrEmpty(secret) ? "7233eefb8657eb0f6a1ed7224fdc0e7f" : secret; // 换取access_token 其中包含了openid // 这里通过code换取的是一个特殊的网页授权access_token,与基础支持中的access_token(该access_token用于调用其他接口)不同。 String strUrl = string.Format("{0}sns/oauth2/access_token?appid={1}&secret={2}&code={3}&grant_type=authorization_code", wxAPIURL, appid, secret, code); MWXUserSampleInfo model = null; model = PublicTools.HttpGetRequest <MWXUserSampleInfo>(strUrl); if (model != null) { return(model.openid); } return(string.Empty); }
/// <summary> /// 通过appID和appsecret获取Access_token /// </summary> /// <returns></returns> private static MAccessToken GetAccesstokenFromWX() { try { //// 微信API地址 string wxAPIURL = WebConfigeOpert.GetWXAPIURL(); //// 微信平台的APPID string appid = WebConfigeOpert.GetWXappid(); //// 微信平台的密码 string secret = WebConfigeOpert.GetWXAppSecret(); string strUrl = wxAPIURL + "cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret; MAccessToken mode = PublicTools.HttpGetRequest <MAccessToken>(strUrl); LogOpert.AddWeiXinMessage("获取wxAccesstoken结果:" + mode.access_token); return(mode); } catch (Exception ex) { LogOpert.AddWeiXinMessage("获取wxAccesstoken异常:" + ex); } return(null); }
/// <summary> /// 获取 WXAppSecret /// </summary> /// <returns></returns> public static string GetWXAppSecret() { if (string.IsNullOrEmpty(WXAppSecret)) { WXAppSecret = PublicTools.GetWebConfigValueByKey("WXAppSecret"); } return(string.IsNullOrEmpty(WXAppSecret) ? "570d5b27e5ff093a0b5fbcefb5ce88a3" : WXAppSecret); }
/// <summary> /// 获取 WXappid /// </summary> /// <returns></returns> public static string GetWXappid() { if (string.IsNullOrEmpty(WXappid)) { WXappid = PublicTools.GetWebConfigValueByKey("WXAPPID"); } return(string.IsNullOrEmpty(WXappid) ? "wx5be00d277a12e77d" : WXappid); }
/// <summary> /// 获取 WXAppSecret /// </summary> /// <returns></returns> public static string GetWXAppSecret() { if (string.IsNullOrEmpty(WXAppSecret)) { WXAppSecret = PublicTools.GetWebConfigValueByKey("WXAppSecret"); } return(string.IsNullOrEmpty(WXAppSecret) ? "93a5a13791a722cd828b7bec87687d1a" : WXAppSecret); }
/// <summary> /// 获取平台名称 /// </summary> /// <returns></returns> public static string GetPlatformName() { if (string.IsNullOrEmpty(platformName)) { platformName = PublicTools.GetWebConfigValueByKey("PlatformName"); } return(string.IsNullOrEmpty(platformName)?"平台": platformName); }
/// <summary> /// 获取 WXtoken /// </summary> /// <returns></returns> public static string GetWXtoken() { if (string.IsNullOrEmpty(WXtoken)) { WXtoken = PublicTools.GetWebConfigValueByKey("WXTOKEN"); } return(string.IsNullOrEmpty(WXtoken) ? "bananastore" : WXtoken); }
/// <summary> /// 获取数据库连接语句 /// </summary> /// <returns></returns> public static string GetSqlString() { if (string.IsNullOrEmpty(SqlString)) { SqlString = PublicTools.GetWebConfigValueByKey("SqlString"); } return(string.IsNullOrEmpty(SqlString) ? "" : SqlString); }
/// <summary> /// 获取平台名称 /// </summary> /// <returns></returns> public static string GetPalteWebSite() { if (string.IsNullOrEmpty(palteWebSite)) { palteWebSite = PublicTools.GetWebConfigValueByKey("PalteWebSite"); } return(string.IsNullOrEmpty(palteWebSite) ? "http://www.pankaikj.com/" : palteWebSite); }
/// <summary> /// 获取 微信消息日志记录路径 /// </summary> /// <returns></returns> public static string GetWXMessageLogPath() { if (string.IsNullOrEmpty(WXMessageLogPath)) { WXMessageLogPath = PublicTools.GetWebConfigValueByKey("WXMessageLogPath"); } return(string.IsNullOrEmpty(WXMessageLogPath) ? @"c:\WXMessageLog" : WXMessageLogPath); }
/// <summary> /// 获取 微信公众号接口地址 /// </summary> /// <returns></returns> public static string GetWXAPIURL() { if (string.IsNullOrEmpty(WXAPIurl)) { WXAPIurl = PublicTools.GetWebConfigValueByKey("WXAPIURL"); } return(string.IsNullOrEmpty(WXAPIurl) ? "https://api.weixin.qq.com/" : WXAPIurl); }
/// <summary> /// 获取 WXappid /// </summary> /// <returns></returns> public static string GetWXappid() { if (string.IsNullOrEmpty(WXappid)) { WXappid = PublicTools.GetWebConfigValueByKey("WXAPPID"); } return(string.IsNullOrEmpty(WXappid) ? "wxb6b83df5a7c9d84c" : WXappid); }
/// <summary> /// 获取 是否记录微信消息日志 /// </summary> /// <returns></returns> public static string GetIsAddWXMessageLog() { if (string.IsNullOrEmpty(isAddWXMessageLog)) { isAddWXMessageLog = PublicTools.GetWebConfigValueByKey("IsAddWXMessageLog"); } return(string.IsNullOrEmpty(isAddWXMessageLog) ? "0" : isAddWXMessageLog); }
/// <summary> /// 删除所有菜单 /// </summary> /// <returns></returns> public MwxResult DeletMenu() { //// 微信API地址 string wxAPIURL = WebConfigeOpert.GetWXAPIURL(); string strUrl = wxAPIURL + "cgi-bin/menu/delete?access_token=" + WXTokenOpert.GetAccessToken(); return(PublicTools.HttpGetRequest <MwxResult>(strUrl)); }
/// <summary> /// 获取所有菜单 /// </summary> /// <returns></returns> public string GetMenu() { //// 微信API地址 string wxAPIURL = WebConfigeOpert.GetWXAPIURL(); string strUrl = wxAPIURL + "cgi-bin/menu/get?access_token=" + WXTokenOpert.GetAccessToken(); return(PublicTools.HttpGetRequestAsString(strUrl)); }
/// <summary> /// 向微信服务器请求创建自定义菜单 /// </summary> /// <param name="jsonStr"></param> /// <returns></returns> public MwxResult AddMenu(string jsonStr) { //// 处理结果对象 MwxResult model = null; //// 微信API地址 string interfaceUrl = WebConfigeOpert.GetWXAPIURL(); interfaceUrl = string.IsNullOrEmpty(interfaceUrl) ? "https://api.weixin.qq.com/" : interfaceUrl; //声明一个HttpWebRequest请求 interfaceUrl = string.Format("{0}cgi-bin/menu/create?access_token={1}", interfaceUrl, WXTokenOpert.GetAccessToken()); model = PublicTools.HttpPostRequest <MwxResult>(interfaceUrl, jsonStr); return(model); }
/// <summary> /// 修改用户备注信息 /// </summary> /// <param name="openid">被修改这者的openid</param> /// <param name="remark">修改后的备注信息</param> /// <returns></returns> public MwxResult CheckUserRemark(string openid, string remark) { //// 处理结果对象 MwxResult model = null; //// 微信API地址 string wxAPIURL = WebConfigeOpert.GetWXAPIURL(); //声明一个HttpWebRequest请求 string interfaceUrl = string.Format("{0}cgi-bin/user/info/updateremark?access_token={1}", wxAPIURL, WXTokenOpert.GetAccessToken()); string jsonStr = "{" + "\"openid\";" + openid + ",\"remark\":" + remark + "}"; model = PublicTools.HttpPostRequest <MwxResult>(interfaceUrl, jsonStr); return(model); }
/// <summary> /// 根据OpenID 获取用户基本信息(需关注公众号) /// </summary> /// <param name="openId"></param> public MWXUserInfo GetUserInfo(string openId) { //// 微信API地址 string wxAPIURL = WebConfigeOpert.GetWXAPIURL(); String strUrl = string.Format("{0}cgi-bin/user/info?access_token={1}&openid={2}&lang=zh_CN", wxAPIURL, WXTokenOpert.GetAccessToken(), openId); MWXUserInfo model = PublicTools.HttpGetRequest <MWXUserInfo>(strUrl); //// 对获取大的用户信息的关注日期和头像做处理 if (model != null) { model.subscribe_time = PublicTools.GetDateTimeWithSecond(Convert.ToInt64(model.subscribe_time)).ToString("yyyy-MM-dd HH:mm:ss"); model.headimgurl = model.headimgurl.Substring(0, model.headimgurl.LastIndexOf('/') + 1); } return(model); }