/// <summary> /// 使用AccessToken进行操作时,如果遇到AccessToken错误的情况,重新获取AccessToken一次,并重试。 /// 使用此方法之前必须使用AccessTokenContainer.Register(_appId, _appSecret);或JsApiTicketContainer.Register(_appId, _appSecret);方法对账号信息进行过注册,否则会出错。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="fun"></param> /// <param name="accessTokenOrAppId">AccessToken或AppId。如果为null,则自动取已经注册的第一个appId/appSecret来信息获取AccessToken。</param> /// <param name="retryIfFaild">请保留默认值true,不用输入。</param> /// <returns></returns> public static T TryCommonApi <T>(Func <string, T> fun, string accessTokenOrAppId = null, bool retryIfFaild = true) where T : WxJsonResult { string appId = null; string accessToken = null; if (accessTokenOrAppId == null) { appId = AccessTokenContainer.GetFirstOrDefaultAppId(); if (appId == null) { throw new WeixinException("尚无已经注册的AppId,请先使用AccessTokenContainer.Register完成注册(全局执行一次即可)!"); } } else if (ApiUtility.IsAppId(accessTokenOrAppId)) { if (!AccessTokenContainer.CheckRegistered(accessTokenOrAppId)) { throw new WeixinException("此appId尚未注册,请先使用AccessTokenContainer.Register完成注册(全局执行一次即可)!"); } appId = accessTokenOrAppId; } else { //accessToken accessToken = accessTokenOrAppId; } T result = null; try { if (accessToken == null) { var accessTokenResult = AccessTokenContainer.GetTokenResult(appId, false); accessToken = accessTokenResult.access_token; } result = fun(accessToken); } catch (ErrorJsonResultException ex) { if (!retryIfFaild && appId != null && ex.JsonResult.errcode == ReturnCode.获取access_token时AppSecret错误或者access_token无效) { //尝试重新验证 var accessTokenResult = AccessTokenContainer.GetTokenResult(appId, true); accessToken = accessTokenResult.access_token; result = TryCommonApi(fun, appId, false); } else { result = Activator.CreateInstance <T>(); result.errcode = ex.JsonResult.errcode; result.errmsg = ex.JsonResult.errmsg; } } return(result); }
public void GetFirstOrDefaultAppIdTest() { //此测试需要使用本地缓存进行测试 var registeredAppId = base._appId;//已经注册的AppId var appId = AccessTokenContainer.GetFirstOrDefaultAppId(); Assert.AreEqual(registeredAppId, appId); //注册多个AppId for (int i = 0; i < 100; i++) { AccessTokenContainer.Register("TestAppId_" + i, "TestAppSecret"); } ////删除部分AppId //var collectionList = AccessTokenContainer.GetCollectionList(); //for (int i = 10; i < 50; i++) //{ // collectionList.Remove("TestAppId_" + i); //} //appId = AccessTokenContainer.GetFirstOrDefaultAppId(); //Assert.AreEqual(registeredAppId, appId); }
/// <summary> /// 拼享惠产品描述编辑 /// </summary> /// <param name="id"></param> /// <returns></returns> public ActionResult PinRichText(int id = 0) { string token = WxHelper.GetToken(webview_appid, webview_appsecret, false); ViewBag.appid = AccessTokenContainer.GetFirstOrDefaultAppId(); ViewBag.ticket = JsApiTicketContainer.GetJsApiTicket(JsApiTicketContainer.GetFirstOrDefaultAppId()); ViewBag.timestamp = JSSDKHelper.GetTimestamp(); ViewBag.nonceStr = JSSDKHelper.GetNoncestr(); ViewBag.signature = JSSDKHelper.GetSignature(ViewBag.ticket, ViewBag.nonceStr, ViewBag.timestamp, Server.UrlDecode(GetPageUrl())); ViewBag.pageUrl = GetPageUrl(); if (id == 0) { //string temp = RedisUtil.Get<string>(PinGoodsBLL.key_new_pin_goods); ViewBag.description = ""; //temp ?? ""; } else { PinGoods good = PinGoodsBLL.SingleModel.GetModel(id); if (good == null) { ViewBag.description = ""; return(Content("产品不存在或已删除")); } ViewBag.description = good.description; } return(View("RichText")); }
public ActionResult RichText(int id = 0) { //string token = AccessTokenContainer.GetAccessToken(AccessTokenContainer.GetFirstOrDefaultAppId()); string token = WxHelper.GetToken(webview_appid, webview_appsecret, false); ViewBag.appid = AccessTokenContainer.GetFirstOrDefaultAppId(); ViewBag.ticket = JsApiTicketContainer.GetJsApiTicket(JsApiTicketContainer.GetFirstOrDefaultAppId()); ViewBag.timestamp = JSSDKHelper.GetTimestamp(); ViewBag.nonceStr = JSSDKHelper.GetNoncestr(); ViewBag.signature = JSSDKHelper.GetSignature(ViewBag.ticket, ViewBag.nonceStr, ViewBag.timestamp, GetPageUrl()); ViewBag.pageUrl = GetPageUrl(); if (id == 0) { ViewBag.description = ""; } else { EntGoods good = EntGoodsBLL.SingleModel.GetModel(id); if (good == null) { ViewBag.description = ""; return(Content("产品不存在或已删除")); } ViewBag.description = good.description; } return(View()); }
public ActionResult PlatStoreDescriptionRichText(int id = 0) { //string token = AccessTokenContainer.GetAccessToken(AccessTokenContainer.GetFirstOrDefaultAppId()); string token = WxHelper.GetToken(webview_appid, webview_appsecret, false); ViewBag.appid = AccessTokenContainer.GetFirstOrDefaultAppId(); ViewBag.ticket = JsApiTicketContainer.GetJsApiTicket(JsApiTicketContainer.GetFirstOrDefaultAppId()); ViewBag.timestamp = JSSDKHelper.GetTimestamp(); ViewBag.nonceStr = JSSDKHelper.GetNoncestr(); ViewBag.signature = JSSDKHelper.GetSignature(ViewBag.ticket, ViewBag.nonceStr, ViewBag.timestamp, GetPageUrl()); log4net.LogHelper.WriteInfo(this.GetType(), $"token={token};appid={ViewBag.appid};ticket={ViewBag.ticket};timestamp={ ViewBag.timestamp};nonceStr={ViewBag.nonceStr};signature={ViewBag.signature};pageUrl={GetPageUrl()}"); if (id == 0) { // string temp = RedisUtil.Get<string>("temp_psd_description_0"); ViewBag.description = "";//temp ?? ""; } else { PlatStore platStore = PlatStoreBLL.SingleModel.GetModel(id); if (platStore == null || platStore.State == -1) { ViewBag.description = ""; return(Content("店铺不存在或已删除")); } ViewBag.description = platStore.StoreDescription; } return(View()); }
/// <summary> /// 使用AccessToken进行操作时,如果遇到AccessToken错误的情况,重新获取AccessToken一次,并重试。 /// 使用此方法之前必须使用AccessTokenContainer.Register(_appId, _appSecret);或JsApiTicketContainer.Register(_appId, _appSecret);方法对账号信息进行过注册,否则会出错。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="fun"></param> /// <param name="accessTokenOrAppKey">AccessToken或AppKey。如果为null,则自动取已经注册的第一个corpId/corpSecret来信息获取AccessToken。</param> /// <param name="retryIfFaild">请保留默认值true,不用输入。</param> /// <returns></returns> public static T TryCommonApi<T>(Func<string, T> fun, string accessTokenOrAppKey, bool retryIfFaild = true) where T : WorkJsonResult { Func<string> accessTokenContainer_GetFirstOrDefaultAppIdFunc = () => AccessTokenContainer.GetFirstOrDefaultAppId(); Func<string, bool> accessTokenContainer_CheckRegisteredFunc = appKey => { /* * 对于企业微信来说,AppId = key = CorpId+'@'+CorpSecret */ return AccessTokenContainer.CheckRegistered(appKey); }; Func<string, bool, IAccessTokenResult> accessTokenContainer_GetAccessTokenResultFunc = (appKey, getNewToken) => { /* * 对于企业微信来说,AppId = key = CorpId+'@'+CorpSecret */ return AccessTokenContainer.GetTokenResult(appKey, getNewToken); }; int invalidCredentialValue = (int)ReturnCode_Work.获取access_token时Secret错误_或者access_token无效; var result = ApiHandlerWapperBase. TryCommonApiBase( PlatformType.Work, accessTokenContainer_GetFirstOrDefaultAppIdFunc, accessTokenContainer_CheckRegisteredFunc, accessTokenContainer_GetAccessTokenResultFunc, invalidCredentialValue, fun, accessTokenOrAppKey, retryIfFaild); return result; }
/// <summary> /// 使用AccessToken进行操作时,如果遇到AccessToken错误的情况,重新获取AccessToken一次,并重试。 /// 使用此方法之前必须使用AccessTokenContainer.Register(_appId, _appSecret);或JsApiTicketContainer.Register(_appId, _appSecret);方法对账号信息进行过注册,否则会出错。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="fun"></param> /// <param name="accessTokenOrAppId">AccessToken或AppId。如果为null,则自动取已经注册的第一个appId/appSecret来信息获取AccessToken。</param> /// <param name="retryIfFaild">请保留默认值true,不用输入。</param> /// <returns></returns> public static T TryCommonApi <T>(Func <string, T> fun, string accessTokenOrAppId = null, bool retryIfFaild = true) where T : WxJsonResult { string appId = null; string accessToken = null; if (accessTokenOrAppId == null) { appId = AccessTokenContainer.GetFirstOrDefaultAppId(); if (appId == null) { throw new UnRegisterAppIdException(null, "尚无已经注册的AppId,请先使用AccessTokenContainer.Register完成注册(全局执行一次即可)!"); } } else if (ApiUtility.IsAppId(accessTokenOrAppId)) { if (!AccessTokenContainer.CheckRegistered(accessTokenOrAppId)) { throw new UnRegisterAppIdException(accessTokenOrAppId, string.Format("此appId({0})尚未注册,请先使用AccessTokenContainer.Register完成注册(全局执行一次即可)!", accessTokenOrAppId)); } appId = accessTokenOrAppId; } else { accessToken = accessTokenOrAppId;//accessToken } T result = null; try { if (accessToken == null) { var accessTokenResult = AccessTokenContainer.GetAccessTokenResult(appId, false); accessToken = accessTokenResult.access_token; } result = fun(accessToken); } catch (ErrorJsonResultException ex) { if (retryIfFaild && appId != null && ex.JsonResult.errcode == ReturnCode.获取access_token时AppSecret错误或者access_token无效) { //尝试重新验证 var accessTokenResult = AccessTokenContainer.GetAccessTokenResult(appId, true);//强制获取并刷新最新的AccessToken accessToken = accessTokenResult.access_token; result = TryCommonApi(fun, appId, false); } else { throw; } } return(result); }
public ActionResult GetJSSDK(string url) { string token = WxHelper.GetToken(webview_appid, webview_appsecret, false); string appId = AccessTokenContainer.GetFirstOrDefaultAppId(); string ticket = JsApiTicketContainer.GetJsApiTicket(webview_appid); string timestamp = JSSDKHelper.GetTimestamp(); string nonceStr = JSSDKHelper.GetNoncestr(); string signature = JSSDKHelper.GetSignature(ticket, nonceStr, timestamp, HttpUtility.UrlDecode(url)); //string accesstoken = AccessTokenContainer.TryGetAccessToken(webview_appid, webview_appsecret); object SDK = new { AppId = webview_appid, Timestamp = int.Parse(timestamp), NonceStr = nonceStr, Signature = signature, Ticket = ticket, Url = url, }; return(ApiModel(isok: true, message: "获取成功", data: SDK)); }
///// 使用AccessToken进行操作时,如果遇到AccessToken错误的情况,重新获取AccessToken一次,并重试 ///// </summary> ///// <typeparam name="T"></typeparam> ///// <param name="appId"></param> ///// <param name="appSecret"></param> ///// <param name="fun">第一个参数为accessToken</param> ///// <param name="retryIfFaild"></param> ///// <returns></returns> //[Obsolete("请使用TryCommonApi()方法")] //public static T Do<T>(Func<string, T> fun, string appId, string appSecret, bool retryIfFaild = true) // where T : WxJsonResult //{ // T result = null; // try // { // var accessToken = AccessTokenContainer.TryGetAccessToken(appId, appSecret, false); // result = fun(accessToken); // } // catch (ErrorJsonResultException ex) // { // if (retryIfFaild && ex.JsonResult.errcode == ReturnCode.获取access_token时AppSecret错误或者access_token无效) // { // //尝试重新验证 // var accessToken = AccessTokenContainer.TryGetAccessToken(appId, appSecret, true); // result = Do(fun, appId, appSecret, false); // } // } // return result; //} #endregion #endregion #if !NET35 && !NET40 #region 异步方法 /// <summary> /// 【异步方法】使用AccessToken进行操作时,如果遇到AccessToken错误的情况,重新获取AccessToken一次,并重试。 /// 使用此方法之前必须使用AccessTokenContainer.Register(_appId, _appSecret);或JsApiTicketContainer.Register(_appId, _appSecret);方法对账号信息进行过注册,否则会出错。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="fun"></param> /// <param name="accessTokenOrAppId">AccessToken或AppId。如果为null,则自动取已经注册的第一个appId/appSecret来信息获取AccessToken。</param> /// <param name="retryIfFaild">请保留默认值true,不用输入。</param> /// <returns></returns> public static async Task <T> TryCommonApiAsync <T>(Func <string, Task <T> > fun, string accessTokenOrAppId = null, bool retryIfFaild = true) where T : WxJsonResult { Func <string> accessTokenContainer_GetFirstOrDefaultAppIdFunc = () => AccessTokenContainer.GetFirstOrDefaultAppId(PlatformType.MP); Func <string, bool> accessTokenContainer_CheckRegisteredFunc = appId => AccessTokenContainer.CheckRegistered(appId); Func <string, bool, Task <IAccessTokenResult> > accessTokenContainer_GetAccessTokenResultAsyncFunc = (appId, getNewToken) => AccessTokenContainer.GetAccessTokenResultAsync(appId, getNewToken); int invalidCredentialValue = (int)ReturnCode.获取access_token时AppSecret错误或者access_token无效; var result = ApiHandlerWapperBase. TryCommonApiBaseAsync( PlatformType.MP, accessTokenContainer_GetFirstOrDefaultAppIdFunc, accessTokenContainer_CheckRegisteredFunc, accessTokenContainer_GetAccessTokenResultAsyncFunc, invalidCredentialValue, fun, accessTokenOrAppId, retryIfFaild); return(await result); }
/// <summary> /// 使用AccessToken进行操作时,如果遇到AccessToken错误的情况,重新获取AccessToken一次,并重试。 /// 使用此方法之前必须使用AccessTokenContainer.Register(_appId, _appSecret);或JsApiTicketContainer.Register(_appId, _appSecret);方法对账号信息进行过注册,否则会出错。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="fun"></param> /// <param name="accessTokenOrAppId">AccessToken或AppId。如果为null,则自动取已经注册的第一个appId/appSecret来信息获取AccessToken。</param> /// <param name="retryIfFaild">请保留默认值true,不用输入。</param> /// <returns></returns> public static T TryCommonApi <T>(Func <string, T> fun, string accessTokenOrAppId = null, bool retryIfFaild = true) where T : WxOpenJsonResult { Func <string> accessTokenContainer_GetFirstOrDefaultAppIdFunc = () => AccessTokenContainer.GetFirstOrDefaultAppId(PlatformType.WeChat_MiniProgram); Func <string, bool> accessTokenContainer_CheckRegisteredFunc = appId => AccessTokenContainer.CheckRegistered(appId); Func <string, bool, IAccessTokenResult> accessTokenContainer_GetAccessTokenResultFunc = (appId, getNewToken) => AccessTokenContainer.GetAccessTokenResult(appId, getNewToken); int invalidCredentialValue = (int)ReturnCode.获取access_token时AppSecret错误或者access_token无效; var result = ApiHandlerWapperBase. TryCommonApiBase( PlatformType.WeChat_OfficialAccount, accessTokenContainer_GetFirstOrDefaultAppIdFunc, accessTokenContainer_CheckRegisteredFunc, accessTokenContainer_GetAccessTokenResultFunc, invalidCredentialValue, fun, accessTokenOrAppId, retryIfFaild); return(result); }
/// <summary> /// 使用AccessToken进行操作时,如果遇到AccessToken错误的情况,重新获取AccessToken一次,并重试。 /// 使用此方法之前必须使用AccessTokenContainer.Register(_appId, _appSecret);或JsApiTicketContainer.Register(_appId, _appSecret);方法对账号信息进行过注册,否则会出错。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="fun"></param> /// <param name="accessTokenOrAppId">AccessToken或AppId。如果为null,则自动取已经注册的第一个appId/appSecret来信息获取AccessToken。</param> /// <param name="retryIfFaild">请保留默认值true,不用输入。</param> /// <returns></returns> public static T TryCommonApi <T>(Func <string, T> fun, string accessTokenOrAppId = null, bool retryIfFaild = true) where T : WxJsonResult { Func <string> accessTokenContainer_GetFirstOrDefaultAppIdFunc = () => AccessTokenContainer.GetFirstOrDefaultAppId(PlatformType.MP); Func <string, bool> accessTokenContainer_CheckRegisteredFunc = appId => AccessTokenContainer.CheckRegistered(appId); Func <string, bool, IAccessTokenResult> accessTokenContainer_GetAccessTokenResultFunc = (appId, getNewToken) => AccessTokenContainer.GetAccessTokenResult(appId, getNewToken); int invalidCredentialValue = (int)ReturnCode.获取access_token时AppSecret错误或者access_token无效; var result = ApiHandlerWapperBase. TryCommonApiBase( PlatformType.MP, accessTokenContainer_GetFirstOrDefaultAppIdFunc, accessTokenContainer_CheckRegisteredFunc, accessTokenContainer_GetAccessTokenResultFunc, invalidCredentialValue, fun, accessTokenOrAppId, retryIfFaild); return(result); #region V1.0方法 //ApiHandlerWapperFactory.ApiHandlerWapperFactoryCollection["s"] = ()=> new Senparc.Weixin.MP.AdvancedAPIs.User.UserInfoJson(); //var platform = ApiHandlerWapperFactory.CurrentPlatform;//当前平台 //string appId = null; //string accessToken = null; //if (accessTokenOrAppId == null) //{ // appId = AccessTokenContainer.GetFirstOrDefaultAppId(); // if (appId == null) // { // throw new UnRegisterAppIdException(null, // "尚无已经注册的AppId,请先使用AccessTokenContainer.Register完成注册(全局执行一次即可)!"); // } //} //else if (ApiUtility.IsAppId(accessTokenOrAppId)) //{ // if (!AccessTokenContainer.CheckRegistered(accessTokenOrAppId)) // { // throw new UnRegisterAppIdException(accessTokenOrAppId, string.Format("此appId({0})尚未注册,请先使用AccessTokenContainer.Register完成注册(全局执行一次即可)!", accessTokenOrAppId)); // } // appId = accessTokenOrAppId; //} //else //{ // accessToken = accessTokenOrAppId;//accessToken //} //T result = null; //try //{ // if (accessToken == null) // { // var accessTokenResult = AccessTokenContainer.GetAccessTokenResult(appId, false); // accessToken = accessTokenResult.access_token; // } // result = fun(accessToken); //} //catch (ErrorJsonResultException ex) //{ // if (retryIfFaild // && appId != null // && ex.JsonResult.errcode == ReturnCode.获取access_token时AppSecret错误或者access_token无效) // { // //尝试重新验证 // var accessTokenResult = AccessTokenContainer.GetAccessTokenResult(appId, true); // //强制获取并刷新最新的AccessToken // accessToken = accessTokenResult.access_token; // result = TryCommonApi(fun, appId, false); // } // else // { // ex.AccessTokenOrAppId = accessTokenOrAppId; // throw; // } //} //catch (WeixinException ex) //{ // ex.AccessTokenOrAppId = accessTokenOrAppId; // throw; //} //return result; #endregion }