/// <summary> /// 使用AccessToken进行操作时,如果遇到AccessToken错误的情况,重新获取AccessToken一次,并重试。 /// 使用此方法之前必须在startup注册 WeiXinContext.RegisterWX /// </summary> /// <typeparam name="T"></typeparam> /// <param name="fun"></param> /// <param name="fun"></param> /// <returns></returns> public static T TryCommonApi <T>(Func <string, T> fun, bool retryIfFaild = true) where T : WxJsonResult, new() { var access_token = WeiXinContext.AccessToken; var result = fun(access_token); if (result.ReturnCode == ReturnCode.请求成功) { return(result); } if (result.ReturnCode == ReturnCode.获取access_token时AppSecret错误或者access_token无效) { if (retryIfFaild) { WeiXinContext.ClearAccessToken(); return(TryCommonApi <T>(fun, false)); } } throw new Exception(result.ToString()); }
public static Dictionary <string, object> TryCommonApi(Func <string, Dictionary <string, object> > fun, bool retryIfFaild = true) { var access_token = WeiXinContext.AccessToken; var result = fun(access_token); if (!result.ContainsKey("errcode")) { return(result); } if (result["errcode"].ToString() == (int)ReturnCode.请求成功 + "") { return(result); } if (result["errcode"].ToString() == (int)ReturnCode.获取access_token时AppSecret错误或者access_token无效 + "") { if (retryIfFaild) { WeiXinContext.ClearAccessToken(); return(TryCommonApi(fun, false)); } } throw new Exception(result.ToString()); }