public override void Executing(IJobExecutionContext context) { var broker = PersistBrokerFactory.GetPersistBroker(); Logger.Debug("开始同步微信公众号图文素材"); try { broker.ExecuteTransaction(() => { var result = new WeChatNewsService(broker).GetDataList(1, 5000); var user = UserIdentityUtil.GetCurrentUser() ?? UserIdentityUtil.GetSystem(); var dataList = (from item in result.item let news = item.content.news_item.FirstOrDefault() select new wechat_news() { wechat_newsId = item.media_id, html_content = new WeChatNewsMaterialService(broker).ConvertWeChatUrlToLocalUrl(news?.content), media_id = item.media_id, update_time = item.update_time, name = news?.title, author = news?.author, digest = news?.digest, thumb_media_id = news?.thumb_media_id, content_source_url = news?.content_source_url, createdBy = user.Id, createdByName = user.Name, modifiedBy = user.Id, modifiedByName = user.Name, createdOn = DateTime.Now, modifiedOn = DateTime.Now, }) .ToList(); broker.BulkCreateOrUpdate(dataList); Logger.Debug($"发现{result.item_count}篇文章需要同步"); Logger.Debug($"同步微信公众号图文素材成功,共同步:{dataList.Count()}篇文章"); }); } catch (Exception e) { Logger.Error("同步微信公众号图文素材失败", e); } }
/// <summary> /// 注册作业 /// </summary> public static void Start() { var logger = LogFactory.GetLogger("startup"); var jobs = ServiceContainer.ResolveAll <IJob>().ToList(); StartService(); logger.Info($"共发现{jobs.Count}个Job待运行"); jobs.Each(item => { if (item == null) { return; } // 创建 Job var instance = item as JobBase; if (sched.CheckExists(instance.JobKey).Result) { return; } var job = instance.GetJobBuilder().Build(); job.JobDataMap.Add("User", UserIdentityUtil.GetSystem()); var triggerBuilder = instance.GetTriggerBuilder(); if (triggerBuilder != null) { // 创建 trigger ITrigger trigger = triggerBuilder.Build(); // 使用 trigger 规划执行任务 job sched.ScheduleJob(job, trigger); logger.Info($"作业[{instance.Name}]运行成功"); if (instance.DefaultTriggerState == TriggerState.Paused) { sched.PauseTrigger(trigger.Key); } } }); }
/// <summary> /// 生成权限 /// </summary> /// <param name="entity"></param> /// <param name="role"></param> /// <param name="type"></param> /// <returns></returns> public static sys_role_privilege GenerateRolePrivilege(BaseEntity entity, sys_role role, int value) { var user = UserIdentityUtil.GetSystem(); var privilege = new sys_role_privilege() { Id = Guid.NewGuid().ToString(), objectid = entity.Id, objectidName = entity.name, object_type = entity.EntityName, sys_roleid = role.Id, sys_roleidName = role.name, createdBy = user.Id, createdByName = user.Name, createdOn = DateTime.Now, modifiedBy = user.Id, modifiedByName = user.Name, modifiedOn = DateTime.Now, privilege = value }; return(privilege); }
public string ResetPassword(string id) { UserIdentityUtil.SetCurrentUser(UserIdentityUtil.GetSystem()); return(new MailVertificationService().ResetPassword(id)); }
public string ActivateUser(string id) { UserIdentityUtil.SetCurrentUser(UserIdentityUtil.GetSystem()); return(new MailVertificationService().ActivateUser(id)); }
public static IApplicationBuilder UseEntityWatcher(this IApplicationBuilder app) { var logger = LogFactory.GetLogger("entity"); UserIdentityUtil.SetCurrentUser(UserIdentityUtil.GetSystem()); var broker = PersistBrokerFactory.GetPersistBroker(); var dialect = broker.DbClient.Driver; var entityList = ServiceContainer.ResolveAll <IEntity>().OrderByDescending(item => item.GetEntityName() == typeof(sys_attrs).Name); broker.ExecuteTransaction(() => { #region 创建表 entityList.Each(item => { var entity = broker.Query(dialect.GetTable(item.GetEntityName())); var attrs = item.GetAttrs(); if (entity == null || entity.Rows.Count == 0) { var attrSql = attrs .Select(e => { return($"{e.Name} {e.Type.GetDescription()}{(e.Length != null ? $"({e.Length.Value})" : "")} {(e.IsRequire.HasValue && e.IsRequire.Value ? "NOT NULL" : "")}{(e.Name == $"{item.GetEntityName()}id" ? " PRIMARY KEY" : "")}"); }) .Aggregate((a, b) => a + ",\r\n" + b); // 创建表 var sql = $@"CREATE TABLE public.{item.GetEntityName()} ({attrSql})"; broker.Execute(sql); logger.Info($"实体{item.GetLogicalName()}({item.GetEntityName()})创建成功"); } }); #endregion #region 创建实体记录和实体字段数据 entityList.Each(item => { #region 实体添加自动写入记录 var entityName = item.GetEntityName(); var entity = broker.Retrieve <sys_entity>("select * from sys_entity where code = @code", new Dictionary <string, object>() { { "@code", entityName } }); if (entity == null) { entity = new sys_entity() { Id = Guid.NewGuid().ToString(), name = item.GetLogicalName(), code = item.GetEntityName(), is_sys = item.IsSystemEntity() }; broker.Create(entity, false); } #endregion var attrs = item.GetAttrs(); var attrsList = new SysEntityService(broker).GetEntityAttrs(entity.Id).Select(e => e.code); #region 实体字段变更(删除字段) attrsList.Each(attr => { if (!attrs.Any(item => item.Name.ToLower() == attr.ToLower())) { var sql = @"DELETE FROM sys_attrs WHERE lower(code) = @code AND entityid = @entityid"; broker.Execute(sql, new Dictionary <string, object>() { { "@code", attr.ToLower() }, { "@entityid", EntityCache.GetEntity(item.GetEntityName())?.Id } }); sql = broker.DbClient.Driver.GetDropColumnSql(item.GetEntityName(), new List <Column>() { new Column() { Name = attr } }); broker.Execute(sql); logger.Debug($"实体{item.GetLogicalName()} ({item.GetEntityName()})删除字段:{attr}"); } }); #endregion #region 实体字段变更(新增字段) attrs.Each(attr => { if (!attrsList.Contains(attr.Name)) { var _attr = new sys_attrs() { Id = Guid.NewGuid().ToString(), name = attr.LogicalName, code = attr.Name, entityid = entity.Id, entityidname = entity.name, entityCode = entity.code, attr_type = attr.Type.ToString().ToLower(), attr_length = attr.Length, isrequire = attr.IsRequire.HasValue && attr.IsRequire.Value, default_value = ConvertUtil.ConToString(attr.DefaultValue) }; broker.Create(_attr); logger.Debug($"实体{item.GetLogicalName()}({item.GetEntityName()})创建字段:{attr.LogicalName}({attr.Name})成功"); } }); #endregion }); #endregion #region 执行版本更新脚本 { var vLogger = LogFactory.GetLogger("version"); FileHelper.GetFileList("*.sql", FolderType.Version) .OrderBy(item => Path.GetFileName(item)) .ToList() .Each(sqlFile => { try { var count = new VersionScriptExecutionLogService(broker).ExecuteScript(sqlFile); if (count == 1) { vLogger.Info($"脚本:{Path.GetFileName(sqlFile)}执行成功"); } } catch (Exception ex) { vLogger.Error($"脚本:{Path.GetFileName(sqlFile)}执行失败", ex); } }); } #endregion }); return(app); }
/// <summary> /// 登录 /// </summary> /// <param name="model"></param> /// <returns></returns> public LoginResponse Login(LoginRequest model) { var code = model.code; var pwd = model.password; var publicKey = model.publicKey; UserIdentityUtil.SetCurrentUser(UserIdentityUtil.GetSystem()); var authUser = Broker.Retrieve <auth_user>("SELECT * FROM auth_user WHERE lower(code) = lower(@code)", new Dictionary <string, object>() { { "@code", code } }); if (authUser == null) { return(new LoginResponse() { result = false, message = "用户名或密码错误" }); } if (authUser.is_lock) { return(new LoginResponse() { result = false, message = "用户已被锁定,请联系管理员" }); } if (string.IsNullOrEmpty(pwd) || string.IsNullOrEmpty(publicKey) || !string.Equals(authUser.password, RSAUtil.Decrypt(pwd, publicKey)) ) { var message = "用户名或密码错误"; if (!authUser.try_times.HasValue) { authUser.try_times = 1; } else { authUser.try_times += 1; if (authUser.try_times > 1) { message = $"用户名或密码已连续错误{authUser.try_times}次,超过五次账号锁定"; } } if (authUser.try_times >= 5) { authUser.is_lock = true; message = $"用户已被锁定,请联系管理员"; } Broker.Update(authUser); return(new LoginResponse() { result = false, message = message }); } if (authUser.try_times > 0) { authUser.try_times = 0; } authUser.last_login_time = DateTime.Now; Broker.Update(authUser); // 返回登录结果、用户信息、用户验证票据信息 var oUser = new LoginResponse { result = true, userName = code, token = JwtHelper.CreateToken(new JwtTokenModel() { Code = authUser.code, Name = authUser.name, Role = authUser.code, Uid = authUser.Id }), userId = authUser.user_infoid, message = "登录成功" }; return(oUser); }
public LoginResponse SignInOrSignUp(LoginRequest model) { UserIdentityUtil.SetCurrentUser(UserIdentityUtil.GetSystem()); return(new SystemService().SignInOrSignUp(model)); }