diff --git a/DHSoftware/Configs/log4net.config b/DHSoftware/Configs/log4net.config deleted file mode 100644 index 1a238b5..0000000 --- a/DHSoftware/Configs/log4net.config +++ /dev/null @@ -1,84 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/DHSoftware/DHSoftware.csproj b/DHSoftware/DHSoftware.csproj index eb49477..92c14da 100644 --- a/DHSoftware/DHSoftware.csproj +++ b/DHSoftware/DHSoftware.csproj @@ -13,13 +13,13 @@ - - PreserveNewest - + - + + PreserveNewest + @@ -55,4 +55,14 @@ + + + + + + + PreserveNewest + + + \ No newline at end of file diff --git a/DHSoftware/LoginWindow.cs b/DHSoftware/LoginWindow.cs index 8403e9d..997b323 100644 --- a/DHSoftware/LoginWindow.cs +++ b/DHSoftware/LoginWindow.cs @@ -3,7 +3,7 @@ using DH.RBAC.Logic.Sys; using DH.RBAC.Model.Sys; using DH.RBAC.Utility.Other; using DHSoftware.Models; -using DHSoftware.Services; + namespace DHSoftware { diff --git a/DHSoftware/Program.cs b/DHSoftware/Program.cs index c3d0df3..77eedb6 100644 --- a/DHSoftware/Program.cs +++ b/DHSoftware/Program.cs @@ -1,5 +1,7 @@ using System; +using System.Data.Entity; using System.Drawing; +using System.Reflection; using System.Windows.Forms; using AntdUI; using DH.Commons.Base; @@ -13,17 +15,21 @@ using DH.RBAC.Utility.Other; using DHSoftware.Utils; using DHSoftware.Views; using Microsoft.VisualBasic.Logging; +using Newtonsoft.Json; using GlobalConfig = DH.RBAC.Common.GlobalConfig; namespace DHSoftware { internal static class Program { + private static MainWindow mainWindow; - /// - /// 应用程序的主入口点。 - /// - [STAThread] + + /// + /// 应用程序的主入口点。 + /// + [STAThread] + static void Main() { // 必须在第一个窗口创建前调用以下两行 @@ -46,10 +52,11 @@ namespace DHSoftware UpdateStep(10, "正在加载数据库", true); try { - MyConfig config = File.ReadAllText(MyEnvironment.RootPath("Configs/config.json")).ToObject(); + MyConfig config = File.ReadAllText(MyEnvironment.RootPath("db/config.json")).ToObject(); GlobalConfig.Config = config; string message = ""; bool flag = BaseLogic.InitDB(config.DbType, config.DbHost, config.DbName, config.DbUserName, config.DbPassword, ref message); + if (!flag) { Console.Write(message); diff --git a/DHSoftware/Utils/DataBaseUtil.cs b/DHSoftware/Utils/DataBaseUtil.cs deleted file mode 100644 index 5c3a96c..0000000 --- a/DHSoftware/Utils/DataBaseUtil.cs +++ /dev/null @@ -1,151 +0,0 @@ -using DHSoftware.Models; -using SqlSugar; - -namespace DHSoftware.Utils -{ - public static class DatabaseUtil - { - private static readonly string DatabasePath = Path.Combine( - Application.StartupPath, - "db", - "RBACSystem.sqlite" - ); - - public static void InitializeDatabase() - { - EnsureDirectoryExists(); - using (var db = GetDatabase()) - { - // 检查初始化状态(通过检查是否存在系统表) - bool isInitialized = db.DbMaintenance.IsAnyTable("RolePermission"); - - if (!isInitialized) - { - // 创建所有表 - db.CodeFirst.InitTables( - typeof(User), - typeof(Role), - typeof(Permission), - typeof(UserRole), - typeof(RolePermission) - ); - - // 初始化基础数据 - InitializeSeedData(db); - } - } - } - - public static SqlSugarClient GetDatabase() - { - return new SqlSugarClient(new ConnectionConfig() - { - ConnectionString = $"Data Source={DatabasePath};", - DbType = DbType.Sqlite, - IsAutoCloseConnection = true, - InitKeyType = InitKeyType.Attribute - }); - } - - private static void EnsureDirectoryExists() - { - var directory = Path.GetDirectoryName(DatabasePath); - if (!Directory.Exists(directory)) - { - Directory.CreateDirectory(directory); - } - } - - private static void InitializeSeedData(SqlSugarClient db) - { - // 初始化角色 - var adminRole = GetOrCreateRole(db, "admin", "系统管理员"); - var userRole = GetOrCreateRole(db, "user", "普通用户"); - - // 初始化权限 - var permissions = new List - { - new Permission { Code = "system:access", Name = "访问系统" }, - new Permission { Code = "user:view", Name = "查看用户" }, - new Permission { Code = "user:edit", Name = "管理用户" }, - new Permission { Code = "role:manage", Name = "角色管理" }, - new Permission { Code = "system:config", Name = "配置权限" }, - new Permission { Code = "system:loadscheme", Name = "加载方案" }, - new Permission { Code = "system:addscheme", Name = "新增方案" }, - new Permission { Code = "system:deletescheme", Name = "删除方案" } - }; - InitializePermissions(db, permissions); - - // 分配权限给管理员角色 - AssignPermissionsToRole(db, adminRole.Id, permissions.Select(p => p.Code).ToList()); - - // 创建默认管理员 - CreateAdminUser(db); - } - - private static Role GetOrCreateRole(SqlSugarClient db, string roleName, string description) - { - var role = db.Queryable() - .First(r => r.RoleName == roleName); - - if (role == null) - { - role = new Role - { - RoleName = roleName, - Description = description - }; - role.Id = db.Insertable(role).ExecuteReturnIdentity(); - } - return role; - } - - private static void InitializePermissions(SqlSugarClient db, List permissions) - { - foreach (var p in permissions) - { - if (!db.Queryable().Any(x => x.Code == p.Code)) - { - db.Insertable(p).ExecuteCommand(); - } - } - } - - private static void AssignPermissionsToRole(SqlSugarClient db, int roleId, List permissionCodes) - { - var existing = db.Queryable() - .Where(rp => rp.RoleId == roleId) - .Select(rp => rp.PermissionCode) - .ToList(); - - foreach (var code in permissionCodes.Except(existing)) - { - db.Insertable(new RolePermission - { - RoleId = roleId, - PermissionCode = code - }).ExecuteCommand(); - } - } - - private static void CreateAdminUser(SqlSugarClient db) - { - if (!db.Queryable().Any(u => u.UserName == "admin")) - { - var admin = new User - { - UserName = "admin", - Password = HashHelper.MD5Encrypt("admin123"), - LastLoginTime = null - }; - admin.Id = db.Insertable(admin).ExecuteReturnIdentity(); - - db.Insertable(new UserRole - { - UserId = admin.Id, - RoleId = db.Queryable().First(r => r.RoleName == "admin").Id - }).ExecuteCommand(); - } - } - } -} \ No newline at end of file diff --git a/DHSoftware/Configs/config.json b/DHSoftware/db/config.json similarity index 100% rename from DHSoftware/Configs/config.json rename to DHSoftware/db/config.json diff --git a/DHSoftware/db/db.sqlite b/DHSoftware/db/db.sqlite new file mode 100644 index 0000000..f16095b Binary files /dev/null and b/DHSoftware/db/db.sqlite differ