/// <summary> /// 及时获得access_token值 /// access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。正常情况下access_token有效期为7200秒, /// 重复获取将导致上次获取的access_token失效。 /// 每日限额获取access_token.我们将access_token保存到数据库里,间隔时间为20分钟,从微信公众平台获得一次。 /// </summary> /// <returns></returns> public string getAccessToken(int wid, out string error) { string token = ""; error = ""; try { Model.wx_userweixin weixininfo = wBll.GetModel(wid); if (weixininfo.AppId == null || weixininfo.AppSecret == null || weixininfo.AppId.Trim().Length <= 0 || weixininfo.AppSecret.Trim().Length <= 0) { error = "appId或者AppSecret未填写完全,请在[我的公众帐号]里补全信息!"; return(""); } XCWeiXin.Model.wx_property_info wxProperty = new XCWeiXin.Model.wx_property_info(); //第一次插入微信属性表 if (!pBll.ExistsWid(wid)) { var result = Senparc.Weixin.MP.CommonAPIs.CommonApi.GetToken(weixininfo.AppId, weixininfo.AppSecret); token = result.access_token; pBll.AddAccess_Token(wid, token); return(token); } wxProperty = pBll.GetModelList("iName='access_token' and wid=" + wid)[0]; TimeSpan chajun = DateTime.Now - wxProperty.createDate.Value; double chajunSecond = chajun.TotalSeconds; if (wxProperty.iContent == null || wxProperty.iContent.Trim() == "" || chajunSecond >= wxProperty.expires_in) { //从微信平台重新获得access_token var result = Senparc.Weixin.MP.CommonAPIs.CommonApi.GetToken(weixininfo.AppId, weixininfo.AppSecret); token = result.access_token; //更新到数据库里 wxProperty.iContent = token; wxProperty.createDate = DateTime.Now; pBll.Update(wxProperty); } else { token = wxProperty.iContent; } } catch (Exception ex) { error = "获得access_token出错:" + ex.Message; } return(token); }
/// <summary> ///【强制刷新】access_token值 /// access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。正常情况下access_token有效期为7200秒, /// 重复获取将导致上次获取的access_token失效。 /// 每日限额获取access_token.我们将access_token保存到数据库里,间隔时间为20分钟,从微信公众平台获得一次。 /// </summary> /// <returns></returns> public static string FlushAccessToken(int wid, out string error) { string token = ""; error = ""; try { wx_property_info pBll = new wx_property_info(); BLL.wx_userweixin wBll = new wx_userweixin(); Model.wx_userweixin weixininfo = wBll.GetModel(wid); if (weixininfo.AppId == null || weixininfo.AppSecret == null || weixininfo.AppId.Trim().Length <= 0 || weixininfo.AppSecret.Trim().Length <= 0) { error = "appId或者AppSecret未填写完全,请在[我的公众帐号]里补全信息!"; return(""); } var result = CommonApi.GetToken(weixininfo.AppId, weixininfo.AppSecret); token = result.access_token; //第一次插入微信属性表 if (!pBll.ExistsWid(wid, "access_token")) { //插入 pBll.AddAccess_Token(wid, token, result.expires_in); } else { WeiXinPF.Model.wx_property_info wxProperty = new WeiXinPF.Model.wx_property_info(); wxProperty = pBll.GetModelList("iName='access_token' and wid=" + wid)[0]; //更新到数据库里 wxProperty.iContent = token; wxProperty.createDate = DateTime.Now; wxProperty.expires_in = result.expires_in; pBll.Update(wxProperty); } } catch (Exception ex) { error = "获得access_token出错:" + ex.Message; WXLogs.AddLog(wid, "access_token", "获得access_token(FlushAccessToken)", error); } return(token); }
/// <summary> /// 及时获得access_token值 /// access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。正常情况下access_token有效期为7200秒, /// 重复获取将导致上次获取的access_token失效。 /// 每日限额获取access_token.我们将access_token保存到数据库里,间隔时间为20分钟,从微信公众平台获得一次。 /// </summary> /// <returns></returns> public static string getAccessToken(int wid, out string error) { string token = ""; error = ""; try { wx_property_info pBll = new wx_property_info(); BLL.wx_userweixin wBll = new wx_userweixin(); Model.wx_userweixin weixininfo = wBll.GetModel(wid); if (weixininfo.AppId == null || weixininfo.AppSecret == null || weixininfo.AppId.Trim().Length <= 0 || weixininfo.AppSecret.Trim().Length <= 0) { error = "appId或者AppSecret未填写完全,请在[我的公众帐号]里补全信息!"; WXLogs.AddLog(wid, "access_token", "获得access_token", error); return(""); } WeiXinPF.Model.wx_property_info wxProperty = new WeiXinPF.Model.wx_property_info(); //第一次插入微信属性表 if (!pBll.ExistsWid(wid, "access_token")) { AccessTokenResult result = CommonApi.GetToken(weixininfo.AppId, weixininfo.AppSecret); token = result.access_token; pBll.AddAccess_Token(wid, token, result.expires_in); return(token); //WeChatAccountManager manager = WeChatAccountManager.CreateInstance(weixininfo.AppId, weixininfo.AppSecret); //Credential credential = new Credential(manager); //token = credential.AccessToken; //pBll.AddAccess_Token(wid, token, 7000); //return token; } wxProperty = pBll.GetModelList("iName='access_token' and wid=" + wid)[0]; double chajunSecond = (DateTime.Now - wxProperty.createDate.Value).TotalSeconds; if (chajunSecond >= wxProperty.expires_in) { //从微信平台重新获得access_token AccessTokenResult result = CommonApi.GetToken(weixininfo.AppId, weixininfo.AppSecret); token = result.access_token; //更新到数据库里 wxProperty.iContent = token; wxProperty.createDate = DateTime.Now; wxProperty.expires_in = result.expires_in; //WeChatAccountManager manager = WeChatAccountManager.CreateInstance(weixininfo.AppId, weixininfo.AppSecret); //Credential credential = new Credential(manager); //token = credential.AccessToken; ////更新到数据库里 //wxProperty.iContent = token; //wxProperty.createDate = DateTime.Now; //wxProperty.expires_in = 7000; //pBll.Update(wxProperty); } else { token = wxProperty.iContent; } } catch (Exception ex) { error = "获得access_token出错:" + ex.Message; WXLogs.AddLog(wid, "access_token", "获得access_token", error); } return(token); }