!提交代码
This commit is contained in:
83
WCS.DAL/AuthDbModel/AuthModels.cs
Normal file
83
WCS.DAL/AuthDbModel/AuthModels.cs
Normal file
@ -0,0 +1,83 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using WCS.DAL.Db;
|
||||
using WCS.Model;
|
||||
|
||||
namespace WCS.DAL.AuthDbModel
|
||||
{
|
||||
/// <summary>
|
||||
/// 用户
|
||||
/// </summary>
|
||||
[SugarTable("User")]
|
||||
public class UserBase
|
||||
{
|
||||
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
/// <summary>
|
||||
/// 登录名
|
||||
/// </summary>
|
||||
public string LoginName { get; set; }
|
||||
/// <summary>
|
||||
/// 密码
|
||||
/// </summary>
|
||||
public string Password { get; set; }
|
||||
/// <summary>
|
||||
/// 角色id(json)
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDataType = StaticConfig.CodeFirst_BigString, IsJson = true)]
|
||||
public List<int> RoleIds { get; set; } = new List<int>();
|
||||
/// <summary>
|
||||
/// 角色名称
|
||||
/// </summary>
|
||||
[SugarColumn(IsIgnore = true)]
|
||||
public List<string> RoleNames { get => (RoleIds == null || !RoleIds.Any()) ? new List<string>() : AuthDbHelp.db.Queryable<RoleBase>().Where(o => RoleIds.Contains(o.Id)).Select(o => o.Name).ToList(); }
|
||||
/// <summary>
|
||||
/// 是否最大权限
|
||||
/// </summary>
|
||||
public bool IsAdmin { get; set; }
|
||||
/// <summary>
|
||||
/// 创建时间
|
||||
/// </summary>
|
||||
public DateTime Time { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 用户所属角色
|
||||
/// </summary>
|
||||
[SugarColumn(IsIgnore = true)]
|
||||
public List<RoleBase> GetRoles { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 角色
|
||||
/// </summary>
|
||||
[SugarTable("Role")]
|
||||
public class RoleBase
|
||||
{
|
||||
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
/// <summary>
|
||||
/// 角色名
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
/// <summary>
|
||||
/// 认证模块(json)
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDataType = StaticConfig.CodeFirst_BigString, IsJson = true)]
|
||||
public List<int> Auths { get; set; } = new List<int>();
|
||||
/// <summary>
|
||||
/// 认证模块名称
|
||||
/// </summary>
|
||||
[SugarColumn(IsIgnore = true)]
|
||||
public List<string> AuthNames { get => (Auths == null || !Auths.Any()) ? new List<string>() : EnumHelps.GetEnumDescriptionList(typeof(AuthEnum), true).Where(o => Auths.Contains(o.Item1)).Select(o => o.Item3).ToList(); }
|
||||
/// <summary>
|
||||
/// 是否最大权限
|
||||
/// </summary>
|
||||
public bool IsAdmin { get; set; }
|
||||
/// <summary>
|
||||
/// 创建时间
|
||||
/// </summary>
|
||||
public DateTime Time { get; set; }
|
||||
}
|
||||
}
|
91
WCS.DAL/Db/AuthEnum/AuthDbHelp.cs
Normal file
91
WCS.DAL/Db/AuthEnum/AuthDbHelp.cs
Normal file
@ -0,0 +1,91 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WCS.DAL.AuthDbModel;
|
||||
|
||||
namespace WCS.DAL
|
||||
{
|
||||
/// <summary>
|
||||
/// 权限数据库
|
||||
/// </summary>
|
||||
public static class AuthDbHelp
|
||||
{
|
||||
public static SqlSugarScope db = new SqlSugarScope(new ConnectionConfig()
|
||||
{
|
||||
ConnectionString = $"Data Source={LocalFile.AuthDbPath};",
|
||||
DbType = DbType.Sqlite,//[Sqlite]安装[System.Data.SQLite.Core];
|
||||
IsAutoCloseConnection = true
|
||||
}, db =>
|
||||
{
|
||||
db.Aop.OnError = ex =>
|
||||
{
|
||||
//Logs.Write($@"{nameof(AuthDbHelp)}{Environment.NewLine}SQL:{ex?.Sql}{Environment.NewLine}Parametres:{JsonConvert.SerializeObject(ex?.Parametres)}{Environment.NewLine}InnerException:{ex?.InnerException?.ToString()}{Environment.NewLine}Exception:{ex?.ToString()}{Environment.NewLine}", LogsType.DbErr);
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 初始化数据
|
||||
/// </summary>
|
||||
public static void InitDb()
|
||||
{
|
||||
//不存在创建数据库,存在不会创建
|
||||
db.DbMaintenance.CreateDatabase();
|
||||
//创建表根据实体类
|
||||
db.CodeFirst.InitTables(typeof(UserBase), typeof(RoleBase));
|
||||
//初始化数据
|
||||
if (!db.Queryable<UserBase>().Any())
|
||||
{
|
||||
var dt = db.GetDate();
|
||||
var userBases = new List<UserBase>()
|
||||
{
|
||||
new UserBase ()
|
||||
{
|
||||
Id = 1,
|
||||
LoginName = "admin",
|
||||
Password = "",
|
||||
RoleIds = new List<int> () { 1 },
|
||||
IsAdmin = true,
|
||||
Time = dt,
|
||||
},
|
||||
new UserBase ()
|
||||
{
|
||||
Id = 2,
|
||||
LoginName = "czy",
|
||||
Password = "",
|
||||
RoleIds = new List<int> () { 2 },
|
||||
IsAdmin = false,
|
||||
Time = dt,
|
||||
},
|
||||
};
|
||||
var roleBases = new List<RoleBase>()
|
||||
{
|
||||
new RoleBase ()
|
||||
{
|
||||
Id = 1,
|
||||
Name = "管理员",
|
||||
Auths = new List<int> () { },
|
||||
IsAdmin = true,
|
||||
Time = dt,
|
||||
},
|
||||
new RoleBase ()
|
||||
{
|
||||
Id = 2,
|
||||
Name = "操作员",
|
||||
Auths = new List<int> () { },
|
||||
IsAdmin = false,
|
||||
Time = dt,
|
||||
},
|
||||
};
|
||||
|
||||
db.Insertable(userBases).ExecuteCommand();
|
||||
db.Insertable(roleBases).ExecuteCommand();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
58
WCS.DAL/Db/AuthEnum/AuthEnum.cs
Normal file
58
WCS.DAL/Db/AuthEnum/AuthEnum.cs
Normal file
@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WCS.DAL
|
||||
{
|
||||
/// <summary>
|
||||
/// 认证项
|
||||
/// </summary>
|
||||
public enum AuthEnum
|
||||
{
|
||||
/* 注意:枚举的值必须大于0 */
|
||||
查询 = 1000,
|
||||
权限 = 2000,
|
||||
//[Description("用户")]
|
||||
//[EnumTree(权限, new[] { 用户新增, 用户删除, 用户修改 })]
|
||||
//用户管理 = 3100, 用户新增 = 3110, 用户删除, 用户修改,
|
||||
//[Description("角色")]
|
||||
//[EnumTree(权限, new[] { 角色新增, 角色删除, 角色修改 })]
|
||||
//角色管理 = 3200, 角色新增 = 3210, 角色删除, 角色修改,
|
||||
设置 = 3000,
|
||||
调试 = 4000,
|
||||
}
|
||||
|
||||
public class EnumTreeAttribute : Attribute
|
||||
{
|
||||
public EnumTreeAttribute() { }
|
||||
|
||||
public EnumTreeAttribute(AuthEnum parent)
|
||||
{
|
||||
Parent = parent;
|
||||
}
|
||||
|
||||
public EnumTreeAttribute(AuthEnum[] childs)
|
||||
{
|
||||
Childs = childs;
|
||||
}
|
||||
|
||||
public EnumTreeAttribute(AuthEnum parent, AuthEnum[] childs)
|
||||
{
|
||||
Parent = parent;
|
||||
Childs = childs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 父级
|
||||
/// </summary>
|
||||
public AuthEnum? Parent { get; set; } = null;
|
||||
/// <summary>
|
||||
/// 子级
|
||||
/// </summary>
|
||||
public AuthEnum[]? Childs { get; set; } = null;
|
||||
}
|
||||
}
|
68
WCS.DAL/Db/AuthEnum/EnumHelps.cs
Normal file
68
WCS.DAL/Db/AuthEnum/EnumHelps.cs
Normal file
@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
namespace WCS.DAL
|
||||
{
|
||||
public static class EnumHelps
|
||||
{
|
||||
public static List<string> GetEnumList(Type type)
|
||||
{
|
||||
List<string> strings = new List<string>();
|
||||
if (type.IsEnum)
|
||||
{
|
||||
var fields = type.GetFields(BindingFlags.Static | BindingFlags.Public) ?? new FieldInfo[] { };
|
||||
foreach (var field in fields)
|
||||
{
|
||||
strings.Add(field.Name);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
return strings;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 得到枚举详情
|
||||
/// </summary>
|
||||
/// <param name="type">枚举</param>
|
||||
/// <param name="isDescriptionNullInName">描述为空时,是否采用名称值</param>
|
||||
/// <returns>枚举值,名称,描述</returns>
|
||||
public static List<Tuple<int, string, string>> GetEnumDescriptionList(Type type, bool isDescriptionNullInName = false)
|
||||
{
|
||||
var strings = new List<Tuple<int, string, string>>();
|
||||
if (type.IsEnum)
|
||||
{
|
||||
var fields = type.GetFields(BindingFlags.Static | BindingFlags.Public);
|
||||
if (fields == null)
|
||||
return strings;
|
||||
|
||||
foreach (var field in fields)
|
||||
{
|
||||
int enumValue = Convert.ToInt32(field.GetRawConstantValue());
|
||||
string enumName = field.Name;
|
||||
string description = string.Empty;
|
||||
|
||||
var descriptionAttribute = field.GetCustomAttribute<DescriptionAttribute>(false);
|
||||
if (descriptionAttribute != null)
|
||||
description = descriptionAttribute.Description;
|
||||
else if (isDescriptionNullInName)
|
||||
description = enumName;
|
||||
|
||||
strings.Add(new Tuple<int, string, string>(enumValue, enumName, description));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
return strings;
|
||||
}
|
||||
}
|
||||
}
|
60
WCS.DAL/Db/DbHelp.cs
Normal file
60
WCS.DAL/Db/DbHelp.cs
Normal file
@ -0,0 +1,60 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WCS.DAL.Db
|
||||
{
|
||||
public static class DbHelp
|
||||
{
|
||||
/// <summary>
|
||||
/// 业务数据库
|
||||
/// </summary>
|
||||
public static SqlSugarScope db = new SqlSugarScope(new ConnectionConfig()
|
||||
{
|
||||
ConnectionString = $"Data Source={LocalFile.DataDbPath};",
|
||||
DbType = DbType.Sqlite,//[Sqlite]安装[System.Data.SQLite];
|
||||
IsAutoCloseConnection = true
|
||||
}, db =>
|
||||
{
|
||||
db.Aop.OnError = ex =>
|
||||
{
|
||||
//TO DO LOG
|
||||
//Logs.Write($@"{nameof(db)}{Environment.NewLine}SQL:{ex?.Sql}{Environment.NewLine}Parametres:{JsonConvert.SerializeObject(ex?.Parametres)}{Environment.NewLine}InnerException:{ex?.InnerException?.ToString()}{Environment.NewLine}Exception:{ex?.ToString()}{Environment.NewLine}", LogsType.DbErr);
|
||||
};
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// 日志数据库
|
||||
/// </summary>
|
||||
public static SqlSugarScope dbLog = new SqlSugarScope(new ConnectionConfig()
|
||||
{
|
||||
ConnectionString = $"Data Source={LocalFile.LogDbPath};",
|
||||
DbType = DbType.Sqlite,//[Sqlite]安装[System.Data.SQLite];
|
||||
IsAutoCloseConnection = true
|
||||
}, db =>
|
||||
{
|
||||
db.Aop.OnError = ex =>
|
||||
{
|
||||
//TO DO LOG
|
||||
//Logs.Write($@"{nameof(dbAuth)}{Environment.NewLine}SQL:{ex?.Sql}{Environment.NewLine}Parametres:{JsonConvert.SerializeObject(ex?.Parametres)}{Environment.NewLine}InnerException:{ex?.InnerException?.ToString()}{Environment.NewLine}Exception:{ex?.ToString()}{Environment.NewLine}", LogsType.DbErr);
|
||||
};
|
||||
});
|
||||
|
||||
public static void InitDb()
|
||||
{
|
||||
//#region 初始化业务数据库
|
||||
////不存在创建数据库,存在不会创建
|
||||
//db.DbMaintenance.CreateDatabase();
|
||||
////创建表根据实体类
|
||||
//db.CodeFirst.InitTables(typeof(ModuleInfo), typeof(ShelfInfo), typeof(StoreInfo));
|
||||
//#endregion
|
||||
|
||||
//#region 初始化日志数据库
|
||||
//dbLog.DbMaintenance.CreateDatabase();
|
||||
//#endregion
|
||||
}
|
||||
}
|
||||
}
|
19
WCS.DAL/LocalFile.cs
Normal file
19
WCS.DAL/LocalFile.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WCS.DAL
|
||||
{
|
||||
public static class LocalFile
|
||||
{
|
||||
public static readonly string AppDir = AppDomain.CurrentDomain.BaseDirectory;
|
||||
|
||||
public static readonly string LogDbPath = Path.Combine(AppDir, "data\\log.db3");
|
||||
|
||||
public static readonly string DataDbPath = Path.Combine(AppDir, "data\\data.db3");
|
||||
|
||||
public static readonly string AuthDbPath = Path.Combine(AppDir, "data\\auth.db3");
|
||||
}
|
||||
}
|
16
WCS.DAL/WCS.DAL.csproj
Normal file
16
WCS.DAL/WCS.DAL.csproj
Normal file
@ -0,0 +1,16 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="SqlSugarCore" Version="5.1.4.149" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\WCS.Model\WCS.Model.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
Reference in New Issue
Block a user