// // 获取用户的最新 OpenId 的方法 // /// <summary> /// /// </summary> /// <param name="userInfo"></param> /// <param name="cachingSystemCode"></param> /// <param name="useCaching"></param> /// <param name="useDataBase"></param> /// <param name="useUserCenterHost"></param> /// <returns></returns> public static string GetUserOpenId(BaseUserInfo userInfo, string cachingSystemCode = null, bool useCaching = false, bool useDataBase = true, bool useUserCenterHost = false) { var result = string.Empty; if (useCaching && userInfo != null && userInfo.UserId > 0) { var key = string.Empty; if (string.IsNullOrEmpty(cachingSystemCode)) { key = "userId:" + userInfo.Id; } else { key = "userId:" + cachingSystemCode + ":" + userInfo.Id; } result = CacheUtil.Get <string>(key); // 2016-04-05 吉日嘎拉 这里代码有错误,不为空时进行处理 if (!string.IsNullOrWhiteSpace(result)) { userInfo.OpenId = result; HttpContext.Current.Session[SessionName] = userInfo; HttpContext.Current.Session["openId"] = userInfo.OpenId; // 2016-04-05 吉日嘎拉,这里可以提前结束了,提高效率 return(result); } if (useDataBase) { var userLogonManager = new BaseUserLogonManager(userInfo); result = userLogonManager.GetUserOpenId(userInfo, cachingSystemCode); } if (string.IsNullOrEmpty(result) && useUserCenterHost) { //停用远程获取 Troy.Cui 2018.07.23 //result = LogonHttpUtil.GetUserOpenId(userInfo, cachingSystemCode); } // 从数据库获取,这里要考虑读写分离的做法 if (!string.IsNullOrWhiteSpace(result)) { userInfo.OpenId = result; HttpContext.Current.Session[SessionName] = userInfo; HttpContext.Current.Session["openId"] = userInfo.OpenId; // 这里这样处理一下,提高下次处理的效率 SetUserOpenId(userInfo.Id.ToString(), userInfo.OpenId, cachingSystemCode); } } return(result); }