コード例 #1
0
        public string BackupConfig(string consulIP, int consulPort, string prefixKey)
        {
            var config  = ConsulConfigHelper.GetConfig <ConsulConfig>(consulIP, consulPort, prefixKey);
            var jsonStr = JsonConvert.SerializeObject(config);

            var fullFileName = CommHelper.GetFullFileName($"backup\\kv\\config_{DateTime.Now.ToString("yyyyMMddHHmmss")}.json");
            var path         = Path.GetDirectoryName(fullFileName);

            if (Directory.Exists(path) == false)
            {
                Directory.CreateDirectory(path);
            }
            System.IO.File.WriteAllText(fullFileName, jsonStr);

            return(fullFileName);
        }
コード例 #2
0
        /// <summary>
        /// 添加配置服务
        /// </summary>
        /// <param name="services"></param>
        public static void AddTdbConfig(this IServiceCollection services)
        {
            //appsettings.json配置
            services.AddTdbJsonConfig();
            //读取appsettings.json配置
            ReadAppConfig();
            //配置有改动时重新读取
            LocalConfigurator.Ins.ConfigReload += RefreshAppConfig;

            //consul配置
            services.AddTdbConsulConfig(App.Consul.IP, App.Consul.Port, App.Consul.ServiceCode + "_");
            //读取consul配置
            ReadConsulConfig();
            //每分钟自动刷新consul配置信息
            RefreshConsulConfigOnTask();

            //消息配置
            var msgFullFileName = CommHelper.GetFullFileName("message.json");
            var msgJsonStr      = File.ReadAllText(msgFullFileName);

            Msg = JsonConvert.DeserializeObject <MsgConfig>(msgJsonStr);
        }
