/// <summary> /// 根据自定义配置获取Token /// </summary> /// <param name="appConfig"></param> /// <returns></returns> public TokenApiResult GetByCustomConfig(IWeChatConfig appConfig) { var url = string.Format("{0}/{1}?grant_type=client_credential&appid={2}&secret={3}", ApiRoot, ApiName, appConfig.AppId, appConfig.AppSecret); var result = Get <TokenApiResult>(url); if (!result.IsSuccess()) { throw new ApiArgumentException("获取接口访问凭据失败:" + result.GetFriendlyMessage() + "(" + result.DetailResult + ")"); } result.ExpiresTime = DateTime.Now.AddSeconds(result.Expires - 30); return(result); }
//public static string SerializeToXml(this WxPayData data) //{ // var strBld = new StringBuilder(); // strBld.AppendLine("<xml>\r\n"); // foreach (var elm in data.GetType().GetProperties().Select((ctx) => // { // var elm = ctx.GetCustomAttributes<XmlElementAttribute>().FirstOrDefault(); // var v = ctx.GetValue(data); // if (v == null) return null; // return new // { // name = elm.ElementName, // value = v // }; // }) // .Where(o => o != null) // .OrderBy(o => o.name) // .Select(o => o)) // { // strBld.AppendLine(string.Format("<{0}>{1}</{0}>", elm.name, elm.value)); // } // strBld.AppendLine("</xml>"); // return strBld.ToString(); //} /// <summary> /// 生成基础授权URL 只能获取 openid /// </summary> /// <param name="config"></param> /// <returns></returns> public static string GenerateBasicAuthorizeUrl(this IWeChatConfig config) { //scope:应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid), //snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且, 即使在未关注的情况下,只要用户授权,也能获取其信息 ) var redirect_uri = HttpUtility.UrlEncode("https://www.yourc.club/wap/pay"); var @params = new string[] { string.Format("appid={0}", config.AppId), string.Format("redirect_uri={0}", redirect_uri), string.Format("scope={0}", "snsapi_base"), string.Format("state={0}", "Joey#wechat_redirect"), string.Format("response_type={0}", "code") }; return(string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?{0}", string.Join("&", @params))); }
/// <summary> /// 获取配置 /// </summary> /// <param name="key">唯一Key</param> /// <returns></returns> public IWeChatConfig GetConfig(object key = null) { if (key == null) { key = GetKey(); } if (key == null) { throw new Exception("Key不能为NULL!"); } IWeChatConfig weChatConfig = null; if (WeChatConfigs.ContainsKey(key)) { weChatConfig = WeChatConfigs[key]; } if (weChatConfig != null) { return(weChatConfig); } var result = WeChatFrameworkFuncsManager.Current.InvokeFunc(WeChatFrameworkFuncTypes.Config_GetWeChatConfigByKey, new WeChatApiCallbackFuncArgInfo { Api = null, Data = key }); weChatConfig = result as IWeChatConfig ?? throw new Exception(string.Format("通过Key:{0}获取Config失败!", key)); if (weChatConfig == null) { throw new Exception("获取微信配置失败。Key:" + key); } if (string.IsNullOrWhiteSpace(weChatConfig.AppId)) { throw new ApiArgumentException("微信配置错误,参数不能为空。Key:" + key, "AppId"); } if (string.IsNullOrWhiteSpace(weChatConfig.AppSecret)) { throw new ApiArgumentException("微信配置错误,参数不能为空。Key:" + key, "AppSecret"); } WeChatConfigs.AddOrUpdate(key, weChatConfig, (tKey, existingVal) => weChatConfig); return(weChatConfig); }
/// <summary> /// 刷新配置以及访问凭据 /// </summary> /// <param name="key"></param> /// <param name="config"></param> public void RefreshConfigAndAccessToken(object key, IWeChatConfig config) { RefreshConfig(key, config); RefreshAccessToken(key); }
/// <summary> /// 刷新配置 /// </summary> /// <param name="key">存储配置key</param> /// <param name="config">微信配置</param> public void RefreshConfig(object key, IWeChatConfig config) { WeChatConfigs.AddOrUpdate(key, config, (tKey, existingVal) => { return(config); }); }
//=======【日志级别】=================================== /* 日志等级,0.不输出日志;1.只输出错误信息; 2.输出错误和正常信息; 3.输出错误信息、正常信息和调试信息 */ public static int GetLogLevel(this IWeChatConfig config) { return(1); }
//=======【代理服务器设置】=================================== /* 默认IP和端口号分别为0.0.0.0和0,此时不开启代理(如有需要才设置) */ public static string GetProxyUrl(this IWeChatConfig config) { return(string.Empty); }
public static string GetSSlCertPassword(this IWeChatConfig config) { return(string.Empty); }