216 lines
8.5 KiB
C#
216 lines
8.5 KiB
C#
using FreeSql;
|
|
|
|
using LiveForum.Code.Base;
|
|
using LiveForum.IService.Others;
|
|
using LiveForum.Model;
|
|
using LiveForum.Model.Dto.Others;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace LiveForum.Service.Others
|
|
{
|
|
public class ConfigService : IConfigService
|
|
{
|
|
private readonly IBaseRepository<T_Agreements> _agreementsRepository;
|
|
private readonly LiveForum.Code.SystemCache.AppConfigManage.AppConfigCacheManager _appConfigCacheManager;
|
|
private readonly ISystemSettingsService _systemSettingsService;
|
|
private readonly IAntiAddictionService _antiAddictionService;
|
|
|
|
/// <summary>
|
|
/// CDK功能开关配置键
|
|
/// </summary>
|
|
private const string CDK_ENABLED_KEY = "cdk_enabled";
|
|
|
|
/// <summary>
|
|
/// 实名认证功能开关配置键
|
|
/// </summary>
|
|
private const string REAL_NAME_ENABLED_KEY = "real_name_enabled";
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
/// <param name="agreementsRepository">协议仓储</param>
|
|
/// <param name="appConfigCacheManager">应用配置缓存管理器</param>
|
|
/// <param name="systemSettingsService">系统设置服务</param>
|
|
public ConfigService(
|
|
IBaseRepository<T_Agreements> agreementsRepository,
|
|
LiveForum.Code.SystemCache.AppConfigManage.AppConfigCacheManager appConfigCacheManager,
|
|
ISystemSettingsService systemSettingsService,
|
|
IAntiAddictionService antiAddictionService)
|
|
{
|
|
_agreementsRepository = agreementsRepository;
|
|
_appConfigCacheManager = appConfigCacheManager;
|
|
_systemSettingsService = systemSettingsService;
|
|
_antiAddictionService = antiAddictionService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取应用配置(全部)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<BaseResponse<Dictionary<string, object>>> GetAppConfig()
|
|
{
|
|
try
|
|
{
|
|
// 使用缓存管理器获取配置(自动处理缓存)
|
|
var configDict = await _appConfigCacheManager.GetAsync();
|
|
|
|
// 创建新字典,避免修改缓存
|
|
var resultDict = new Dictionary<string, object>(configDict);
|
|
|
|
// 从数据库获取 CDK 启用状态并添加到配置中
|
|
var cdkEnabled = await _systemSettingsService.GetSettingAsync(CDK_ENABLED_KEY);
|
|
var isCdkEnabled = string.Equals(cdkEnabled, "true", StringComparison.OrdinalIgnoreCase);
|
|
resultDict["cdkEnabled"] = isCdkEnabled;
|
|
|
|
// 从数据库获取实名认证启用状态
|
|
var realNameEnabled = await _systemSettingsService.GetSettingAsync(REAL_NAME_ENABLED_KEY);
|
|
var isRealNameEnabled = string.Equals(realNameEnabled, "true", StringComparison.OrdinalIgnoreCase);
|
|
resultDict["realNameEnabled"] = isRealNameEnabled;
|
|
|
|
// 获取防沉迷规则列表
|
|
var antiAddictionRules = await _antiAddictionService.GetAllRulesAsync();
|
|
resultDict["antiAddictionRules"] = antiAddictionRules;
|
|
|
|
return new BaseResponse<Dictionary<string, object>>(resultDict);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new BaseResponse<Dictionary<string, object>>(ResponseCode.Error, $"获取应用配置失败:{ex.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取单个配置值
|
|
/// </summary>
|
|
/// <typeparam name="T">返回值类型(如 string, int, bool 等,支持引用类型)</typeparam>
|
|
/// <param name="key">配置键,支持嵌套路径,如 "AppIcon" 或 "upload_config:cosdomain"</param>
|
|
/// <param name="defaultValue">默认值(如果配置不存在时返回)</param>
|
|
/// <returns>配置值,如果不存在返回默认值</returns>
|
|
public async Task<BaseResponse<T>> GetConfigValue<T>(string key, T defaultValue = default(T))
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrWhiteSpace(key))
|
|
{
|
|
return new BaseResponse<T>(ResponseCode.Error, "配置键不能为空");
|
|
}
|
|
|
|
// 使用缓存管理器获取配置值
|
|
var value = await _appConfigCacheManager.GetValueAsync<T>(key, defaultValue);
|
|
return new BaseResponse<T>(value);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new BaseResponse<T>(ResponseCode.Error, $"获取配置值失败:{ex.Message}", defaultValue);
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取协议内容
|
|
/// </summary>
|
|
/// <param name="request">请求参数</param>
|
|
/// <returns></returns>
|
|
public async Task<BaseResponse<AgreementDto>> GetAgreement(GetAgreementReq request)
|
|
{
|
|
try
|
|
{
|
|
// 验证协议类型
|
|
if (string.IsNullOrWhiteSpace(request.AgreementType))
|
|
{
|
|
return new BaseResponse<AgreementDto>(ResponseCode.Error, "协议类型不能为空");
|
|
}
|
|
|
|
// 转换协议类型
|
|
byte agreementType = 0;
|
|
if (request.AgreementType.ToLower() == "user")
|
|
{
|
|
agreementType = 1; // 用户协议
|
|
}
|
|
else if (request.AgreementType.ToLower() == "privacy")
|
|
{
|
|
agreementType = 2; // 隐私政策
|
|
}
|
|
else
|
|
{
|
|
return new BaseResponse<AgreementDto>(ResponseCode.Error, "无效的协议类型");
|
|
}
|
|
|
|
// 查询协议内容
|
|
var agreement = await _agreementsRepository.Select
|
|
.Where(x => x.AgreementType == agreementType && x.IsActive)
|
|
.OrderByDescending(x => x.EffectiveDate)
|
|
.FirstAsync();
|
|
|
|
if (agreement == null)
|
|
{
|
|
return new BaseResponse<AgreementDto>(ResponseCode.Error, "未找到协议内容");
|
|
}
|
|
|
|
var agreementDto = new AgreementDto
|
|
{
|
|
AgreementId = agreement.Id,
|
|
Title = agreement.Title,
|
|
Content = agreement.Content,
|
|
AgreementType = agreement.AgreementType,
|
|
Version = agreement.Version,
|
|
EffectiveDate = agreement.EffectiveDate
|
|
};
|
|
|
|
return new BaseResponse<AgreementDto>(agreementDto);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new BaseResponse<AgreementDto>(ResponseCode.Error, $"获取协议失败:{ex.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查更新
|
|
/// </summary>
|
|
/// <param name="request">请求参数</param>
|
|
/// <returns></returns>
|
|
public async Task<BaseResponse<UpdateInfoDto>> CheckUpdate(CheckUpdateReq request)
|
|
{
|
|
try
|
|
{
|
|
// 验证请求参数
|
|
if (string.IsNullOrWhiteSpace(request.CurrentVersion))
|
|
{
|
|
return new BaseResponse<UpdateInfoDto>(ResponseCode.Error, "当前版本号不能为空");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(request.Platform))
|
|
{
|
|
return new BaseResponse<UpdateInfoDto>(ResponseCode.Error, "平台类型不能为空");
|
|
}
|
|
|
|
// 这里是简化实现,实际项目中应该从数据库或配置文件获取
|
|
var updateInfo = new UpdateInfoDto
|
|
{
|
|
HasUpdate = false,
|
|
LatestVersion = "1.0.0",
|
|
UpdateUrl = "",
|
|
ForceUpdate = false,
|
|
UpdateLog = "",
|
|
FileSize = 0
|
|
};
|
|
|
|
// TODO: 实现版本比较逻辑
|
|
// 这里可以添加实际的版本检查逻辑,比如从数据库或配置文件读取最新版本信息
|
|
|
|
return new BaseResponse<UpdateInfoDto>(updateInfo);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new BaseResponse<UpdateInfoDto>(ResponseCode.Error, $"检查更新失败:{ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|