优化代码、数据库字符串连接

This commit is contained in:
不做码农 2023-06-04 16:43:35 +08:00
parent 2e39825b5c
commit cdd4b42f0a
7 changed files with 98 additions and 37 deletions

View File

@ -1,4 +1,6 @@

using System.Collections.Generic;
namespace Infrastructure
{
/// <summary>
@ -15,6 +17,7 @@ namespace Infrastructure
public ALIYUN_OSS ALIYUN_OSS { get; set; }
public JwtSettings JwtSettings { get; set; }
public Gen Gen { get; set; }
public List<DbConfigs> DbConfigs { get; set; }
}
/// <summary>
/// 发送邮件数据配置
@ -81,4 +84,11 @@ namespace Infrastructure
}
public class DbConfigs
{
public string Conn { get; set; }
public int DbType { get; set; }
public string ConfigId { get; set; }
public bool IsAutoCloseConnection { get; set; }
}
}

View File

@ -3,12 +3,7 @@ using Infrastructure.Attribute;
using Infrastructure.Enums;
using Infrastructure.Model;
using Mapster;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;
using ZR.Admin.WebApi.Extensions;
using ZR.Admin.WebApi.Filters;
using ZR.Model.System;

View File

@ -22,31 +22,36 @@ namespace ZR.Admin.WebApi.Extensions
//仅本人数据权限
public static long DATA_SCOPE_SELF = 5;
/// <summary>
/// 初始化db
/// </summary>
/// <param name="Configuration"></param>
public static void AddDb(IConfiguration Configuration)
{
string connStr = Configuration.GetConnectionString("conn_db");
int dbType = Convert.ToInt32(Configuration.GetConnectionString("conn_db_type"));
List<DbConfigs> dbConfigs = Configuration.GetSection("DbConfigs").Get<List<DbConfigs>>();
var iocList = new List<IocConfig>() {
new IocConfig() {
ConfigId = "0",//默认db
ConnectionString = connStr,
DbType = (IocDbType)dbType,
IsAutoCloseConnection = true
},
new IocConfig() {
ConfigId = "1",
ConnectionString = connStr,
DbType = (IocDbType)dbType,
IsAutoCloseConnection = true
var iocList = new List<IocConfig>();
foreach (var item in dbConfigs)
{
iocList.Add(new IocConfig()
{
ConfigId = item.ConfigId,
ConnectionString = item.Conn,
DbType = (IocDbType)item.DbType,
IsAutoCloseConnection = item.IsAutoCloseConnection
});
}
};
SugarIocServices.AddSqlSugar(iocList);
ICacheService cache = new SqlSugarCache();
SugarIocServices.ConfigurationSugar(db =>
{
//db0数据过滤
var u = App.User;
if (u != null)
{
FilterData(0);
//ConfigId = 1的数据权限过滤
//FilterData1(1);
}
iocList.ForEach(iocConfig =>
{
@ -55,6 +60,12 @@ namespace ZR.Admin.WebApi.Extensions
});
}
/// <summary>
/// 数据库Aop设置
/// </summary>
/// <param name="db"></param>
/// <param name="iocConfig"></param>
/// <param name="cache"></param>
private static void SetSugarAop(SqlSugarClient db, IocConfig iocConfig, ICacheService cache)
{
var config = db.GetConnection(iocConfig.ConfigId).CurrentConnectionConfig;
@ -108,7 +119,7 @@ namespace ZR.Admin.WebApi.Extensions
}
/// <summary>
/// 初始化db
/// 创建db、表
/// </summary>
/// <param name="service"></param>
public static void InitDb(this IServiceProvider service)
@ -137,8 +148,6 @@ namespace ZR.Admin.WebApi.Extensions
/// <param name="configId">多库id</param>
private static void FilterData(int configId)
{
var u = App.User;
if (u == null) return;
//获取当前用户的信息
var user = JwtUtil.GetLoginUser(App.HttpContext);
if (user == null) return;
@ -184,5 +193,35 @@ namespace ZR.Admin.WebApi.Extensions
db.QueryFilter.AddTableFilter(expLoginlog.ToExpression());
}
}
private static void FilterData1(int configId)
{
//获取当前用户的信息
var user = JwtUtil.GetLoginUser(App.HttpContext);
if (user == null) return;
var db = DbScoped.SugarScope.GetConnectionScope(configId);
foreach (var role in user.Roles.OrderBy(f => f.DataScope))
{
long dataScope = role.DataScope;
if (DATA_SCOPE_ALL.Equals(dataScope))//所有权限
{
break;
}
else if (DATA_SCOPE_CUSTOM.Equals(dataScope))//自定数据权限
{
}
else if (DATA_SCOPE_DEPT.Equals(dataScope))//本部门数据
{
}
else if (DATA_SCOPE_DEPT_AND_CHILD.Equals(dataScope))//本部门及以下数据
{
}
else if (DATA_SCOPE_SELF.Equals(dataScope))//仅本人数据
{
}
}
}
}
}

View File

@ -1,8 +1,4 @@
using Infrastructure;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Quartz.Spi;
using System;
using Quartz.Spi;
using ZR.Tasks;
namespace ZR.Admin.WebApi.Extensions
@ -12,6 +8,11 @@ namespace ZR.Admin.WebApi.Extensions
/// </summary>
public static class TasksExtension
{
/// <summary>
/// 注册任务
/// </summary>
/// <param name="services"></param>
/// <exception cref="ArgumentNullException"></exception>
public static void AddTaskSchedulers(this IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));

View File

@ -6,10 +6,15 @@
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings": {
"conn_db": "Data Source=LAPTOP-STKF2M8H\\SQLEXPRESS;User ID=admin;Password=admin123;Initial Catalog=ZrAdmin;", //
"conn_db_type": "1" // MySql = 0, SqlServer = 1, Oracle = 3PgSql = 4
},
"dbConfigs": [
{
"Conn": "Data Source=LAPTOP-STKF2M8H\\SQLEXPRESS;User ID=admin;Password=admin123;Initial Catalog=ZrAdmin;",
"DbType": 1, // MySql = 0, SqlServer = 1, Oracle = 3PgSql = 4
"ConfigId": "0", //
"IsAutoCloseConnection": true
}
//...
],
"urls": "http://localhost:8888", //urldevServer
"corsUrls": "http://localhost:8887", //","
"JwtSettings": {
@ -43,6 +48,7 @@
"CorpSecret": "",
"SendUser": "@all"
},
//
"gen": {
"conn": "Data Source=LAPTOP-STKF2M8H\\SQLEXPRESS;User ID=admin;Password=admin123;Initial Catalog=ZrAdmin;",
"dbType": 1, //MySql = 0, SqlServer = 1

View File

@ -168,6 +168,7 @@ namespace ZR.Repository
{
return Context.Storageable(t);
}
/// <summary>
///
/// </summary>
@ -189,6 +190,11 @@ namespace ZR.Repository
}
}
/// <summary>
/// 使用事务
/// </summary>
/// <param name="action"></param>
/// <returns></returns>
public bool UseTran2(Action action)
{
var result = Context.Ado.UseTran(() => action());

View File

@ -217,6 +217,10 @@ namespace ZR.Service
{
throw new CustomException("密码强度不符合要求");
}
if (!Tools.CheckUserName(dto.Username))
{
throw new CustomException("用户名不符合要求");
}
//密码md5
string password = NETCore.Encrypt.EncryptProvider.Md5(dto.Password);