/// <summary> /// ComponentBag /// </summary> public ComponentBag() { ComponentAccessTokenResult = new ComponentAccessTokenResult(); ComponentAccessTokenExpireTime = DateTime.MinValue; PreAuthCodeResult = new PreAuthCodeResult(); PreAuthCodeExpireTime = DateTime.MinValue; }
/// <summary> /// 获取平台token /// </summary> /// <param name="appid"></param> /// <param name="appSecret"></param> /// <param name="tiket"></param> /// <returns></returns> public static ComponentAccessTokenResult GetComonentToken(string appid, string appSecret, string tiket) { var datatoken = new { component_appid = appid, component_appsecret = appSecret, component_verify_ticket = tiket }; string dataJson = JsonConvert.SerializeObject(datatoken); string result = HttpHelper.DoPostJson(_api_component_tokenUrl, dataJson); ComponentAccessTokenResult token = GetResultModel <ComponentAccessTokenResult>(result); return(token); }
public OpenPlatConfig getCurrentModel(string appid = "", bool isreflsh = false, bool isqiangzhi = false) { if (string.IsNullOrEmpty(appid)) { appid = ConfigurationManager.AppSettings["Component_Appid"]; } OpenPlatConfig model = RedisUtil.Get <OpenPlatConfig>(string.Format(key, appid)); if (model != null) { if (!((model.token_time.AddMinutes(minutes) < DateTime.Now))) { return(model); } } lock (_objlock) { model = RedisUtil.Get <OpenPlatConfig>(string.Format(key, appid)); if (model != null) { if (!((model.token_time.AddMinutes(minutes) < DateTime.Now))) { return(model); } } model = GetModel(string.Format("component_Appid='{0}'", appid)); if (model == null) { throw new Exception("没有设置第三方平台信息"); } if ((model.token_time.AddMinutes(minutes) < DateTime.Now) || string.IsNullOrEmpty(model.component_access_token))//刷新token { try { string tiket = GetComponentVerifyTicket(model.component_Appid); ComponentAccessTokenResult token = WxRequest.GetComonentToken(model.component_Appid, model.component_AppSecret, tiket); if (token != null && token.component_access_token != null && token.component_access_token.Length > 0) { model.component_access_token = token.component_access_token; model.token_time = DateTime.Now; bool result = Update(model, "component_access_token,token_time"); if (!result) { log4net.LogHelper.WriteInfo(this.GetType(), "保存刷新平台后Token失败"); } } else { log4net.LogHelper.WriteInfo(this.GetType(), "第三方平台刷新token失败1:" + Utility.SerializeHelper.SerToJson(token)); } } catch (Exception ex) { log4net.LogHelper.WriteError(this.GetType(), ex); } } RedisUtil.Set <OpenPlatConfig>(string.Format(key, appid), model, TimeSpan.FromMinutes(minutes)); } return(model); }