!103 feat(#I54RIV): 完善 FreeSql 支持

* refactor: 改为 petapoco 服务
* feat: 完善 Freesql
This commit is contained in:
Nine 2022-04-26 10:35:21 +00:00 committed by Argo
parent 89b9bf7e3a
commit c78fbcf87c
6 changed files with 379 additions and 9 deletions

View File

@ -24,6 +24,9 @@ public static class ServiceCollectionExtensions
/// <returns></returns>
public static IServiceCollection AddFreeSql(this IServiceCollection services, Action<IServiceProvider, FreeSqlBuilder> freeSqlBuilder)
{
// 增加缓存服务
services.AddCacheManager();
services.TryAddSingleton<IFreeSql>(provider =>
{
var builder = new FreeSqlBuilder();
@ -45,6 +48,7 @@ public static class ServiceCollectionExtensions
services.AddSingleton<INavigation, NavigationService>();
services.AddSingleton<IRole, RoleService>();
services.AddSingleton<IUser, UserService>();
services.AddSingleton<ITrace, TraceService>();
return services;
}
}

View File

@ -7,6 +7,7 @@ using BootstrapAdmin.DataAccess.Models;
using BootstrapAdmin.Web.Core;
using Longbow.Security.Cryptography;
using Microsoft.Extensions.Configuration;
using System.Data;
namespace BootStarpAdmin.DataAccess.FreeSql.Service;
@ -175,23 +176,284 @@ class DictService : IDict
return url;
}
public bool SavDefaultApp(bool enabled)
public bool SavDefaultApp(bool enabled) => SaveDict(new Dict
{
throw new NotImplementedException();
}
Category = "网站设置",
Name = "默认应用程序",
Code = enabled ? "1" : "0",
Define = EnumDictDefine.System
});
public bool GetEnableDefaultApp()
{
throw new NotImplementedException();
var dicts = GetAll();
var code = dicts.FirstOrDefault(d => d.Category == "网站设置" && d.Name == "默认应用程序")?.Code ?? "0";
return code == "1";
}
public string GetIconFolderPath()
{
throw new NotImplementedException();
var dicts = GetAll();
return dicts.FirstOrDefault(d => d.Name == "头像路径" && d.Category == "头像地址" && d.Define == EnumDictDefine.System)?.Code ?? "/images/uploder/";
}
public string GetDefaultIcon()
{
throw new NotImplementedException();
var dicts = GetAll();
return dicts.FirstOrDefault(d => d.Name == "头像文件" && d.Category == "头像地址" && d.Define == EnumDictDefine.System)?.Code ?? "default.jpg";
}
public string? GetIpLocatorName()
{
var dicts = GetAll();
return dicts.FirstOrDefault(s => s.Category == "网站设置" && s.Name == "IP地理位置接口" && s.Define == EnumDictDefine.System)?.Code;
}
public string? GetIpLocatorUrl(string? name)
{
var dicts = GetAll();
return string.IsNullOrWhiteSpace(name) ? null : dicts.FirstOrDefault(s => s.Category == "地理位置" && s.Name == name && s.Define == EnumDictDefine.System)?.Code;
}
public bool GetAppSiderbar()
{
var dicts = GetAll();
return dicts.FirstOrDefault(s => s.Category == "网站设置" && s.Name == "侧边栏状态" && s.Define == EnumDictDefine.System)?.Code == "1";
}
public bool SaveAppSiderbar(bool value) => SaveDict(new Dict { Category = "网站设置", Name = "侧边栏状态", Code = value ? "1" : "0" });
public bool GetAppTitle()
{
var dicts = GetAll();
return dicts.FirstOrDefault(s => s.Category == "网站设置" && s.Name == "卡片标题状态" && s.Define == EnumDictDefine.System)?.Code == "1";
}
public bool SaveAppTitle(bool value) => SaveDict(new Dict { Category = "网站设置", Name = "卡片标题状态", Code = value ? "1" : "0" });
public bool GetAppFixHeader()
{
var dicts = GetAll();
return dicts.FirstOrDefault(s => s.Category == "网站设置" && s.Name == "固定表头" && s.Define == EnumDictDefine.System)?.Code == "1";
}
public bool SaveAppFixHeader(bool value) => SaveDict(new Dict { Category = "网站设置", Name = "固定表头", Code = value ? "1" : "0" });
public bool GetAppHealthCheck()
{
var dicts = GetAll();
return dicts.FirstOrDefault(s => s.Category == "网站设置" && s.Name == "健康检查" && s.Define == EnumDictDefine.System)?.Code == "1";
}
public bool SaveAppHealthCheck(bool value) => SaveDict(new Dict { Category = "网站设置", Name = "健康检查", Code = value ? "1" : "0" });
public bool GetAppMobileLogin()
{
var dicts = GetAll();
return dicts.FirstOrDefault(s => s.Category == "网站设置" && s.Name == "短信验证码登录" && s.Define == EnumDictDefine.System)?.Code == "1";
}
public bool SaveAppMobileLogin(bool value) => SaveDict(new Dict { Category = "网站设置", Name = "短信验证码登录", Code = value ? "1" : "0" });
public bool GetAppOAuthLogin()
{
var dicts = GetAll();
return dicts.FirstOrDefault(s => s.Category == "网站设置" && s.Name == "OAuth 认证登录" && s.Define == EnumDictDefine.System)?.Code == "1";
}
public bool SaveAppOAuthLogin(bool value) => SaveDict(new Dict { Category = "网站设置", Name = "OAuth 认证登录", Code = value ? "1" : "0" });
public bool GetAutoLockScreen()
{
var dicts = GetAll();
return dicts.FirstOrDefault(s => s.Category == "网站设置" && s.Name == "自动锁屏" && s.Define == EnumDictDefine.System)?.Code == "1";
}
public bool SaveAutoLockScreen(bool value) => SaveDict(new Dict { Category = "网站设置", Name = "自动锁屏", Code = value ? "1" : "0" });
public int GetAutoLockScreenInterval()
{
var dicts = GetAll();
var value = dicts.FirstOrDefault(s => s.Category == "网站设置" && s.Name == "自动锁屏时长" && s.Define == EnumDictDefine.System)?.Code ?? "0";
_ = int.TryParse(value, out var ret);
return ret;
}
public bool SaveAutoLockScreenInterval(int value) => SaveDict(new Dict { Category = "网站设置", Name = "自动锁屏时长", Code = value.ToString() });
public Dictionary<string, string> GetIpLocators()
{
var dicts = GetAll();
return dicts.Where(d => d.Category == "地理位置服务").Select(d => new KeyValuePair<string, string>(d.Code, d.Name)).OrderBy(i => i.Value).ToDictionary(i => i.Key, i => i.Value);
}
public string? GetIpLocator()
{
var dicts = GetAll();
return dicts.FirstOrDefault(s => s.Category == "网站设置" && s.Name == "IP地理位置接口" && s.Define == EnumDictDefine.System)?.Code;
}
public bool SaveCurrentIp(string value) => SaveDict(new Dict { Category = "网站设置", Name = "IP地理位置接口", Code = value });
public int GetCookieExpired()
{
var dicts = GetAll();
var value = dicts.FirstOrDefault(s => s.Category == "网站设置" && s.Name == "Cookie保留时长" && s.Define == EnumDictDefine.System)?.Code ?? "0";
_ = int.TryParse(value, out var ret);
return ret;
}
public bool SaveCookieExpired(int value) => SaveDict(new Dict { Category = "网站设置", Name = "Cookie保留时长", Code = value.ToString() });
public int GetExceptionExpired()
{
var dicts = GetAll();
var value = dicts.FirstOrDefault(s => s.Category == "网站设置" && s.Name == "程序异常保留时长" && s.Define == EnumDictDefine.System)?.Code ?? "0";
_ = int.TryParse(value, out var ret);
return ret;
}
public bool SaveExceptionExpired(int value) => SaveDict(new Dict { Category = "网站设置", Name = "程序异常保留时长", Code = value.ToString() });
public int GetOperateExpired()
{
var dicts = GetAll();
var value = dicts.FirstOrDefault(s => s.Category == "网站设置" && s.Name == "操作日志保留时长" && s.Define == EnumDictDefine.System)?.Code ?? "0";
_ = int.TryParse(value, out var ret);
return ret;
}
public bool SaveOperateExpired(int value) => SaveDict(new Dict { Category = "网站设置", Name = "操作日志保留时长", Code = value.ToString() });
public int GetLoginExpired()
{
var dicts = GetAll();
var value = dicts.FirstOrDefault(s => s.Category == "网站设置" && s.Name == "登录日志保留时长" && s.Define == EnumDictDefine.System)?.Code ?? "0";
_ = int.TryParse(value, out var ret);
return ret;
}
public bool SaveLoginExpired(int value) => SaveDict(new Dict { Category = "网站设置", Name = "登录日志保留时长", Code = value.ToString() });
public int GetAccessExpired()
{
var dicts = GetAll();
var value = dicts.FirstOrDefault(s => s.Category == "网站设置" && s.Name == "访问日志保留时长" && s.Define == EnumDictDefine.System)?.Code ?? "0";
_ = int.TryParse(value, out var ret);
return ret;
}
public bool SaveAccessExpired(int value) => SaveDict(new Dict { Category = "网站设置", Name = "访问日志保留时长", Code = value.ToString() });
public int GetIPCacheExpired()
{
var dicts = GetAll();
var value = dicts.FirstOrDefault(s => s.Category == "网站设置" && s.Name == "IP请求缓存时长" && s.Define == EnumDictDefine.System)?.Code ?? "0";
_ = int.TryParse(value, out var ret);
return ret;
}
public bool SaveIPCacheExpired(int value) => SaveDict(new Dict { Category = "网站设置", Name = "IP请求缓存时长", Code = value.ToString() });
public Dictionary<string, string>? GetClients()
{
var dicts = GetAll();
return dicts.Where(s => s.Category == "应用程序" && s.Code != "BA").ToDictionary(s => s.Name, s => s.Code);
}
public string GetClientUrl(string name)
{
var dicts = GetAll();
return dicts.Where(s => s.Category == "应用首页" && s.Name == name).FirstOrDefault()?.Code ?? "";
}
public bool ExistsAppId(string appId)
{
var dicts = GetAll();
return dicts.Exists(s => s.Category == "应用程序" && s.Code == appId);
}
public bool SaveClient(ClientApp client)
{
var ret = false;
if (!string.IsNullOrEmpty(client.AppId))
{
DeleteClient(client.AppId);
try
{
var items = new List<Dict>()
{
new Dict { Category = "应用程序", Name = client.AppName, Code = client.AppId, Define = EnumDictDefine.System },
new Dict { Category = "应用首页", Name = client.AppId, Code = client.HomeUrl, Define = EnumDictDefine.System },
new Dict { Category = client.AppId, Name = "网站页脚", Code = client.Footer, Define = EnumDictDefine.Customer },
new Dict { Category = client.AppId, Name = "网站标题", Code = client.Title, Define = EnumDictDefine.Customer },
new Dict { Category = client.AppId, Name = "favicon", Code = client.Favicon, Define = EnumDictDefine.Customer },
new Dict { Category = client.AppId, Name = "网站图标", Code = client.Icon, Define = EnumDictDefine.Customer },
new Dict { Category = client.AppId, Name = "个人中心地址", Code = client.ProfileUrl, Define = EnumDictDefine.Customer },
new Dict { Category = client.AppId, Name = "系统设置地址", Code = client.SettingsUrl, Define = EnumDictDefine.Customer },
new Dict { Category = client.AppId, Name = "系统通知地址", Code = client.NotificationUrl, Define = EnumDictDefine.Customer }
};
ret = FreeSql.Insert(items).ExecuteAffrows() > 0;
}
catch
{
throw;
}
}
return ret;
}
public ClientApp GetClient(string appId)
{
var dicts = GetAll();
return new ClientApp()
{
AppId = appId,
AppName = dicts.FirstOrDefault(s => s.Category == "应用程序" && s.Code == appId)?.Name,
HomeUrl = dicts.FirstOrDefault(s => s.Category == "应用首页" && s.Name == appId)?.Code,
ProfileUrl = dicts.FirstOrDefault(s => s.Category == appId && s.Name == "个人中心地址")?.Code,
SettingsUrl = dicts.FirstOrDefault(s => s.Category == appId && s.Name == "系统设置地址")?.Code,
NotificationUrl = dicts.FirstOrDefault(s => s.Category == appId && s.Name == "系统通知地址")?.Code,
Title = dicts.FirstOrDefault(s => s.Category == appId && s.Name == "网站标题")?.Code,
Footer = dicts.FirstOrDefault(s => s.Category == appId && s.Name == "网站页脚")?.Code,
Icon = dicts.FirstOrDefault(s => s.Category == appId && s.Name == "网站图标")?.Code,
Favicon = dicts.FirstOrDefault(s => s.Category == appId && s.Name == "favicon")?.Code,
};
}
public bool DeleteClient(string appId)
{
bool ret;
try
{
FreeSql.Transaction(() =>
{
FreeSql.Ado.ExecuteNonQuery("delete Dicts where Category=@Category and Name=@Name and Define=@Define", new { Category = "应用首页", Name = appId, Define = EnumDictDefine.System });
FreeSql.Ado.ExecuteNonQuery("delete Dicts where Category=@Category and Code=@Code and Define=@Define", new { Category = "应用程序", Code = appId, Define = EnumDictDefine.System });
FreeSql.Ado.ExecuteNonQuery("delete Dicts where Category=@Category and Name in (@Names)", new
{
Category = appId,
Names = new List<string>
{
"网站标题",
"网站页脚",
"favicon",
"网站图标",
"个人中心地址",
"系统设置地址",
"系统通知地址"
}
});
});
ret = true;
}
catch (Exception)
{
throw;
}
return ret;
}
}