コード例 #3
0
ファイル: ManageTools.cs プロジェクト: dabintang/tdb.account
        ///// <summary>
        ///// 初始化数据库
        ///// </summary>
        ///// <returns>是否成功</returns>
        //public bool InitDB()
        //{
        //    try
        //    {
        //        //开始事物
        //        this.userDAL.AsTenant().BeginTran();

        //        #region 添加超级管理员账号

        //        var userSuperAdmin = new User();
        //        userSuperAdmin.LoginName = CstUser.SuperAdmin;
        //        userSuperAdmin.UserName = "******";
        //        userSuperAdmin.Password = EncryptHelper.Md5(AccConfig.Consul.DefaultPassword);
        //        userSuperAdmin.Gender = (int)EnumGender.Unknown;
        //        userSuperAdmin.Birthday = null;
        //        userSuperAdmin.MobilePhone = "";
        //        userSuperAdmin.Email = "";
        //        userSuperAdmin.Enable = true;
        //        userSuperAdmin.Creater = "0";
        //        userSuperAdmin.CreateTime = DateTime.Now;
        //        userSuperAdmin.Updater = "0";
        //        userSuperAdmin.UpdateTime = DateTime.Now;

        //        if (this.userDAL.ExistUser(userSuperAdmin.LoginName) == false)
        //        {
        //            this.userDAL.AddUser(userSuperAdmin);
        //        }

        //        #endregion

        //        #region 添加角色

        //        var roleSuperAdmin = new Role();
        //        roleSuperAdmin.RoleCode = CstRole.SuperAdmin;
        //        roleSuperAdmin.RoleName = "超级管理员";
        //        roleSuperAdmin.Enable = true;
        //        roleSuperAdmin.Remark = "超级管理员角色";

        //        if (this.roleDAL.ExistRole(roleSuperAdmin.RoleCode) == false)
        //        {
        //            this.roleDAL.AddRole(roleSuperAdmin);
        //        }

        //        #endregion

        //        #region 添加权限

        //        var authManageUser = new Authority();
        //        authManageUser.AuthorityCode = CstAuthority.ManageUser;
        //        authManageUser.AuthorityName = "管理用户";
        //        authManageUser.Enable = true;
        //        authManageUser.Remark = "增删查改用户的权限";

        //        if (this.authorityDAL.ExistAuthority(authManageUser.AuthorityCode) == false)
        //        {
        //            this.authorityDAL.AddAuthority(authManageUser);
        //        }

        //        #endregion

        //        #region 添加角色权限

        //        var roleAuthManageUser = new RoleAuthority();
        //        roleAuthManageUser.RoleCode = CstRole.SuperAdmin;
        //        roleAuthManageUser.AuthorityCode = CstAuthority.ManageUser;
        //        roleAuthManageUser.Creater = "0";
        //        roleAuthManageUser.CreateTime = DateTime.Now;

        //        this.roleAuthorityDAL.SetRoleAuthority(roleAuthManageUser.RoleCode, new List<RoleAuthority>() { roleAuthManageUser });

        //        #endregion

        //        #region 添加超级管理员账号角色

        //        var userRoleSuperAdmin = new UserRole();
        //        userRoleSuperAdmin.LoginName = userSuperAdmin.LoginName;
        //        userRoleSuperAdmin.RoleCode = roleSuperAdmin.RoleCode;
        //        userRoleSuperAdmin.Creater = "0";
        //        userRoleSuperAdmin.CreateTime = DateTime.Now;

        //        this.userRoleDAL.SetUserRole(userRoleSuperAdmin.LoginName, new List<UserRole>() { userRoleSuperAdmin });

        //        #endregion

        //        //提交事物
        //        this.userDAL.AsTenant().CommitTran();

        //        return true;
        //    }
        //    catch (Exception ex)
        //    {
        //        Logger.Ins.Error(ex, "初始化数据库异常");

        //        //回滚事物
        //        this.userDAL.AsTenant().RollbackTran();

        //        return false;
        //    }
        //}

        /// <summary>
        /// 初始化数据库
        /// </summary>
        /// <returns>是否成功</returns>
        public bool InitDB()
        {
            try
            {
                //消息配置
                var fullFileName = CommHelper.GetFullFileName("initData.json");
                var jsonStr      = File.ReadAllText(fullFileName);
                var initData     = JsonConvert.DeserializeObject <InitDataInfo>(jsonStr);

                //开始事物
                this.userDAL.AsTenant().BeginTran();

                #region 用户

                foreach (var user in initData.Users)
                {
                    user.Password = EncryptHelper.Md5(user.Password);
                    if (this.userDAL.ExistUser(user.LoginName) == false)
                    {
                        this.userDAL.AddUser(user);
                    }
                }

                #endregion

                #region 添加角色

                foreach (var role in initData.Roles)
                {
                    if (this.roleDAL.ExistRole(role.RoleCode) == false)
                    {
                        this.roleDAL.AddRole(role);
                    }
                }

                #endregion

                #region 添加权限

                foreach (var authority in initData.Authoritys)
                {
                    if (this.authorityDAL.ExistAuthority(authority.AuthorityCode) == false)
                    {
                        this.authorityDAL.AddAuthority(authority);
                    }
                }

                #endregion

                #region 添加角色权限

                var lstRoleCode = initData.RoleAuthoritys.Select(m => m.RoleCode).Distinct().ToList();
                foreach (var roleCode in lstRoleCode)
                {
                    var lstRoleAuthority = initData.RoleAuthoritys.FindAll(m => m.RoleCode == roleCode);
                    this.roleAuthorityDAL.SetRoleAuthority(roleCode, lstRoleAuthority);
                }

                #endregion

                #region 添加超级管理员账号角色

                var lstLoginName = initData.UserRoles.Select(m => m.LoginName).Distinct().ToList();
                foreach (var loginName in lstLoginName)
                {
                    var lstUserRole = initData.UserRoles.FindAll(m => m.LoginName == loginName);
                    this.userRoleDAL.SetUserRole(loginName, lstUserRole);
                }

                #endregion

                //提交事物
                this.userDAL.AsTenant().CommitTran();

                return(true);
            }
            catch (Exception ex)
            {
                Logger.Ins.Error(ex, "初始化数据库异常");

                //回滚事物
                this.userDAL.AsTenant().RollbackTran();

                return(false);
            }
        }