/// <summary> /// 刷新jsapi_ticket,数据库更新 /// </summary> /// <param name="accessToken"></param> /// <param name="mpInfo"></param> /// <param name="errMsg">错误信息</param> /// <returns></returns> private string RefreshJsApiTicket(string accessToken, WeixinMPInfo mpInfo, out string errMsg) { string ticket = WeixinTokenBuildHelper.BuildJsApiTickets(accessToken, out errMsg); if (ticket == "") { return(""); } //尝试更新,没有就新增 WxTokensModel model = WxTokensBLL.Instance.GetModel((int)HQEnums.WxTokenTypeOptions.Jsticket); if (model == null) { WxTokensBLL.Instance.Add(new WxTokensModel() { BuildTime = DateTime.Now, Token = ticket, TypeKey = (int)HQEnums.WxTokenTypeOptions.Jsticket }); } else { model.Token = ticket; model.BuildTime = DateTime.Now; WxTokensBLL.Instance.Update(model); } return(ticket); }
/// <summary> /// 刷新accessToken,数据库更新 /// </summary> /// <param name="mpInfo">公众账号APPID等信息</param> /// <param name="errMsg">错误信息</param> /// <returns></returns> private string RefreshAccessToken(WeixinMPInfo mpInfo, out string errMsg) { string accessToken = WeixinTokenBuildHelper.BuildAccessToken(mpInfo.AppId, mpInfo.AppSecret, out errMsg); if (accessToken == "") { return(""); } //尝试更新,没有就新增 WxTokensModel model = WxTokensBLL.Instance.GetModel((int)HQEnums.WxTokenTypeOptions.Accesstoken); if (model == null) { WxTokensBLL.Instance.Add(new WxTokensModel() { BuildTime = DateTime.Now, Token = accessToken, TypeKey = (int)HQEnums.WxTokenTypeOptions.Accesstoken }); } else { model.Token = accessToken; model.BuildTime = DateTime.Now; WxTokensBLL.Instance.Update(model); } return(accessToken); }
/// <summary> /// 执行批量生产 /// </summary> /// <param name="lstMpInfos"></param> private void Execute(List <WeixinMPInfo> lstMpInfos) { DateTime dtBegin = DateTime.Now; LogHelper.Write(string.Format("开始执行Token更新操作,共{0}个...", lstMpInfos.Count)); int completed = 0, successed = 0; foreach (WeixinMPInfo mpInfo in lstMpInfos) { ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object obj) { WeixinMPInfo _mpInfo = (WeixinMPInfo)obj; string accessToken = ""; string jsapiTicket = ""; string errMsg = ""; try { accessToken = this.RefreshAccessToken(_mpInfo, out errMsg); if (!string.IsNullOrEmpty(accessToken)) { jsapiTicket = this.RefreshJsApiTicket(accessToken, _mpInfo, out errMsg); } if (errMsg != "") { LogHelper.Write(">>>更新token失败:" + errMsg); } } catch (Exception ex) { LogHelper.Write(string.Format(">>>更新token失败:{0}", ex.Message)); } lock (lockHelper) { completed++; if (accessToken != "" && jsapiTicket != "") { successed++; } if (completed >= lstMpInfos.Count) { TimeSpan tsEplase = DateTime.Now.Subtract(dtBegin); LogHelper.Write(string.Format("执行Token更新操作结束,耗时:{0}分{1}秒,共操作{2}个,成功{3}个", tsEplase.Minutes, tsEplase.Seconds, completed, successed)); } } }), mpInfo); } }