/// <summary> /// 获取预授权码。 /// 先从本地缓存获取,没有则从微信端获取。 /// </summary> /// <param name="manager"></param> /// <param name="suiteId"></param> /// <param name="appIds"></param> /// <param name="suiteAccessToken"></param> /// <param name="errMessage"></param> /// <returns></returns> public static string GetPreAuthCode(this TokenManager manager, string suiteId, IList <int> appIds, string suiteAccessToken, out string errMessage) { errMessage = string.Empty; string cacheKey = string.Empty; appIds = appIds == null ? new List <int>() : appIds; if (appIds == null || appIds.Count() == 0) { cacheKey = string.Format("PREAUTHCODE:{0}:{1}:all", suiteId, suiteAccessToken); } else { cacheKey = string.Format("PREAUTHCODE:{0}:{1}:{2}", suiteId, suiteAccessToken, string.Join("-", appIds.OrderBy(p => p))); } string pre_auth_code = CacheManager.Instance().Get <string>(cacheKey); if (!string.IsNullOrEmpty(pre_auth_code)) { return(pre_auth_code); } var request = new GetPreAuthCodeReqeust(); request.suite_id = suiteId; request.appid = appIds.ToList(); var result = ApiHelper.Instance().GetPreAuthCode(suiteAccessToken, request); if (result.errcode != 0) { errMessage = string.Format("GetPreAuthCode: errcode:{0},errmsg:{1}", result.errcode, result.errmsg); return(pre_auth_code); } //缓存pre_auth_code pre_auth_code = result.pre_auth_code; CacheManager.Instance().Set(cacheKey, pre_auth_code, result.expires_in); return(pre_auth_code); }
/// <summary> /// 获取预授权码 /// 该Api.Weixin用于获取预授权码。预授权码用于企业号授权时的应用提供商安全验证。 /// </summary> /// <param name="helper"></param> /// <param name="request"></param> /// <param name="suite_access_token"></param> /// <returns></returns> public static GetPreAuthCodeResult GetPreAuthCode(this ApiHelper helper, string suite_access_token, GetPreAuthCodeReqeust request) { string url = string.Format("{0}service/get_pre_auth_code?suite_access_token={1}", baseUrl, suite_access_token); return(HttpHelper.HttpPost.GetJsonResult <GetPreAuthCodeReqeust, GetPreAuthCodeResult>(url, request)); }