/// <summary> /// 获取公众号的AccessToken /// </summary> /// <returns>微信提供的AccessToken</returns> public string GetAccessToken() { if (WeChatConfig == null) { throw new NullReferenceException("请使用构造ConnectLinkUp(idOrAppId),或初始化Initialize(idOrAppId)"); } string accessToken = String.Empty; //先从数据库获取Access_Token,如果不存在或已过期,则重新跟微信拿Access_Token string appId = WeChatConfig.AppID; string accessTokenKey = string.Format(RedisKeyPrefix.WECHAT_ACCESS_TOKEN, appId); string updateTokenKey = string.Format(RedisKeyPrefix.WECHAT_TOKEN_CONCURRENT, appId); do { if ("Concurrent".Equals(accessToken)) { SystemLogHelper.Info(MethodBase.GetCurrentMethod(), "GetAccessToken(),已有线程直接去微信获取AccessToken,在此等待"); Thread.Sleep(500);//发生并发的线程需要回到这里,等待单线程更新完成 } AccessToken wechatAccessToken = redisHelper.StringGet <AccessToken>(accessTokenKey); if (wechatAccessToken != null) { accessToken = wechatAccessToken.access_token; } else//Redis里面的Token已经失效,需要单线程去更新Token { accessToken = ConcurrentControl.SingleUserFunc(updateTokenKey, "Concurrent", () => { SystemLogHelper.Info(MethodBase.GetCurrentMethod(), "获取公众号的AccessToken,GetAccessToken(),直接去微信获取AccessToken"); return(GetWeChatAccessToken()); }); } } while ("Concurrent".Equals(accessToken)); return(accessToken); }
/// <summary> /// 检测数据 /// </summary> /// <param name="obj"></param> /// <param name="checkRepeated"></param> internal void CheckData(object obj, bool checkRepeated) { //var types = CRL.TypeCache.GetProperties(obj.GetType(), true).Values; var types = TypeCache.GetTable(obj.GetType()).Fields; string msg = ""; var sb = new StringBuilder(); //检测数据约束 foreach (Attribute.FieldInnerAttribute p in types) { string value = p.GetValue(obj) + ""; if (!string.IsNullOrEmpty(value) && p.MemberName != "AddTime" && checkRepeated) { sb.Append(value.GetHashCode().ToString()); } if (p.PropertyType == typeof(System.String)) { if (p.NotNull && string.IsNullOrEmpty(value)) { msg = string.Format("对象{0}属性{1}值不能为空", obj.GetType(), p.MemberName); throw new Exception(msg); } if (value.Length > p.Length && p.Length < 3000) { msg = string.Format("对象{0}属性{1}长度超过了设定值{2}[{3}]", obj.GetType(), p.MemberName, p.Length, value); throw new Exception(msg); } } } if (checkRepeated) { //string concurrentKey = "insertRepeatedCheck_" + StringHelper.EncryptMD5(sb.ToString()); string concurrentKey = "insertRepeatedCheck_" + sb.ToString().GetHashCode(); if (!ConcurrentControl.Check(concurrentKey, 1)) { throw new Exception("检测到有重复提交的数据,在" + obj.GetType()); } } //校验数据 if (obj is IModel) { msg = (obj as IModel).CheckData(); } if (!string.IsNullOrEmpty(msg)) { msg = string.Format("数据校验证失败,在类型{0} {1} 请核对校验规则", obj.GetType(), msg); throw new Exception(msg); } }
/// <summary> /// 保存笑话数据集合 /// </summary> /// <param name="jokeInfoDtos">多个笑话数据的集合</param> /// <returns>保存成功的记录数</returns> public int SaveRangeJokes(List <JokeInfoDto> jokeInfoDtos) { if (jokeInfoDtos == null || jokeInfoDtos.Count == 0) { return(0); } Guid firstId = jokeInfoDtos[0].Id; string concurrencyKey = string.Format(RedisKeyPrefix.SAVE_JOKE_CONCURRENCY, firstId); int saveResult = ConcurrentControl.SingleUserFunc(concurrencyKey, 0, () => { List <JokeInfo> jokeInfoList = AutoMapperHelper.MapToList <JokeInfo>(jokeInfoDtos); jokeInfoList.ForEach(joke => { joke.CreateBy = nameof(SaveRangeJokes) + Thread.CurrentThread.ManagedThreadId; joke.Version = SysConfigReader.Version; }); int row = jokeInfoService.AddNeedJokeInfo(jokeInfoList); return(row); }); return(saveResult); }