View File

@ -2,14 +2,31 @@
// Licensed under the LGPL License, Version 3.0. See License.txt in the project root for license information.
// Website: https://admin.blazor.zone
using BootstrapAdmin.DataAccess.Models;
using BootstrapAdmin.Web.Core;
namespace BootStarpAdmin.DataAccess.FreeSql.Service;
class LoginService : ILogin
{
public Task<bool> Log(string userName, bool result)
private IFreeSql FreeSql { get; }
public LoginService(IFreeSql freeSql) => FreeSql = freeSql;
public bool Log(string userName, string? IP, string? OS, string? browser, string? address, string? userAgent, bool result)
{
return Task.FromResult(true);
var loginUser = new LoginLog()
{
UserName = userName,
LoginTime = DateTime.Now,
Ip = IP,
City = address,
OS = OS,
Browser = browser,
UserAgent = userAgent,
Result = result ? "登录成功" : "登录失败"
};
FreeSql.Insert(loginUser).ExecuteAffrows();
return true;
}
}

View File

@ -0,0 +1,84 @@
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the LGPL License, Version 3.0. See License.txt in the project root for license information.
// Website: https://admin.blazor.zone
using BootstrapAdmin.DataAccess.Models;
using BootstrapAdmin.Web.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BootStarpAdmin.DataAccess.FreeSql.Service;
/// <summary>
///
/// </summary>
public class TraceService : ITrace
{
private IFreeSql FreeSql { get; }
/// <summary>
///
/// </summary>
/// <param name="freeSql"></param>
public TraceService(IFreeSql freeSql) => FreeSql = freeSql;
/// <summary>
///
/// </summary>
/// <param name="searchText"></param>
/// <param name="filter"></param>
/// <param name="pageIndex"></param>
/// <param name="pageItems"></param>
/// <param name="sortList"></param>
/// <returns></returns>
public (IEnumerable<Trace> Items, int ItemsCount) GetAll(string? searchText, TraceFilter filter, int pageIndex, int pageItems, List<string> sortList)
{
var items = FreeSql.Select<Trace>();
if (!string.IsNullOrEmpty(searchText))
{
items.Where("UserName Like @searchText or Ip Like @searchText or RequestUrl Like @searchText", new { searchText });
}
if (!string.IsNullOrEmpty(filter.UserName))
{
items.Where("UserName Like @UserName", new { filter.UserName });
}
if (!string.IsNullOrEmpty(filter.Ip))
{
items.Where("Ip Like @Ip", new { filter.Ip });
}
if (!string.IsNullOrEmpty(filter.RequestUrl))
{
items.Where("ErrorPage Like @RequestUrl", new { filter.RequestUrl });
}
items.Where("LogTime >= @0 and LogTime <= @1", new { filter.Star, filter.End });
if (sortList.Any())
{
items.OrderBy(string.Join(", ", sortList));
}
else
{
items.OrderBy("Logtime desc");
}
var traces = items.Count(out var count).Page(pageIndex, pageItems).ToList();
return (traces, Convert.ToInt32(count));
}
/// <summary>
///
/// </summary>
/// <param name="trace"></param>
public void Log(Trace trace)
{
FreeSql.Insert(trace).ExecuteAffrows();
}
}

View File

@ -6,6 +6,9 @@ using BootstrapAdmin.DataAccess.Models;
namespace BootstrapAdmin.Web.Components;
/// <summary>
///
/// </summary>
public partial class MenuEditor
{
/// <summary>

View File

@ -67,7 +67,7 @@ namespace Microsoft.Extensions.DependencyInjection
// var connString = configuration.GetConnectionString("bb");
// builder.UseConnectionString(FreeSql.DataType.Sqlite, connString);
//#if DEBUG
// //调试sql语句输出
// 调试sql语句输出
// builder.UseMonitorCommand(cmd => System.Console.WriteLine(cmd.CommandText));
//#endif
// });